@@ -35,32 +35,6 @@ import { Radio } from "./radio";
35
35
import { RangeSensor , State } from "./state" ;
36
36
import { ModuleWrapper } from "./wasm" ;
37
37
38
- enum StopKind {
39
- /**
40
- * The main Wasm function returned control to us in a normal way.
41
- */
42
- Default = "default" ,
43
- /**
44
- * The program called panic.
45
- */
46
- Panic = "panic" ,
47
- /**
48
- * The program requested a reset.
49
- */
50
- Reset = "reset" ,
51
- /**
52
- * An internal mode where we do not display the stop state UI as we plan to immediately reset.
53
- * Used for user-requested flash or reset.
54
- */
55
- BriefStop = "brief" ,
56
- /**
57
- * The user requested the program be interrupted.
58
- *
59
- * Note the program could finish for other reasons, but should always count as a user stop.
60
- */
61
- UserStop = "user" ,
62
- }
63
-
64
38
export class PanicError extends Error {
65
39
constructor ( public code : number ) {
66
40
super ( "panic" ) ;
@@ -100,6 +74,8 @@ export class Board {
100
74
radio : Radio ;
101
75
dataLogging : DataLogging ;
102
76
77
+ private panicTimeout : any ;
78
+
103
79
public serialInputBuffer : number [ ] = [ ] ;
104
80
105
81
private stoppedOverlay : HTMLDivElement ;
@@ -132,19 +108,10 @@ export class Board {
132
108
*/
133
109
private module : ModuleWrapper | undefined ;
134
110
/**
135
- * Controls the action after the user program completes.
136
- *
137
- * Determined by a combination of user actions (stop, reset etc) and program actions.
111
+ * If undefined, then when main finishes we stay stopped.
112
+ * Otherwise we perform the action then clear this field.
138
113
*/
139
- private stopKind : StopKind = StopKind . Default ;
140
- /**
141
- * Timeout for a pending start call due to StopKind.Reset.
142
- */
143
- private pendingRestartTimeout : any ;
144
- /**
145
- * Timeout for the next frame of the panic animation.
146
- */
147
- private panicTimeout : any ;
114
+ private afterStopped : ( ( ) => void ) | undefined ;
148
115
149
116
constructor (
150
117
private notifications : Notifications ,
@@ -389,8 +356,6 @@ export class Board {
389
356
if ( this . modulePromise || this . module ) {
390
357
throw new Error ( "Module already exists!" ) ;
391
358
}
392
- clearTimeout ( this . pendingRestartTimeout ) ;
393
- this . pendingRestartTimeout = null ;
394
359
395
360
this . modulePromise = this . createModule ( ) ;
396
361
const module = await this . modulePromise ;
@@ -400,17 +365,11 @@ export class Board {
400
365
this . displayRunningState ( ) ;
401
366
await module . start ( ) ;
402
367
} catch ( e : any ) {
403
- // Take care not to overwrite another kind of stop just because the program
404
- // called restart or panic.
405
368
if ( e instanceof PanicError ) {
406
- if ( this . stopKind === StopKind . Default ) {
407
- this . stopKind = StopKind . Panic ;
408
- panicCode = e . code ;
409
- }
369
+ panicCode = e . code ;
410
370
} else if ( e instanceof ResetError ) {
411
- if ( this . stopKind === StopKind . Default ) {
412
- this . stopKind = StopKind . Reset ;
413
- }
371
+ const noChangeRestart = ( ) => { } ;
372
+ this . afterStopped = noChangeRestart ;
414
373
} else {
415
374
this . notifications . onInternalError ( e ) ;
416
375
}
@@ -427,52 +386,30 @@ export class Board {
427
386
this . modulePromise = undefined ;
428
387
this . module = undefined ;
429
388
430
- switch ( this . stopKind ) {
431
- case StopKind . Panic : {
432
- if ( panicCode === undefined ) {
433
- throw new Error ( "Must be set" ) ;
434
- }
435
- this . displayPanic ( panicCode ) ;
436
- break ;
437
- }
438
- case StopKind . Reset : {
439
- this . pendingRestartTimeout = setTimeout ( ( ) => this . start ( ) , 0 ) ;
440
- break ;
441
- }
442
- case StopKind . BriefStop : {
443
- // Skip the stopped state.
444
- break ;
445
- }
446
- case StopKind . UserStop : /* Fall through */
447
- case StopKind . Default : {
389
+ if ( panicCode !== undefined ) {
390
+ this . displayPanic ( panicCode ) ;
391
+ } else {
392
+ if ( this . afterStopped ) {
393
+ this . afterStopped ( ) ;
394
+ this . afterStopped = undefined ;
395
+ setTimeout ( ( ) => this . start ( ) , 0 ) ;
396
+ } else {
448
397
this . displayStoppedState ( ) ;
449
- break ;
450
- }
451
- default : {
452
- throw new Error ( "Unknown stop kind: " + this . stopKind ) ;
453
398
}
454
399
}
455
- this . stopKind = StopKind . Default ;
456
400
}
457
401
458
- async stop ( brief : boolean = false ) : Promise < void > {
402
+ async stop (
403
+ afterStopped : ( ( ) => void ) | undefined = undefined
404
+ ) : Promise < void > {
405
+ this . afterStopped = afterStopped ;
459
406
if ( this . panicTimeout ) {
460
407
clearTimeout ( this . panicTimeout ) ;
461
408
this . panicTimeout = null ;
462
409
this . display . clear ( ) ;
463
- if ( ! brief ) {
464
- this . displayStoppedState ( ) ;
465
- }
466
- }
467
- if ( this . pendingRestartTimeout ) {
468
- clearTimeout ( this . pendingRestartTimeout ) ;
469
- this . pendingRestartTimeout = null ;
470
- if ( ! brief ) {
471
- this . displayStoppedState ( ) ;
472
- }
410
+ this . displayStoppedState ( ) ;
473
411
}
474
412
if ( this . modulePromise ) {
475
- this . stopKind = brief ? StopKind . BriefStop : StopKind . UserStop ;
476
413
// Avoid this.module as we might still be creating it (async).
477
414
const module = await this . modulePromise ;
478
415
module . requestStop ( ) ;
@@ -485,10 +422,11 @@ export class Board {
485
422
486
423
/**
487
424
* An external reset.
425
+ * reset() in MicroPython code throws ResetError.
488
426
*/
489
427
async reset ( ) : Promise < void > {
490
- await this . stop ( true ) ;
491
- return this . start ( ) ;
428
+ const noChangeRestart = ( ) => { } ;
429
+ this . stop ( noChangeRestart ) ;
492
430
}
493
431
494
432
async flash ( filesystem : Record < string , Uint8Array > ) : Promise < void > {
@@ -502,7 +440,7 @@ export class Board {
502
440
} ;
503
441
if ( this . modulePromise ) {
504
442
// If it's running then we need to stop before flash.
505
- await this . stop ( true ) ;
443
+ return this . stop ( flashFileSystem ) ;
506
444
}
507
445
flashFileSystem ( ) ;
508
446
return this . start ( ) ;
0 commit comments