-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfsm_actions.py
More file actions
38 lines (33 loc) · 1.14 KB
/
Copy pathfsm_actions.py
File metadata and controls
38 lines (33 loc) · 1.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
"""This module holds the actions associated to this project
"""
from utime import sleep_ms, sleep
from machine import Pin
from digital_clock import DigitalClock
from ssd1306 import SSD1306_I2C
from machine import disable_irq, enable_irq
from fsm import FSM
def read_button(pin: Pin, delay: int = 20) -> int:
"""Filters by software the noise a push_button generates
Args:
pin (Pin): The pin where the push button is connected
Source:
https://docs.micropython.org/en/latest/pyboard/tutorial/debounce.html
"""
cur_value = pin.value()
active = 0
while active < delay:
if pin.value() != cur_value:
active += 1
else:
active = 0
sleep_ms(1)
return cur_value
def init_fsm(fsm: FSM, event: dict[str, int]) -> None:
"""Set the transition rules of the FSM to be implemented
Args:
fsm (FSM): A non-initialized finite state machine
"""
fsm.set_transition_rule(0, event['unconditional'], 1)
fsm.set_transition_rule(1, event['default'], 1)
fsm.set_transition_rule(1, event['press button'], 2)
fsm.set_transition_rule(2, event['unconditional'], 1)