diff options
| author | François Cartegnie <281376+fcartegnie@users.noreply.github.com> | 2024-08-20 13:43:09 +0700 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-08-20 13:43:09 +0700 |
| commit | f5874d607315ff88b27414299089b8528b5bb07c (patch) | |
| tree | 9db23b8e6cdfb06d8bab77e83224e604f20c4629 /src/main.c | |
| parent | b25d62d1362cfd711e6617a9212e3eddc4e9fddd (diff) | |
| parent | c3912e01a2767334103cd139d875d50c834f3f05 (diff) | |
Merge pull request #35 from kienvo/usb
feat: Receive data over USB
Diffstat (limited to 'src/main.c')
| -rw-r--r-- | src/main.c | 52 |
1 files changed, 52 insertions, 0 deletions
@@ -10,6 +10,8 @@ #include "ble/setup.h"
#include "ble/profile.h"
+#include "usb/usb.h"
+
#define FB_WIDTH (LED_COLS * 4)
#define SCROLL_IRATIO (16)
#define SCAN_F (2000)
@@ -92,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;
@@ -123,10 +159,26 @@ void handle_mode_transition() prev_mode = mode;
}
+static void debug_init()
+{
+ GPIOA_SetBits(GPIO_Pin_9);
+ GPIOA_ModeCfg(GPIO_Pin_8, GPIO_ModeIN_PU);
+ GPIOA_ModeCfg(GPIO_Pin_9, GPIO_ModeOut_PP_5mA);
+ UART1_DefInit();
+ UART1_BaudRateCfg(921600);
+}
+
int main()
{
SetSysClock(CLK_SOURCE_PLL_60MHz);
+ debug_init();
+ PRINT("\nDebug console is on UART%d\n", DEBUG);
+
+ cdc_onWrite(usb_receive);
+ hiddev_onWrite(usb_receive);
+ usb_start();
+
led_init();
TMR0_TimerInit(SCAN_T / 2);
TMR0_ITCfg(ENABLE, TMR0_3_IT_CYC_END);
|