"""Hardware test: the panel shows the keys of U2, a pressed key drawn inverted, with the raw input ports and an event count. Every event goes to stdout too. No MPD and no backlight timeout: only the panel and the key expander.""" import cairo from . import keypad, render # render picks the Pango version from gi.repository import Pango # noqa: E402 # pin order, as wired: P0.3-P0.7, then P1.0-P1.3, then P1.4-P1.7 ROWS = ( (3, 4, 5, 6, 7), (8, 9, 10, 11), (12, 13, 14, 15), ) BOX_W, BOX_H, GAP = 44, 26, 3 TOP = 16 INFO_Y = TOP + 3 * (BOX_H + GAP) + 4 class KeyTest(object): def __init__(self, width, height, present): self.width, self.height = width, height self.present = present self.surface = cairo.ImageSurface(cairo.FORMAT_RGB24, width, height) self.raw = None self.presses = 0 self.repeats = 0 self.last = "" self.errors = 0 self.down = set() def on_event(self, key, value): if value == 1: self.presses += 1 self.down.add(key) self.last = key elif value == 2: self.repeats += 1 else: self.down.discard(key) print("%-6s %s" % (key, ("released", "pressed", "repeat")[value]), flush=True) if value != 2: self.draw() def on_raw(self, raw): self.raw = raw print("raw P0=%02x P1=%02x" % (raw & 0xff, raw >> 8), flush=True) self.draw() def draw(self, errors=None): if errors is not None: self.errors = errors cr = cairo.Context(self.surface) cr.set_source_rgb(1, 1, 1) cr.paint() cr.set_source_rgb(0, 0, 0) render._text(cr, render.FONT_STATUS, 1, 1, self.width - 2, Pango.Alignment.LEFT, "Test klawiszy") cr.set_line_width(1) for r, row in enumerate(ROWS): for c, bit in enumerate(row): x = 1 + c * (BOX_W + GAP) y = TOP + r * (BOX_H + GAP) key = keypad.KEY_PINS[bit] pressed = key in self.down cr.set_source_rgb(0, 0, 0) cr.rectangle(x + 0.5, y + 0.5, BOX_W - 1, BOX_H - 1) if pressed: cr.fill() cr.set_source_rgb(1, 1, 1) else: cr.stroke() render._text(cr, render.FONT_SMALL, x, y + 2, BOX_W, Pango.Alignment.CENTER, key) render._text(cr, render.FONT_SMALL, x, y + 13, BOX_W, Pango.Alignment.CENTER, keypad.pin_name(bit)) cr.set_source_rgb(0, 0, 0) raw = "--" if self.raw is None else "P0=%02x P1=%02x" % (self.raw & 0xff, self.raw >> 8) lines = ( "wejścia: " + raw, "naciśnięcia: %d powtórzenia: %d" % (self.presses, self.repeats), "ostatni: " + (self.last or "-"), "błędy odczytu: %d" % self.errors, ) for i, text in enumerate(lines): render._text(cr, render.FONT_SMALL, 1, INFO_Y + i * 11, self.width - 2, Pango.Alignment.LEFT, text) del cr self.surface.flush() self.present(self.surface.get_data(), self.surface.get_stride(), 0, 0, self.width, self.height)