def test_screen(self): # Testing the screen - drawing a "crosshair" to test screen edges and lines c = Canvas(self.o) c.line((0, 0, 10, 0)) c.line(("-1", 0, "-10", 0)) c.line((0, "-1", 10, "-1")) c.line(("-1", "-1", "-10", "-1")) c.line((0, 0, 0, 10)) c.line((0, "-1", 0, "-10")) c.line(("-1", 10, "-1", 0)) c.line(("-1", "-1", "-1", "-10")) c.line((0, 0, "-1", "-1")) c.line((0, "-1", "-1", 0)) eh = ExitHelper(self.i, ["KEY_ENTER", "KEY_LEFT"]).start() c.display() sleep(1) for x in range(30): if eh.do_run(): if x % 10 == 0: c.invert() c.display() sleep(0.1) else: break # Filling the screen (still using the same ExitHelper) c = Canvas(self.o) for x in range(60): if eh.do_run(): if x % 20 == 0: c.invert() c.display() sleep(0.1) else: break
def wait_loop(self, context_to_watch): eh = ExitHelper(self.i, keys="*").start() while eh.do_run(): sleep(self.sleep_time) self.refresh(context_to_watch) self.started_at = None return eh.last_key
def test_charger(self): #Testing charging detection PrettyPrinter("Testing charger detection", self.i, self.o, 1) from zerophone_hw import Charger charger = Charger() eh = ExitHelper(self.i, ["KEY_LEFT", "KEY_ENTER"]).start() if charger.connected(): PrettyPrinter( "Charging, unplug charger to continue \n Enter to bypass", None, self.o, 0) while charger.connected() and eh.do_run(): sleep(1) else: PrettyPrinter( "Not charging, plug charger to continue \n Enter to bypass", None, self.o, 0) while not charger.connected() and eh.do_run(): sleep(1)
def callback(): GPIO.setmode(GPIO.BCM) GPIO.setup(18, GPIO.IN) eh = ExitHelper(i).start() while eh.do_run(): state = GPIO.input(18) if state: GraphicsPrinter(local_path("no_fire.png"), None, o, 0.1) else: GraphicsPrinter(local_path("fire.png"), None, o, 0.1)
def test_usb_port(self): from zerophone_hw import USB_DCDC eh = ExitHelper(self.i, ["KEY_LEFT", "KEY_ENTER"]).start() PrettyPrinter("Press Enter to test USB", None, self.o, 0) counter = 5 for x in range(50): if eh.do_run(): if x % 10 == 0: counter -= 1 sleep(0.1) else: break if counter > 0: PrettyPrinter( "Insert or remove a USB device \n press Enter to skip", None, self.o, 0) dcdc = USB_DCDC() dcdc.on()
def test_headphone_jack(self): #Testing audio jack sound PrettyPrinter("Testing audio jack", self.i, self.o, 1) if self.br: if self.br.running: PrettyPrinter( "Audio jack test music not yet downloaded, waiting...", None, self.o, 0) eh = ExitHelper(self.i, ["KEY_LEFT", "KEY_ENTER"]).start() while self.br.running and eh.do_run(): sleep(0.1) if eh.do_exit(): return elif self.br.failed: PrettyPrinter("Failed to download test music!", self.i, self.o, 1) disclaimer = [ "Track used:" "", "Otis McDonald", "-", "Otis McMusic", "YT AudioLibrary" ] Printer([s.center(self.o.cols) for s in disclaimer], self.i, self.o, 3) PrettyPrinter("Press C1 to restart music, C2 to continue testing", self.i, self.o) import pygame pygame.mixer.init() pygame.mixer.music.load(music_path) pygame.mixer.music.play() continue_event = Event() def restart(): pygame.mixer.music.stop() pygame.mixer.init() pygame.mixer.music.load(music_path) pygame.mixer.music.play() def stop(): pygame.mixer.music.stop() continue_event.set() self.i.clear_keymap() self.i.set_callback("KEY_F1", restart) self.i.set_callback("KEY_F2", stop) self.i.set_callback("KEY_ENTER", stop) continue_event.wait()
def game_loop(self): self.set_keymap() self.refresh() while self.game.get_game_state( ) == 'not over' and not self.do_exit.isSet(): sleep(1) if self.do_exit.isSet(): return #Waiting for player to click any of five primary keys #Then, prompting to restart the game eh = ExitHelper(self.i, keys=self.i.reserved_keys).start() while eh.do_run(): sleep(0.1) do_restart = DialogBox("ync", self.i, self.o, message="Restart the game?").activate() if do_restart is None: #Cancel, leaving the playing field as-is return elif do_restart is False: #No, not restarting, thus exiting the game self.do_exit.set() else: self.start_new_game( ) #Yes, restarting (game_loop will be entered once more from on_start() )
def wait_for_connection(): eh = ExitHelper(i).start() while eh.do_run() and init.running and not init.failed: sleep(1) eh.stop()
def callback(): try: #Testing I2C - 0x12 should answer, 0x20 should raise IOError with busy errno from smbus import SMBus bus = SMBus(1) try: bus.read_byte(0x12) except IOError: PrettyPrinter("Keypad does not respond!", i, o) else: PrettyPrinter("Keypad found!", i, o) #Checking IO expander expander_ok = False try: bus.read_byte(0x20) except IOError as e: if e.errno == 16: PrettyPrinter("IO expander OK!", i, o) expander_ok = True elif e.errno == 121: PrettyPrinter("IO expander not found!", i, o) else: PrettyPrinter("IO expander driver not loaded!", i, o) #Launching splashscreen GraphicsPrinter("splash.png", i, o, 2) #Launching key_test app from app folder, that's symlinked from example app folder PrettyPrinter("Testing keypad", i, o, 1) import key_test key_test.init_app(i, o) key_test.callback() #Following things depend on I2C IO expander, #which might not be present: if expander_ok: #Testing charging detection PrettyPrinter("Testing charger detection", i, o, 1) from zerophone_hw import is_charging eh = ExitHelper(i, ["KEY_LEFT", "KEY_ENTER"]).start() if is_charging(): PrettyPrinter( "Charging, unplug charger to continue \n Enter to bypass", None, o, 0) while is_charging() and eh.do_run(): sleep(1) else: PrettyPrinter( "Not charging, plug charger to continue \n Enter to bypass", None, o, 0) while not is_charging() and eh.do_run(): sleep(1) #Testing the RGB LED PrettyPrinter("Testing RGB LED", i, o, 1) from zerophone_hw import RGB_LED led = RGB_LED() for color in ["red", "green", "blue"]: led.set_color(color) Printer(color.center(o.cols), i, o, 3) led.set_color("none") #Testing audio jack sound PrettyPrinter("Testing audio jack", i, o, 1) if not downloaded.isSet(): PrettyPrinter( "Audio jack test music not yet downloaded, waiting...", i, o) downloaded.wait() disclaimer = [ "Track used:" "", "Otis McDonald", "-", "Otis McMusic", "YT AudioLibrary" ] Printer([s.center(o.cols) for s in disclaimer], i, o, 3) PrettyPrinter("Press C1 to restart music, C2 to continue testing", i, o) import pygame pygame.mixer.init() pygame.mixer.music.load(music_path) pygame.mixer.music.play() continue_event = Event() def restart(): pygame.mixer.music.stop() pygame.mixer.init() pygame.mixer.music.load(music_path) pygame.mixer.music.play() def stop(): pygame.mixer.music.stop() continue_event.set() i.clear_keymap() i.set_callback("KEY_F1", restart) i.set_callback("KEY_F2", stop) i.set_callback("KEY_ENTER", stop) continue_event.wait() #Self-test passed, it seems! except: exc = format_exc() PrettyPrinter(exc, i, o, 10) else: PrettyPrinter("Self-test passed!", i, o, 3, skippable=False)
class KeyScreen(BaseUIElement): sleep_time = 0.1 default_key_sequence = ["KEY_ENTER", "KEY_*"] def __init__(self, i, o, timeout=3, key_sequence=None): self.key_sequence = key_sequence if key_sequence else self.default_key_sequence self.key_sequence_position = 0 self._locked = Event() self.timeout = timeout self.reset_timeout() BaseUIElement.__init__(self, i, o, name="KeyScreen lockscreen") def wait_loop(self, last_key=None): # some key was pressed before that self.eh = ExitHelper(self.i, keys="*").start() if last_key: self.eh.last_key = last_key return while self.eh.do_run(): sleep(self.sleep_time) def before_activate(self): self.locked = True self.key_sequence_position = 0 if self.eh.last_key == self.key_sequence[0]: self.key_sequence_position += 1 self.check_locked() def check_locked(self): if len(self.key_sequence) == self.key_sequence_position: self.unlock() def receive_key(self, key): if key == self.key_sequence[self.key_sequence_position]: self.key_sequence_position += 1 self.check_locked() else: self.deactivate() def get_return_value(self): return self.locked def idle_loop(self): self.check_timeout() sleep(self.sleep_time) @property def is_active(self): return self.in_foreground and self.locked @property def locked(self): return self._locked.isSet() @locked.setter def locked(self, value): self._locked.set() if value else self._locked.clear() def reset_timeout(self): self.timeout_counter = 0 def check_timeout(self): self.timeout_counter += 1 if self.timeout_counter * self.sleep_time > self.timeout: logger.info("Timed out") self.deactivate() def unlock(self): self.locked = False def set_first_key_press(self): self.first_key_pressed = True self.refresh() def generate_keymap(self): return {} def configure_input(self): self.i.set_streaming(self.receive_key) def refresh(self): c = Canvas(self.o) charheight = 16 font = c.load_font("Fixedsys62.ttf", charheight) key_name = self.key_sequence[self.key_sequence_position][len("KEY_"):] c.centered_text("Press {}".format(key_name.lower().capitalize()), font=font) self.o.display_image(c.get_image())
class PinScreen(object): accepted_keys = [ "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "*", "#" ] sleep_time = 0.1 def __init__(self, i, o, pin, timeout=3): self.pin = pin self.i = i self.o = o self.timeout = timeout self.reset_timeout() self.locked = Event() self.active = Event() self.clear() def wait_loop(self, last_key=None): # some key was pressed before that self.eh = ExitHelper(self.i, keys="*").start() if last_key: self.eh.last_key = last_key return while self.eh.do_run(): sleep(self.sleep_time) def activate(self): self.set_keymap() self.active.set() self.locked.set() self.refresh() while self.active.isSet() and self.locked.isSet(): self.check_timeout() sleep(self.sleep_time) return self.locked.isSet() def reset_timeout(self): self.timeout_counter = 0 def check_timeout(self): self.timeout_counter += 1 if self.timeout_counter * self.sleep_time > self.timeout: logger.info("Timed out") self.deactivate() def deactivate(self): self.clear() self.active.clear() def clear(self): self.value = [] def set_keymap(self): keymap = {"KEY_LEFT": self.backspace} self.i.stop_listen() self.i.set_keymap(keymap) self.i.set_streaming(self.receive_key) self.i.listen() def receive_key(self, key): if not self.active.isSet(): return self.reset_timeout() if key.startswith("KEY_"): value = key[4:] if value in self.accepted_keys: self.add_character(value) def backspace(self): if self.value: self.value = self.value[:-1] self.refresh() def add_character(self, character): if len(self.value) < len(self.pin): self.value.append(character) self.refresh() if len(self.value) == len(self.pin): self.check_password() def check_password(self): if self.pin == "".join(self.value): logger.info("Password correct!") self.locked.clear() else: self.value = [] self.refresh() def refresh(self): c = Canvas(self.o) charwidth = 20 charheight = 32 font = c.load_font("Fixedsys62.ttf", charheight) pin_width = len(self.pin) * charwidth x_offset = (self.o.width - pin_width) / 2 y_offset = 15 c.line((x_offset, y_offset + charheight, str(-x_offset), y_offset + charheight)) c.line((x_offset, y_offset + charheight, x_offset, y_offset + charheight - 5)) for x in range(len(self.pin)): i = x + 1 if x in range(len(self.value)): c.text("*", (x_offset + charwidth * x, y_offset), font=font) c.line((x_offset + charwidth * i, y_offset + charheight, x_offset + charwidth * i, y_offset + charheight - 5)) self.o.display_image(c.get_image())