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_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_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_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 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 __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 isp_pinouts_page(o): isp = [['miso', 'vcc'], ['sck', 'mosi'], ['rst', 'gnd']] c = Canvas(o) headline = "ISP header pinout" ctb = c.get_centered_text_bounds(headline) c.text(headline, (ctb.left, 2)) c = draw_pinout(c, o, isp, lo=2) c = draw_pinout(c, o, isp, lo=2, reversed=True) ctb = c.get_centered_text_bounds("cable") c.text("board", (ctb.left / 2 - 5, "-15")) c.text("cable", (ctb.left / 2 + c.width / 2 - 5, "-15")) return c
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 uap_pinouts_page(o): uap = [['mosi', 'vcc'], ['nc', 'txd'], ['rst', 'rxd'], ['sck', 'nc'], ['miso', 'gnd']] c = Canvas(o) headline = "USBASP pinout" ctb = c.get_centered_text_bounds(headline) c.text(headline, (ctb.left, 0)) c = draw_pinout(c, o, uap, lo=2, ph=8, tto=-1) c = draw_pinout(c, o, uap, lo=2, ph=8, tto=-1, reversed=True) ctb = c.get_centered_text_bounds("cable") c.text("board", (ctb.left / 2 - 5, str(-o.char_height - 2))) c.text("cable", (ctb.left / 2 + c.width / 2 - 5, str(-o.char_height - 2))) return c
def zp_pinouts_page(o): zpp = [['vcc'], ['gnd'], ['mosi'], ['miso'], ['sck'], ['rst']] c = Canvas(o) headline = "ZeroPhone" ctb = c.get_centered_text_bounds(headline) c.text(headline, (ctb.left + 10, 2)) c.text("Gamma pinout", (ctb.left + 10, 12)) c.text("(side header)", (ctb.left + 10, 22)) c = draw_pinout(c, o, zpp, lo=5) return c
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 init(): global columns, char_height, font1, mode, dir, degree, blink_count, waiting, help_page, values getcontext().prec = 15 columns = 16 # display width in chars char_height = int(o.height / 4) c1 = Canvas(o) font1 = c1.load_font("Fixedsys62.ttf", char_height) mode = 0 # number1 dir = +1 # move down degree = False # radians blink_count = 0.0 blink.clear() waiting.clear() help_page = 0 values = Values()
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_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_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 test_base_image(self): """Tests whether the base_image kwarg works""" w = 128 h = 64 i = Image.new("1", (w, h) ) c = Canvas(get_mock_output(), base_image=i, name=c_name) assert(c.image == i) assert(c.size == (w, h))
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 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 activate_lockscreen(self): if not self.context.request_exclusive(): logger.info("Can't get exclusive access!") return False #Couldn't get exclusive access! screens = self.get_screens() self.locked = True c = Canvas(self.o) c.centered_text("Locked") remove_left_failsafe(self.i) if self.showing_other_context: self.context.request_context_start(self.showing_other_context) while self.locked: key = None if self.showing_other_context: key = self.contextscreen.wait_loop(self.showing_other_context) else: c.display() lockscreen_obj = screens.get(settings["name"], KeyScreen) args = settings.get("args", []) kwargs = settings.get("kwargs", {}) lockscreen = lockscreen_obj(self.i, self.o, *args, **kwargs) lockscreen.wait_loop(last_key=key) logger.info("Lockscreen triggered") self.locked = lockscreen.activate() logger.info("Finished, restarting loop") self.context.rescind_exclusive() if self.showing_other_context: pass # stop the context that we might've started? idk
def test_coords_filtering(self): """tests whether the coordinate filtering works""" w = 128 h = 64 c = Canvas(get_mock_output(width=w, height=h), name=c_name) assert (c.check_coordinates((0, 1)) == (0, 1)) assert (c.check_coordinates(("-2", "-3")) == (w-2, h-3)) assert (c.check_coordinates((0, 1, 2, 3)) == (0, 1, 2, 3)) assert (c.check_coordinates((0, 1, "-2", "-3")) == (0, 1, w-2, h-3)) assert (c.check_coordinates(("-0", "-1", "-2", "-3")) == (w, h-1, w-2, h-3)) assert (c.check_coordinates(("-0", "1", "-2", "-3")) == (w, h+1, w-2, h-3))
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 show(lines): if len(lines) < 4: max = len(lines) else: max = 4 index = values.get_index() if index == 1: index = 2 # row 3 c = Canvas(o) for n in range(max): y = n * char_height c.text(lines[n], (0, y), font=font1) if n == index and blink.isSet(): # row 0 or 3 && blink c.invert_rect( (127 - 8, y + char_height - 4, 127, y + char_height - 1)) c.display()
def test_coords_filtering(self): """tests whether the coordinate filtering works""" w = 128 h = 64 c = Canvas(get_mock_output(width=w, height=h), name=c_name) assert (c.check_coordinates((0, 1)) == (0, 1)) assert (c.check_coordinates(("-2", "-3")) == (w - 2, h - 3)) assert (c.check_coordinates((0, 1, 2, 3)) == (0, 1, 2, 3)) assert (c.check_coordinates( (0, 1, "-2", "-3")) == (0, 1, w - 2, h - 3)) # check_coordinates will rearrange coordinates in a way that x1 < x2 and y1 < y2 assert (c.check_coordinates( ("-0", "-1", "-2", "-3")) == (w - 2, h - 3, w, h - 1)) assert (c.check_coordinates( ("-0", "1", "-2", "-3")) == (w - 2, h - 3, w, h + 1))
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 learn_about_help_icon(self): o = HelpOverlay("test") c = Canvas(self.o) c.text("See this icon?->", (1, 1)) c.text("When it appears,", (3, 10)) c.text("press F5 to get help", (3, 19)) c.text("Try now, or", (3, 28)) c.text("press ENTER to skip", (3, 37)) if not is_emulator(): c.text("F5:", (5, 51)) c.paste(local_path("f5_button_location.png"), (30, 50), invert=True) o.draw_icon(c) Refresher(c.get_image, self.i, self.o, keymap={ "KEY_ENTER": "deactivate", "KEY_F5": self.on_help_button_press }).activate() return True
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 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