def test_invert_rgb(self): """tests that rgb canvas inversion doesn't fail with RGB displays and returns a valid RGB image""" test_image = get_image("canvas_10.png") o = get_mock_output(mode="RGB") c = Canvas(o, name=c_name) c.text("Hello world", (5, 5)) c.invert() assert (c.get_image().mode == o.device_mode) assert (imgs_are_equal(c.get_image(), test_image.convert("RGB")))
def test_invert(self): """tests that inversion works with default display""" test_image = get_image("canvas_10.png") o = get_mock_output() c = Canvas(o, name=c_name) c.text("Hello world", (5, 5)) c.invert() assert (c.get_image().mode == o.device_mode) assert (imgs_are_equal(c.get_image(), test_image))
def test_paste(self): test_image = get_image("canvas_13.png") image_to_paste = get_image("canvas_14.png") o = get_mock_output(mode="RGB") c = Canvas(o, name=c_name) c.text("Hello world", (16, 16)) c.paste(image_to_paste) assert (c.get_image().mode == o.device_mode) assert (imgs_are_equal(c.get_image(), test_image.convert("RGB")))
def learn_about_5_buttons(self): c = Canvas(self.o) c.centered_text( "Let's go through\nthe main buttons\nand their meanings") GraphicsPrinter(c.get_image(), self.i, self.o, 5, invert=False) c.clear() c.centered_text("Press the buttons\nto test\nThen ENTER\nto continue") GraphicsPrinter(c.get_image(), self.i, self.o, 5, invert=False) c.clear() # First, show the left/right/up/down buttons # TODO: different behaviour for ZP and emulator? c.text("Enter", (48, 22)) c.text("Continue", (39, 30)) c.text("Left", (2, 18)) c.text("Back", (2, 26)) c.text("Cancel", (2, 34)) c.text("Right", (92, 22)) c.text("Option", (90, 30)) c.text("Up", (56, 5)) c.text("Down", (52, "-18")) image = c.get_image() def process_key(key, state): # invert/deinvert areas on the canvas when buttons are pressed/released # on drivers that don't support key states, will toggle inversion on every press # on drivers that support key states, will "highlight" the buttons pressed print(key, state) if state != KEY_HELD: if key == "KEY_UP": c.invert_rect((64 - 20, 2, 64 + 20, 22)) elif key == "KEY_DOWN": c.invert_rect((64 - 20, "-2", 64 + 20, "-22")) elif key == "KEY_LEFT": c.invert_rect((2, 32 - 15, 38, 32 + 15)) elif key == "KEY_RIGHT": c.invert_rect(("-2", 32 - 10, "-40", 32 + 10)) keys = ["KEY_UP", "KEY_DOWN", "KEY_LEFT", "KEY_RIGHT"] keymap = {"KEY_ENTER": "deactivate"} for key in keys: cb = cb_needs_key_state(lambda st, x=key: process_key(x, st)) keymap[key] = cb Refresher(c.get_image, self.i, self.o, override_left=False, keymap=keymap).activate() return True
def test_drawing_vertical_text(self): """tests the vertical text drawing""" test_image = get_image("canvas_9.png") o = get_mock_output() c = Canvas(o, name=c_name) c.vertical_text("Personal", (0, 0)) assert (imgs_are_equal(c.get_image(), test_image))
def test_howto_example_drawing_line(self): """tests the fifth canvas example from howto""" test_image = get_image("canvas_5.png") o = get_mock_output() c = Canvas(o, name=c_name) c.circle(("-8", 8, 4)) assert(imgs_are_equal(c.get_image(), test_image))
def test_howto_example_drawing_rectangle(self): """tests the fourth canvas example from howto""" test_image = get_image("canvas_4.png") o = get_mock_output() c = Canvas(o, name=c_name) c.rectangle((10, 4, 20, "-10")) assert(imgs_are_equal(c.get_image(), test_image))
def test_howto_example_drawing_text(self): """tests the second text canvas example from howto""" test_image = get_image("canvas_7.png") o = get_mock_output() c = Canvas(o, name=c_name) c.centered_text("Hello world") assert(imgs_are_equal(c.get_image(), test_image))
def test_howto_example_drawing_line(self): """tests the third canvas example from howto""" test_image = get_image("canvas_3.png") o = get_mock_output() c = Canvas(o, name=c_name) c.line((10, 4, "-8", "-4")) assert(imgs_are_equal(c.get_image(), test_image))
def test_howto_example_drawing_text(self): """tests the first canvas example from howto""" test_image = get_image("canvas_2.png") o = get_mock_output() c = Canvas(o, name=c_name) c.text("Hello world", (0, 0)) assert(imgs_are_equal(c.get_image(), test_image))
def render_clock(self, time, **kwargs): c = Canvas(self.o) width, height = c.size self.draw_text(c, time, **kwargs) self.draw_analog_clock(c, time, **kwargs) if self.countdown: self.draw_countdown(c, **kwargs) return c.get_image()
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())
def test_howto_example_invert_region(self): """tests the sixth canvas example from howto""" test_image = get_image("canvas_6.png") o = get_mock_output() c = Canvas(o, name=c_name) c.text("Hello world", (5, 5)) c.invert_rect((35, 5, 80, 17)) assert(imgs_are_equal(c.get_image(), test_image))
def get_yes_icon(width=40, height=40): o = MockOutput(width, height) c = Canvas(o) cx, cy = c.get_center() c.circle((cx, cy, min(width / 2, height / 2) - 1), fill="white") c.line((cx - 15, cy, cx, cy + 15), fill="black", width=5) c.line((cx - 1, cy + 15, cx + 10, cy - 12), fill="black", width=4) return c.get_image()
def test_howto_example_drawing_basics(self): """tests the first canvas example from howto""" test_image = get_image("canvas_1.png") o = get_mock_output() c = Canvas(o, name=c_name) c.point((1, 2)) c.point( ( (2, 1), (2, 3), (3, 4) ) ) c.display() # Shouldn't throw an exception assert(imgs_are_equal(c.get_image(), test_image))
def test_howto_example_drawing_centered_text(self): """tests the third text canvas example from howto""" test_image = get_image("canvas_8.png") o = get_mock_output() c = Canvas(o, name=c_name) ctc = c.get_centered_text_bounds("a") c.text("a", (ctc.left, 0)) c.text("b", (str(ctc.left-ctc.right), ctc.top)) c.text("c", (ctc.left, str(ctc.top-ctc.bottom))) c.text("d", (0, ctc.top)) assert(imgs_are_equal(c.get_image(), test_image))
def render_totp(name, secret): c = Canvas(o) totp_font = ("Fixedsys62.ttf", 32) try: totp_value = pyotp.TOTP(secret).now() except TypeError: totp_font = ("Fixedsys62.ttf", 16) totp_value = "Incorrect\nsecret!" c.centered_text(totp_value, font=totp_font) left_coord = c.get_centered_text_bounds(name).left c.text(name, (left_coord, 5)) return c.get_image()
def test_drawing_custom_shape_text(self): """tests the custom shape text drawing""" test_image = get_image("canvas_8.png") o = get_mock_output() c = Canvas(o, name=c_name) ctc = c.get_centered_text_bounds("a") def coords_cb(i, ch): return [(ctc.left, 0), (str(ctc.left - ctc.right), ctc.top), (ctc.left, str(ctc.top - ctc.bottom)), (0, ctc.top)][i] c.custom_shape_text("abcd", coords_cb) assert (imgs_are_equal(c.get_image(), test_image))
def make_image_from_status(o, status, success_message=None): c = Canvas(o) if status[0] == "Success": c.bitmap((44, 3), get_yes_icon(), fill=c.default_color) if success_message: status.append(success_message) else: c.bitmap((44, 3), get_no_icon(), fill=c.default_color) top_start = 45 top_increment = 10 for i, s in enumerate(status[1:]): ctb = c.get_centered_text_bounds(s) c.text(s, (ctb.left, top_start + top_increment * i)) return c.get_image()
def refresh(self): now = datetime.now() hhmm = now.strftime("%H:%M") ss = now.strftime("%S") ddmmyy = now.strftime("%d%m%y") c = Canvas(self.o) #c.line((0, 8, c.width, 8), fill="white") c.text(hhmm, (5, 8), font=("Fixedsys62.ttf", 32)) c.text(ss, (87, 23)) c.text(ddmmyy, (90, 12)) c.text("0 notifications", (10, 39)) self.draw_battery_icon(c) self.draw_network_icon(c) image = c.get_image() self.p.runcall( self.o.display_image, image )
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())
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!") try: dcdc = USB_DCDC() dcdc.on() except: logger.exception("Can't turn the ZP USB DC-DC on!") 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()
def on_zeromenu_button_press(self): c = Canvas(self.o) c.centered_text("Good job!\nZeroMenu button\nseems to work!") GraphicsPrinter(c.get_image(), self.i, self.o, 3, invert=False) raise RefresherExitException
def on_help_button_press(self): c = Canvas(self.o) c.centered_text("Good job!\nThe button\nseems to work\n;-P") GraphicsPrinter(c.get_image(), self.i, self.o, 3, invert=False) raise RefresherExitException