aboutsummaryrefslogtreecommitdiff
path: root/src/main.c
diff options
context:
space:
mode:
authorDien-Nhung Nguyen-Phu <kein@kienlab.com>2024-07-11 21:23:07 +0700
committerDien-Nhung Nguyen-Phu <kein@kienlab.com>2024-07-11 22:18:56 +0700
commit49fc27a23e0f5ca46337493cdac02ee116f786e3 (patch)
treed46124776c6fcf295ef18a18f2f90c128e2369e0 /src/main.c
parent4f3b6f4283e0240060c04a137175cacfb385dc16 (diff)
usb: add hiddev and write to flash
Diffstat (limited to 'src/main.c')
-rw-r--r--src/main.c35
1 files changed, 35 insertions, 0 deletions
diff --git a/src/main.c b/src/main.c
index 4f46181..f2e788c 100644
--- a/src/main.c
+++ b/src/main.c
@@ -94,6 +94,40 @@ void ble_start()
legacy_registerService();
}
+static void usb_receive(uint8_t *buf, uint16_t len)
+{
+ static uint16_t rx_len, data_len;
+ static uint8_t *data;
+
+ PRINT("dump first 8 bytes: %02x %02x %02x %02x %02x %02x %02x %02x\n",
+ buf[0], buf[1], buf[2], buf[3],
+ buf[4], buf[5], buf[6], buf[7]);
+
+ if (rx_len == 0) {
+ if (memcmp(buf, "wang", 5))
+ return;
+
+ int init_len = len > LEGACY_HEADER_SIZE ? len : sizeof(data_legacy_t);
+ init_len += MAX_PACKET_SIZE;
+ data = malloc(init_len);
+ }
+
+ memcpy(data + rx_len, buf, len);
+ rx_len += len;
+
+ if (!data_len) {
+ data_legacy_t *d = (data_legacy_t *)data;
+ uint16_t n = bigendian16_sum(d->sizes, 8);
+ data_len = LEGACY_HEADER_SIZE + LED_ROWS * n;
+ data = realloc(data, data_len);
+ }
+
+ if ((rx_len > LEGACY_HEADER_SIZE) && rx_len >= data_len) {
+ data_flatSave(data, data_len);
+ SYS_ResetExecute();
+ }
+}
+
void handle_mode_transition()
{
static int prev_mode;
@@ -141,6 +175,7 @@ int main()
debug_init();
PRINT("\nDebug console is on UART%d\n", DEBUG);
+ hiddev_onWrite(usb_receive);
usb_start();
led_init();