|
| 1 | +import os |
| 2 | +import sys |
| 3 | +import io |
| 4 | +import M5 |
| 5 | +import time |
| 6 | + |
| 7 | +from collections import namedtuple |
| 8 | +from M5 import * |
| 9 | +from machine import Pin, UART |
| 10 | +from utility import print_error_msg |
| 11 | + |
| 12 | +Point = namedtuple("Point", "x y timestamp") |
| 13 | +Button = namedtuple("Button", "x y w h label pin") |
| 14 | + |
| 15 | +BLACK = 0x222222 |
| 16 | +BACKGROUND = 0xEACDCD |
| 17 | +BORDER = 0xFC0000 |
| 18 | + |
| 19 | +IO1 = Pin(1, Pin.OUT) |
| 20 | +IO2 = Pin(2, Pin.OUT) |
| 21 | +uart = UART(1, baudrate=115200, tx=43, rx=44) |
| 22 | +B10058399 = Button(32, 32, 256, 48, "10058399", IO2) |
| 23 | +B10058400 = Button(32, 112, 256, 48, "10058400", IO1) |
| 24 | + |
| 25 | +touched = None |
| 26 | + |
| 27 | + |
| 28 | +def setup(): |
| 29 | + global IO1, IO2 |
| 30 | + M5.begin() |
| 31 | + IO1.value(True) |
| 32 | + IO2.value(True) |
| 33 | + screen() |
| 34 | + |
| 35 | + |
| 36 | +def screen(): |
| 37 | + Widgets.setRotation(1) |
| 38 | + Widgets.fillScreen(BLACK) |
| 39 | + button(B10058399) |
| 40 | + button(B10058400) |
| 41 | + |
| 42 | + |
| 43 | +def button(b): |
| 44 | + Widgets.Rectangle(b.x, b.y, b.w, b.h, BORDER, BACKGROUND) |
| 45 | + Widgets.Label(b.label, b.x + 80, b.y + 14, 1.0, BLACK, BACKGROUND, Widgets.FONTS.DejaVu18) |
| 46 | + |
| 47 | + |
| 48 | +def pressed(b): |
| 49 | + global touched |
| 50 | + |
| 51 | + return touched.x >= b.x and touched.x <= (b.x + b.w) and touched.y >= b.y and touched.y <= (b.y + b.h) |
| 52 | + |
| 53 | + |
| 54 | +def press(b): |
| 55 | + if b.pin.value(): |
| 56 | + b.pin.value(False) |
| 57 | + return True |
| 58 | + |
| 59 | + return False |
| 60 | + |
| 61 | + |
| 62 | +def release(): |
| 63 | + global IO1, IO2 |
| 64 | + if not IO2.value(): |
| 65 | + IO2.value(True) |
| 66 | + print(f"released {B10058399.label}") |
| 67 | + |
| 68 | + if not IO1.value(): |
| 69 | + IO1.value(True) |
| 70 | + print(f"released {B10058400.label}") |
| 71 | + |
| 72 | + |
| 73 | +def loop(): |
| 74 | + global IO1, IO2, touched |
| 75 | + M5.update() |
| 76 | + if M5.Touch.getCount() > 0: |
| 77 | + now = time.ticks_ms() |
| 78 | + point = M5.Touch.getTouchPointRaw() |
| 79 | + |
| 80 | + if touched is None: |
| 81 | + touched = Point(point[0], point[1], now) |
| 82 | + else: |
| 83 | + dt = now - touched.timestamp |
| 84 | + dx = point[0] - touched.x |
| 85 | + dy = point[1] - touched.y |
| 86 | + touched = Point(point[0], point[1], now) |
| 87 | + |
| 88 | + if dt > 250 or abs(dx) > 10 or abs(dy) > 10: |
| 89 | + if pressed(B10058399): |
| 90 | + uart.write("CARD 10058399\n") |
| 91 | + Speaker.tone(880, 125) # A5 |
| 92 | + |
| 93 | + if pressed(B10058400): |
| 94 | + uart.write("CARD 10058400\n") |
| 95 | + Speaker.tone(1046, 125) # C6'ish |
| 96 | + else: |
| 97 | + release() |
| 98 | + |
| 99 | + time.sleep(0.05) |
| 100 | + |
| 101 | + |
| 102 | +if __name__ == "__main__": |
| 103 | + try: |
| 104 | + setup() |
| 105 | + while True: |
| 106 | + loop() |
| 107 | + except (Exception, KeyboardInterrupt) as e: |
| 108 | + try: |
| 109 | + print_error_msg(e) |
| 110 | + except ImportError: |
| 111 | + print("please update to latest firmware") |
0 commit comments