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 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 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_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_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 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_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_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_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 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_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 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 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 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 __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 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 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 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 pk2_pinouts_page(o): pkp = [['rst'], ['vcc'], ['gnd'], ['miso'], ['sck'], ['mosi']] c = Canvas(o) headline = "PICkit2" ctb = c.get_centered_text_bounds(headline) c.text(headline, (ctb.left + 10, 2)) c.text("pinout", (ctb.left + 10, 12)) c = draw_pinout(c, o, pkp, lo=5) return c pk2 = [['mosi', 'vcc'], ['nc', 'txd'], ['rst', 'rxd'], ['sck', 'nc'], ['miso', 'gnd']] c = Canvas(o) headline = "PicKit2 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 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 draw_field(): c = Canvas(o) c.rectangle((0, 0, width-1, height-1)) c.point(snake) if not(applex and appley): create_apple() if level == 1: # We draw the apple c.point([(applex, appley-1), # the leaf (applex-1, appley),(applex, appley),(applex+1, appley), # the top half (applex-1, appley+1),(applex, appley+1),(applex+1, appley+1)]) # the lower half else: c.point((applex, appley)) c.display() # Display the canvas on the screen
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 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()