Coder Social home page Coder Social logo

microstate's Introduction

microstate

a micro footprint C++ state machine

This C++ state machine is a header-only "library" that is designed to ease the programming of embedded devices. As a typical use case, it supports structuring the execution of your code within a single main loop, e.g., you can define multiple state machines that each control certain aspects of your device's behavior as if you had multiple threads of execution running in parallel.

Usage of the state machine is pretty simple:

  1. define your state labels using an enum class
  2. define the state machine
  3. execute the state machine step by step

Example:

#include "microstate.h"

enum class MyStates {
	initial,
	counting,
	return_
};

int foo;

auto my_state_machine = mst::make_state_machine(
	MyStates::initial,					// set start state
	MyStates::return_,					// set return state

	mst::make_state(					// define states
		MyStates::inital,
		[]() -> MyStates {
			foo = 0;
			return MyStates::counting
		}
	),

	mst::make_state(								
		MyStates::counting,
		[]() -> MyStates {
			if (++foo < 10)
				return MyStates::counting
			return MyStates::return_
		}
	)	
);


// ...

bool perform_counting = false;		// one way to activate a state machine
					// could be by using global indicators

// ...

{

	// somewhere in your main loop

	if ((perform_counting) && (my_state_machine.execute_step())) {
		perform_counting = false;
	}

}

Execute_step() returns true only if the return state is reached, otherwise it will return false. The basic idea is to have one main state machine that runs continuously (i.e., no state returns the return state) and triggers the execution of smaller state machines for specific tasks via, e.g., global indicators as used above. The smaller state machines do reach the return state (after which they reset to the start state automatically).

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.