Skip to content

Commit 6c4e484

Browse files
committed
M5: commonalized API swipe/keycode implementation
1 parent b9db065 commit 6c4e484

File tree

8 files changed

+103
-117
lines changed

8 files changed

+103
-117
lines changed

m5-stack/Rev.0/TODO.md

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
44
## [x] PCB
55
## [x] Design Review
66
## [x] CoreS3
7-
8-
## [ ] Firmware
7+
## [x] Firmware
98
- [x] CLI
109
- [x] LED
1110
- [x] SK6812
@@ -14,17 +13,13 @@
1413
- [x] UART
1514
- [x] sys: memory usage
1615
- [x] USB
17-
- [x] bootsel
18-
- [x] card
19-
- [x] keycode
20-
- [x] card + keycode
21-
- [x] error/ok
22-
- [ ] api.c
16+
- [x] api.c
2317

24-
- [ ] Check power module
2518
- [x] README
2619
- [x] photos
2720

21+
- [ ] Check power module
22+
2823
- [ ] USB + MacOS
2924

3025

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#pragma once
2+
3+
const char *swipe(const char *);
4+
const char *keycode(const char *);
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#include <stdint.h>
2+
#include <stdio.h>
3+
#include <string.h>
4+
5+
#include <M5.h>
6+
#include <log.h>
7+
#include <wiegand.h>
8+
9+
#define LOGTAG "API"
10+
11+
const char *swipe(char *msg) {
12+
char *token = strtok(msg, " ,");
13+
14+
if (token != NULL) {
15+
uint32_t u32;
16+
17+
if (sscanf(msg, "%u", &u32) < 1) {
18+
return ERR_BAD_REQUEST;
19+
}
20+
21+
uint32_t facility_code = u32 / 100000;
22+
uint32_t card = u32 % 100000;
23+
char *code;
24+
25+
if (facility_code < 1 || facility_code > 255 || card > 65535) {
26+
return ERR_BAD_REQUEST;
27+
}
28+
29+
if (!write_card(facility_code, card)) {
30+
debugf(LOGTAG, "card %u%05u error", facility_code, card);
31+
return ERR_WRITE;
32+
}
33+
34+
if ((code = strtok(NULL, " ,")) != NULL) {
35+
int N = strlen(code);
36+
for (int i = 0; i < N; i++) {
37+
if (!write_keycode(code[i])) {
38+
debugf(LOGTAG, "keycode %c error", code[i]);
39+
return ERR_WRITE;
40+
}
41+
}
42+
}
43+
44+
return ERR_OK;
45+
}
46+
47+
return "";
48+
}
49+
50+
const char *keycode(char *msg) {
51+
int N = strlen(msg);
52+
53+
if (N > 0) {
54+
for (int i = 0; i < N; i++) {
55+
if (!write_keycode(msg[i])) {
56+
debugf(LOGTAG, "keycode %c error", msg[i]);
57+
return ERR_WRITE;
58+
}
59+
}
60+
61+
return ERR_OK;
62+
}
63+
64+
return "";
65+
}

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ void exec(char *cmd);
2727

2828
void set_LED(const char *);
2929
void blink_LED(const char *);
30-
void swipe(char *);
31-
void keypad(const char *);
30+
void cli_swipe(char *);
31+
void cli_keypad(const char *);
3232
void set_keypad_mode(const char *);
3333
void reboot();
3434
void bootsel();
@@ -346,9 +346,9 @@ void exec(char *cmd) {
346346
cli.last.ix = cli.buffer.ix;
347347

348348
if (strncasecmp(cmd, "card ", 5) == 0) {
349-
swipe(&cmd[5]);
349+
cli_swipe(&cmd[5]);
350350
} else if (strncasecmp(cmd, "code ", 5) == 0) {
351-
keypad(&cmd[5]);
351+
cli_keypad(&cmd[5]);
352352
} else if (strncasecmp(cmd, "set LED ", 8) == 0) {
353353
set_LED(&cmd[8]);
354354
} else if (strncasecmp(cmd, "blink ", 6) == 0) {
@@ -397,7 +397,7 @@ void bootsel() {
397397
* the command and writes the card and keycode (if present) to the Wiegand
398398
* interface.
399399
*/
400-
void swipe(char *cmd) {
400+
void cli_swipe(char *cmd) {
401401
uint32_t facility_code = FACILITY_CODE;
402402
uint32_t card = 0;
403403
char *token = strtok(cmd, " ,");
@@ -458,7 +458,7 @@ void swipe(char *cmd) {
458458
* Sends the keycode code as 4-bit/8-bit burst mode Wiegand.
459459
*
460460
*/
461-
void keypad(const char *cmd) {
461+
void cli_keypad(const char *cmd) {
462462
int N = strlen(cmd);
463463

464464
if (N > 0) {

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

Lines changed: 10 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <tusb.h>
77

88
#include <M5.h>
9+
#include <api.h>
910
#include <buffer.h>
1011
#include <log.h>
1112
#include <sys.h>
@@ -83,7 +84,7 @@ void uart_exec(const uart_inst_t *, char *);
8384
void uart_write(const uart_inst_t *, const char *);
8485

8586
void uart_swipe(const uart_inst_t *uart, char *msg);
86-
void uart_keypad(const uart_inst_t *uart, char *msg);
87+
void uart_keycode(const uart_inst_t *uart, char *msg);
8788

8889
void uart0_rx(buffer *);
8990
void uart1_rx(buffer *);
@@ -235,64 +236,23 @@ void uart_exec(const uart_inst_t *uart, char *msg) {
235236
if (strncasecmp(msg, "swipe ", 6) == 0) {
236237
uart_swipe(uart, &msg[6]);
237238
} else if (strncasecmp(msg, "code ", 5) == 0) {
238-
uart_keypad(uart, &msg[5]);
239+
uart_keycode(uart, &msg[5]);
239240
}
240241
}
241242

242243
void uart_swipe(const uart_inst_t *uart, char *msg) {
243-
char *token = strtok(msg, " ,");
244+
const char *reply = swipe(msg);
244245

245-
if (token != NULL) {
246-
uint32_t u32;
247-
248-
if (sscanf(msg, "%u", &u32) < 1) {
249-
uart_write(uart, ERR_BAD_REQUEST);
250-
return;
251-
}
252-
253-
uint32_t facility_code = u32 / 100000;
254-
uint32_t card = u32 % 100000;
255-
char *code;
256-
257-
if (facility_code < 1 || facility_code > 255 || card > 65535) {
258-
uart_write(uart, ERR_BAD_REQUEST);
259-
return;
260-
}
261-
262-
if (!write_card(facility_code, card)) {
263-
uart_write(uart, ERR_WRITE);
264-
debugf(LOGTAG, "card %u%05u error", facility_code, card);
265-
return;
266-
}
267-
268-
if ((code = strtok(NULL, " ,")) != NULL) {
269-
int N = strlen(code);
270-
for (int i = 0; i < N; i++) {
271-
if (!write_keycode(code[i])) {
272-
uart_write(uart, ERR_WRITE);
273-
debugf(LOGTAG, "keycode %c error", code[i]);
274-
return;
275-
}
276-
}
277-
}
278-
279-
uart_write(uart, ERR_OK);
246+
if (strlen(reply) > 0) {
247+
uart_write(uart, reply);
280248
}
281249
}
282250

283-
void uart_keypad(const uart_inst_t *uart, char *msg) {
284-
int N = strlen(msg);
285-
286-
if (N > 0) {
287-
for (int i = 0; i < N; i++) {
288-
if (!write_keycode(msg[i])) {
289-
uart_write(uart, ERR_WRITE);
290-
debugf(LOGTAG, "keycode %c error", msg[i]);
291-
return;
292-
}
293-
}
251+
void uart_keycode(const uart_inst_t *uart, char *msg) {
252+
const char *reply = keycode(msg);
294253

295-
uart_write(uart, ERR_OK);
254+
if (strlen(reply) > 0) {
255+
uart_write(uart, reply);
296256
}
297257
}
298258

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

Lines changed: 12 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include <tusb.h>
22

33
#include <M5.h>
4+
#include <api.h>
45
#include <buffer.h>
56
#include <log.h>
67
#include <sys.h>
@@ -58,8 +59,8 @@ struct {
5859
void usb_rx(buffer *);
5960
void usb_rxchar(uint8_t);
6061
void usb_exec(char *);
61-
void usb_keypad(char *);
62-
void usb_swipe(char *);
62+
void usb_keycode(const char *);
63+
void usb_swipe(const char *);
6364
bool usb_write(const char *);
6465

6566
struct rxh RXH = {
@@ -180,64 +181,23 @@ void usb_exec(char *msg) {
180181
if (strncasecmp(msg, "swipe ", 6) == 0) {
181182
usb_swipe(&msg[6]);
182183
} else if (strncasecmp(msg, "code ", 5) == 0) {
183-
usb_keypad(&msg[5]);
184+
usb_keycode(&msg[5]);
184185
}
185186
}
186187

187-
void usb_swipe(char *msg) {
188-
char *token = strtok(msg, " ,");
188+
void usb_swipe(const char *msg) {
189+
const char *reply = swipe(msg);
189190

190-
if (token != NULL) {
191-
uint32_t u32;
192-
193-
if (sscanf(msg, "%u", &u32) < 1) {
194-
usb_write(ERR_BAD_REQUEST);
195-
return;
196-
}
197-
198-
uint32_t facility_code = u32 / 100000;
199-
uint32_t card = u32 % 100000;
200-
char *code;
201-
202-
if (facility_code < 1 || facility_code > 255 || card > 65535) {
203-
usb_write(ERR_BAD_REQUEST);
204-
return;
205-
}
206-
207-
if (!write_card(facility_code, card)) {
208-
usb_write(ERR_WRITE);
209-
debugf(LOGTAG, "card %u%05u error", facility_code, card);
210-
return;
211-
}
212-
213-
if ((code = strtok(NULL, " ,")) != NULL) {
214-
int N = strlen(code);
215-
for (int i = 0; i < N; i++) {
216-
if (!write_keycode(code[i])) {
217-
usb_write(ERR_WRITE);
218-
debugf(LOGTAG, "keycode %c error", code[i]);
219-
return;
220-
}
221-
}
222-
}
223-
224-
usb_write(ERR_OK);
191+
if (strlen(reply) > 0) {
192+
usb_write(reply);
225193
}
226194
}
227195

228-
void usb_keypad(char *msg) {
229-
int N = strlen(msg);
230-
231-
if (N > 0) {
232-
for (int i = 0; i < N; i++) {
233-
if (!write_keycode(msg[i])) {
234-
usb_write(ERR_WRITE);
235-
debugf(LOGTAG, "keycode %c error", msg[i]);
236-
return;
237-
}
238-
}
196+
void usb_keycode(const char *msg) {
197+
const char *reply = keycode(msg);
239198

240-
usb_write(ERR_OK);
199+
if (strlen(reply) > 0) {
200+
usb_write(reply);
241201
}
242202
}
243203

m5-stack/Rev.0/firmware/tiny-coreS3/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ add_executable(${TARGET_NAME}
4040
${CORE}/src/LED.c
4141
${CORE}/src/GPIO.c
4242
${CORE}/src/wiegand.c
43+
${CORE}/src/api.c
4344
)
4445

4546
pico_generate_pio_header(

m5-stack/Rev.0/firmware/tiny-usb/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ add_executable(${TARGET_NAME}
4141
${CORE}/src/LED.c
4242
${CORE}/src/GPIO.c
4343
${CORE}/src/wiegand.c
44+
${CORE}/src/api.c
4445
)
4546

4647
pico_generate_pio_header(

0 commit comments

Comments
 (0)