Skip to content

Commit c7da094

Browse files
committed
M5: uart0 'CARD' command
1 parent 1c01f69 commit c7da094

File tree

13 files changed

+309
-23
lines changed

13 files changed

+309
-23
lines changed

m5-stack/Rev.0/TODO.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@
1111
- [x] Wiegand
1212
- [x] GPIO
1313
- [ ] UART
14+
- [x] card
15+
- [ ] keycode
16+
- [ ] card + keycode
17+
- [ ] error/ok
1418
- [ ] USB
1519
- [x] bootsel
1620

m5-stack/Rev.0/coreS3/gpio/main.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def button(b):
4747
def pressed(b):
4848
global touched
4949

50-
return touched.x >= b.x and touched.x <= (b.x + b.w) and touched.y >= b.y and touched.y <= (b.y + b.h):
50+
return touched.x >= b.x and touched.x <= (b.x + b.w) and touched.y >= b.y and touched.y <= (b.y + b.h)
5151

5252

5353
def press(b):
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
PORT = /dev/tty.usbmodem142201
2+
# PORT = /dev/cu.usbmodem142201
3+
4+
flash:
5+
esptool --chip esp32s3 --port $(PORT) erase-flash
6+
esptool --chip esp32s3 --port $(PORT) write_flash -z 0x0000 ~/Development/tools/m5/uiflow-e986468-esp32s3-spiram-16mb-cores3-v2.2.8-20250606.bin
7+
8+
format:
9+
# . .venv/bin/activate; black --line-length 120 .
10+
black --line-length 120 .
11+
12+
run:
13+
ampy --port $(PORT) put main.py
14+
15+
tty:
16+
minicom -b 115200 -o -D $(PORT)
17+
18+

m5-stack/Rev.0/coreS3/uart/main.py

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
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")

m5-stack/Rev.0/firmware/core/include/M5.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ extern const uint32_t MSG;
1010
extern const uint32_t MSG_LED;
1111
extern const uint32_t MSG_IO6;
1212
extern const uint32_t MSG_IO7;
13+
extern const uint32_t MSG_RX;
1314
extern const uint32_t MSG_TTY;
1415
extern const uint32_t MSG_TICK;
1516
extern const uint32_t MSG_WATCHDOG;

m5-stack/Rev.0/firmware/core/include/hwconfig.h

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
#include <pico/types.h>
55

66
typedef struct constants {
7-
uint LED;
8-
97
struct {
108
uint gpio;
119
PIO pio;
@@ -25,6 +23,18 @@ typedef struct constants {
2523
int sm;
2624
} wiegand;
2725

26+
uint LED;
27+
28+
struct {
29+
uint tx;
30+
uint rx;
31+
} UART0;
32+
33+
struct {
34+
uint tx;
35+
uint rx;
36+
} UART1;
37+
2838
struct {
2939
uint IO6;
3040
uint IO7;

m5-stack/Rev.0/firmware/core/include/uart.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,8 @@
22

33
#include <stdbool.h>
44

5-
extern bool uart_initialise();
5+
struct buffer;
6+
7+
extern void UART_init();
8+
extern void UART_start();
9+
extern void UART_rx(struct buffer *);

m5-stack/Rev.0/firmware/core/src/sys.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <cli.h>
1111
#include <log.h>
1212
#include <sys.h>
13+
#include <uart.h>
1314
#include <wiegand.h>
1415

1516
#define LOGTAG "SYS"
@@ -21,6 +22,7 @@ const uint32_t MSG = 0xf0000000;
2122
const uint32_t MSG_LED = 0x10000000;
2223
const uint32_t MSG_IO6 = 0x20000000;
2324
const uint32_t MSG_IO7 = 0x30000000;
25+
const uint32_t MSG_RX = 0x40000000;
2426
const uint32_t MSG_TTY = 0xc0000000;
2527
const uint32_t MSG_LOG = 0xd0000000;
2628
const uint32_t MSG_WATCHDOG = 0xe0000000;
@@ -166,6 +168,12 @@ void dispatch(uint32_t v) {
166168
}
167169
}
168170

171+
if ((v & MSG) == MSG_RX) {
172+
struct buffer *b = (struct buffer *)(SRAM_BASE | (v & 0x0fffffff));
173+
174+
UART_rx(b);
175+
}
176+
169177
if ((v & MSG) == MSG_WATCHDOG) {
170178
watchdog_update();
171179
}

0 commit comments

Comments
 (0)