8000 GitHub - rvt/statemachine: Simple statemachine in c++
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

rvt/statemachine

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Build Status License: MIT

Small library to create a simple state machine in c++

This library provides a way to create a simple state machine

Example

auto firstState = new State;
    auto secondState = new StateTimed{10};
    auto thirdState = new State;

    auto firstStateId = firstState->id();
    auto secondStateId = secondState->id();
    auto thirdStateId = thirdState->id();
    auto run1 = [secondStateId]() {
        std::cerr << "firstState\n";
        return secondStateId;
    };
    auto run2 = [thirdStateId]() {
        std::cerr << "secondState\n";
        return thirdStateId;
    };
    auto run3 = [thirdStateId]() {
        std::cerr << "thirdState\n";
        return thirdStateId;
    };

    firstState->setRunnable(run1);
    secondState->setRunnable(run2);
    thirdState->setRunnable(run3);

    StateMachine<3> machine {{firstState, secondState, thirdState} };
    machine.start();
    // Should initially be at the first state
    REQUIRE(machine.current(firstState->id()) == true);

    // then second state
    machine.handle();
    REQUIRE(machine.current(secondState->id()) == true);

    // Still at second state
    machine.handle();
    REQUIRE(machine.current(secondState->id()) == true);

    // should have been advanced to third state
    millisStubbed = 11;
    machine.handle();
    REQUIRE(machine.current(thirdState->id()) == true);

An example can be find here bbq-controller/src/main.cpp where it´s used to connect to wifi and Mosquitto. It will detected if the connection drops and re-connects

License

This code is released under the MIT License.

V 1.0.1

  • Each state should return a state number instead of this

V 1.0.2

  • Allow to reference state numbers instead of order added
  • Allow to create the state prior to the runnable code

About

Simple statemachine in c++

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published
0