From 6716fbc7a1ec5d14887d54b7d0286a149d15d46a Mon Sep 17 00:00:00 2001 From: Sébastien Dailly Date: Sun, 13 Aug 2023 10:45:56 +0200 Subject: Added a sleep function in the keyboard --- src/code.py | 39 +++++++++++++++++++++++++++++---------- 1 file changed, 29 insertions(+), 10 deletions(-) (limited to 'src/code.py') diff --git a/src/code.py b/src/code.py index 813574a..ad634ad 100644 --- a/src/code.py +++ b/src/code.py @@ -28,6 +28,7 @@ from supervisor import runtime from actions import Action import skeleton import menu +from sleep_mode import Power # CONFIGURABLES ------------------------ @@ -43,6 +44,8 @@ macropad = MacroPad() macropad.display.auto_refresh = False macropad.pixels.auto_write = False +power = Power(macropad) + Action().set_macropad(macropad) # Load all the macro key setups from .py files in MACRO_FOLDER @@ -76,6 +79,7 @@ if not apps: app_index = 0 + def run_application(app): # Read encoder position. If it's changed, declare it as a button. position = app.macropad.encoder @@ -91,32 +95,41 @@ def run_application(app): event = app.macropad.keys.events.get() if not event: app.tick() + power.tick() return # No key events key_number = event.key_number pressed = event.pressed + power.execute_action(key_number, pressed or False) return app.execute_action(key_number, pressed or False) def switch_layout(configuration): global app app = skeleton.Handler(macropad, configuration) + power.set_configuration(configuration) app.start() def search_and_switch(layout): - found_application = False for local_app in apps: if local_app.title == layout and app is not local_app: - found_application = True switch_layout(local_app) break +def stack(layout): + for configuration in apps: + if configuration.title == layout and app is not configuration: + app.stack(configuration) + app.start() + break + def send_layout(_any): if usb_cdc.data.connected: usb_cdc.data.write(bytes("%s\n" % app.name, "utf-8")) actions = { "layout" : search_and_switch, + "stack" : stack, "get_layout" : send_layout, } @@ -128,13 +141,18 @@ while True: # First read from the serial # If we have an application name from the command line, switch to this app. - if usb_cdc.data.connected and usb_cdc.data.in_waiting != 0: - line = usb_cdc.data.read(usb_cdc.data.in_waiting).strip() - commands = json.loads(line) - for command in commands: - for key, value in command.items(): - actions[key](value) - continue + if usb_cdc.data.connected: + while usb_cdc.data.in_waiting != 0: + line = usb_cdc.data.readline(usb_cdc.data.in_waiting).strip() + try: + commands = json.loads(line) + for command in commands: + for key, value in command.items(): + actions[key](value) + continue + except ValueError: + print(line) + pass # Handle encoder button. If state has changed, and if there's a # corresponding macro, set up variables to act on this just like @@ -152,7 +170,8 @@ while True: if configuration is None: continue switch_layout(configuration) break - send_layout(None) + if configuration.is_layout: + send_layout(None) run_application(app) continue -- cgit v1.2.3