53 lines
1.4 KiB
Python
53 lines
1.4 KiB
Python
import digitalio
|
|
from micropython import const
|
|
|
|
|
|
class PinState:
|
|
PRESSED: int = const(0)
|
|
RELEASED: int = const(1)
|
|
|
|
|
|
class PinEvent:
|
|
NO_EVENT: int = const(0)
|
|
PRESSED: int = const(1)
|
|
RELEASED: int = const(2)
|
|
|
|
|
|
class PinStates:
|
|
current: int
|
|
previous: int
|
|
|
|
def __init__(self, current: int, previous: int):
|
|
self.current = current
|
|
self.previous = previous
|
|
|
|
|
|
class Pin:
|
|
index: int
|
|
hw_pin: digitalio.DigitalInOut
|
|
previous_state: int
|
|
|
|
def __init__(self, index, hw_pin: digitalio.DigitalInOut):
|
|
self.index = index
|
|
self.hw_pin = hw_pin
|
|
self.previous_state = PinState.RELEASED
|
|
|
|
def _consume_next_state(self) -> PinStates:
|
|
states = PinStates(int(self.hw_pin.value), int(self.previous_state))
|
|
self.previous_state = states.current
|
|
return states
|
|
|
|
def read_event(self) -> int:
|
|
states = self._consume_next_state()
|
|
if states.previous == PinState.RELEASED:
|
|
if states.current == PinState.RELEASED:
|
|
return PinEvent.NO_EVENT
|
|
if states.current == PinState.PRESSED:
|
|
return PinEvent.PRESSED
|
|
if states.previous == PinState.PRESSED:
|
|
if states.current == PinState.PRESSED:
|
|
return PinEvent.NO_EVENT
|
|
if states.current == PinState.RELEASED:
|
|
return PinEvent.RELEASED
|
|
raise NotImplementedError()
|