1
0
mirror of https://github.com/whowechina/aic_pico.git synced 2024-11-27 23:10:48 +01:00

Light follows aime or bana led commands

This commit is contained in:
whowechina 2024-04-21 16:03:32 +08:00
parent 6eb14a6ed1
commit 924b46ca53
6 changed files with 1074 additions and 947 deletions

View File

@ -19,4 +19,6 @@ bool bana_feed(int c);
/* bana activity expires at this time */
uint64_t bana_expire_time();
uint32_t bana_led_color();
#endif

View File

@ -365,7 +365,7 @@ static void cmd_led_rgb()
send_response();
}
static void aime_handle_frame()
static void handle_frame()
{
switch (request.cmd) {
case CMD_TO_NORMAL_MODE:
@ -485,7 +485,7 @@ bool aime_feed(int c)
if (req_ctx.len != 0 && req_ctx.len == request.len) {
if (req_ctx.check_sum == c) {
aime_handle_frame();
handle_frame();
req_ctx.active = false;
expire_time = time_us_64() + AIME_EXPIRE_TIME;
}

View File

@ -24,34 +24,38 @@ static void putc_trap(uint8_t byte)
static bana_putc_func bana_putc = putc_trap;
static void bana_puts(const char *str, size_t len)
{
for (size_t i = 0; i < len; i++) {
bana_putc(str[i]);
}
}
void bana_init(bana_putc_func putc_func)
{
bana_putc = putc_func;
}
/*
static uint8_t mifare_keys[2][6]; // 'KeyA' and 'KeyB'
static union __attribute__((packed)) {
typedef union __attribute__((packed)) {
struct {
};
uint8_t raw[256];
} response;
static union __attribute__((packed)) {
struct {
uint8_t padding[3];
uint8_t len;
uint8_t len_check;
} hdr;
uint8_t dir;
uint8_t cmd;
uint8_t data[0];
};
uint8_t raw[256];
} request;
uint8_t raw[48];
} message_t;
static message_t request, response;
struct {
bool active;
uint8_t len;
uint8_t check_sum;
bool escaping;
uint64_t time;
uint8_t frame_len;
uint32_t time;
} req_ctx;
*/
typedef struct __attribute__((packed)) {
uint8_t count;
@ -68,8 +72,89 @@ typedef struct __attribute__((packed)) {
static uint64_t expire_time;
static void send_response()
{
uint8_t checksum = 0xff;
for (int i = 0; i < response.hdr.len; i++) {
checksum += response.raw[5 + i];
}
memcpy(response.hdr.padding, "\x00\x00\xff", 3);
response.hdr.len_check = ~response.hdr.len + 1;
response.raw[5 + response.hdr.len] = ~checksum;
response.raw[6 + response.hdr.len] = 0;
int total_len = 7 + response.hdr.len;
bana_puts((const char *)response.raw, total_len);
printf("\nResp: %d %d\n", response.hdr.len, response.cmd);
for (int i = 0; i < total_len; i++) {
printf(">%02x", response.raw[i]);
}
}
static void send_simple_response(uint8_t code)
{
response.hdr.len = 2;
response.dir = 0xd5;
response.cmd = code;
send_response();
}
static void send_ack()
{
bana_puts("\x00\x00\xff\x00\xff\x00", 6);
}
static uint32_t led_color = 0;
static void handle_frame()
{
printf("\nBana: %d %02x", request.hdr.len, request.cmd);
if (request.hdr.len == 0) {
send_ack();
return;
}
if (request.hdr.len + 7 != req_ctx.frame_len) {
return;
}
if (request.hdr.len != 0) {
printf("Req: %d %02x", request.hdr.len, request.cmd);
send_simple_response(request.cmd + 1);
}
}
bool bana_feed(int c)
{
uint32_t now = time_us_32();
if ((req_ctx.frame_len == sizeof(request)) ||
(now - req_ctx.time > 100000)) {
req_ctx.frame_len = 0;
}
req_ctx.time = now;
request.raw[req_ctx.frame_len] = c;
req_ctx.frame_len++;
if ((req_ctx.frame_len == 1) && (request.raw[0] == 0x55)) {
req_ctx.frame_len = 0;
} if ((req_ctx.frame_len == 3) &&
(memcmp(request.hdr.padding, "\x00\x00\xff", 3) != 0)) {
request.raw[0] = request.raw[1];
request.raw[1] = request.raw[2];
req_ctx.frame_len--;
} if ((req_ctx.frame_len == 6) && (request.hdr.len == 0)) {
req_ctx.frame_len = 0;
} else if (req_ctx.frame_len == request.hdr.len + 7) {
handle_frame();
req_ctx.frame_len = 0;
expire_time = time_us_64() + BANA_EXPIRE_TIME;
}
return true;
}
@ -77,3 +162,8 @@ uint64_t bana_expire_time()
{
return expire_time;
}
uint32_t bana_led_color()
{
return led_color;
}

View File

@ -237,10 +237,18 @@ void light_init()
}
}
static bool rainbow = true;
void light_set_rainbow(bool enable)
{
rainbow = enable;
}
void light_update()
{
if (rainbow) {
generate_color_wheel();
rainbow_update();
rainbow_fade();
}
drive_led();
}

View File

@ -20,6 +20,8 @@ uint32_t rgb32_from_hsv(uint8_t h, uint8_t s, uint8_t v);
void light_set_color(unsigned index, uint32_t color);
void light_set_color_all(uint32_t color);
void light_set_rainbow(bool enable);
void light_stimulate();
#endif

View File

@ -33,6 +33,7 @@
#include "nfc.h"
#include "aime.h"
#include "bana.h"
static struct {
uint8_t current[9];
@ -94,12 +95,27 @@ void report_usb_hid()
report_hid_key();
}
static void light_effect()
{
uint64_t now = time_us_64();
if (now < aime_expire_time()) {
light_set_rainbow(false);
light_set_color_all(aime_led_color());
} else if (now < bana_expire_time()) {
light_set_rainbow(false);
light_set_color_all(bana_led_color());
} else {
light_set_rainbow(true);
}
light_update();
}
static mutex_t core1_io_lock;
static void core1_loop()
{
while (1) {
if (mutex_try_enter(&core1_io_lock, NULL)) {
light_update();
light_effect();
mutex_exit(&core1_io_lock);
}
cli_fps_count(1);
@ -142,7 +158,8 @@ static void update_cardio(nfc_card_t *card)
void detect_card()
{
if (time_us_64() < aime_expire_time()) {
if ((time_us_64() < aime_expire_time()) ||
(time_us_64() < bana_expire_time())) {
return;
}
@ -174,13 +191,21 @@ 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));
if (count <= 0) {
return;
}
printf("\n> ");
for (int i = 0; i < count; i++) {
printf("|%02x", buf[i]);
if ((aic_cfg->mode & 0xf0) == 0) {
aime_feed(buf[i]);
} else {
//bana_feed(buf[i]);
bana_feed(buf[i]);
}
}
light_set_color_all(aime_led_color());
}
}
@ -238,7 +263,7 @@ void init()
aime_set_mode(aic_cfg->mode);
}
//bana_init();
bana_init(cdc_aime_putc);
cli_init("aic_pico>", "\n << AIC Pico >>\n"
" https://github.com/whowechina\n\n");