aboutsummaryrefslogtreecommitdiff
path: root/src/main.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/main.c')
-rw-r--r--src/main.c52
1 files changed, 52 insertions, 0 deletions
diff --git a/src/main.c b/src/main.c
index 5c0aa26..cec43be 100644
--- a/src/main.c
+++ b/src/main.c
@@ -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);