From e1815605f6bc6e8691b5a78940403912b0727d50 Mon Sep 17 00:00:00 2001 From: Esteban Fuentealba Date: Mon, 11 Mar 2024 01:57:17 -0300 Subject: [PATCH] Update GB Link Camera 2.0 New Serial API closes #18 --- .../malveke_gb_link_camera/application.fam | 2 +- .../malveke_gb_link_camera/link_camera.c | 77 +------------ .../malveke_gb_link_camera/link_camera.h | 1 + .../external/malveke_gb_link_camera/uart.c | 104 ++++++++++-------- .../external/malveke_gb_link_camera/uart.h | 11 +- 5 files changed, 70 insertions(+), 125 deletions(-) diff --git a/flipper_companion_apps/applications/external/malveke_gb_link_camera/application.fam b/flipper_companion_apps/applications/external/malveke_gb_link_camera/application.fam index 8f053b99aa0..d3ba7dd0ed6 100644 --- a/flipper_companion_apps/applications/external/malveke_gb_link_camera/application.fam +++ b/flipper_companion_apps/applications/external/malveke_gb_link_camera/application.fam @@ -10,7 +10,7 @@ App( ], stack_size=1 * 1024, order=10, - fap_version=[1,1], + fap_version=[2,0], fap_libs=["assets"], fap_icon="icons/boilerplate_10px.png", fap_icon_assets="icons", diff --git a/flipper_companion_apps/applications/external/malveke_gb_link_camera/link_camera.c b/flipper_companion_apps/applications/external/malveke_gb_link_camera/link_camera.c index 7c7c076804c..6a54c2ce51e 100644 --- a/flipper_companion_apps/applications/external/malveke_gb_link_camera/link_camera.c +++ b/flipper_companion_apps/applications/external/malveke_gb_link_camera/link_camera.c @@ -1,7 +1,6 @@ #include "link_camera.h" #include #include -// #include QRCode qrcode; uint8_t qrcodeData[((((4 * 8 + 17) * (4 * 8 + 17)) + 7) / 8)]; @@ -21,52 +20,9 @@ static void view_draw_callback(Canvas *canvas, void *_model) if (model->connected) { - // canvas_draw_str_aligned(canvas, 128/2, 64/3, AlignCenter, AlignCenter, "Connected"); - // canvas_draw_str_aligned(canvas, 128/2, 64/2, AlignCenter, AlignCenter, model->ip); - /* - char ssid[64]; // Incrementa el tamaño a 64 - char password[64]; // Incrementa el tamaño a 64 - char qrstr[150]; // Deja qrstr con el tamaño actual - snprintf(ssid, sizeof(ssid), "%s", "[MALVEKE] Flipper GB Camera"); - snprintf(password, sizeof(password), "%s", "12345678"); - snprintf(qrstr, sizeof(qrstr), "WIFI:S:%s;T:WPA2;P:%s;;", ssid, password); - qrcode_initText(&qrcode, qrcodeData, 2, 0, qrstr); - // canvas_draw_frame(canvas, 63, 63, 0, 0); - - for (uint8_t y = 0; y < qrcode.size; y++) { - // Cada módulo horizontal - for (uint8_t x = 0; x < qrcode.size; x++) { - if (qrcode_getModule(&qrcode, x, y)) { - if(qrcode_getModule(&qrcode, x, y)) { - // canvas_draw_dot(canvas, x, y); - int scale = 2; - // // Dibuja cada punto dos veces para escalar al doble - canvas_draw_dot(canvas, (x * scale) + 2, (y * scale) + 4); - canvas_draw_dot(canvas, (x * scale + 1) + 2, (y * scale) + 4); - canvas_draw_dot(canvas, (x * scale) + 2, (y * scale + 1) + 4); - canvas_draw_dot(canvas, (x * scale + 1) + 2, (y * scale + 1) + 4); - } - } - } - } - canvas_set_font(canvas, FontPrimary); - canvas_draw_str(canvas, 62, 11, "SSID"); - canvas_set_font(canvas, FontSecondary); - // u8g2_SetFont(&canvas->fb, u8g2_font_5x7_tf); //u8g2_font_micro_tr); - // canvas_set_font(canvas, FontBatteryPercent); - canvas_draw_str(canvas, 62, 20, "[MALVEKE] Flipper GB Camera"); - canvas_set_font(canvas, FontPrimary); - canvas_draw_str(canvas, 62, 30, "Password"); - canvas_set_font(canvas, FontSecondary); - // canvas_set_font(canvas, u8g2_font_5x7_tf); - canvas_draw_str(canvas, 62, 40, "12345678"); - - */ canvas_set_font(canvas, FontPrimary); canvas_draw_str(canvas, 2, 11, "SSID"); canvas_set_font(canvas, FontSecondary); - // u8g2_SetFont(&canvas->fb, u8g2_font_5x7_tf); //u8g2_font_micro_tr); - // canvas_set_font(canvas, FontBatteryPercent); canvas_draw_str(canvas, 2, 20, "[MALVEKE] Flipper GB Cam"); canvas_set_font(canvas, FontPrimary); canvas_draw_str(canvas, 2, 30, "Password"); @@ -91,16 +47,6 @@ static void view_draw_callback(Canvas *canvas, void *_model) elements_button_center(canvas, "Ok"); } - // if (!model->initialized){ - // canvas_clear(canvas); - // canvas_draw_icon(canvas, 74, 16, &I_DolphinCommon_56x48); - // canvas_set_font(canvas, FontSecondary); - // canvas_draw_str(canvas, 8, 12, "Waiting MALVEKE Board..."); - // // canvas_draw_str(canvas, 20, 24, "VCC - 3V3/5V"); - // // canvas_draw_str(canvas, 20, 34, "GND - GND"); - // // canvas_draw_str(canvas, 20, 44, "U0R - TX"); - // // canvas_draw_str(canvas, 20, 54, "U0T - RX"); - // } } static bool view_input_callback(InputEvent *event, void *context) { @@ -133,8 +79,8 @@ static bool view_input_callback(InputEvent *event, void *context) model->initialized = true; }, true); - uart_tx((uint8_t *)("c"), 1); - uart_tx((uint8_t *)("\n"), 1); + uart_tx(instance->uart, (uint8_t *)("c"), 1); + uart_tx(instance->uart, (uint8_t *)("\n"), 1); } } @@ -158,10 +104,8 @@ void handle_rx_data_cb(uint8_t *buf, size_t len, void *context) { furi_assert(context); UNUSED(len); - // UNUSED(buf); LinkCameraApp *instance = (LinkCameraApp *)context; - // FURI_LOG_I("UART", "[in]: %s", (char*)buf); with_view_model( instance->view, LinkCameraModel * model, @@ -243,21 +187,6 @@ static LinkCameraApp *link_camera_alloc() view_dispatcher_add_view(app->view_dispatcher, 0, app->view); view_dispatcher_switch_to_view(app->view_dispatcher, 0); - // furi_hal_power_disable_external_3_3v(); - // furi_delay_ms(100); - // furi_hal_power_enable_external_3_3v(); - // furi_delay_ms(200); - - // with_view_model( - // app->view, - // LinkCameraModel * model, - // { - // model->initialized = true; - // }, - // true); - // uart_tx((uint8_t*)("c"), 1); - // uart_tx((uint8_t*)("\n"), 1); - return app; } @@ -269,7 +198,5 @@ int32_t link_camera_app(void *p) view_dispatcher_run(app->view_dispatcher); link_camera_free(app); - // furi_hal_power_disable_otg(); - return 0; } diff --git a/flipper_companion_apps/applications/external/malveke_gb_link_camera/link_camera.h b/flipper_companion_apps/applications/external/malveke_gb_link_camera/link_camera.h index 26db50f5731..34bd32e0d32 100644 --- a/flipper_companion_apps/applications/external/malveke_gb_link_camera/link_camera.h +++ b/flipper_companion_apps/applications/external/malveke_gb_link_camera/link_camera.h @@ -4,6 +4,7 @@ #include #include #include +#include #include "cJSON.h" #include "qrcode.h" #include "uart.h" diff --git a/flipper_companion_apps/applications/external/malveke_gb_link_camera/uart.c b/flipper_companion_apps/applications/external/malveke_gb_link_camera/uart.c index 829ab3c11fd..c305bca146f 100644 --- a/flipper_companion_apps/applications/external/malveke_gb_link_camera/uart.c +++ b/flipper_companion_apps/applications/external/malveke_gb_link_camera/uart.c @@ -1,23 +1,28 @@ #include "uart.h" -#define UART_CH (FuriHalUartIdUSART1) -#define LP_UART_CH (FuriHalUartIdLPUART1) -#define BAUDRATE (115200) +#define UART_CH (FuriHalSerialIdUsart) +#define LP_UART_CH (FuriHalSerialIdLpuart) +#define BAUDRATE (115200UL) struct Uart { void* app; - FuriHalUartId channel; FuriThread* rx_thread; + FuriHalSerialHandle* serial_handle; + FuriHalSerialId channel; + FuriThread* worker_thread; FuriStreamBuffer* rx_stream; uint8_t rx_buf[RX_BUF_SIZE + 1]; void (*handle_rx_data_cb)(uint8_t* buf, size_t len, void* context); }; - typedef enum { WorkerEvtStop = (1 << 0), WorkerEvtRxDone = (1 << 1), -} WorkerEvtFlags; +} WorkerEventFlags; + +#define WORKER_ALL_RX_EVENTS (WorkerEvtStop | WorkerEvtRxDone) + + void uart_set_handle_rx_data_cb( Uart* uart, @@ -26,17 +31,29 @@ void uart_set_handle_rx_data_cb( uart->handle_rx_data_cb = handle_rx_data_cb; } -#define WORKER_ALL_RX_EVENTS (WorkerEvtStop | WorkerEvtRxDone) - -void uart_on_irq_cb(UartIrqEvent ev, uint8_t data, void* context) { +static void wifi_marauder_uart_on_irq_cb( + FuriHalSerialHandle* handle, + FuriHalSerialRxEvent event, + void* context) { Uart* uart = (Uart*)context; - if(ev == UartIrqEventRXNE) { + if(event == FuriHalSerialRxEventData) { + uint8_t data = furi_hal_serial_async_rx(handle); furi_stream_buffer_send(uart->rx_stream, &data, 1, 0); furi_thread_flags_set(furi_thread_get_id(uart->rx_thread), WorkerEvtRxDone); } } +static void uart_on_irq_cb(FuriHalSerialHandle* handle, FuriHalSerialRxEvent event, void* context) { + Uart* uart = (Uart*)context; + UNUSED(handle); + + if(event & (FuriHalSerialRxEventData | FuriHalSerialRxEventIdle)) { + uint8_t data = furi_hal_serial_async_rx(handle); + furi_stream_buffer_send(uart->rx_stream, &data, 1, 0); + furi_thread_flags_set(furi_thread_get_id(uart->rx_thread), WorkerEvtRxDone); + } +} // Define una constante para el prefijo que estamos buscando #define JSON_PREFIX "JSON:" @@ -50,26 +67,25 @@ static bool json_capture_active = false; // Prototipo de la función // static void process_json_buffer(); - static void process_json_buffer(void* context) { Uart* uart = (Uart*)context; // Agregamos el terminador nulo al final del buffer json_buffer[json_buffer_index] = '\0'; - if (uart->handle_rx_data_cb) { - uart->handle_rx_data_cb((uint8_t *)json_buffer, json_buffer_index, uart->app); + if(uart->handle_rx_data_cb) { + uart->handle_rx_data_cb((uint8_t*)json_buffer, json_buffer_index, uart->app); memset(json_buffer, 0, sizeof(json_buffer)); } - + // Reiniciamos el buffer json_buffer_index = 0; } static void uart_echo_push_to_list(void* context, uint8_t data) { Uart* uart = (Uart*)context; - if (!json_capture_active) { - if (data == JSON_PREFIX[json_buffer_index]) { + if(!json_capture_active) { + if(data == JSON_PREFIX[json_buffer_index]) { json_buffer[json_buffer_index++] = data; // Agregar el carácter al buffer - if (json_buffer_index == strlen(JSON_PREFIX)) { + if(json_buffer_index == strlen(JSON_PREFIX)) { // Encontramos el prefijo, comenzamos a capturar json_buffer_index = 0; json_capture_active = true; @@ -81,7 +97,7 @@ static void uart_echo_push_to_list(void* context, uint8_t data) { } else { // Capturamos caracteres hasta encontrar '\n' json_buffer[json_buffer_index++] = data; - if (data == '\n') { + if(data == '\n') { // Terminamos de capturar la línea, procesamos el buffer json_capture_active = false; process_json_buffer(uart); @@ -93,7 +109,8 @@ static int32_t uart_worker(void* context) { Uart* uart = (Uart*)context; while(1) { - uint32_t events = furi_thread_flags_wait(WORKER_ALL_RX_EVENTS, FuriFlagWaitAny, FuriWaitForever); + uint32_t events = + furi_thread_flags_wait(WORKER_ALL_RX_EVENTS, FuriFlagWaitAny, FuriWaitForever); furi_check((events & FuriFlagError) == 0); if(events & WorkerEvtStop) break; @@ -103,17 +120,20 @@ static int32_t uart_worker(void* context) { do { uint8_t data[64]; length = furi_stream_buffer_receive(uart->rx_stream, data, 64, 0); - // FURI_LOG_I("UART", "[in]: %s", (char*)data); + if(length > 0) { for(size_t i = 0; i < length; i++) { uart_echo_push_to_list(uart, data[i]); + // FURI_LOG_I("UART", "[in]: %c - %d", (const char)data[i], data[i]); } } } while(length > 0); } else if(uart->channel == LP_UART_CH) { - size_t len = furi_stream_buffer_receive(uart->rx_stream, uart->rx_buf, RX_BUF_SIZE, 0); + size_t len = + furi_stream_buffer_receive(uart->rx_stream, uart->rx_buf, RX_BUF_SIZE, 0); if(len > 0) { - if(uart->handle_rx_data_cb) uart->handle_rx_data_cb(uart->rx_buf, len, uart->app); + if(uart->handle_rx_data_cb) + uart->handle_rx_data_cb(uart->rx_buf, len, uart->app); } } } @@ -122,19 +142,13 @@ static int32_t uart_worker(void* context) { return 0; } - -void uart_tx(uint8_t* data, size_t len) { - furi_hal_uart_tx(UART_CH, data, len); +void uart_tx(void* app, uint8_t* data, size_t len) { + Uart* uart = (Uart*)app; + furi_hal_serial_tx(uart->serial_handle, data, len); } -void lp_uart_tx(uint8_t* data, size_t len) { - furi_hal_uart_tx(LP_UART_CH, data, len); -} - -Uart* - _uart_init(void* app, FuriHalUartId channel, const char* thread_name) { +Uart* _uart_init(void* app, FuriHalSerialId channel, const char* thread_name) { Uart* uart = (Uart*)malloc(sizeof(Uart)); - uart->app = app; uart->channel = channel; uart->rx_stream = furi_stream_buffer_alloc(RX_BUF_SIZE, 1); @@ -144,13 +158,18 @@ Uart* furi_thread_set_context(uart->rx_thread, uart); furi_thread_set_callback(uart->rx_thread, uart_worker); furi_thread_start(uart->rx_thread); - if(channel == FuriHalUartIdUSART1) { - furi_hal_console_disable(); - } else if(channel == FuriHalUartIdLPUART1) { - furi_hal_uart_init(channel, BAUDRATE); + uart->serial_handle = furi_hal_serial_control_acquire(channel); + if(!uart->serial_handle) { + furi_delay_ms(5000); } - furi_hal_uart_set_br(channel, BAUDRATE); - furi_hal_uart_set_irq_cb(channel, uart_on_irq_cb, uart); + furi_check(uart->serial_handle); + furi_hal_serial_init(uart->serial_handle, BAUDRATE); + furi_hal_serial_async_rx_start( + uart->serial_handle, + channel == FuriHalSerialIdUsart ? uart_on_irq_cb : wifi_marauder_uart_on_irq_cb, + uart, + false); + return uart; } @@ -166,15 +185,12 @@ Uart* lp_uart_init(void* app) { void uart_free(Uart* uart) { furi_assert(uart); - furi_thread_flags_set(furi_thread_get_id(uart->rx_thread), WorkerEvtStop); + furi_thread_flags_set(furi_thread_get_id(uart->rx_thread), WorkerEvtStop); furi_thread_join(uart->rx_thread); furi_thread_free(uart->rx_thread); - furi_hal_uart_set_irq_cb(uart->channel, NULL, NULL); - if(uart->channel == FuriHalUartIdLPUART1) { - furi_hal_uart_deinit(uart->channel); - } - furi_hal_console_enable(); + furi_hal_serial_deinit(uart->serial_handle); + furi_hal_serial_control_release(uart->serial_handle); free(uart); } diff --git a/flipper_companion_apps/applications/external/malveke_gb_link_camera/uart.h b/flipper_companion_apps/applications/external/malveke_gb_link_camera/uart.h index 13202cec0cc..b7a6664b3b7 100644 --- a/flipper_companion_apps/applications/external/malveke_gb_link_camera/uart.h +++ b/flipper_companion_apps/applications/external/malveke_gb_link_camera/uart.h @@ -3,16 +3,17 @@ #pragma once -#include "furi_hal.h" - +#include +#include #define RX_BUF_SIZE (1024) typedef struct Uart Uart; -void uart_set_handle_rx_data_cb(Uart* uart, void (*handle_rx_data_cb)(uint8_t* buf, size_t len, void* context)); -void uart_tx(uint8_t* data, size_t len); -void lp_uart_tx(uint8_t* data, size_t len); +void uart_set_handle_rx_data_cb( + Uart* uart, + void (*handle_rx_data_cb)(uint8_t* buf, size_t len, void* context)); +void uart_tx(void* app, uint8_t* data, size_t len); Uart* usart_init(void* app); Uart* lp_uart_init(void* app); void uart_free(Uart* uart);