feat: working gpio buttons
This commit is contained in:
@@ -17,7 +17,7 @@ FONT_2_PATH = RESOURCES_PATH / "Px437_IBM_BIOS.ttf"
|
||||
LOADING_IMG_PATH = RESOURCES_PATH / "loading.png"
|
||||
BACKGROUND_IMG_PATH = RESOURCES_PATH / "background.png"
|
||||
|
||||
ESTIMATED_BOOT_TIME_SECONDS = 5.0
|
||||
ESTIMATED_BOOT_TIME_SECONDS = 1.0
|
||||
|
||||
SELECTED_RECT_BOUNDS = [
|
||||
(0, 0, 0, 0),
|
||||
@@ -57,4 +57,17 @@ GROUPS_DELTA = [0, 12, 33]
|
||||
GROUPS_SIZE = [6, 7, 2]
|
||||
|
||||
GPIO_BOUNCE = 0.05
|
||||
GPIO_BUTTONS = [(21, [ecodes.KEY_R])]
|
||||
GPIO_BUTTONS = [
|
||||
(21, ecodes.KEY_LEFTSHIFT),
|
||||
(12, ecodes.KEY_KP1),
|
||||
(7, ecodes.KEY_KP2),
|
||||
(25, ecodes.KEY_KP3),
|
||||
(16, ecodes.KEY_KP4),
|
||||
(1, ecodes.KEY_KP5),
|
||||
(8, ecodes.KEY_KP6),
|
||||
(24, ecodes.KEY_KPENTER),
|
||||
]
|
||||
|
||||
DEMO_KEYCODE = ecodes.KEY_BACKSPACE
|
||||
DEMO_GPIO = 21
|
||||
DEMO_HOLD = 5.0
|
||||
|
||||
@@ -62,6 +62,7 @@ class ForgeData:
|
||||
def __enter__(self) -> typing.Self:
|
||||
if self.__data.try_restore_backup() or self.__data.read():
|
||||
self.state = ForgeState.RESTORED
|
||||
self.state = ForgeState.RUNNING # TODO temporary
|
||||
return self
|
||||
|
||||
def __exit__(self, *_) -> None:
|
||||
|
||||
+22
-11
@@ -3,38 +3,49 @@ import typing
|
||||
import gpiozero
|
||||
from evdev import ecodes, uinput
|
||||
|
||||
from .constants import GPIO_BOUNCE, GPIO_BUTTONS
|
||||
from .constants import DEMO_GPIO, DEMO_HOLD, DEMO_KEYCODE, GPIO_BOUNCE, GPIO_BUTTONS
|
||||
|
||||
|
||||
class Button:
|
||||
def __init__(self, ui: uinput.UInput, gpio_pin: int, keycodes: list[int]) -> None:
|
||||
def __init__(self, ui: uinput.UInput, gpio_pin: int, keycode: int) -> None:
|
||||
self.__ui = ui
|
||||
self.__btn = gpiozero.Button(gpio_pin, pull_up=True, bounce_time=GPIO_BOUNCE)
|
||||
self.__keycodes = keycodes
|
||||
self.__btn = gpiozero.Button(
|
||||
gpio_pin,
|
||||
pull_up=True,
|
||||
hold_time=DEMO_HOLD,
|
||||
hold_repeat=False,
|
||||
bounce_time=GPIO_BOUNCE,
|
||||
)
|
||||
self.__keycode = keycode
|
||||
self.__btn.when_activated = self.press
|
||||
self.__btn.when_deactivated = self.release
|
||||
if gpio_pin == DEMO_GPIO:
|
||||
self.__btn.when_held = self.demo_hold
|
||||
|
||||
def press(self) -> None:
|
||||
for keycode in self.__keycodes:
|
||||
self.__ui.write(ecodes.EV_KEY, keycode, 1)
|
||||
self.__ui.write(ecodes.EV_KEY, self.__keycode, 1)
|
||||
self.__ui.syn()
|
||||
|
||||
def release(self) -> None:
|
||||
for keycode in self.__keycodes:
|
||||
self.__ui.write(ecodes.EV_KEY, keycode, 0)
|
||||
self.__ui.write(ecodes.EV_KEY, self.__keycode, 0)
|
||||
self.__ui.syn()
|
||||
|
||||
def demo_hold(self) -> None:
|
||||
self.__ui.write(ecodes.EV_KEY, self.__keycode, 0)
|
||||
self.__ui.write(ecodes.EV_KEY, DEMO_KEYCODE, 1)
|
||||
self.__ui.write(ecodes.EV_KEY, DEMO_KEYCODE, 0)
|
||||
self.__ui.syn()
|
||||
|
||||
|
||||
class Inputs:
|
||||
def __init__(self):
|
||||
self.__buttons: list[Button] = []
|
||||
self.__ui: uinput.UInput | None = None
|
||||
|
||||
def __enter__(self) -> typing.Self:
|
||||
self.__ui = uinput.UInput()
|
||||
|
||||
for gpio_pin, keycodes in GPIO_BUTTONS:
|
||||
self.__buttons += [Button(self.__ui, gpio_pin, keycodes)]
|
||||
for gpio_pin, keycode in GPIO_BUTTONS:
|
||||
Button(self.__ui, gpio_pin, keycode)
|
||||
|
||||
return self
|
||||
|
||||
|
||||
Reference in New Issue
Block a user