Skip to main content

Some statemachine functions for MicroPython

Project description

Eydam-Prototyping: ep_statemachine

Python-Modul to implement a simple statemachine.

Example:

import ep_statemachine
i = 0
        
def inc():
    nonlocal i
    i = i + 1

workflow = [
    "press coffee button",
    "press coffee button",
    "press espresso button",
    "press espresso button",
    "press off button",
    "press off button",
]

# define states
s_sleep = es.state("sleep", initial=True, entry_action=inc)
s_select_input = es.state("select_input", entry_action=inc)
s_make_coffee = es.state("make coffee")
s_make_espresso = es.state("make espresso")
s_done = es.state("done")
s_off = es.state("off")

# define transitions
t_wake_up = es.transition(s_select_input, identifier="wake up", condition=lambda: re.match("press (coffee|espresso|off) button", workflow[i]) is not None)
s_sleep.add_transition(t_wake_up)

t_coffee = es.transition(s_make_coffee, identifier="coffee", condition=lambda: workflow[i] == "press coffee button")
s_select_input.add_transition(t_coffee)

t_espresso = es.transition(s_make_espresso, identifier="espresso", condition=lambda: workflow[i] == "press espresso button")
s_select_input.add_transition(t_espresso)

t_finished = es.transition(s_done, identifier="finished", condition=lambda: True)
s_make_coffee.add_transition(t_finished)
s_make_espresso.add_transition(t_finished)

t_back_to_sleep = es.transition(s_sleep, identifier="back to sleep", condition=lambda: True)
s_done.add_transition(t_back_to_sleep)

t_turn_off = es.transition(s_off, identifier="turn off", condition=lambda:workflow[i] == "press off button")
s_select_input.add_transition(t_turn_off)

sm = es.statemachine([s_sleep, s_select_input, s_make_coffee, s_make_espresso, s_done, s_off])
sm.init()

# do one step after another
sm.step()

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page