Skip to content

An example of an event-driven finite state machine (fsm) in Rust, by way of modeling a mechanical lock

License

Notifications You must be signed in to change notification settings

moonbench/rust-combo-lock-fsm

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

This is an interactive example of an event-driven finite state machine in Rust, demonstrated by modeling a mechanical combination lock.

The state machine

The states and transitions implemented by this look like:

State machine diagram

There is a shared combination value in the machine which is an array of i8s.

There are three states: UnlockedOpen, UnlockedClosed, and Locked.

There are five events: SetCode, Close, Open, Lock, and Unlock. And the states each respond to a subset of the events.

It can be represented textually like:

   ______________                ________________                 ________
  |              | -- CLOSE --> |                |               |        |
  |   Unlocked   |              |    Unlocked    | --- LOCK ---> | Locked |<-------.
  |     Open     |              |     Closed     |               |        |        |
  |______________| <-- OPEN --- |________________|               |________|        |
       |    ^                           ^                           |              |
       |    |                           |                    UNLOCK |         (NO) |
       \____/                           |                    _______V__________    |
                                        |                   /                  \   |
      SET CODE                          \___________________| Is Correct Code? |___/
                                                    (YES)   \__________________/

Interactive

This demo has an interactive command-line component to let the user interact with the state machine.

There are no depedndencies besides Rust, so simply download the code and run it with cargo run.

You can then interact with the machine. Type help for a list of all of the commands.

Finite State Machine Lock Demo

         ________
        |  ____  |
        | |    | |
        |_|    | |
         ______|_| OPEN
        |   __   |
        |  [  ]  |
        |   []   | UNLOCKED
        |   []   |
         \______/

Enter command: set 11 12 13
[DONE] Code changed
         ________
        |  ____  |
        | |    | |
        |_|    | |
         ______|_| OPEN
        |   __   |
        |  [  ]  |
        |   []   | UNLOCKED
        |   []   |
         \______/

Enter command: close
[DONE] Lock closed
         ________
        |  ____  |
        |_|____|_| CLOSED
        |   __   |
        |  [  ]  |
        |   []   | UNLOCKED
        |   []   |
         \______/

Enter command: lock
[DONE] Locked
         ________
        |  ____  |
        |_|____|_| CLOSED
        |   __   |
        |  [**]  |
        |   []   | LOCKED
        |   []   |
         \______/

Enter command: open
[ERROR] Not allowed for a locked lock. Unlock it first.
         ________
        |  ____  |
        |_|____|_| CLOSED
        |   __   |
        |  [**]  |
        |   []   | LOCKED
        |   []   |
         \______/

Enter command: unlock 0 0 0
[ERROR] Invalid combination, still locked
         ________
        |  ____  |
        |_|____|_| CLOSED
        |   __   |
        |  [**]  |
        |   []   | LOCKED
        |   []   |
         \______/

Enter command: unlock 11 12 13
[DONE] Valid combination, unlocked!
         ________
        |  ____  |
        |_|____|_| CLOSED
        |   __   |
        |  [  ]  |
        |   []   | UNLOCKED
        |   []   |
         \______/

Enter command: open
[DONE] Lock opened
         ________
        |  ____  |
        | |    | |
        |_|    | |
         ______|_| OPEN
        |   __   |
        |  [  ]  |
        |   []   | UNLOCKED
        |   []   |
         \______/

A post with more details: https://moonbench.xyz/projects/rust-event-driven-finite-state-machine/

About

An example of an event-driven finite state machine (fsm) in Rust, by way of modeling a mechanical lock

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages