project with xstate / fsm port for Espruino

Posted on
  • Hello,
    Im new to Espruino but have a good background in computer science and have been working since I retired, to learn a sound microcontroller platform for some projects I have in mind. Ive honed in on the xstate finite state machine­te-fsm/ as a great framework for structuing code for automation applications. Ive had great success with this on Raspberry pi running Node. But am trying now to find a lower total cost option. Ive had some success with xstate on low.js on esp32, but not many drivers available,extent of open source is limited and seems to have a small community.

    So im trying to port xstate/fsm to Espruino* whcih does not seem so limited. It is a challange for my experience. But I have got a basic port and some simple xstate machine configs to run. Generally the issues Im having with the more sophisticated examples are Javascript features failing execution in Espruino. Some ive cracked with work arounds eg (module exports format, functions with optional parameters, multiple const defs in array format) and some are proving more difficult eg Set objects.

    Not sure how successfull this will be but happy to share all results, write up cases and test the work on Espruino boards (im using esp32 initially) and def would like to publish the xstate/fsm port in the tested modules.

    Does this work seem benneficial to the community ??

    Is there a list anywhere of what javascript features are or are not in scope at them moment and where to best ask/confirm or note any apparent gaps ?? Any pointers for using the forum or github issues appreciated.

    • I appreciate there is an FSM in the existing device modules but would like a few more of the features available in xstate (eg named events, triggering specific state changes) and xstate appears to be getting lots of traction with a large community.


  • Wed 2021.08.18

    Is there a list anywhere of what javascript features are or are not in scope at them moment
    Javascript features failing execution

    Hi Simon, @SimonGAndrews welcome to the world of Espruino!

    Not sure if this is what you are after, as I'm not familiar with the described environment.

    Any pointers for using the forum

    A few years back I started to notice a pattern of questions individuals always seemed to ask, so I put together helpful hints for others to find insight.

    At the top of the forum topic 'Tutorials'


    or open that thread and search the plethora of links that many usually require halfway down the page:

    Writing an effective forum post

    where to best ask/confirm or note any apparent gaps?

    Right here for starters. If a really specific topic stands out, then maybe a new thread within either the ESP32 of if language specific beneath that under other, would assist others in the future. The moderator will coach or move when appropriate, so no worries. We are all easy going here.

  • Hi,

    Thanks for posting up - this sounds really interesting.

    Personally, I wouldn't advocate using frameworks inside Espruino as often you just end up including a bunch of bloat. But that doesn't mean you shouldn't try!

    As @Robin said the Features page has a list of what's supported and what isn't. You may find you have some success transpiling the module to an earlier version of JavaScript to remove some of the features that aren't available.

    In terms of Set, you may find you can just use Object accesses - for instance:

    mySet = {}l
    Set.prototype.add(value) -> mySet[value]=1
    Set.prototype.clear() -> mySet={};
    Set.prototype.delete(value) -> delete mySet[value]
    Set.prototype.has(value) -> mySet[value]

    It's more limited with the types of values you can add, but it may still be enough for you.

  • Thanks both @Robin and @Gordon for the positive response and helpfull advice. Robins tutorial and the features page and set alternative idea were just what was needed. Im working a fork of XState/fsm in github (xstate-fsm-Espruino) which is very much WIP.

    Hoping bloat will not be an issues as Xstate is designed with that risk in mind.

    I got a positive reponse from davidkpiano (author of Xstate) also on thier Discord community.­8994652170/810916847573532742/8786072587­85808385

  • Hi, progressing slowly but surely with xstate for Espruino. But got a problem trying to find a method in Espruino to return the name of a function.

    • Looks like is not supported
    • also many of the polyfills and work arounds parse the name out of function.toString() But Espruino does not put the function name in the function.toString output.

    Its not a show stopper , but any ideas would be appreciated.

    1 Attachment

    • functionName.PNG
  • quick status on the project above is I now have a working version published in the repository , with installation guide and the first examples !! check out the demo­fsm-Espruino/blob/main/examples/030_butt­onEvent/buttonEvent.gif

  • So been continuing with xstate-fsm-Espruino (as above ... XState/fsm for Espruino microcontrollers) but in test scenarios was very quickly missing the hierarchical functionality I had gotten use to with full XState on the Raspberry pi. So I've started to work on putting limited state chart functionality on top of xstate-fsm for Espruino. Early tests are can add hierarchical states with a few functions that manipulate state value (using a full XState ID format Inc custom IDs)' , get state parents etc. Hopefully still not breaking minimal memory constraints of the Espruino micros. Or deviating from XState principals. Will publish when a little more proven. Great fun in any event, and learning loads from XState as an example of functional programming in JS and Espruino, But what to call xstate-fsmPlus-Espruino ???

  • so Ive had my implementation of Xstate in Espruino on hold while working on getting USB Host working on Pico (both essential to my project) . But the status of Xstate in Espruino work is well beyond proof of concept and is a point of testing nested states , which are one of the most basic yet usefull extensions of statecharts over simple finite state machines.

    But in parallel the Xstate world is moving on leaps and bounds.­ublic-beta
    Now the capability is there to design with drag and drop , inside visual studio, a state chart representation of a finite state engine and generate the javascript code and object definition of the state engine.
    So this means with a small javascript framework in Espruino we would be able to change the logic of our microcontroller in a standard , visual representation, readily transferrible from one Espruino Hardware environment to another. Theres a great vision here. (just got to get USB host working !!)

    check out Xstate when you can and imagine it working in Espruino.

    will update ASAP

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview

project with xstate / fsm port for Espruino

Posted by Avatar for SimonGAndrews @SimonGAndrews