
Show HN: µFSM – A state chart library for embedded applications - j0p
https://github.com/jonpe960/ufsm/tree/ufsm-0.1.0
======
aylons
Very nice! Anything that allows something more sophisticated than switch:case
in embedded systems are welcome! Also, state machines are way too often
overlooked as a proper model for embedded systems architecture. My guess is
that proper implementation may become hard (specially if you want to nest
them), and there are lots of hardware engineers doing firmware.

In my case, when I want to use a state machine for a project, I usually go
with Quantum Leaps [1] tools. Nowadays their website seems cluttered and
management-focused, but the tech is solid and the tools are very robust.

They also distribute a book [2] on how to use the model using their framework
as an example, and I even implemented my version of it as an exercise years
ago.

It does not have (or did not, I haven't checked in the last few years) a tool
to translate from XMI to their framework, but they have a freeware to draw the
chart. Frankly, I have not even used it: I draw the state machine using some
editor and it is easy enough to translate to code and maintain. Maybe not the
best, but up until now, very nice to deal with the limited complexity of most
embedded systems.

[1] [https://www.state-machine.com/doc/concepts#HSM](https://www.state-
machine.com/doc/concepts#HSM) [2] [https://www.state-
machine.com/psicc2/](https://www.state-machine.com/psicc2/)

~~~
elcritch
I second the use of statemachine for embedded systems.

They’re particularly ideal for embedded systems. The limited ram in many
embedded systems makes a full dynamic language difficult to fit but a state
chart interpreter can be very minimal while production runs can be compiled
directly to code. Memory management in state charts can often be much easier
to handle.

There’s also a lot of benefit to describing valid states for your system both
in terms of correctness and the ability of algorithmically finding an optimal
reduced state machine.

Personally I am fond of SCXML as it’s fairly concise, can include code
snippets, and has a relatively well documented standard easy to display by
converting to Vue/React/WebComponents. XMI with its connection to UML didn’t
seem appealing to me, but this project seems pretty robust!

~~~
oldgeezr
> They’re particularly ideal for embedded systems.

While I agree, I think this promotes the idea that they're only good for
embedded systems, especially small ones. State machines are not always the
right tool for the job... only most of the time :)

State machines(or DFAs for you CS guys) are used all over. For instance,
AFAIK, grep is generally implemented with a state-machine, albeit one which is
dynamically constructed at run-time(which is a pretty cool concept if you ask
me).

~~~
elcritch
True! Statecharts are handy in lots of domains. Embedded happens to be a prime
target due to the memory constraints combined with often limited behavioral
requirements.

IMHO, most websites / UI's implement half of a poorly understood state machine
(to roughly paraphrase the Common Lisp quip). Qt apparently also has an
interesting statechart engine as well: [http://blog.qt.io/blog/2017/01/23/qt-
scxml-state-chart-suppo...](http://blog.qt.io/blog/2017/01/23/qt-scxml-state-
chart-support-qt-creator/)

------
slig
A must read for anyone interested
[https://statecharts.github.io/](https://statecharts.github.io/) Also I highly
recommend to check out `xstate` TypeScript library. It's a breeze to work with
it.

~~~
davidkpiano
Thanks so much for the praise (I created `xstate`)! I'm working hard on making
it fully SCXML-compatible.

~~~
slig
Thank you for your work on it!

------
kuwze
On the topic of state-machines, there are some really cool typed libraries out
there. The ones that stand out are motor[0] for Haskell and ST[1] for Idris.

[0]:
[https://hackage.haskell.org/package/motor](https://hackage.haskell.org/package/motor)

[1]: [http://docs.idris-lang.org/en/latest/st/state.html](http://docs.idris-
lang.org/en/latest/st/state.html)

------
nsomaru
For Django there's django-fsm[0] and django-viewflow[1]. I've used the former
to implement some flow for an ecommerce website.

[0] [https://github.com/kmmbvnr/django-
fsm/](https://github.com/kmmbvnr/django-fsm/)

[1]
[https://github.com/viewflow/viewflow](https://github.com/viewflow/viewflow)

------
samatman
I'm impressed by this and like it a lot.

If you don't mind a 'drive-by' feature request: it would be fantastic if you
were to support the PlantUML state DSL:

[http://plantuml.com/state-diagram](http://plantuml.com/state-diagram)

That would push me from "interesting proof of concept" to figuring out how to
use it in projects.

~~~
j0p
Thanks =) I had a quick look att plantuml and it seems to support SCXML
export, which I will add in a near future which might be useful for you.

~~~
samatman
Great!

This has important consequences for visually impaired programmers, so I'm glad
of that as well.

------
0x5345414e
Thanks for sharing! I'm working on a device and I've been searching for a good
FSM library in C with no dynamic allocation :)

~~~
okreallywtf
Same here, I've started 2 or 3 times and not liked any of my own
implementations, this might actually be useful to me _tonight_.

------
sitkack
I'd like to see a combination of this and
[https://en.wikipedia.org/wiki/Subtext_(programming_language)](https://en.wikipedia.org/wiki/Subtext_\(programming_language\))
along with a visualizer debugger that could scrub forwards and backwards in
time.

------
solarapex
There are so many FSM implementations. The problem is when you want to
implement something like this in different platforms, one has to "change the
engine" along with all the connectors. Why not to use something standard like
BPMN as the base?

------
mogsie
I added it to the growing list of statechart libraries in various languages:
[https://statecharts.github.io/resources.html#tools--
librarie...](https://statecharts.github.io/resources.html#tools--libraries)

------
elviejo
Is there a programming language where FSM are a first class citizen?? And not
just a library??

~~~
rdtsc
Yes. Erlang has
[http://erlang.org/doc/man/gen_statem.html](http://erlang.org/doc/man/gen_statem.html)

It is part of the Erlang's standard library. SSH and SSL state and connection
management has been re-written recently to use this built-in module.

Here is a video of Raimo Niskanen, the author, describing the module:

[https://www.youtube.com/watch?v=f_jl6MR3kXQ](https://www.youtube.com/watch?v=f_jl6MR3kXQ)

The title is "The tool that you never knew that you always wanted", which was
borrowed from another member of the community (Jesper L. Andersen)

------
rhencke
Thanks for sharing this. I really like the idea of being able to extract the
state machine from a higher-level spec you've included here. The DHCP client
example especially is really nice.

------
analognoise
It requires a paid tool (Staruml) to extract a fsm from a state chart?

Regardless, this looks very cool I will dive in later.

~~~
j0p
I've used the evaluation version

------
_pmf_
What do you recommend as editor? Does Papyrus work?

~~~
j0p
I've only tested with StarUML, but the plan is to support more tools

------
nrclark
this looks awesome!!

------
speps
Probably worth having a look at this:
[https://youtu.be/c9Xt6Me3mJ4?t=4196](https://youtu.be/c9Xt6Me3mJ4?t=4196)

Embedded shouldn't rhyme with "C".

~~~
dljsjr
It's true that embedded software doesn't _need_ to be written in C but if
you're distributing a _library_ that's aimed at embedded systems then C is
still a terrific choice for a ton of reasons. The goal of a library should be
to embrace the path of least resistance for adoption. And when you're working
in the embedded space that means targeting the least common denominator. Are
all embedded systems extremely resource constrained? No, but many are. Do all
applications running on embedded platforms need to be real-time? No, but many
do.

Writing in C means you can get small binaries, with minimal runtimes, easy
control over things like memory allocation, and if you want to use the library
you don't _have_ to write C because almost any language with any form of FFI
is going to target C before it targets C++.

