forked from CRImier/pyLCI
-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding the "connect a USB keyboard as input device when the keypad is…
… offline" fallback
- Loading branch information
Showing
3 changed files
with
181 additions
and
3 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
from time import sleep | ||
from threading import Event | ||
|
||
from ui import Canvas, Refresher | ||
from ui.base_ui import BaseUIElement | ||
from apps import ZeroApp | ||
from helpers import ExitHelper, setup_logger, remove_left_failsafe, read_or_create_config, local_path_gen, save_config_method_gen | ||
|
||
logger = setup_logger(__name__, "info") | ||
|
||
from __main__ import input_device_manager as device_manager | ||
|
||
from zerophone_hw import USB_DCDC | ||
|
||
class KeyboardFallbackApp(ZeroApp): | ||
|
||
do_not_activate_events = ["usb_keyboard_connected"] | ||
|
||
def __init__(self, *args, **kwargs): | ||
ZeroApp.__init__(self, *args, **kwargs) | ||
self.active = Event() | ||
self.pop_on_event = Event() | ||
self.pop_on_event.set() | ||
self.c = Canvas(self.o) | ||
device_manager.register_monitor_callback(self.process_dm_event) | ||
self.i.set_streaming(self.deactivate) | ||
self.state = None | ||
self.status_image = "No image" | ||
self.r = Refresher(self.get_status_image, self.i, self.o, name="Keyboard fallback status refresher") | ||
|
||
def deactivate(self, keyname, *args, **kwargs): | ||
# Upon receiving *any* key when active, it's our hint from the user | ||
# to not appear again ;-P | ||
# Unless the keyboard was just connected, of course | ||
if self.state == "usb_keyboard_connected": | ||
self.r.deactivate() | ||
self.context.signal_background() | ||
else: | ||
self.pop_on_event.clear() | ||
|
||
def go_into_foreground(self): | ||
if not self.pop_on_event.isSet(): | ||
return False | ||
return self.context.request_switch() | ||
|
||
def process_dm_event(self, event): | ||
self.c.clear() | ||
self.state = event | ||
if not self.context.is_active() and self.pop_on_event.isSet() \ | ||
and event not in self.do_not_activate_events: | ||
if not self.go_into_foreground(): | ||
return | ||
if event == "usb_keyboard_connected": | ||
self.c.centered_text("USB kb connected") | ||
elif event == "custom_i2c_disconnected": | ||
self.c.centered_text("Keypad not found!") | ||
elif event == "looking_for_usb_keyboard": | ||
self.c.centered_text("Keypad not found!", ch=16) | ||
self.c.centered_text("Looking for USB kb") | ||
elif event == "usb_keyboard_found": | ||
self.c.centered_text("USB keyboard found") | ||
elif event == "usb_keyboard_disconnected": | ||
self.c.centered_text("USB kb disconnected!") | ||
self.status_image = self.c.get_image() | ||
if self.state == "usb_keyboard_connected": | ||
sleep(2) | ||
if self.state == "usb_keyboard_connected": | ||
self.r.deactivate() | ||
self.context.signal_background() | ||
|
||
def set_context(self, c): | ||
self.context = c | ||
c.set_target(self.show_status) | ||
|
||
def get_status_image(self): | ||
return self.status_image | ||
|
||
def show_status(self): | ||
self.r.activate() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters