Crate sfsm[][src]

Static finite state machine

State machines are a existential 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 help to break down complexity, they must also be well documented to be understandable.

Rust lends itself to implement state machines fairly well 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:

  • 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

 extern crate sfsm_proc;
 extern crate sfsm_base;
 use sfsm_proc::add_state_machine;

 // To start out, first define the state machine.
 add_state_machine!(
    StaticSfms,  // Name of the state machine. Used to run it later
    InitState,   // The initial state the state machine will start with
    [
         // Define all states. These states must correspond to a struct
         InitState,
         EndState,
         WaitingState
    ],
    [
         // Define all transitions with: Src -> Dst
         InitState -> WaitingState,
         WaitingState -> EndState
    ]
 );

 // Add the structs that correspond to the defined states.
 use sfsm_base::State;
 use sfsm::sfsm_base::Transition;
 struct InitState {
 }

 struct WaitingState {
    counter: u32,
 }

 struct EndState {
 }

 // Implement the states traits
 // ...
 impl State for WaitingState {
     fn entry(&mut self) {
         println!("****************************************");
         println!("Waiting: Enter");
     }
     fn execute(&mut self) {
         self.counter += 1;
         println!("Waiting: Execute");
     }
     fn exit(&mut self) {
         println!("Waiting: Exit");
     }
 }

 // ...

 // Then implement the transitions
 // ...
 impl Transition<WaitingState> for InitState {
     fn entry(&mut self) {
         println!("Init -> Waiting: Enter");
     }
     fn execute(&mut self) {
         println!("Init -> Waiting: Execute");
     }
     fn exit(&mut self) {
         println!("Init -> Waiting: Exit");
     }
     fn guard(&self) -> bool {
         return true;
     }
 }
 impl Into<WaitingState> for InitState {
     fn into(self) -> WaitingState {
         WaitingState {
             counter: 0,
         }
     }
 }

 // And then run the state machine.
 let init = InitState {
 };

 // Create the state machine with the name defined and pass the initial state into it.
 let mut sfsm = StaticSfms::new(init);

 sfsm.step();

 sfsm.step();

 sfsm.step();

 sfsm.step();

This will then produce the following output:

 ****************************************
 Init: Enter
 Init -> Waiting: Enter
 Init: Execute
 Init -> Waiting: Execute
 Init: Exit
 Init -> Waiting: Exit
 ****************************************
 Waiting: Enter
 Waiting -> End: Enter
 Waiting: Execute
 Waiting -> End: Execute
 Waiting: Execute
 Waiting -> End: Execute
 Waiting: Exit
 Waiting -> End: Exit
 ****************************************
 End: Enter
 End: Execute

For more detailed descriptions about the traits, look at the sfsm-base doc.

Re-exports

pub extern crate sfsm_base;
pub extern crate sfsm_proc;