Skip to content

Commit b031dff

Browse files
committed
M5: returned ok/error on UART command
1 parent ed51a61 commit b031dff

File tree

4 files changed

+126
-36
lines changed

4 files changed

+126
-36
lines changed

m5-stack/Rev.0/TODO.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@
1515
- [x] SK6812
1616
- [x] Wiegand
1717
- [x] GPIO
18-
- [ ] UART
18+
- [x] UART
1919
- [x] uart0
2020
- [x] uart1
2121
- [x] card
2222
- [x] keycode
2323
- [x] card + keycode
24-
- [ ] error/ok
24+
- [x] error/ok
2525
- [ ] USB
2626
- [x] bootsel
2727
- [ ] card

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

Lines changed: 68 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import os
2+
import re
23
import sys
34
import io
45
import M5
@@ -11,23 +12,29 @@
1112

1213
Point = namedtuple("Point", "x y timestamp")
1314
Button = namedtuple("Button", "x y w h label pressed")
15+
Label = namedtuple("Label", "x y w h text")
1416

1517
BLACK = 0x222222
1618
BACKGROUND = 0xEACDCD
19+
GRAY = 0x808080
20+
DARKGRAY = 0x444444
21+
RED = 0xC04040
1722
BORDER = 0xFC0000
1823

1924
IO1 = Pin(1, Pin.OUT)
2025
IO2 = Pin(2, Pin.OUT)
2126
uart1 = UART(1, baudrate=115200, tx=43, rx=44)
2227
uart2 = UART(2, baudrate=115200, tx=17, rx=18)
2328

24-
B10058399 = Button(32, 32, 120, 48, '10058399', lambda: press(IO2))
25-
B10058400 = Button(32 + 136, 32, 120, 48, '10058400', lambda: press(IO1))
26-
B10058401 = Button(32, 96, 120, 48, '10058401', lambda: swipe(uart1, '10058401', None))
27-
C1357 = Button(32 + 136, 96, 120, 48, '1357#', lambda: keypad(uart1, '1357'))
28-
B10058402 = Button(32, 160, 256, 48, '10058402 + 7531#', lambda: swipe(uart2, '10058402', '7531'))
29+
B10058399 = Button(32, 8, 120, 48, "10058399", lambda: press(IO2))
30+
B10058400 = Button(32 + 136, 8, 120, 48, "10058400", lambda: press(IO1))
31+
B10058401 = Button(32, 8 + 64, 120, 48, "10058401", lambda: swipe(uart1, "10058401", None))
32+
C1357 = Button(32 + 136, 8 + 64, 120, 48, "1357#", lambda: keypad(uart1, "1357"))
33+
B10058402 = Button(32, 8 + 128, 256, 48, "10058402 + 7531#", lambda: swipe(uart2, "10058402", "7531"))
34+
LMSG = Label(0, 216, 320, 24, "")
2935

3036
touched = None
37+
message = None
3138

3239

3340
def setup():
@@ -46,23 +53,32 @@ def screen():
4653
button(B10058401)
4754
button(B10058402)
4855
button(C1357)
56+
label(LMSG)
4957

5058

5159
def button(b):
52-
offset = int(b.w/2 - 12*len(b.label)/2)
60+
offset = int(b.w / 2 - 12 * len(b.label) / 2)
5361
Widgets.Rectangle(b.x, b.y, b.w, b.h, BORDER, BACKGROUND)
5462
Widgets.Label(b.label, b.x + offset, b.y + 14, 1.0, BLACK, BACKGROUND, Widgets.FONTS.DejaVu18)
5563

5664

65+
def label(l):
66+
global message
67+
Widgets.Rectangle(l.x, l.y, l.w, l.h, DARKGRAY, DARKGRAY)
68+
message = Widgets.Label(l.text, l.x + 16, l.y + 8, 1.0, RED, DARKGRAY, Widgets.FONTS.DejaVu12)
69+
70+
5771
def pressed(b):
5872
global touched
5973

6074
return touched.x >= b.x and touched.x <= (b.x + b.w) and touched.y >= b.y and touched.y <= (b.y + b.h)
6175

6276

6377
def press(pin):
78+
message.setText(f"%-48s" % "")
6479
if pin.value():
6580
pin.value(False)
81+
message.setText(f"%-48s" % "Ok")
6682
return True
6783

6884
return False
@@ -81,17 +97,58 @@ def release():
8197

8298

8399
def swipe(uart, card, code):
100+
display("")
101+
84102
if code is None:
85-
uart.write(f'SWIPE {card}\n')
103+
uart.write(f"SWIPE {card}\n")
86104
else:
87-
uart.write(f'SWIPE {card} {code}#\n')
105+
uart.write(f"SWIPE {card} {code}#\n")
88106

89-
return True
107+
return read(uart)
90108

91109

92110
def keypad(uart, code):
93-
uart.write(f'CODE {code}#\n')
94-
return True
111+
display("")
112+
uart.write(f"CODE {code}#\n")
113+
114+
return read(uart)
115+
116+
117+
def read(uart):
118+
timeout = 500 # milliseconds
119+
start = time.ticks_ms()
120+
response = b""
121+
122+
while time.ticks_diff(time.ticks_ms(), start) < timeout:
123+
if uart.any():
124+
response += uart.read()
125+
if b"\n" in response:
126+
break
127+
128+
if response:
129+
line = response.decode().strip()
130+
print(f">>> {line}")
131+
if line == "OK":
132+
display("Ok")
133+
return True
134+
135+
match = re.compile(r"^ERROR\s+(\d+)\s+(.+)$").match(line)
136+
if match:
137+
code = int(match.group(1))
138+
msg = match.group(2)
139+
display(f"** ERROR {code} {msg}")
140+
return False
141+
142+
display("** ERROR <unknown>")
143+
return False
144+
145+
display(f"** ERROR <no reply>")
146+
return False
147+
148+
149+
def display(msg):
150+
global message
151+
message.setText(f"%-128s" % f"{msg}")
95152

96153

97154
def loop():

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

Lines changed: 54 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@
2222

2323
extern const constants HW;
2424

25+
const char *ERR_OK = "OK\n";
26+
const char *ERR_BAD_REQUEST = "ERROR 1000 bad request\n";
27+
const char *ERR_INVALID_CARD = "ERROR 1001 invalid card\n";
28+
const char *ERR_WRITE = "ERROR 1002 write failed\n";
29+
2530
typedef struct line {
2631
char bytes[64];
2732
int ix;
@@ -77,11 +82,13 @@ void on_uart1_rx();
7782
bool on_uart_monitor(repeating_timer_t *);
7883
void uart0_rxchar(uint8_t);
7984
void uart1_rxchar(uint8_t);
80-
void uart_rxchar(uint8_t, line *);
81-
void uart_exec(char *);
85+
void uart_flush(uint8_t);
86+
void uart_rxchar(const uart_inst_t *, uint8_t, line *);
87+
void uart_exec(const uart_inst_t *, char *);
88+
void uart_write(const uart_inst_t *, const char *);
8289

83-
void uart_swipe(char *msg);
84-
void uart_keypad(char *msg);
90+
void uart_swipe(const uart_inst_t *uart, char *msg);
91+
void uart_keypad(const uart_inst_t *uart, char *msg);
8592

8693
void UART_init() {
8794
// ... uart0
@@ -173,23 +180,32 @@ bool on_uart_monitor(repeating_timer_t *rt) {
173180
}
174181

175182
void UART_rx(uart_inst_t *uart, buffer *b) {
176-
buffer_flush(b, uart0_rxchar);
183+
if (uart == uart0) {
184+
buffer_flush(b, uart0_rxchar);
185+
} else if (uart == uart1) {
186+
buffer_flush(b, uart1_rxchar);
187+
} else {
188+
buffer_flush(b, uart_flush);
189+
}
177190
}
178191

179192
void uart0_rxchar(uint8_t ch) {
180-
uart_rxchar(ch, &UART.UART0.line);
193+
uart_rxchar(uart0, ch, &UART.UART0.line);
181194
}
182195

183196
void uart1_rxchar(uint8_t ch) {
184-
uart_rxchar(ch, &UART.UART1.line);
197+
uart_rxchar(uart1, ch, &UART.UART1.line);
198+
}
199+
200+
void uart_flush(uint8_t ch) {
185201
}
186202

187-
void uart_rxchar(uint8_t ch, line *line) {
203+
void uart_rxchar(const uart_inst_t *uart, uint8_t ch, line *line) {
188204
switch (ch) {
189205
case CR:
190206
case LF:
191207
if (line->ix > 0) {
192-
uart_exec(line->bytes);
208+
uart_exec(uart, line->bytes);
193209
}
194210

195211
memset(line->bytes, 0, sizeof(line->bytes));
@@ -204,26 +220,27 @@ void uart_rxchar(uint8_t ch, line *line) {
204220
}
205221
}
206222

207-
void uart_exec(char *msg) {
223+
void uart_exec(const uart_inst_t *uart, char *msg) {
208224
char s[64];
209225

210226
snprintf(s, sizeof(s), "%s", msg);
211227
debugf(LOGTAG, s);
212228

213229
if (strncasecmp(msg, "swipe ", 6) == 0) {
214-
uart_swipe(&msg[6]);
230+
uart_swipe(uart, &msg[6]);
215231
} else if (strncasecmp(msg, "code ", 5) == 0) {
216-
uart_keypad(&msg[5]);
232+
uart_keypad(uart, &msg[5]);
217233
}
218234
}
219235

220-
void uart_swipe(char *msg) {
236+
void uart_swipe(const uart_inst_t *uart, char *msg) {
221237
char *token = strtok(msg, " ,");
222238

223239
if (token != NULL) {
224240
uint32_t u32;
225241

226242
if (sscanf(msg, "%u", &u32) < 1) {
243+
uart_write(uart, ERR_BAD_REQUEST);
227244
return;
228245
}
229246

@@ -232,10 +249,12 @@ void uart_swipe(char *msg) {
232249
char *code;
233250

234251
if (facility_code < 1 || facility_code > 255 || card > 65535) {
252+
uart_write(uart, ERR_BAD_REQUEST);
235253
return;
236254
}
237255

238256
if (!write_card(facility_code, card)) {
257+
uart_write(uart, ERR_WRITE);
239258
debugf(LOGTAG, "card %u%05u error", facility_code, card);
240259
return;
241260
}
@@ -244,27 +263,39 @@ void uart_swipe(char *msg) {
244263
int N = strlen(code);
245264
for (int i = 0; i < N; i++) {
246265
if (!write_keycode(code[i])) {
266+
uart_write(uart, ERR_WRITE);
247267
debugf(LOGTAG, "keycode %c error", code[i]);
248268
return;
249269
}
250270
}
251271
}
272+
273+
uart_write(uart, ERR_OK);
252274
}
253275
}
254276

255-
void uart_keypad(char *msg) {
277+
void uart_keypad(const uart_inst_t *uart, char *msg) {
256278
int N = strlen(msg);
257279

258-
for (int i = 0; i < N; i++) {
259-
if (!write_keycode(msg[i])) {
260-
debugf(LOGTAG, "keycode %c error", msg[i]);
261-
return;
280+
if (N > 0) {
281+
for (int i = 0; i < N; i++) {
282+
if (!write_keycode(msg[i])) {
283+
uart_write(uart, ERR_WRITE);
284+
debugf(LOGTAG, "keycode %c error", msg[i]);
285+
return;
286+
}
262287
}
288+
289+
uart_write(uart, ERR_OK);
263290
}
264291
}
265292

266-
// bool uart0_write(const uint8_t *bytes, int N) {
267-
// uart_write_blocking(uart0, bytes, N);
268-
//
269-
// return true;
270-
// }
293+
void uart_write(const uart_inst_t *uart, const char *msg) {
294+
int N = strlen(msg);
295+
296+
if (uart == uart0) {
297+
uart_write_blocking(uart0, msg, N);
298+
} else if (uart == uart1) {
299+
uart_write_blocking(uart1, msg, N);
300+
}
301+
}

m5-stack/Rev.1/NOTES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@
6262
26. Buzzer IO
6363
27. SMT headers
6464
- https://electronics.stackexchange.com/questions/415542/are-these-tiny-vertical-dual-row-pcb-mounted-connector-pins-standard/719876#719876
65+
28. PCB notch for CoreS3 power switch
66+
29. Slot in enclosure for USB cable
6567

6668
- (?) https://www.ti.com/product/MSPM0C1104
6769
- (?) Add HWPR as optional power

0 commit comments

Comments
 (0)