LateralT

Posts tagged with "code"

fsmlib: a small and simple library for Finite State Machines

TLDR: get it here

I wrote this to scratch my own itch. Not because I suffer from NIH syndrome, but because I genuinely searched the web for finite small and simple state machine libraries and I couldn't find what I was looking for. They were all either:

  • Overly complex or bloated.
  • Toy libraries with no real use at all.
  • Ad-hoc implementations suited to a very specific application (but advertised as general-purpose.

So I rolled my own. This one is:

  • Small. Only one source file and one header file. Ideal to dump them into your code base and forget about them.
  • Feature-rich. Specially for a small library. I did it with a pragmatic approach, not an academic one, so you can use it to create both Mealy and Moore FSMs or any kind of hybrid monstrosity you can think of.
  • Application agnostic. It doesn't make any assumptions about the target application.
  • POSIX compatible, in all systems that implement timer_* functions and the itimerspec struct (so no MacOS).
Features:
  • Asynchronous (threaded) and synchronous states.
  • Asynchronous (threaded) and synchronous FSMs.
  • Entry and exit actions.
  • Time-triggered and event-triggered transitions.
  • Transition-triggered functions.

I think there might be some things that could be improved, removed or refactored. There are some "TODO" notes sprinkled over the code, but nothing very important. If you can improve it in any way, please do it.