From baa6a42b2ce041897950062f616e384db3fe8b22 Mon Sep 17 00:00:00 2001 From: whowechina Date: Sun, 5 Nov 2023 21:23:28 +0800 Subject: [PATCH] Sega AIME IO working --- firmware/src/aime.c | 40 ++++++++++++++-------------------------- firmware/src/aime.h | 8 ++++++-- firmware/src/main.c | 25 ++++++++++++++++++++----- firmware/src/rgb.c | 2 +- 4 files changed, 41 insertions(+), 34 deletions(-) diff --git a/firmware/src/aime.c b/firmware/src/aime.c index cf9d9d0..5d988f0 100644 --- a/firmware/src/aime.c +++ b/firmware/src/aime.c @@ -13,6 +13,7 @@ #include "hardware/i2c.h" #include "pn532.h" +#include "aime.h" #define FRAME_TIMEOUT 200000 @@ -80,6 +81,12 @@ static struct { const uint8_t syscode[2]; } virtual_aic = { true, false, "", "\x00\xf1\x00\x00\x00\x01\x43\x00", "\x88\xb4" }; +static void putc_trap(uint8_t byte) +{ +} + +static aime_putc_func aime_putc = putc_trap; + void aime_set_baudrate(int mode) { baudrate_mode = (mode == 0) ? 0 : 1; @@ -90,11 +97,9 @@ void aime_set_virtual_aic(bool enable) virtual_aic.enabled = enable; } -static int aime_interface = -1; - -void aime_init(int interface) +void aime_init(aime_putc_func putc_func) { - aime_interface = interface; + aime_putc = putc_func; pn532_config_sam(); } @@ -158,23 +163,21 @@ static void build_response(int payload_len) static void send_response() { uint8_t checksum = 0; - uint8_t sync = 0xe0; - tud_cdc_n_write(aime_interface, &sync, 1); + aime_putc(sync); for (int i = 0; i < response.len; i++) { uint8_t c = response.raw[i]; checksum += c; if (c == 0xe0 || c == 0xd0) { uint8_t escape = 0xd0; - tud_cdc_n_write(aime_interface, &escape, 1); + aime_putc(escape); c--; } - tud_cdc_n_write(aime_interface, &c, 1); + aime_putc(c); } - tud_cdc_n_write(aime_interface, &checksum, 1); - tud_cdc_n_write_flush(aime_interface); + aime_putc(checksum); } static void send_simple_response(uint8_t status) @@ -643,7 +646,7 @@ static void aime_handle_frame() } } -static bool aime_feed(int c) +bool aime_feed(int c) { if (c == 0xe0) { req_ctx.active = true; @@ -683,21 +686,6 @@ static bool aime_feed(int c) return true; } -void aime_update() -{ - if (req_ctx.active && time_us_64() - req_ctx.time > FRAME_TIMEOUT) { - req_ctx.active = false; - } - - if (tud_cdc_n_available(aime_interface)) { - uint8_t buf[32]; - uint32_t count = tud_cdc_n_read(aime_interface, buf, sizeof(buf)); - for (int i = 0; i < count; i++) { - aime_feed(buf[i]); - } - } -} - uint32_t aime_led_color() { return led_color; diff --git a/firmware/src/aime.h b/firmware/src/aime.h index dd0b841..7419209 100644 --- a/firmware/src/aime.h +++ b/firmware/src/aime.h @@ -6,8 +6,12 @@ #ifndef AIME_H #define AIME_H -void aime_init(int interface); -void aime_update(); +/* return true if accepts a byte, false if rejects */ +typedef void (*aime_putc_func)(uint8_t byte); + +void aime_init(aime_putc_func putc_func); +bool aime_feed(int c); + uint32_t aime_led_color(); // mode 0 or 1 diff --git a/firmware/src/main.c b/firmware/src/main.c index 42af08e..ecb7fd8 100644 --- a/firmware/src/main.c +++ b/firmware/src/main.c @@ -111,13 +111,28 @@ void detect_card() memset(cardio.current, 0, 9); } +const int aime_intf = 1; +static void cdc_aime_putc(uint8_t byte) +{ + tud_cdc_n_write(aime_intf, &byte, 1); + tud_cdc_n_write_flush(aime_intf); +} + +static void aime_run() +{ + if (tud_cdc_n_available(aime_intf)) { + uint8_t buf[32]; + int count = tud_cdc_n_read(aime_intf, buf, sizeof(buf)); + for (int i = 0; i < count; i++) { + aime_feed(buf[i]); + } + } +} + void wait_loop() { tud_task(); - cli_run(); - aime_update(); - cli_fps_count(0); } @@ -127,7 +142,7 @@ static void core0_loop() tud_task(); cli_run(); - aime_update(); + aime_run(); save_loop(); cli_fps_count(0); @@ -154,7 +169,7 @@ void init() pn532_init(I2C_PORT, I2C_SCL, I2C_SDA, I2C_FREQ); pn532_set_wait_loop(wait_loop); - aime_init(1); + aime_init(cdc_aime_putc); cli_init("aic_pico>", "\n << AIC Pico >>\n" " https://github.com/whowechina\n\n"); diff --git a/firmware/src/rgb.c b/firmware/src/rgb.c index 8b4dbf1..a856264 100644 --- a/firmware/src/rgb.c +++ b/firmware/src/rgb.c @@ -111,7 +111,7 @@ static void generate_color_wheel() old_level = aic_cfg->led.level; for (int i = 0; i < COLOR_WHEEL_SIZE; i++) { - color_wheel[i] = rgb32_from_hsv(i, 250, 255); + color_wheel[i] = rgb32_from_hsv(i, 208, 255); color_wheel[i] = apply_level(color_wheel[i]); } }