@@ -28,6 +28,7 @@ class MicroPythonBoard {
28
28
constructor ( ) {
29
29
this . port = null
30
30
this . serial = null
31
+ this . reject_run = null
31
32
}
32
33
33
34
list_ports ( ) {
@@ -113,7 +114,10 @@ class MicroPythonBoard {
113
114
}
114
115
115
116
async get_prompt ( ) {
116
- const out = await this . write_and_read_until ( `\r\x02\x03` , '\r\n>>>' )
117
+ await sleep ( 100 )
118
+ await this . stop ( )
119
+ await sleep ( 100 )
120
+ const out = await this . write_and_read_until ( `\r\x03\x02` , '\r\n>>>' )
117
121
return Promise . resolve ( out )
118
122
}
119
123
@@ -147,10 +151,22 @@ class MicroPythonBoard {
147
151
148
152
async run ( code , data_consumer ) {
149
153
data_consumer = data_consumer || function ( ) { }
150
- await this . enter_raw_repl ( )
151
- const output = await this . exec_raw ( code || '#' , data_consumer )
152
- await this . exit_raw_repl ( )
153
- return Promise . resolve ( output )
154
+ return new Promise ( async ( resolve , reject ) => {
155
+ if ( this . reject_run ) {
156
+ this . reject_run ( new Error ( 're-run' ) )
157
+ this . reject_run = null
158
+ }
159
+ this . reject_run = reject
160
+ try {
161
+ await this . enter_raw_repl ( )
162
+ const output = await this . exec_raw ( code || '#' , data_consumer )
163
+ await this . exit_raw_repl ( )
164
+ return resolve ( output )
165
+ } catch ( e ) {
166
+ reject ( e )
167
+ this . reject_run = null
168
+ }
169
+ } )
154
170
}
155
171
156
172
async eval ( k ) {
@@ -159,12 +175,20 @@ class MicroPythonBoard {
159
175
}
160
176
161
177
async stop ( ) {
178
+ if ( this . reject_run ) {
179
+ this . reject_run ( new Error ( 'pre stop' ) )
180
+ this . reject_run = null
181
+ }
162
182
// Dismiss any data with ctrl-C
163
183
await this . serial . write ( Buffer . from ( `\x03` ) )
164
184
return Promise . resolve ( )
165
185
}
166
186
167
187
async reset ( ) {
188
+ if ( this . reject_run ) {
189
+ this . reject_run ( new Error ( 'pre reset' ) )
190
+ this . reject_run = null
191
+ }
168
192
// Dismiss any data with ctrl-C
169
193
await this . serial . write ( Buffer . from ( `\x03` ) )
170
194
// Soft reboot
0 commit comments