2021-11-19 22:19:31 +00:00
|
|
|
#include <furi.h>
|
2024-01-19 20:20:37 +00:00
|
|
|
#include <furi_hal.h>
|
|
|
|
|
2021-11-19 22:19:31 +00:00
|
|
|
#include <gui/gui.h>
|
|
|
|
#include <gui/elements.h>
|
|
|
|
#include <gui/view_dispatcher.h>
|
|
|
|
#include <gui/modules/dialog_ex.h>
|
|
|
|
|
2024-01-19 20:20:37 +00:00
|
|
|
#include <notification/notification.h>
|
|
|
|
#include <notification/notification_messages.h>
|
|
|
|
|
2021-11-19 22:19:31 +00:00
|
|
|
#define LINES_ON_SCREEN 6
|
|
|
|
#define COLUMNS_ON_SCREEN 21
|
2023-06-29 11:23:04 +00:00
|
|
|
#define TAG "UartEcho"
|
|
|
|
#define DEFAULT_BAUD_RATE 230400
|
2021-11-19 22:19:31 +00:00
|
|
|
|
|
|
|
typedef struct UartDumpModel UartDumpModel;
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
Gui* gui;
|
|
|
|
NotificationApp* notification;
|
|
|
|
ViewDispatcher* view_dispatcher;
|
|
|
|
View* view;
|
|
|
|
FuriThread* worker_thread;
|
2022-10-07 12:27:11 +00:00
|
|
|
FuriStreamBuffer* rx_stream;
|
2024-01-19 20:20:37 +00:00
|
|
|
FuriHalSerialHandle* serial_handle;
|
2021-11-19 22:19:31 +00:00
|
|
|
} UartEchoApp;
|
|
|
|
|
|
|
|
typedef struct {
|
2022-10-05 15:15:23 +00:00
|
|
|
FuriString* text;
|
2021-11-19 22:19:31 +00:00
|
|
|
} ListElement;
|
|
|
|
|
|
|
|
struct UartDumpModel {
|
|
|
|
ListElement* list[LINES_ON_SCREEN];
|
|
|
|
uint8_t line;
|
|
|
|
|
|
|
|
char last_char;
|
|
|
|
bool escape;
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef enum {
|
|
|
|
WorkerEventReserved = (1 << 0), // Reserved for StreamBuffer internal event
|
|
|
|
WorkerEventStop = (1 << 1),
|
2024-01-19 20:20:37 +00:00
|
|
|
WorkerEventRxData = (1 << 2),
|
|
|
|
WorkerEventRxIdle = (1 << 3),
|
|
|
|
WorkerEventRxOverrunError = (1 << 4),
|
|
|
|
WorkerEventRxFramingError = (1 << 5),
|
|
|
|
WorkerEventRxNoiseError = (1 << 6),
|
2021-11-19 22:19:31 +00:00
|
|
|
} WorkerEventFlags;
|
|
|
|
|
2024-01-19 20:20:37 +00:00
|
|
|
#define WORKER_EVENTS_MASK \
|
|
|
|
(WorkerEventStop | WorkerEventRxData | WorkerEventRxIdle | WorkerEventRxOverrunError | \
|
|
|
|
WorkerEventRxFramingError | WorkerEventRxNoiseError)
|
2021-11-19 22:19:31 +00:00
|
|
|
|
|
|
|
const NotificationSequence sequence_notification = {
|
2022-04-29 14:21:12 +00:00
|
|
|
&message_display_backlight_on,
|
2021-11-19 22:19:31 +00:00
|
|
|
&message_green_255,
|
|
|
|
&message_delay_10,
|
|
|
|
NULL,
|
|
|
|
};
|
|
|
|
|
|
|
|
static void uart_echo_view_draw_callback(Canvas* canvas, void* _model) {
|
|
|
|
UartDumpModel* model = _model;
|
|
|
|
|
|
|
|
// Prepare canvas
|
|
|
|
canvas_clear(canvas);
|
|
|
|
canvas_set_color(canvas, ColorBlack);
|
|
|
|
canvas_set_font(canvas, FontKeyboard);
|
|
|
|
|
|
|
|
for(size_t i = 0; i < LINES_ON_SCREEN; i++) {
|
|
|
|
canvas_draw_str(
|
|
|
|
canvas,
|
|
|
|
0,
|
|
|
|
(i + 1) * (canvas_current_font_height(canvas) - 1),
|
2022-10-05 15:15:23 +00:00
|
|
|
furi_string_get_cstr(model->list[i]->text));
|
2021-11-19 22:19:31 +00:00
|
|
|
|
|
|
|
if(i == model->line) {
|
2022-10-05 15:15:23 +00:00
|
|
|
uint8_t width =
|
|
|
|
canvas_string_width(canvas, furi_string_get_cstr(model->list[i]->text));
|
2021-11-19 22:19:31 +00:00
|
|
|
|
|
|
|
canvas_draw_box(
|
|
|
|
canvas,
|
|
|
|
width,
|
|
|
|
(i) * (canvas_current_font_height(canvas) - 1) + 2,
|
|
|
|
2,
|
|
|
|
canvas_current_font_height(canvas) - 2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool uart_echo_view_input_callback(InputEvent* event, void* context) {
|
2022-05-06 13:37:10 +00:00
|
|
|
UNUSED(event);
|
|
|
|
UNUSED(context);
|
|
|
|
return false;
|
2021-11-19 22:19:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static uint32_t uart_echo_exit(void* context) {
|
2022-05-06 13:37:10 +00:00
|
|
|
UNUSED(context);
|
2021-11-19 22:19:31 +00:00
|
|
|
return VIEW_NONE;
|
|
|
|
}
|
|
|
|
|
2024-01-19 20:20:37 +00:00
|
|
|
static void
|
|
|
|
uart_echo_on_irq_cb(FuriHalSerialHandle* handle, FuriHalSerialRxEvent event, void* context) {
|
2021-11-19 22:19:31 +00:00
|
|
|
furi_assert(context);
|
2024-01-19 20:20:37 +00:00
|
|
|
UNUSED(handle);
|
2021-11-19 22:19:31 +00:00
|
|
|
UartEchoApp* app = context;
|
2024-01-19 20:20:37 +00:00
|
|
|
volatile FuriHalSerialRxEvent event_copy = event;
|
|
|
|
UNUSED(event_copy);
|
2021-11-19 22:19:31 +00:00
|
|
|
|
2024-01-19 20:20:37 +00:00
|
|
|
WorkerEventFlags flag = 0;
|
|
|
|
|
|
|
|
if(event & FuriHalSerialRxEventData) {
|
|
|
|
uint8_t data = furi_hal_serial_async_rx(handle);
|
2022-10-07 12:27:11 +00:00
|
|
|
furi_stream_buffer_send(app->rx_stream, &data, 1, 0);
|
2024-01-19 20:20:37 +00:00
|
|
|
flag |= WorkerEventRxData;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(event & FuriHalSerialRxEventIdle) {
|
|
|
|
//idle line detected, packet transmission may have ended
|
|
|
|
flag |= WorkerEventRxIdle;
|
|
|
|
}
|
|
|
|
|
|
|
|
//error detected
|
|
|
|
if(event & FuriHalSerialRxEventFrameError) {
|
|
|
|
flag |= WorkerEventRxFramingError;
|
|
|
|
}
|
|
|
|
if(event & FuriHalSerialRxEventNoiseError) {
|
|
|
|
flag |= WorkerEventRxNoiseError;
|
2021-11-19 22:19:31 +00:00
|
|
|
}
|
2024-01-19 20:20:37 +00:00
|
|
|
if(event & FuriHalSerialRxEventOverrunError) {
|
|
|
|
flag |= WorkerEventRxOverrunError;
|
|
|
|
}
|
|
|
|
|
|
|
|
furi_thread_flags_set(furi_thread_get_id(app->worker_thread), flag);
|
2021-11-19 22:19:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void uart_echo_push_to_list(UartDumpModel* model, const char data) {
|
|
|
|
if(model->escape) {
|
|
|
|
// escape code end with letter
|
|
|
|
if((data >= 'a' && data <= 'z') || (data >= 'A' && data <= 'Z')) {
|
|
|
|
model->escape = false;
|
|
|
|
}
|
|
|
|
} else if(data == '[' && model->last_char == '\e') {
|
|
|
|
// "Esc[" is a escape code
|
|
|
|
model->escape = true;
|
|
|
|
} else if((data >= ' ' && data <= '~') || (data == '\n' || data == '\r')) {
|
|
|
|
bool new_string_needed = false;
|
2022-10-05 15:15:23 +00:00
|
|
|
if(furi_string_size(model->list[model->line]->text) >= COLUMNS_ON_SCREEN) {
|
2021-11-19 22:19:31 +00:00
|
|
|
new_string_needed = true;
|
|
|
|
} else if((data == '\n' || data == '\r')) {
|
|
|
|
// pack line breaks
|
|
|
|
if(model->last_char != '\n' && model->last_char != '\r') {
|
|
|
|
new_string_needed = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(new_string_needed) {
|
|
|
|
if((model->line + 1) < LINES_ON_SCREEN) {
|
|
|
|
model->line += 1;
|
|
|
|
} else {
|
|
|
|
ListElement* first = model->list[0];
|
|
|
|
|
|
|
|
for(size_t i = 1; i < LINES_ON_SCREEN; i++) {
|
|
|
|
model->list[i - 1] = model->list[i];
|
|
|
|
}
|
|
|
|
|
2022-10-05 15:15:23 +00:00
|
|
|
furi_string_reset(first->text);
|
2021-11-19 22:19:31 +00:00
|
|
|
model->list[model->line] = first;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(data != '\n' && data != '\r') {
|
2022-10-05 15:15:23 +00:00
|
|
|
furi_string_push_back(model->list[model->line]->text, data);
|
2021-11-19 22:19:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
model->last_char = data;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int32_t uart_echo_worker(void* context) {
|
|
|
|
furi_assert(context);
|
|
|
|
UartEchoApp* app = context;
|
|
|
|
|
|
|
|
while(1) {
|
2022-06-20 14:54:48 +00:00
|
|
|
uint32_t events =
|
2022-07-20 10:56:33 +00:00
|
|
|
furi_thread_flags_wait(WORKER_EVENTS_MASK, FuriFlagWaitAny, FuriWaitForever);
|
|
|
|
furi_check((events & FuriFlagError) == 0);
|
2021-11-19 22:19:31 +00:00
|
|
|
|
|
|
|
if(events & WorkerEventStop) break;
|
2024-01-19 20:20:37 +00:00
|
|
|
if(events & WorkerEventRxData) {
|
2021-11-19 22:19:31 +00:00
|
|
|
size_t length = 0;
|
|
|
|
do {
|
|
|
|
uint8_t data[64];
|
2022-10-07 12:27:11 +00:00
|
|
|
length = furi_stream_buffer_receive(app->rx_stream, data, 64, 0);
|
2021-11-19 22:19:31 +00:00
|
|
|
if(length > 0) {
|
2024-01-19 20:20:37 +00:00
|
|
|
furi_hal_serial_tx(app->serial_handle, data, length);
|
2021-11-19 22:19:31 +00:00
|
|
|
with_view_model(
|
2022-10-08 17:38:29 +00:00
|
|
|
app->view,
|
|
|
|
UartDumpModel * model,
|
|
|
|
{
|
2021-11-19 22:19:31 +00:00
|
|
|
for(size_t i = 0; i < length; i++) {
|
|
|
|
uart_echo_push_to_list(model, data[i]);
|
|
|
|
}
|
2022-10-08 17:38:29 +00:00
|
|
|
},
|
|
|
|
false);
|
2021-11-19 22:19:31 +00:00
|
|
|
}
|
|
|
|
} while(length > 0);
|
|
|
|
|
|
|
|
notification_message(app->notification, &sequence_notification);
|
|
|
|
with_view_model(
|
2022-10-08 17:38:29 +00:00
|
|
|
app->view, UartDumpModel * model, { UNUSED(model); }, true);
|
2021-11-19 22:19:31 +00:00
|
|
|
}
|
2024-01-19 20:20:37 +00:00
|
|
|
|
|
|
|
if(events & WorkerEventRxIdle) {
|
|
|
|
furi_hal_serial_tx(app->serial_handle, (uint8_t*)"\r\nDetect IDLE\r\n", 15);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(events &
|
|
|
|
(WorkerEventRxOverrunError | WorkerEventRxFramingError | WorkerEventRxNoiseError)) {
|
|
|
|
if(events & WorkerEventRxOverrunError) {
|
|
|
|
furi_hal_serial_tx(app->serial_handle, (uint8_t*)"\r\nDetect ORE\r\n", 14);
|
|
|
|
}
|
|
|
|
if(events & WorkerEventRxFramingError) {
|
|
|
|
furi_hal_serial_tx(app->serial_handle, (uint8_t*)"\r\nDetect FE\r\n", 13);
|
|
|
|
}
|
|
|
|
if(events & WorkerEventRxNoiseError) {
|
|
|
|
furi_hal_serial_tx(app->serial_handle, (uint8_t*)"\r\nDetect NE\r\n", 13);
|
|
|
|
}
|
|
|
|
}
|
2021-11-19 22:19:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-06-29 11:23:04 +00:00
|
|
|
static UartEchoApp* uart_echo_app_alloc(uint32_t baudrate) {
|
2022-02-18 19:53:46 +00:00
|
|
|
UartEchoApp* app = malloc(sizeof(UartEchoApp));
|
2021-11-19 22:19:31 +00:00
|
|
|
|
2022-10-07 12:27:11 +00:00
|
|
|
app->rx_stream = furi_stream_buffer_alloc(2048, 1);
|
2021-11-19 22:19:31 +00:00
|
|
|
|
|
|
|
// Gui
|
2022-07-26 12:21:51 +00:00
|
|
|
app->gui = furi_record_open(RECORD_GUI);
|
|
|
|
app->notification = furi_record_open(RECORD_NOTIFICATION);
|
2021-11-19 22:19:31 +00:00
|
|
|
|
|
|
|
// View dispatcher
|
|
|
|
app->view_dispatcher = view_dispatcher_alloc();
|
|
|
|
view_dispatcher_enable_queue(app->view_dispatcher);
|
|
|
|
view_dispatcher_attach_to_gui(app->view_dispatcher, app->gui, ViewDispatcherTypeFullscreen);
|
|
|
|
|
|
|
|
// Views
|
|
|
|
app->view = view_alloc();
|
|
|
|
view_set_draw_callback(app->view, uart_echo_view_draw_callback);
|
|
|
|
view_set_input_callback(app->view, uart_echo_view_input_callback);
|
|
|
|
view_allocate_model(app->view, ViewModelTypeLocking, sizeof(UartDumpModel));
|
|
|
|
with_view_model(
|
2022-10-08 17:38:29 +00:00
|
|
|
app->view,
|
|
|
|
UartDumpModel * model,
|
|
|
|
{
|
2021-11-19 22:19:31 +00:00
|
|
|
for(size_t i = 0; i < LINES_ON_SCREEN; i++) {
|
|
|
|
model->line = 0;
|
|
|
|
model->escape = false;
|
2022-02-18 19:53:46 +00:00
|
|
|
model->list[i] = malloc(sizeof(ListElement));
|
2022-10-05 15:15:23 +00:00
|
|
|
model->list[i]->text = furi_string_alloc();
|
2021-11-19 22:19:31 +00:00
|
|
|
}
|
2022-10-08 17:38:29 +00:00
|
|
|
},
|
|
|
|
true);
|
2021-11-19 22:19:31 +00:00
|
|
|
|
|
|
|
view_set_previous_callback(app->view, uart_echo_exit);
|
|
|
|
view_dispatcher_add_view(app->view_dispatcher, 0, app->view);
|
|
|
|
view_dispatcher_switch_to_view(app->view_dispatcher, 0);
|
|
|
|
|
2022-12-07 11:46:50 +00:00
|
|
|
app->worker_thread = furi_thread_alloc_ex("UsbUartWorker", 1024, uart_echo_worker, app);
|
|
|
|
furi_thread_start(app->worker_thread);
|
|
|
|
|
2021-11-19 22:19:31 +00:00
|
|
|
// Enable uart listener
|
2024-01-19 20:20:37 +00:00
|
|
|
app->serial_handle = furi_hal_serial_control_acquire(FuriHalSerialIdUsart);
|
|
|
|
furi_check(app->serial_handle);
|
|
|
|
furi_hal_serial_init(app->serial_handle, baudrate);
|
|
|
|
|
|
|
|
furi_hal_serial_async_rx_start(app->serial_handle, uart_echo_on_irq_cb, app, true);
|
2021-11-19 22:19:31 +00:00
|
|
|
|
|
|
|
return app;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void uart_echo_app_free(UartEchoApp* app) {
|
|
|
|
furi_assert(app);
|
|
|
|
|
2022-06-20 14:54:48 +00:00
|
|
|
furi_thread_flags_set(furi_thread_get_id(app->worker_thread), WorkerEventStop);
|
2021-11-19 22:19:31 +00:00
|
|
|
furi_thread_join(app->worker_thread);
|
|
|
|
furi_thread_free(app->worker_thread);
|
|
|
|
|
2024-01-19 20:20:37 +00:00
|
|
|
furi_hal_serial_deinit(app->serial_handle);
|
|
|
|
furi_hal_serial_control_release(app->serial_handle);
|
|
|
|
|
2021-11-19 22:19:31 +00:00
|
|
|
// Free views
|
|
|
|
view_dispatcher_remove_view(app->view_dispatcher, 0);
|
|
|
|
|
|
|
|
with_view_model(
|
2022-10-08 17:38:29 +00:00
|
|
|
app->view,
|
|
|
|
UartDumpModel * model,
|
|
|
|
{
|
2021-11-19 22:19:31 +00:00
|
|
|
for(size_t i = 0; i < LINES_ON_SCREEN; i++) {
|
2022-10-05 15:15:23 +00:00
|
|
|
furi_string_free(model->list[i]->text);
|
2021-11-19 22:19:31 +00:00
|
|
|
free(model->list[i]);
|
|
|
|
}
|
2022-10-08 17:38:29 +00:00
|
|
|
},
|
|
|
|
true);
|
2021-11-19 22:19:31 +00:00
|
|
|
view_free(app->view);
|
|
|
|
view_dispatcher_free(app->view_dispatcher);
|
|
|
|
|
|
|
|
// Close gui record
|
2022-07-26 12:21:51 +00:00
|
|
|
furi_record_close(RECORD_GUI);
|
|
|
|
furi_record_close(RECORD_NOTIFICATION);
|
2021-11-19 22:19:31 +00:00
|
|
|
app->gui = NULL;
|
|
|
|
|
2022-10-07 12:27:11 +00:00
|
|
|
furi_stream_buffer_free(app->rx_stream);
|
2021-11-19 22:19:31 +00:00
|
|
|
|
|
|
|
// Free rest
|
|
|
|
free(app);
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t uart_echo_app(void* p) {
|
2023-06-29 11:23:04 +00:00
|
|
|
uint32_t baudrate = DEFAULT_BAUD_RATE;
|
|
|
|
if(p) {
|
|
|
|
const char* baudrate_str = p;
|
|
|
|
if(sscanf(baudrate_str, "%lu", &baudrate) != 1) {
|
|
|
|
FURI_LOG_E(TAG, "Invalid baudrate: %s", baudrate_str);
|
|
|
|
baudrate = DEFAULT_BAUD_RATE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
FURI_LOG_I(TAG, "Using baudrate: %lu", baudrate);
|
|
|
|
|
|
|
|
UartEchoApp* app = uart_echo_app_alloc(baudrate);
|
2021-11-19 22:19:31 +00:00
|
|
|
view_dispatcher_run(app->view_dispatcher);
|
|
|
|
uart_echo_app_free(app);
|
|
|
|
return 0;
|
2022-02-18 19:53:46 +00:00
|
|
|
}
|