flowchart LR
BTN["Button ISR"] -->|post| Q[("Queue")]
TMR["Timer"] -->|post| Q
BLE["BLE"] -->|post| Q
Q -->|wait| L["loop()"]
L --> T["handler_table<br/>[state][event]"]
T --> H["Handler → next state"]
Organizing buttons, timers, BLE and sensors with an event queue and a state transition table
delay() or while waits stop everythingloop()flowchart LR
BTN["Button ISR"] -->|post| Q[("Queue")]
TMR["Timer"] -->|post| Q
BLE["BLE"] -->|post| Q
Q -->|wait| L["loop()"]
L --> T["handler_table<br/>[state][event]"]
T --> H["Handler → next state"]
Event = ID + payload copied by value + timestampxQueueSendToBackFromISR; from a task a non-waiting xQueueSendToBack
event_post()portMAX_DELAYdropped| State | BUTTON_SHORT | BUTTON_LONG | TIMER_100MS | TIMER_1S | CMD_START | CMD_STOP |
|---|---|---|---|---|---|---|
| WAIT | → MEAS | — | update latest | — | → MEAS | — |
| MEAS | — | → WAIT | sample | aggregate & notify | — | → WAIT |
nullptr = ignore. Gaps can be discussed before writing codetypedef State (*EventHandler)(const Event &ev);
const EventHandler handler_table[STATE_MAX][EVT_MAX] = {
[STATE_WAIT] = { [EVT_BUTTON_SHORT] = wait_on_button_short, ... },
[STATE_MEAS] = { [EVT_BUTTON_LONG] = meas_on_button_long, ... },
};
State dispatch(State cur, const Event &ev) {
EventHandler h = handler_table[cur][ev.id];
return h ? h(ev) : cur;
}| Location | Context | What to do |
|---|---|---|
Button attachInterrupt |
ISR | Record time + post |
SoftwareTimer |
Timer task | post |
| Bluefruit callbacks | BLE task | Interpret + post |
| Handlers | loop task | Processing (I²C, sending, etc.) |
delay / Serial / I²C forbidden; shared variables are volatile; debounce00 01 info / 01 00 start / 01 01 stop / 02 01 latest valuerequestDevice({ filters: [{ services: [UUID] }] })delay() in handlers (compute the LED from millis())event_init() firstble.h collides with SoftDevice)app/ from hardware and test on the PC