Simple Statemachine

This rust crate defines the statemachine!() macro. As the single argument the macro takes the definition of a statemachine, described in a simple and easy to ready DSL.

The statemachine macro supports guards, actions on triggers, state entry and exit. Events may carry payload, e.g. for implementation of communication systems.

The full documentation is available on docs.rs.

Usage

Add this to your Cargo.toml: toml [dependencies] simple-statemachine = "1.0"

Example

This implements the pedestrian part of a simple traffic light with a button to switch the traffic light to "walk".

```rust use std::thread; use std::time::Duration; use simple_statemachine::statemachine;

statemachine!{ Name TrafficLightStatemachine InitialState DontWalk

DontWalk { ButtonPressed ==setbuttontopressed=> DontWalk TimerFired[checkbuttonispressed] ==switchtowalk=> Walk TimerFired => DontWalk } Walk { OnEntry setbuttontonotpressed TimerFired ==switchtodont_walk=> DontWalk } }

struct LightSwitch{ buttonpressed:bool } impl TrafficLightStatemachineHandler for LightSwitch{ fn checkbuttonispressed(&self) -> bool { self.buttonpressed } fn setbuttontopressed(&mut self) { self.buttonpressed=true; } fn setbuttontonotpressed(&mut self) { self.buttonpressed=false; } fn switchtowalk(&mut self) { println!("Switching \"Don't Walk\" off"); println!("Switching \"Walk\" on"); } fn switchtodont_walk(&mut self) { println!("Switching \"Walk\" off"); println!("Switching \"Don't Walk\" on"); } } ```