Code Monkey home page Code Monkey logo

chsm's Introduction

CHSM - Hierarchical State Machine (HSM) Framework for C

⚠️ Experimental code, expect breaking changes.

The project consists of two main parts: an editor (CHSM) to draw state machines and a C library (CRF) that is able to drive the C code generated by the editor.

CHSM (editor) features:

  • Written in Python and Vanilla Javascript. (Tested with Python 3.7+ and Chromium on RPi4 and with Chrome and Firefox on Win10.)
  • The editor can generate C code from the drawings.
  • Application file format is Html. Can be opened without the editor in view-only mode with any browser.
  • Comments can be added to any string.
  • Comments are displayed as tooltips on mouse hover and added as actual C comments to the generated code.
  • Mostly follows the UML statecharts specification (event deferral is an exception).

The CRF (C Reactive Framework) design was heavily influenced by Miro Samek's excellent book: Practical UML Statecharts in C/C++. If you are not familiar with UML statechars then I recommend reading it. All the important concepts are explained very clearly in a first few chapters with easy to understand examples.

CRF features:

  • Run-to-completion (RTC) execution model
  • Written in C99. (Uses C11 atomics when available, if not, than the application must define two simple atomic functions.)
  • State transitions are precalculated at code generation to avoid the runtime cost of hierarchy discovery.
  • All states are represented by functions that process events with a switch statement.
  • Optional event pools that provide simple O(1) dynamic memory handling to releive modules from buffering.
  • Wait-free multi-producer single-consumer event queues for each state machines.
  • Events can be deferred and recalled.
  • Events can be safely generated and distributed by interrupts.
  • Optional event generators for "analog" int32 values and uint8_t bitfields.
  • Unit tests (using the Unity framework).

Dependencies

Of the GUI and code generation

  1. Chrome. Theoretically other browsers can be used, but only Chrome was tested.

  2. Python 3.7+ The following packages are required:

    • eel
    • docopt

    You can install the packages like this: pip3 install eel docopt

Of the C unit tests

The C code uses the Unity unit testing framework for running the tests. The CMake script that builds the tests assumes that the Unity and the CHSM repos are in the same directory.

Cloning: git clone https://github.com/ThrowTheSwitch/Unity.git

Usage

  1. Clone the repo
  2. Checkout the master branch
  3. Navigate into the chsm folder, open a command prompt and run this command: python3 cgen/chsm_backend.py

The result should be a new window with a simple state machine already in it.

chsm's People

Contributors

jszeman avatar

Stargazers

Zsolt avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

xsession

chsm's Issues

State resize can result in external substates

It is possible to resize a state so that a substate remains out of the border, but it is still connected as children.

Suggested resolution: limit resize operation so that the top left corners of all substates are still inside the body of the state after the operation finished.

Autosave

Automatic save, for the edited html. For example every 5min.

Transition can stuck under a state.

Steps to reproduce:

  1. Draw a state
  2. Draw a transition from the state to itself in the header of the state.

This is caused by prepending transitions to the drawing (to remove flickering and make it easier to click on state borders during transition drawing operations). Should be easy to fix by managing stacking order during transition operations.

Implement transition redraw features

Operations to implement:

  • Simplify path: when a line segment is dragged to line up with an adjacent segment the segments shall be converted to one continous segment.
  • Split path: double clicking on a line segment shall dividide it into two.
  • Redraw from segment: press 'r' than click on a line segment. The transition end point shall be detached from the target state, and all vertices after the given line segments shall be deleted. The user than shall be able to finish drawing the transition normally.

Branch: feature/tr-redraw

Change text in state

The model and the gui modules shall be prepared for changing the text in the states. Adding a new line shall increase the height of the state while keeping the position constant. All child states shall be moved accordingly and the transitions with connectors in the state or in one of its substates shall be updated.

Move multiple states together

Expected operation:

  • Press CTRL key
  • Select states by clicking on them
  • Release CTRL key
  • Drag any of the selected states to drag all of them
  • Transitions internal to the selected group shall be moved with the group without rerouting (translate only)

Saving the changes with Ctrl+S

Now when i press the Ctrl+S it want to save the whole html like save as, insted of the actual changes in the textfields.

implementation of model.states() function

Currently the states() func returns with the children of the 'top' as an array. The problem with this is that the 'top' must have all other states in it's children array. If not, then many function won't work. For example the gui won't render them, or the model.find_state_at() func won't find a state under the specified coordinate.

I think the implementation of this func should build a new array according to the current states in the model pool.

CRF code is not thread safe.

The CRF C code is not thread safe, it was written just have something that implements the interfaces in a limited way to enable testing the whole concept.
The cqueue and cpool modules should be rewritten to allow calling them from the main thread and from (non-nested) interrupt handlers.

Implement delete state operation.

Expected operation:

  1. Press 'd'
  2. Move cursor onto the state (header) to be deleted
  3. The state border shall be highlighted in red at this point
  4. A mouse click shall delete the state
  5. All connected transitions shall be deleted as well. The user is required to redraw/reroute any transitions that have to be preserved before the del operation.
  6. Pressing ESC any time shall abort the operation.

@progszem: feature/del-state branch was created for this issue.

Property editor

There shall be a way to edit the text on the states and transitions. Best would be a sidebar on the left that would show all the text (title, event handlers, comments) of the last clicked state or transition in editable widgets.

Initial state

There shall be a way to mark a substate as the initial state. The standard UML solution is a start pseudostate and a transition to the initial state. Should not be very hard to implement with a specially rendered state with 2x2 size and some CSS magic.

Implement undo/redo

Theoretically it should be enough to just save the app.model.data every time we exit the idle state and the restore it on command. This might be overkill, but implementing it is simple and involves minimum amount of cooperation from the operations side.

Drawing transitions

If i start drawing transitions then it would like a nice feature, that after the first line drawed it stays in drawing mode until press escape or right mouse click.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.