mirror of
https://github.com/Next-Flip/Momentum-Firmware.git
synced 2026-04-26 03:39:58 -07:00
Add UART Terminal app
by cool4uma https://github.com/cool4uma/UART_Terminal/tree/main
This commit is contained in:
@@ -0,0 +1,30 @@
|
||||
#include "uart_terminal_scene.h"
|
||||
|
||||
// Generate scene on_enter handlers array
|
||||
#define ADD_SCENE(prefix, name, id) prefix##_scene_##name##_on_enter,
|
||||
void (*const uart_terminal_scene_on_enter_handlers[])(void*) = {
|
||||
#include "uart_terminal_scene_config.h"
|
||||
};
|
||||
#undef ADD_SCENE
|
||||
|
||||
// Generate scene on_event handlers array
|
||||
#define ADD_SCENE(prefix, name, id) prefix##_scene_##name##_on_event,
|
||||
bool (*const uart_terminal_scene_on_event_handlers[])(void* context, SceneManagerEvent event) = {
|
||||
#include "uart_terminal_scene_config.h"
|
||||
};
|
||||
#undef ADD_SCENE
|
||||
|
||||
// Generate scene on_exit handlers array
|
||||
#define ADD_SCENE(prefix, name, id) prefix##_scene_##name##_on_exit,
|
||||
void (*const uart_terminal_scene_on_exit_handlers[])(void* context) = {
|
||||
#include "uart_terminal_scene_config.h"
|
||||
};
|
||||
#undef ADD_SCENE
|
||||
|
||||
// Initialize scene handlers configuration structure
|
||||
const SceneManagerHandlers uart_terminal_scene_handlers = {
|
||||
.on_enter_handlers = uart_terminal_scene_on_enter_handlers,
|
||||
.on_event_handlers = uart_terminal_scene_on_event_handlers,
|
||||
.on_exit_handlers = uart_terminal_scene_on_exit_handlers,
|
||||
.scene_num = UART_TerminalSceneNum,
|
||||
};
|
||||
@@ -0,0 +1,29 @@
|
||||
#pragma once
|
||||
|
||||
#include <gui/scene_manager.h>
|
||||
|
||||
// Generate scene id and total number
|
||||
#define ADD_SCENE(prefix, name, id) UART_TerminalScene##id,
|
||||
typedef enum {
|
||||
#include "uart_terminal_scene_config.h"
|
||||
UART_TerminalSceneNum,
|
||||
} UART_TerminalScene;
|
||||
#undef ADD_SCENE
|
||||
|
||||
extern const SceneManagerHandlers uart_terminal_scene_handlers;
|
||||
|
||||
// Generate scene on_enter handlers declaration
|
||||
#define ADD_SCENE(prefix, name, id) void prefix##_scene_##name##_on_enter(void*);
|
||||
#include "uart_terminal_scene_config.h"
|
||||
#undef ADD_SCENE
|
||||
|
||||
// Generate scene on_event handlers declaration
|
||||
#define ADD_SCENE(prefix, name, id) \
|
||||
bool prefix##_scene_##name##_on_event(void* context, SceneManagerEvent event);
|
||||
#include "uart_terminal_scene_config.h"
|
||||
#undef ADD_SCENE
|
||||
|
||||
// Generate scene on_exit handlers declaration
|
||||
#define ADD_SCENE(prefix, name, id) void prefix##_scene_##name##_on_exit(void* context);
|
||||
#include "uart_terminal_scene_config.h"
|
||||
#undef ADD_SCENE
|
||||
@@ -0,0 +1,3 @@
|
||||
ADD_SCENE(uart_terminal, start, Start)
|
||||
ADD_SCENE(uart_terminal, console_output, ConsoleOutput)
|
||||
ADD_SCENE(uart_terminal, text_input, UART_TextInput)
|
||||
@@ -0,0 +1,147 @@
|
||||
#include "../uart_terminal_app_i.h"
|
||||
|
||||
void uart_terminal_console_output_handle_rx_data_cb(uint8_t* buf, size_t len, void* context) {
|
||||
furi_assert(context);
|
||||
UART_TerminalApp* app = context;
|
||||
|
||||
// If text box store gets too big, then truncate it
|
||||
app->text_box_store_strlen += len;
|
||||
if(app->text_box_store_strlen >= UART_TERMINAL_TEXT_BOX_STORE_SIZE - 1) {
|
||||
furi_string_right(app->text_box_store, app->text_box_store_strlen / 2);
|
||||
app->text_box_store_strlen = furi_string_size(app->text_box_store) + len;
|
||||
}
|
||||
|
||||
// Null-terminate buf and append to text box store
|
||||
buf[len] = '\0';
|
||||
furi_string_cat_printf(app->text_box_store, "%s", buf);
|
||||
|
||||
view_dispatcher_send_custom_event(
|
||||
app->view_dispatcher, UART_TerminalEventRefreshConsoleOutput);
|
||||
}
|
||||
|
||||
void uart_terminal_scene_console_output_on_enter(void* context) {
|
||||
UART_TerminalApp* app = context;
|
||||
|
||||
TextBox* text_box = app->text_box;
|
||||
text_box_reset(app->text_box);
|
||||
text_box_set_font(text_box, TextBoxFontText);
|
||||
if(app->focus_console_start) {
|
||||
text_box_set_focus(text_box, TextBoxFocusStart);
|
||||
} else {
|
||||
text_box_set_focus(text_box, TextBoxFocusEnd);
|
||||
}
|
||||
|
||||
//Change baudrate ///////////////////////////////////////////////////////////////////////////
|
||||
if(0 == strncmp("2400", app->selected_tx_string, strlen("2400")) && app->BAUDRATE != 2400) {
|
||||
uart_terminal_uart_free(app->uart);
|
||||
app->BAUDRATE = 2400;
|
||||
app->uart = uart_terminal_uart_init(app);
|
||||
}
|
||||
if(0 == strncmp("9600", app->selected_tx_string, strlen("9600")) && app->BAUDRATE != 9600) {
|
||||
uart_terminal_uart_free(app->uart);
|
||||
app->BAUDRATE = 9600;
|
||||
app->uart = uart_terminal_uart_init(app);
|
||||
}
|
||||
if(0 == strncmp("19200", app->selected_tx_string, strlen("19200")) && app->BAUDRATE != 19200) {
|
||||
uart_terminal_uart_free(app->uart);
|
||||
app->BAUDRATE = 19200;
|
||||
app->uart = uart_terminal_uart_init(app);
|
||||
}
|
||||
if(0 == strncmp("38400", app->selected_tx_string, strlen("38400")) && app->BAUDRATE != 38400) {
|
||||
uart_terminal_uart_free(app->uart);
|
||||
app->BAUDRATE = 38400;
|
||||
app->uart = uart_terminal_uart_init(app);
|
||||
}
|
||||
if(0 == strncmp("57600", app->selected_tx_string, strlen("57600")) && app->BAUDRATE != 57600) {
|
||||
uart_terminal_uart_free(app->uart);
|
||||
app->BAUDRATE = 57600;
|
||||
app->uart = uart_terminal_uart_init(app);
|
||||
}
|
||||
if(0 == strncmp("115200", app->selected_tx_string, strlen("115200")) &&
|
||||
app->BAUDRATE != 115200) {
|
||||
uart_terminal_uart_free(app->uart);
|
||||
app->BAUDRATE = 115200;
|
||||
app->uart = uart_terminal_uart_init(app);
|
||||
}
|
||||
if(0 == strncmp("230400", app->selected_tx_string, strlen("230400")) &&
|
||||
app->BAUDRATE != 230400) {
|
||||
uart_terminal_uart_free(app->uart);
|
||||
app->BAUDRATE = 230400;
|
||||
app->uart = uart_terminal_uart_init(app);
|
||||
}
|
||||
if(0 == strncmp("460800", app->selected_tx_string, strlen("460800")) &&
|
||||
app->BAUDRATE != 460800) {
|
||||
uart_terminal_uart_free(app->uart);
|
||||
app->BAUDRATE = 460800;
|
||||
app->uart = uart_terminal_uart_init(app);
|
||||
}
|
||||
if(0 == strncmp("921600", app->selected_tx_string, strlen("921600")) &&
|
||||
app->BAUDRATE != 921600) {
|
||||
uart_terminal_uart_free(app->uart);
|
||||
app->BAUDRATE = 921600;
|
||||
app->uart = uart_terminal_uart_init(app);
|
||||
}
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
if(app->is_command) {
|
||||
furi_string_reset(app->text_box_store);
|
||||
app->text_box_store_strlen = 0;
|
||||
|
||||
if(0 == strncmp("help", app->selected_tx_string, strlen("help"))) {
|
||||
const char* help_msg =
|
||||
"UART terminal for Flipper\n\nI'm in github: cool4uma\n\nThis app is a modified\nWiFi Marauder companion,\nThanks 0xchocolate(github)\nfor great code and app.\n\n";
|
||||
furi_string_cat_str(app->text_box_store, help_msg);
|
||||
app->text_box_store_strlen += strlen(help_msg);
|
||||
}
|
||||
|
||||
if(app->show_stopscan_tip) {
|
||||
const char* help_msg = "Press BACK to return\n";
|
||||
furi_string_cat_str(app->text_box_store, help_msg);
|
||||
app->text_box_store_strlen += strlen(help_msg);
|
||||
}
|
||||
}
|
||||
|
||||
// Set starting text - for "View Log", this will just be what was already in the text box store
|
||||
text_box_set_text(app->text_box, furi_string_get_cstr(app->text_box_store));
|
||||
|
||||
scene_manager_set_scene_state(app->scene_manager, UART_TerminalSceneConsoleOutput, 0);
|
||||
view_dispatcher_switch_to_view(app->view_dispatcher, UART_TerminalAppViewConsoleOutput);
|
||||
|
||||
// Register callback to receive data
|
||||
uart_terminal_uart_set_handle_rx_data_cb(
|
||||
app->uart, uart_terminal_console_output_handle_rx_data_cb); // setup callback for rx thread
|
||||
|
||||
// Send command with newline '\n'
|
||||
if(app->is_command && app->selected_tx_string) {
|
||||
uart_terminal_uart_tx(
|
||||
(uint8_t*)(app->selected_tx_string), strlen(app->selected_tx_string));
|
||||
uart_terminal_uart_tx((uint8_t*)("\n"), 1);
|
||||
}
|
||||
}
|
||||
|
||||
bool uart_terminal_scene_console_output_on_event(void* context, SceneManagerEvent event) {
|
||||
UART_TerminalApp* app = context;
|
||||
|
||||
bool consumed = false;
|
||||
|
||||
if(event.type == SceneManagerEventTypeCustom) {
|
||||
text_box_set_text(app->text_box, furi_string_get_cstr(app->text_box_store));
|
||||
consumed = true;
|
||||
} else if(event.type == SceneManagerEventTypeTick) {
|
||||
consumed = true;
|
||||
}
|
||||
|
||||
return consumed;
|
||||
}
|
||||
|
||||
void uart_terminal_scene_console_output_on_exit(void* context) {
|
||||
UART_TerminalApp* app = context;
|
||||
|
||||
// Unregister rx callback
|
||||
uart_terminal_uart_set_handle_rx_data_cb(app->uart, NULL);
|
||||
|
||||
// Automatically logut when exiting view
|
||||
//if(app->is_command) {
|
||||
// uart_terminal_uart_tx((uint8_t*)("exit\n"), strlen("exit\n"));
|
||||
//}
|
||||
}
|
||||
@@ -0,0 +1,137 @@
|
||||
#include "../uart_terminal_app_i.h"
|
||||
|
||||
// For each command, define whether additional arguments are needed
|
||||
// (enabling text input to fill them out), and whether the console
|
||||
// text box should focus at the start of the output or the end
|
||||
typedef enum { NO_ARGS = 0, INPUT_ARGS, TOGGLE_ARGS } InputArgs;
|
||||
|
||||
typedef enum { FOCUS_CONSOLE_END = 0, FOCUS_CONSOLE_START, FOCUS_CONSOLE_TOGGLE } FocusConsole;
|
||||
|
||||
#define SHOW_STOPSCAN_TIP (true)
|
||||
#define NO_TIP (false)
|
||||
|
||||
#define MAX_OPTIONS (9)
|
||||
typedef struct {
|
||||
const char* item_string;
|
||||
const char* options_menu[MAX_OPTIONS];
|
||||
int num_options_menu;
|
||||
const char* actual_commands[MAX_OPTIONS];
|
||||
InputArgs needs_keyboard;
|
||||
FocusConsole focus_console;
|
||||
bool show_stopscan_tip;
|
||||
} UART_TerminalItem;
|
||||
|
||||
// NUM_MENU_ITEMS defined in uart_terminal_app_i.h - if you add an entry here, increment it!
|
||||
const UART_TerminalItem items[NUM_MENU_ITEMS] = {
|
||||
{"Console",
|
||||
{"115200", "2400", "9600", "19200", "38400", "57600", "230400", "460800", "921600"},
|
||||
9,
|
||||
{"115200", "2400", "9600", "19200", "38400", "57600", "230400", "460800", "921600"},
|
||||
NO_ARGS,
|
||||
FOCUS_CONSOLE_TOGGLE,
|
||||
NO_TIP},
|
||||
{"Send command", {""}, 1, {""}, INPUT_ARGS, FOCUS_CONSOLE_END, NO_TIP},
|
||||
{"Fast cmd",
|
||||
{"help", "uptime", "date", "df -h", "ps", "dmesg", "reboot", "poweroff"},
|
||||
8,
|
||||
{"help", "uptime", "date", "df -h", "ps", "dmesg", "reboot", "poweroff"},
|
||||
INPUT_ARGS,
|
||||
FOCUS_CONSOLE_END,
|
||||
NO_TIP},
|
||||
{"Help", {""}, 1, {"help"}, NO_ARGS, FOCUS_CONSOLE_START, SHOW_STOPSCAN_TIP},
|
||||
};
|
||||
|
||||
static void uart_terminal_scene_start_var_list_enter_callback(void* context, uint32_t index) {
|
||||
furi_assert(context);
|
||||
UART_TerminalApp* app = context;
|
||||
|
||||
furi_assert(index < NUM_MENU_ITEMS);
|
||||
const UART_TerminalItem* item = &items[index];
|
||||
|
||||
const int selected_option_index = app->selected_option_index[index];
|
||||
furi_assert(selected_option_index < item->num_options_menu);
|
||||
app->selected_tx_string = item->actual_commands[selected_option_index];
|
||||
app->is_command = (1 <= index);
|
||||
app->is_custom_tx_string = false;
|
||||
app->selected_menu_index = index;
|
||||
app->focus_console_start = (item->focus_console == FOCUS_CONSOLE_TOGGLE) ?
|
||||
(selected_option_index == 0) :
|
||||
item->focus_console;
|
||||
app->show_stopscan_tip = item->show_stopscan_tip;
|
||||
|
||||
bool needs_keyboard = (item->needs_keyboard == TOGGLE_ARGS) ? (selected_option_index != 0) :
|
||||
item->needs_keyboard;
|
||||
if(needs_keyboard) {
|
||||
view_dispatcher_send_custom_event(app->view_dispatcher, UART_TerminalEventStartKeyboard);
|
||||
} else {
|
||||
view_dispatcher_send_custom_event(app->view_dispatcher, UART_TerminalEventStartConsole);
|
||||
}
|
||||
}
|
||||
|
||||
static void uart_terminal_scene_start_var_list_change_callback(VariableItem* item) {
|
||||
furi_assert(item);
|
||||
|
||||
UART_TerminalApp* app = variable_item_get_context(item);
|
||||
furi_assert(app);
|
||||
|
||||
const UART_TerminalItem* menu_item = &items[app->selected_menu_index];
|
||||
uint8_t item_index = variable_item_get_current_value_index(item);
|
||||
furi_assert(item_index < menu_item->num_options_menu);
|
||||
variable_item_set_current_value_text(item, menu_item->options_menu[item_index]);
|
||||
app->selected_option_index[app->selected_menu_index] = item_index;
|
||||
}
|
||||
|
||||
void uart_terminal_scene_start_on_enter(void* context) {
|
||||
UART_TerminalApp* app = context;
|
||||
VariableItemList* var_item_list = app->var_item_list;
|
||||
|
||||
variable_item_list_set_enter_callback(
|
||||
var_item_list, uart_terminal_scene_start_var_list_enter_callback, app);
|
||||
|
||||
VariableItem* item;
|
||||
for(int i = 0; i < NUM_MENU_ITEMS; ++i) {
|
||||
item = variable_item_list_add(
|
||||
var_item_list,
|
||||
items[i].item_string,
|
||||
items[i].num_options_menu,
|
||||
uart_terminal_scene_start_var_list_change_callback,
|
||||
app);
|
||||
variable_item_set_current_value_index(item, app->selected_option_index[i]);
|
||||
variable_item_set_current_value_text(
|
||||
item, items[i].options_menu[app->selected_option_index[i]]);
|
||||
}
|
||||
|
||||
variable_item_list_set_selected_item(
|
||||
var_item_list, scene_manager_get_scene_state(app->scene_manager, UART_TerminalSceneStart));
|
||||
|
||||
view_dispatcher_switch_to_view(app->view_dispatcher, UART_TerminalAppViewVarItemList);
|
||||
}
|
||||
|
||||
bool uart_terminal_scene_start_on_event(void* context, SceneManagerEvent event) {
|
||||
UNUSED(context);
|
||||
UART_TerminalApp* app = context;
|
||||
bool consumed = false;
|
||||
|
||||
if(event.type == SceneManagerEventTypeCustom) {
|
||||
if(event.event == UART_TerminalEventStartKeyboard) {
|
||||
scene_manager_set_scene_state(
|
||||
app->scene_manager, UART_TerminalSceneStart, app->selected_menu_index);
|
||||
scene_manager_next_scene(app->scene_manager, UART_TerminalAppViewTextInput);
|
||||
} else if(event.event == UART_TerminalEventStartConsole) {
|
||||
scene_manager_set_scene_state(
|
||||
app->scene_manager, UART_TerminalSceneStart, app->selected_menu_index);
|
||||
scene_manager_next_scene(app->scene_manager, UART_TerminalAppViewConsoleOutput);
|
||||
}
|
||||
consumed = true;
|
||||
} else if(event.type == SceneManagerEventTypeTick) {
|
||||
app->selected_menu_index = variable_item_list_get_selected_item_index(app->var_item_list);
|
||||
consumed = true;
|
||||
}
|
||||
|
||||
return consumed;
|
||||
}
|
||||
|
||||
void uart_terminal_scene_start_on_exit(void* context) {
|
||||
UART_TerminalApp* app = context;
|
||||
variable_item_list_reset(app->var_item_list);
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
#include "../uart_terminal_app_i.h"
|
||||
|
||||
void uart_terminal_scene_text_input_callback(void* context) {
|
||||
UART_TerminalApp* app = context;
|
||||
|
||||
view_dispatcher_send_custom_event(app->view_dispatcher, UART_TerminalEventStartConsole);
|
||||
}
|
||||
|
||||
void uart_terminal_scene_text_input_on_enter(void* context) {
|
||||
UART_TerminalApp* app = context;
|
||||
|
||||
if(false == app->is_custom_tx_string) {
|
||||
// Fill text input with selected string so that user can add to it
|
||||
size_t length = strlen(app->selected_tx_string);
|
||||
furi_assert(length < UART_TERMINAL_TEXT_INPUT_STORE_SIZE);
|
||||
bzero(app->text_input_store, UART_TERMINAL_TEXT_INPUT_STORE_SIZE);
|
||||
strncpy(app->text_input_store, app->selected_tx_string, length);
|
||||
|
||||
// Add space - because flipper keyboard currently doesn't have a space
|
||||
//app->text_input_store[length] = ' ';
|
||||
app->text_input_store[length + 1] = '\0';
|
||||
app->is_custom_tx_string = true;
|
||||
}
|
||||
|
||||
// Setup view
|
||||
UART_TextInput* text_input = app->text_input;
|
||||
// Add help message to header
|
||||
uart_text_input_set_header_text(text_input, "Send command to UART");
|
||||
uart_text_input_set_result_callback(
|
||||
text_input,
|
||||
uart_terminal_scene_text_input_callback,
|
||||
app,
|
||||
app->text_input_store,
|
||||
UART_TERMINAL_TEXT_INPUT_STORE_SIZE,
|
||||
false);
|
||||
|
||||
view_dispatcher_switch_to_view(app->view_dispatcher, UART_TerminalAppViewTextInput);
|
||||
}
|
||||
|
||||
bool uart_terminal_scene_text_input_on_event(void* context, SceneManagerEvent event) {
|
||||
UART_TerminalApp* app = context;
|
||||
bool consumed = false;
|
||||
|
||||
if(event.type == SceneManagerEventTypeCustom) {
|
||||
if(event.event == UART_TerminalEventStartConsole) {
|
||||
// Point to custom string to send
|
||||
app->selected_tx_string = app->text_input_store;
|
||||
scene_manager_next_scene(app->scene_manager, UART_TerminalAppViewConsoleOutput);
|
||||
consumed = true;
|
||||
}
|
||||
}
|
||||
|
||||
return consumed;
|
||||
}
|
||||
|
||||
void uart_terminal_scene_text_input_on_exit(void* context) {
|
||||
UART_TerminalApp* app = context;
|
||||
|
||||
uart_text_input_reset(app->text_input);
|
||||
}
|
||||
Reference in New Issue
Block a user