1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
#![cfg_attr(not(test), no_std)] //! # Static finite state machine //! //! Sfsm tries to achieve these objectives, by providing a state machine generator in sfsm-proc and //! a transition as well as state trait in sfsm-proc. With this, the user can specify the whole state //! machine on a few lines that are easy to review. From this definition, the whole state machine //! can be generated without relying on dynamic mechanisms and thus allows to be fully static. //! All that is left to do, is to implement the states and transition necessary to fulfill the //! Transition and State traits. //! //! State machines are an essential part of many software architectures and are particularly common on low //! level systems such as embedded systems. They allow a complicated system to be broken down into many //! small states with clearly defined transitions between each other. But while they help to break down //! complexity, they must also be well documented to be understandable. //! //! Rust is well suited to implementing state machines thanks the way its enums are designed. //! Unfortunately this still comes with a large amount of boilerplate. //! //! Sfsm aims to let the user implement simple, efficient and easy to review state machines that are usable //! on embedded systems. The main objectives therefore are: //! //! The main objectives therefore are: //! - no_std compatibility //! - Self documenting //! - Easy to use //! - Low cost //! //! Sfsm tries to achieve these objectives by providing a state machine generator in sfsm-proc and a //! transition as well as state trait in sfsm-proc. With this, the user can specify the whole state machine on //! a few lines that are easy to review. From this definition, the whole state machine can be generated //! without relying on dynamic mechanisms and thus allows to be fully static. All that is left to do is to //! implement the states and transition necessary to fulfill the Transition and State traits. //! //! //! # How to use //! To see the whole example, expand the source //!```rust //! extern crate sfsm_proc; //! extern crate sfsm_base; //! use sfsm_proc::{add_state_machine, is_state, match_state_entry}; //! use sfsm_base::{State, Transition}; //! use std::marker::PhantomData; //! //! // To start out, first define the state machine. //! add_state_machine!( //! Hiker, // Name of the state machine. Used to run it later //! Hike<Up>, // The initial state the state machine will start with //! [ //! // Define all states. These states must correspond to a struct //! Hike<Up>, //! Hike<Down>, //! Picknic //! ], //! [ //! // Define all transitions with: Src -> Dst //! Hike<Up> -> Picknic, //! Picknic -> Hike<Down> //! ] //! ); //! //! // Add the structs that correspond to the defined states. //! struct Up {}; //! struct Down {}; //! //! struct Hike<Dir> { //! marker: PhantomData<Dir>, //! is_down: bool, //! } //! //! struct Picknic { //! apples: u32, //! } //! //! // Implement the states traits //! # impl State for Hike<Up> { //! # fn entry(&mut self) { //! # println!("****************************************"); //! # println!("Hike<Up>: Start hiking up"); //! # self.is_down = false; //! # } //! # fn execute(&mut self) { //! # println!("Hike<Up>: Keep walking"); //! # } //! # fn exit(&mut self) { //! # println!("Hike<Up>: Take a break"); //! # } //! # } //! impl State for Picknic { //! fn entry(&mut self) { //! println!("****************************************"); //! println!("Picknic: Start eating a picknick"); //! } //! fn execute(&mut self) { //! self.apples -= 1; //! println!("Picknic: Eat an apple"); //! } //! fn exit(&mut self) { //! println!("Picknic: Get up"); //! } //! } //! //! # impl State for Hike<Down> { //! # fn entry(&mut self) { //! # println!("****************************************"); //! # println!("Hike<Down>: Start walking back down"); //! # } //! # fn execute(&mut self) { //! # println!("Hike<Down>: Keep walking"); //! # } //! # fn exit(&mut self) { //! # println!("Hike<Down>: Go back home"); //! # println!("****************************************"); //! # self.is_down = true; //! # } //! # } //! //! // Note: The state implementation for Hike<Up> and Hike<Down> is hidden //! //! // ... //! //! // Then implement the transitions //! # impl Transition<Picknic> for Hike<Up> { //! # fn guard(&self) -> bool { //! # return true; //! # } //! # } //! impl Transition<Hike<Down>> for Picknic { //! fn entry(&mut self) { //! // You might want to do something here. Like starting to count apples //! } //! fn execute(&mut self) { //! // You could keep counting apples //! } //! fn exit(&mut self) { //! // You might want to sum up what you counted before //! } //! fn guard(&self) -> bool { //! return self.apples == 0; //! } //! } //! //! # impl Into<Picknic> for Hike<Up> { //! # fn into(self) -> Picknic { //! # Picknic { //! # apples: 3, //! # } //! # } //! # } //! impl Into<Hike<Down>> for Picknic { //! fn into(self) -> Hike<Down> { //! Hike { //! marker: PhantomData, //! is_down: false, //! } //! } //! } //! //! // Note: The transition Hike<Up> -> Picknic is hidden //! //! // ... //! //! // And then run the state machine. //! let init: Hike<Up> = Hike { //! marker: PhantomData, //! is_down: true, //! }; //! //! // Create the state machine with the name defined and pass the initial state into it. //! let mut sfsm = Hiker::new(init); //! //! // If you want to check which state the machine currently is in, you can peek it. //! // Note that the generated enum will be named: [CHOOSEN_NAME_OF_SFSM]States and the entries //! // will be called [NAME_OF_STRUCT_WITH_TYPES]State //! let in_state = sfsm.peek_state(); //! //! // The is_state! macro helps you to quickly test if its the state you expect. //! assert!(is_state!(in_state, Hiker, Hike<Up>)); //! //! // Start stepping! //! sfsm.step(); //! assert!(is_state!(sfsm.peek_state(), Hiker, Picknic)); //! //! sfsm.step(); //! assert!(is_state!(sfsm.peek_state(), Hiker, Picknic)); //! //! sfsm.step(); //! assert!(is_state!(sfsm.peek_state(), Hiker, Picknic)); //! //! sfsm.step(); //! assert!(is_state!(sfsm.peek_state(), Hiker, Hike<Down>)); //! //! sfsm.step(); //! //! // Once you are done using the state machine, you can stop it and return the current state. //! let exit = sfsm.stop(); //! assert!(is_state!(exit, Hiker, Hike<Down>)); //! //! match exit { //! // If you don't want to type out the state enum use the match_state_entry! macro here //! // It generates the following: [SFSM_NAME]States::[STATE_NAME_AND_TYPES]State(state) //! // Otherwise you have to type it out manually with the given schema. //! match_state_entry!(Hiker, Hike<Down>, exit_state) => { //! // Access "exit_state" here //! assert!(exit_state.unwrap().is_down); //! }, //! _ => { //! assert!(false); //! } //! } //! //!``` //! This will then produce the following output: //!```text //! **************************************** //! Hike<Up>: Start hiking up //! Hike<Up>: Keep walking //! Hike<Up>: Take a break //! **************************************** //! Picknic: Start eating a picknick //! Picknic: Eat an apple //! Picknic: Eat an apple //! Picknic: Eat an apple //! Picknic: Get up //! **************************************** //! Hike<Down>: Start walking back down //! Hike<Down>: Keep walking //! Hike<Down>: Go back home //! **************************************** //!``` //! For more detailed descriptions about the traits, look at the sfsm-base doc. extern crate sfsm_proc; extern crate sfsm_base; pub use sfsm_proc::*; pub use sfsm_base::*;