Skip to content

Commit

Permalink
merge fixes pt1
Browse files Browse the repository at this point in the history
by Willy-JL
  • Loading branch information
xMasterX committed Jan 19, 2024
1 parent 25919b0 commit 822dfe6
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 83 deletions.
50 changes: 25 additions & 25 deletions base_pack/camera_suite/views/camera_suite_view_camera.c
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ static bool camera_suite_view_camera_input(InputEvent* event, void* context) {
UNUSED(model);

// Stop camera stream.
furi_hal_uart_tx(FuriHalUartIdUSART1, (uint8_t[]){'s'}, 1);
furi_hal_serial_tx(instance->serial_handle, (uint8_t[]){'s'}, 1);
furi_delay_ms(50);

// Go back to the main menu.
Expand All @@ -217,13 +217,13 @@ static bool camera_suite_view_camera_input(InputEvent* event, void* context) {

if(model->is_inverted) {
// Camera: Set invert to false on the ESP32-CAM.
furi_hal_uart_tx(FuriHalUartIdUSART1, (uint8_t[]){'i'}, 1);
furi_hal_serial_tx(instance->serial_handle, (uint8_t[]){'i'}, 1);
furi_delay_ms(50);

model->is_inverted = false;
} else {
// Camera: Set invert to true on the ESP32-CAM.
furi_hal_uart_tx(FuriHalUartIdUSART1, (uint8_t[]){'I'}, 1);
furi_hal_serial_tx(instance->serial_handle, (uint8_t[]){'I'}, 1);
furi_delay_ms(50);

model->is_inverted = true;
Expand All @@ -246,13 +246,13 @@ static bool camera_suite_view_camera_input(InputEvent* event, void* context) {

if(model->is_dithering_enabled) {
// Camera: Disable dithering.
furi_hal_uart_tx(FuriHalUartIdUSART1, (uint8_t[]){'d'}, 1);
furi_hal_serial_tx(instance->serial_handle, (uint8_t[]){'d'}, 1);
furi_delay_ms(50);

model->is_dithering_enabled = false;
} else {
// Camera: Enable dithering.
furi_hal_uart_tx(FuriHalUartIdUSART1, (uint8_t[]){'D'}, 1);
furi_hal_serial_tx(instance->serial_handle, (uint8_t[]){'D'}, 1);
furi_delay_ms(50);

model->is_dithering_enabled = true;
Expand All @@ -276,7 +276,7 @@ static bool camera_suite_view_camera_input(InputEvent* event, void* context) {
camera_suite_led_set_rgb(instance->context, 0, 0, 255);

// Camera: Increase contrast.
furi_hal_uart_tx(FuriHalUartIdUSART1, (uint8_t[]){'C'}, 1);
furi_hal_serial_tx(instance->serial_handle, (uint8_t[]){'C'}, 1);
furi_delay_ms(50);

instance->callback(CameraSuiteCustomEventSceneCameraUp, instance->context);
Expand All @@ -297,7 +297,7 @@ static bool camera_suite_view_camera_input(InputEvent* event, void* context) {
camera_suite_led_set_rgb(instance->context, 0, 0, 255);

// Camera: Reduce contrast.
furi_hal_uart_tx(FuriHalUartIdUSART1, (uint8_t[]){'c'}, 1);
furi_hal_serial_tx(instance->serial_handle, (uint8_t[]){'c'}, 1);
furi_delay_ms(50);

instance->callback(CameraSuiteCustomEventSceneCameraDown, instance->context);
Expand All @@ -316,7 +316,7 @@ static bool camera_suite_view_camera_input(InputEvent* event, void* context) {
camera_suite_led_set_rgb(instance->context, 0, 0, 255);

// @todo - Save picture directly to ESP32-CAM.
// furi_hal_uart_tx(FuriHalUartIdUSART1, (uint8_t[]){'P'}, 1);
// furi_hal_serial_tx(instance->serial_handle, (uint8_t[]){'P'}, 1);

// Save currently displayed image to the Flipper Zero SD card.
save_image_to_flipper_sd_card(model);
Expand Down Expand Up @@ -349,22 +349,22 @@ static void camera_suite_view_camera_enter(void* context) {
CameraSuite* instance_context = instance->context;

// Start camera stream.
furi_hal_uart_tx(FuriHalUartIdUSART1, (uint8_t[]){'S'}, 1);
furi_hal_serial_tx(instance->serial_handle, (uint8_t[]){'S'}, 1);
furi_delay_ms(50);

// Get/set dither type.
uint8_t dither_type = instance_context->dither;
furi_hal_uart_tx(FuriHalUartIdUSART1, &dither_type, 1);
furi_hal_serial_tx(instance->serial_handle, &dither_type, 1);
furi_delay_ms(50);

// Make sure the camera is not inverted.
furi_hal_uart_tx(FuriHalUartIdUSART1, (uint8_t[]){'i'}, 1);
furi_hal_serial_tx(instance->serial_handle, (uint8_t[]){'i'}, 1);
furi_delay_ms(50);

// Toggle flash on or off based on the current state. If the user has this
// on the flash will stay on the entire time the user is in the camera view.
uint8_t flash_state = instance_context->flash ? 'F' : 'f';
furi_hal_uart_tx(FuriHalUartIdUSART1, &flash_state, 1);
furi_hal_serial_tx(instance->serial_handle, &flash_state, 1);
furi_delay_ms(50);

with_view_model(
Expand All @@ -374,17 +374,18 @@ static void camera_suite_view_camera_enter(void* context) {
true);
}

static void camera_on_irq_cb(UartIrqEvent uartIrqEvent, uint8_t data, void* context) {
furi_assert(uartIrqEvent);
furi_assert(data);
static void
camera_on_irq_cb(FuriHalSerialHandle* handle, FuriHalSerialRxEvent event, void* context) {
furi_assert(handle);
furi_assert(context);

// Cast `context` to `CameraSuiteViewCamera*` and store it in `instance`.
CameraSuiteViewCamera* instance = context;

// If `uartIrqEvent` is `UartIrqEventRXNE`, send the data to the
// `rx_stream` and set the `WorkerEventRx` flag.
if(uartIrqEvent == UartIrqEventRXNE) {
if(event == FuriHalSerialRxEventData) {
uint8_t data = furi_hal_serial_async_rx(handle);
furi_stream_buffer_send(instance->rx_stream, &data, 1, 0);
furi_thread_flags_set(furi_thread_get_id(instance->worker_thread), WorkerEventRx);
}
Expand Down Expand Up @@ -512,32 +513,31 @@ CameraSuiteViewCamera* camera_suite_view_camera_alloc() {
instance->worker_thread = thread;
furi_thread_start(instance->worker_thread);

// Enable uart listener
furi_hal_console_disable();

// 115200 is the default baud rate for the ESP32-CAM.
furi_hal_uart_set_br(FuriHalUartIdUSART1, 230400);
instance->serial_handle = furi_hal_serial_control_acquire(UART_CH);
furi_check(instance->serial_handle);
furi_hal_serial_init(instance->serial_handle, 230400);

// Enable UART1 and set the IRQ callback.
furi_hal_uart_set_irq_cb(FuriHalUartIdUSART1, camera_on_irq_cb, instance);
furi_hal_serial_async_rx_start(instance->serial_handle, camera_on_irq_cb, instance, false);

return instance;
}

void camera_suite_view_camera_free(CameraSuiteViewCamera* instance) {
furi_assert(instance);

// Remove the IRQ callback.
furi_hal_uart_set_irq_cb(FuriHalUartIdUSART1, NULL, NULL);

// Free the worker thread.
furi_thread_flags_set(furi_thread_get_id(instance->worker_thread), WorkerEventStop);
furi_thread_join(instance->worker_thread);
furi_thread_free(instance->worker_thread);

// Free the allocated stream buffer.
furi_stream_buffer_free(instance->rx_stream);

// Re-enable the console.
// furi_hal_console_enable();
furi_hal_serial_deinit(instance->serial_handle);
furi_hal_serial_control_release(instance->serial_handle);

with_view_model(
instance->view, UartDumpModel * model, { UNUSED(model); }, true);
Expand Down
7 changes: 5 additions & 2 deletions base_pack/camera_suite/views/camera_suite_view_camera.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

#include <furi.h>
#include <furi_hal.h>
#include <furi_hal_console.h>
#include <furi_hal_uart.h>
#include <furi_hal_serial_control.h>
#include <furi_hal_serial.h>
#include <gui/elements.h>
#include <gui/gui.h>
#include <gui/icon_i.h>
Expand All @@ -19,6 +19,8 @@

#include <camera_suite_icons.h>

#define UART_CH (FuriHalSerialIdUsart)

#define BITMAP_HEADER_LENGTH 62
#define FRAME_BIT_DEPTH 1
#define FRAME_BUFFER_LENGTH 1024
Expand Down Expand Up @@ -49,6 +51,7 @@ typedef void (*CameraSuiteViewCameraCallback)(CameraSuiteCustomEvent event, void
typedef struct CameraSuiteViewCamera {
CameraSuiteViewCameraCallback callback;
FuriStreamBuffer* rx_stream;
FuriHalSerialHandle* serial_handle;
FuriThread* worker_thread;
NotificationApp* notification;
View* view;
Expand Down
60 changes: 30 additions & 30 deletions base_pack/dap_link/dap_link.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
#include <furi.h>
#include <furi_hal_version.h>
#include <furi_hal_gpio.h>
#include <furi_hal_uart.h>
#include <furi_hal_console.h>
#include <furi_hal_serial_control.h>
#include <furi_hal_serial.h>
#include <furi_hal_resources.h>
#include <furi_hal_power.h>
#include <stm32wbxx_ll_usart.h>
Expand Down Expand Up @@ -275,14 +275,15 @@ typedef enum {
typedef struct {
FuriStreamBuffer* rx_stream;
FuriThreadId thread_id;
FuriHalUartId uart_id;
FuriHalSerialHandle* serial_handle;
struct usb_cdc_line_coding line_coding;
} CDCProcess;

static void cdc_uart_irq_cb(UartIrqEvent ev, uint8_t data, void* ctx) {
static void cdc_uart_irq_cb(FuriHalSerialHandle* handle, FuriHalSerialRxEvent event, void* ctx) {
CDCProcess* app = ctx;

if(ev == UartIrqEventRXNE) {
if(event == FuriHalSerialRxEventData) {
uint8_t data = furi_hal_serial_async_rx(handle);
furi_stream_buffer_send(app->rx_stream, &data, 1, 0);
furi_thread_flags_set(app->thread_id, CdcThreadEventUartRx);
}
Expand Down Expand Up @@ -316,54 +317,52 @@ static void cdc_usb_config_callback(struct usb_cdc_line_coding* config, void* co
furi_thread_flags_set(app->thread_id, CdcThreadEventCdcConfig);
}

static FuriHalUartId cdc_init_uart(
static void cdc_init_uart(
CDCProcess* app,
DapUartType type,
DapUartTXRX swap,
uint32_t baudrate,
void (*cb)(UartIrqEvent ev, uint8_t data, void* ctx),
void (*cb)(FuriHalSerialHandle* handle, FuriHalSerialRxEvent event, void* ctx),
void* ctx) {
FuriHalUartId uart_id = FuriHalUartIdUSART1;
if(baudrate == 0) baudrate = 115200;

switch(type) {
case DapUartTypeUSART1:
uart_id = FuriHalUartIdUSART1;
furi_hal_console_disable();
furi_hal_uart_deinit(uart_id);
app->serial_handle = furi_hal_serial_control_acquire(FuriHalSerialIdUsart);
furi_check(app->serial_handle);
if(swap == DapUartTXRXSwap) {
LL_USART_SetTXRXSwap(USART1, LL_USART_TXRX_SWAPPED);
} else {
LL_USART_SetTXRXSwap(USART1, LL_USART_TXRX_STANDARD);
}
furi_hal_uart_init(uart_id, baudrate);
furi_hal_uart_set_irq_cb(uart_id, cb, ctx);
furi_hal_serial_init(app->serial_handle, baudrate);
furi_hal_serial_async_rx_start(app->serial_handle, cb, ctx, false);
break;
case DapUartTypeLPUART1:
uart_id = FuriHalUartIdLPUART1;
furi_hal_uart_deinit(uart_id);
app->serial_handle = furi_hal_serial_control_acquire(FuriHalSerialIdLpuart);
furi_check(app->serial_handle);
if(swap == DapUartTXRXSwap) {
LL_LPUART_SetTXRXSwap(LPUART1, LL_LPUART_TXRX_SWAPPED);
} else {
LL_LPUART_SetTXRXSwap(LPUART1, LL_LPUART_TXRX_STANDARD);
}
furi_hal_uart_init(uart_id, baudrate);
furi_hal_uart_set_irq_cb(uart_id, cb, ctx);
furi_hal_serial_init(app->serial_handle, baudrate);
furi_hal_serial_async_rx_start(app->serial_handle, cb, ctx, false);
break;
}

return uart_id;
}

static void cdc_deinit_uart(DapUartType type) {
static void cdc_deinit_uart(CDCProcess* app, DapUartType type) {
switch(type) {
case DapUartTypeUSART1:
furi_hal_uart_deinit(FuriHalUartIdUSART1);
furi_hal_serial_deinit(app->serial_handle);
LL_USART_SetTXRXSwap(USART1, LL_USART_TXRX_STANDARD);
furi_hal_console_init();
furi_hal_serial_control_release(app->serial_handle);
break;
case DapUartTypeLPUART1:
furi_hal_uart_deinit(FuriHalUartIdLPUART1);
furi_hal_serial_deinit(app->serial_handle);
LL_LPUART_SetTXRXSwap(LPUART1, LL_LPUART_TXRX_STANDARD);
furi_hal_serial_control_release(app->serial_handle);
break;
}
}
Expand All @@ -385,8 +384,8 @@ static int32_t dap_cdc_process(void* p) {
const uint8_t rx_buffer_size = 64;
uint8_t* rx_buffer = malloc(rx_buffer_size);

app->uart_id = cdc_init_uart(
uart_pins_prev, uart_swap_prev, dap_state->cdc_baudrate, cdc_uart_irq_cb, app);
cdc_init_uart(
app, uart_pins_prev, uart_swap_prev, dap_state->cdc_baudrate, cdc_uart_irq_cb, app);

dap_cdc_usb_set_context(app);
dap_cdc_usb_set_rx_callback(cdc_usb_rx_callback);
Expand All @@ -405,7 +404,7 @@ static int32_t dap_cdc_process(void* p) {
if(dap_state->cdc_baudrate != app->line_coding.dwDTERate) {
dap_state->cdc_baudrate = app->line_coding.dwDTERate;
if(dap_state->cdc_baudrate > 0) {
furi_hal_uart_set_br(app->uart_id, dap_state->cdc_baudrate);
furi_hal_serial_set_br(app->serial_handle, dap_state->cdc_baudrate);
}
}
}
Expand All @@ -424,18 +423,19 @@ static int32_t dap_cdc_process(void* p) {
if(events & CdcThreadEventCdcRx) {
size_t len = dap_cdc_usb_rx(rx_buffer, rx_buffer_size);
if(len > 0) {
furi_hal_uart_tx(app->uart_id, rx_buffer, len);
furi_hal_serial_tx(app->serial_handle, rx_buffer, len);
}
dap_state->cdc_tx_counter += len;
}

if(events & CdcThreadEventApplyConfig) {
if(uart_pins_prev != dap_app->config.uart_pins ||
uart_swap_prev != dap_app->config.uart_swap) {
cdc_deinit_uart(uart_pins_prev);
cdc_deinit_uart(app, uart_pins_prev);
uart_pins_prev = dap_app->config.uart_pins;
uart_swap_prev = dap_app->config.uart_swap;
app->uart_id = cdc_init_uart(
cdc_init_uart(
app,
uart_pins_prev,
uart_swap_prev,
dap_state->cdc_baudrate,
Expand All @@ -456,7 +456,7 @@ static int32_t dap_cdc_process(void* p) {
}
}

cdc_deinit_uart(uart_pins_prev);
cdc_deinit_uart(app, uart_pins_prev);
free(rx_buffer);
furi_stream_buffer_free(app->rx_stream);
free(app);
Expand Down
Loading

0 comments on commit 822dfe6

Please sign in to comment.