@@ -8,6 +8,7 @@ const CHAR_SNAKE = "🐍";
88
99const MODE_NORMAL = 1 ;
1010const MODE_RAW = 2 ;
11+ const MODE_PRE_PROMPT = 3 ;
1112
1213const TYPE_DIR = 16384 ;
1314const TYPE_FILE = 32768 ;
@@ -24,11 +25,13 @@ const PROMPT_CHECK_INTERVAL = 50;
2425
2526const REGEX_PROMPT_RAW_MODE = / r a w R E P L ; C T R L - B t o e x i t / ;
2627const 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 ./ ;
2729
2830const modes = [
2931 "Unknown" ,
3032 "Normal" ,
3133 "Raw" ,
34+ "Pre-Prompt" ,
3235] ;
3336
3437// Class to use python code to get file information
@@ -89,7 +92,7 @@ with open("${path}", "wb") as f:
8992 if ( modificationTime ) {
9093 code += `os.utime("${ path } ", (os.path.getatime("${ path } "), ${ modificationTime } ))\n` ;
9194 }
92- await this . _repl . execRawMode ( code ) ;
95+ await this . _repl . runCode ( code ) ;
9396 }
9497
9598 async _writeTextFile ( path , contents , offset = 0 , modificationTime = null ) {
@@ -106,7 +109,7 @@ with open("${path}", "w") as f:
106109 if ( modificationTime ) {
107110 code += `os.utime("${ path } ", (os.path.getatime("${ path } "), ${ modificationTime } ))\n` ;
108111 }
109- await this . _repl . execRawMode ( code ) ;
112+ await this . _repl . runCode ( code ) ;
110113 }
111114
112115 // 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:
130133 byte_string = f.read()
131134 print(binascii.b2a_base64(byte_string, False))
132135` ;
133- let result = await this . _repl . execRawMode ( code ) ;
136+ let result = await this . _repl . runCode ( code ) ;
134137 if ( this . _checkReplErrors ( ) ) {
135138 return null ;
136139 }
@@ -160,7 +163,7 @@ with open("${path}", "rb") as f:
160163with open("${ path } ", "r") as f:
161164 print(f.read())
162165` ;
163- let result = await this . _repl . execRawMode ( code ) ;
166+ let result = await this . _repl . runCode ( code ) ;
164167 if ( await this . _checkReplErrors ( ) ) {
165168 return null ;
166169 }
@@ -204,7 +207,7 @@ for item in contents:
204207 result = os.stat("${ path } " + item)
205208 print(item, result[0], result[6], result[9])
206209` ;
207- const result = await this . _repl . execRawMode ( code ) ;
210+ const result = await this . _repl . runCode ( code ) ;
208211
209212 let contents = [ ] ;
210213 if ( ! result ) {
233236except:
234237 print(False)
235238` ;
236- let result = await this . _repl . execRawMode ( code ) ;
239+ let result = await this . _repl . runCode ( code ) ;
237240 let isReadOnly = result . match ( "True" ) != null ;
238241 this . _repl . terminalOutput = true ;
239242
@@ -247,7 +250,7 @@ except:
247250 if ( modificationTime ) {
248251 code += `os.utime("${ path } ", (os.path.getatime("${ path } "), ${ modificationTime } ))\n` ;
249252 }
250- await this . _repl . execRawMode ( code ) ;
253+ await this . _repl . runCode ( code ) ;
251254 this . _checkReplErrors ( ) ;
252255 this . _repl . terminalOutput = true ;
253256 }
@@ -264,7 +267,7 @@ if stat[0] == ${TYPE_FILE}:
264267else:
265268 os.rmdir("${ path } ")
266269` ;
267- await this . _repl . execRawMode ( code ) ;
270+ await this . _repl . runCode ( code ) ;
268271 this . _checkReplErrors ( ) ;
269272 this . _repl . terminalOutput = true ;
270273 }
@@ -279,7 +282,7 @@ else:
279282import os
280283os.rename("${ oldPath } ", "${ newPath } ")
281284` ;
282- await this . _repl . execRawMode ( code ) ;
285+ await this . _repl . runCode ( code ) ;
283286 let error = this . _checkReplErrors ( ) ;
284287 this . _repl . terminalOutput = true ;
285288 return ! error ;
@@ -457,9 +460,21 @@ export class REPL {
457460
458461 const rawModRegex = new RegExp ( REGEX_PROMPT_RAW_MODE , 'g' ) ;
459462 const normalModRegex = new RegExp ( REGEX_PROMPT_NORMAL_MODE , 'g' ) ;
463+ const prePromptRegex = new RegExp ( REGEX_PRE_PROMPT , 'g' ) ;
460464
461465 let lastRawPosition = this . _findLastRegexPosition ( rawModRegex , buffer ) ;
462466 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+ }
463478
464479 if ( lastRawPosition > lastNormalPosition ) {
465480 this . _mode = MODE_RAW ;
@@ -471,6 +486,9 @@ export class REPL {
471486
472487 // If no mode changes detected, we will assume normal mode with code running
473488 if ( ! this . _mode ) {
489+ if ( DEBUG ) {
490+ console . log ( "No mode detected. Restarting Device." ) ;
491+ }
474492 await this . softRestart ( ) ;
475493 await this . serialTransmit ( CHAR_CTRL_C ) ;
476494 await this . _sleep ( 1000 ) ;
@@ -568,6 +586,8 @@ export class REPL {
568586 console . log ( "REPL at Normal Mode prompt" ) ;
569587 }
570588 this . _pythonCodeRunning = false ;
589+ } else {
590+ console . log ( "Normal Prompt not detected." ) ;
571591 }
572592 }
573593
@@ -652,7 +672,7 @@ export class REPL {
652672 await this . _detectCurrentMode ( ) ;
653673 await this . _sleep ( 100 ) ;
654674 }
655- } , 1000
675+ } , 3000
656676 ) ;
657677 } catch ( error ) {
658678 console . log ( "Awaiting mode change timed out." ) ;
@@ -662,6 +682,9 @@ export class REPL {
662682 // Raw mode allows code execution without echoing back to the terminal
663683 async _enterRawMode ( ) {
664684 if ( this . _mode == MODE_RAW ) {
685+ if ( DEBUG ) {
686+ console . log ( "Already in Raw Mode" ) ;
687+ }
665688 await this . _exitRawMode ( ) ;
666689 }
667690 await this . _waitForModeChange ( MODE_RAW , CHAR_CTRL_A ) ;
@@ -725,6 +748,7 @@ export class REPL {
725748 console . error ( "Default serial transmit function called. Message: " + msg ) ;
726749 throw new Error ( "REPL serialTransmit must be connected to an external transmit function" ) ;
727750 } else {
751+ console . log ( "Transmitting: " + msg ) ;
728752 return await this . serialTransmit ( msg ) ;
729753 }
730754 }
0 commit comments