Listing 2: Definition of the Bomb FSM from Figure 2(b).
1: void Bomb::initial(Event const *e) { 2: myTimeout = 2; 3: initialTran((State)&Bomb::setting); // initial transition 4: } 5: void Bomb::setting(Event const *e) { 6: switch (e->sig) { 7: case UP_SIG: 8: if (myTimeout < 10) { 9: ++myTimeout; 10: } 11: break; 12: case DOWN_SIG: 13: if (myTimeout > 1) { 14: --myTimeout; 15: } 16: break; 17: case ARM_SIG: 18: tran((State)&Bomb::timing); 19: break; 20: } 21: } 22: void Bomb::timing(Event const *e) { 23: switch (e->sig) { 24: case ENTRY_SIG: 25: SetTimer(locHwnd, 1, 1000, 0); // start ticking every 1000 ms 26: break; 27: case TICK_SIG: 28: if (myTimeout > 0) { 29: --myTimeout; 30: } 31: else { // timeout expired 32: tran((State)&Bomb::blast); 33: } 34: break; 35: case EXIT_SIG: 36: KillTimer(locHwnd, 1); // don't leak the timer! 37: break; 38: } 39: }