Skip to content

Commit

Permalink
Update GB Link Camera 2.0 New Serial API closes #18
Browse files Browse the repository at this point in the history
  • Loading branch information
EstebanFuentealba committed Mar 11, 2024
1 parent dc51657 commit e181560
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 125 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#include "link_camera.h"
#include <gui/elements.h>
#include <malveke_gb_link_camera_icons.h>
// #include <u8g2_glue.h>

QRCode qrcode;
uint8_t qrcodeData[((((4 * 8 + 17) * (4 * 8 + 17)) + 7) / 8)];
Expand All @@ -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");
Expand All @@ -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)
{
Expand Down Expand Up @@ -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);

}
}
Expand All @@ -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,
Expand Down Expand Up @@ -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;
}

Expand All @@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <gui/icon_i.h>
#include <storage/storage.h>
#include <gui/view_dispatcher.h>
#include <expansion/expansion.h>
#include "cJSON.h"
#include "qrcode.h"
#include "uart.h"
Expand Down
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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:"
Expand All @@ -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;
Expand All @@ -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);
Expand All @@ -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;
Expand All @@ -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);
}
}
}
Expand All @@ -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);
Expand All @@ -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;
}
Expand All @@ -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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,17 @@

#pragma once

#include "furi_hal.h"

#include <furi.h>
#include <furi_hal.h>

#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);
Expand Down

0 comments on commit e181560

Please sign in to comment.