BME554L -Fall 2025 - Palmeri
Duke University
Invalid Date
@startuml
[*] -> Init
state Init {
init_run: Do Stuff
}
state RhythmDetect {
rhythm_detect_run: Detect Rhythm
}
state Sleep {
sleep_run: Save Power
}
state Pace {
pace_run: Pace
}
state OTA {
ota_init: Download Update
ota_init -> ota_run
ota_run: Over The Air Update
ota_run -> ota_exit
ota_exit: Post Successful OTA
}
state Error {
error_run: Put device into safe mode
}
Init -> RhythmDetect : Successful Init
Init -d-> Error : Init Fault Error Code
RhythmDetect -> Pace : Arrhythmia Detected
Pace -> RhythmDetect : Pacing Timer Expired
RhythmDetect -d-> Sleep : NSR
Sleep -> RhythmDetect: Sleep Timer Expired
Sleep -> OTA : OTA Button Pressed
OTA -> RhythmDetect : OTA Success Kernel Event Posted
Error -> [*]
@enduml
ISR
), which calls a callback function.enum device_states { init, run, sleep };
int device_state = init; // initialize state
/* structure to bookkeep state variables */
struct device_state_vars {
int var1;
int var2;
};
while (1) {
switch (device_state) {
case init:
/* do stuff to initialize device */
device_state = run; // change the state
break; // exit the switch statement
case run:
/* run device */
if (condition) {
device_state = sleep;
}
break;
case sleep:
/* sleep device */
if (condition) {
device_state = run;
}
break;
default:
/* handle unexpected state */
break;
}
}
The switch-case approach loses some of its elegance when there are many states and many transitions and states have entry / exit routines.