@@ -8,6 +8,7 @@ const CHAR_SNAKE = "🐍";
8
8
9
9
const MODE_NORMAL = 1 ;
10
10
const MODE_RAW = 2 ;
11
+ const MODE_PRE_PROMPT = 3 ;
11
12
12
13
const TYPE_DIR = 16384 ;
13
14
const TYPE_FILE = 32768 ;
@@ -24,11 +25,13 @@ const PROMPT_CHECK_INTERVAL = 50;
24
25
25
26
const REGEX_PROMPT_RAW_MODE = / r a w R E P L ; C T R L - B t o e x i t / ;
26
27
const REGEX_PROMPT_NORMAL_MODE = / > > > / ;
28
+ const REGEX_PRE_PROMPT = / P r e s s a n y k e y t o e n t e r t h e R E P L ./ ;
27
29
28
30
const modes = [
29
31
"Unknown" ,
30
32
"Normal" ,
31
33
"Raw" ,
34
+ "Pre-Prompt" ,
32
35
] ;
33
36
34
37
// Class to use python code to get file information
@@ -89,7 +92,7 @@ with open("${path}", "wb") as f:
89
92
if ( modificationTime ) {
90
93
code += `os.utime("${ path } ", (os.path.getatime("${ path } "), ${ modificationTime } ))\n` ;
91
94
}
92
- await this . _repl . execRawMode ( code ) ;
95
+ await this . _repl . runCode ( code ) ;
93
96
}
94
97
95
98
async _writeTextFile ( path , contents , offset = 0 , modificationTime = null ) {
@@ -106,7 +109,7 @@ with open("${path}", "w") as f:
106
109
if ( modificationTime ) {
107
110
code += `os.utime("${ path } ", (os.path.getatime("${ path } "), ${ modificationTime } ))\n` ;
108
111
}
109
- await this . _repl . execRawMode ( code ) ;
112
+ await this . _repl . runCode ( code ) ;
110
113
}
111
114
112
115
// Write a file to the device path with contents beginning at offset. Modification time can be set and if raw is true, contents is written as binary
@@ -130,7 +133,7 @@ with open("${path}", "rb") as f:
130
133
byte_string = f.read()
131
134
print(binascii.b2a_base64(byte_string, False))
132
135
` ;
133
- let result = await this . _repl . execRawMode ( code ) ;
136
+ let result = await this . _repl . runCode ( code ) ;
134
137
if ( this . _checkReplErrors ( ) ) {
135
138
return null ;
136
139
}
@@ -160,7 +163,7 @@ with open("${path}", "rb") as f:
160
163
with open("${ path } ", "r") as f:
161
164
print(f.read())
162
165
` ;
163
- let result = await this . _repl . execRawMode ( code ) ;
166
+ let result = await this . _repl . runCode ( code ) ;
164
167
if ( await this . _checkReplErrors ( ) ) {
165
168
return null ;
166
169
}
@@ -204,7 +207,7 @@ for item in contents:
204
207
result = os.stat("${ path } " + item)
205
208
print(item, result[0], result[6], result[9])
206
209
` ;
207
- const result = await this . _repl . execRawMode ( code ) ;
210
+ const result = await this . _repl . runCode ( code ) ;
208
211
209
212
let contents = [ ] ;
210
213
if ( ! result ) {
233
236
except:
234
237
print(False)
235
238
` ;
236
- let result = await this . _repl . execRawMode ( code ) ;
239
+ let result = await this . _repl . runCode ( code ) ;
237
240
let isReadOnly = result . match ( "True" ) != null ;
238
241
this . _repl . terminalOutput = true ;
239
242
@@ -247,7 +250,7 @@ except:
247
250
if ( modificationTime ) {
248
251
code += `os.utime("${ path } ", (os.path.getatime("${ path } "), ${ modificationTime } ))\n` ;
249
252
}
250
- await this . _repl . execRawMode ( code ) ;
253
+ await this . _repl . runCode ( code ) ;
251
254
this . _checkReplErrors ( ) ;
252
255
this . _repl . terminalOutput = true ;
253
256
}
@@ -264,7 +267,7 @@ if stat[0] == ${TYPE_FILE}:
264
267
else:
265
268
os.rmdir("${ path } ")
266
269
` ;
267
- await this . _repl . execRawMode ( code ) ;
270
+ await this . _repl . runCode ( code ) ;
268
271
this . _checkReplErrors ( ) ;
269
272
this . _repl . terminalOutput = true ;
270
273
}
@@ -279,7 +282,7 @@ else:
279
282
import os
280
283
os.rename("${ oldPath } ", "${ newPath } ")
281
284
` ;
282
- await this . _repl . execRawMode ( code ) ;
285
+ await this . _repl . runCode ( code ) ;
283
286
let error = this . _checkReplErrors ( ) ;
284
287
this . _repl . terminalOutput = true ;
285
288
return ! error ;
@@ -457,9 +460,21 @@ export class REPL {
457
460
458
461
const rawModRegex = new RegExp ( REGEX_PROMPT_RAW_MODE , 'g' ) ;
459
462
const normalModRegex = new RegExp ( REGEX_PROMPT_NORMAL_MODE , 'g' ) ;
463
+ const prePromptRegex = new RegExp ( REGEX_PRE_PROMPT , 'g' ) ;
460
464
461
465
let lastRawPosition = this . _findLastRegexPosition ( rawModRegex , buffer ) ;
462
466
let lastNormalPosition = this . _findLastRegexPosition ( normalModRegex , buffer ) ;
467
+ let lastPrePromptPosition = this . _findLastRegexPosition ( prePromptRegex , buffer ) ;
468
+
469
+ if ( lastPrePromptPosition > lastNormalPosition && lastPrePromptPosition > lastRawPosition ) {
470
+ this . _mode = MODE_PRE_PROMPT ;
471
+ if ( DEBUG ) {
472
+ console . log ( "Pre-Prompt Detected" ) ;
473
+ }
474
+ this . _serialInputBuffer . movePointer ( lastPrePromptPosition ) ;
475
+ await this . serialTransmit ( CHAR_CTRL_C ) ;
476
+ return ;
477
+ }
463
478
464
479
if ( lastRawPosition > lastNormalPosition ) {
465
480
this . _mode = MODE_RAW ;
@@ -471,6 +486,9 @@ export class REPL {
471
486
472
487
// If no mode changes detected, we will assume normal mode with code running
473
488
if ( ! this . _mode ) {
489
+ if ( DEBUG ) {
490
+ console . log ( "No mode detected. Restarting Device." ) ;
491
+ }
474
492
await this . softRestart ( ) ;
475
493
await this . serialTransmit ( CHAR_CTRL_C ) ;
476
494
await this . _sleep ( 1000 ) ;
@@ -568,6 +586,8 @@ export class REPL {
568
586
console . log ( "REPL at Normal Mode prompt" ) ;
569
587
}
570
588
this . _pythonCodeRunning = false ;
589
+ } else {
590
+ console . log ( "Normal Prompt not detected." ) ;
571
591
}
572
592
}
573
593
@@ -652,7 +672,7 @@ export class REPL {
652
672
await this . _detectCurrentMode ( ) ;
653
673
await this . _sleep ( 100 ) ;
654
674
}
655
- } , 1000
675
+ } , 3000
656
676
) ;
657
677
} catch ( error ) {
658
678
console . log ( "Awaiting mode change timed out." ) ;
@@ -662,6 +682,9 @@ export class REPL {
662
682
// Raw mode allows code execution without echoing back to the terminal
663
683
async _enterRawMode ( ) {
664
684
if ( this . _mode == MODE_RAW ) {
685
+ if ( DEBUG ) {
686
+ console . log ( "Already in Raw Mode" ) ;
687
+ }
665
688
await this . _exitRawMode ( ) ;
666
689
}
667
690
await this . _waitForModeChange ( MODE_RAW , CHAR_CTRL_A ) ;
@@ -725,6 +748,7 @@ export class REPL {
725
748
console . error ( "Default serial transmit function called. Message: " + msg ) ;
726
749
throw new Error ( "REPL serialTransmit must be connected to an external transmit function" ) ;
727
750
} else {
751
+ console . log ( "Transmitting: " + msg ) ;
728
752
return await this . serialTransmit ( msg ) ;
729
753
}
730
754
}
0 commit comments