bionsourcing.blogg.se

Stack the states online unblocked
Stack the states online unblocked











#Stack the states online unblocked code

The code presented in the OP does not have any of these properties, and therefore it cannot be called a "state machine". Variable" (could be encapsulated inside a class) which indicates the currently active state, (2) clear rules ofĬhanging the "state variable", which are called "state transitions", and (3) it should be possible to depict the operation of the code in a stateĭiagram. So, to call a piece of code "state machine", it must have at the very leastĪ few properties, such as: (1) a clearly identifiable, central "state Specifically, "remembering" the context, means remembering only the current state (in one state-variable) instead of many variables. This is also known as "spaghetti" or BBM (Big Ball of Mud).Īnd here is where state machine comes in. A state machine is a very effective way of remembering the relevant context between events in the form of state. Then you have many layers of IFs and ELSEs, where the flags and variables are tested and set. The ad hoc, "manual context management" typically uses a bunch of variables and flags that "remember" the history of the system. So, in event-driven code the context must be preserved differently, typically in some static (or global) variables. The context then cannot be preserved on the call stack, because the stack is unwound after each call. The code has to process every event to-completion (RTC - Run-to-Completion processing) and then, after each event, the code returns back to the event-loop. In contrast, truly event-driven code does not block. and after the second delay, you turn the LED on. For example, after the first delay(), you known that its time to turn the LED off. When the call unblocks, you pick up exactly where you left off, so you know what to do. Between the events, the context is preserved on the call stack. In the Blinky code, you have two hard-coded TIMEOUT events (each corresponding to the delay() call). In a sequential code then the expected sequence of events is hard-coded. Unblocking then represents the delivery of that event. In a sequential code, every blocking call represents explicit waiting for a given event. BTW, The aforementioned presentation also explains the concept of a state machine.īut here quickly, the main difference is how the context is preserved between events. My whole recent presentation at the Embedded Online Conference uses this exact Arduino-Blink example to explain the difference between the sequential, blocking code (like your Blink) and event-driven, non-blocking code that can become a state machine. In fact, it is an antithesis of a state machine. No! The code you are showing is NOT a state machine.











Stack the states online unblocked