uart_terminal_scene_start.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. //** Includes sniffbt and sniffskim for compatible ESP32-WROOM hardware.
  2. //uart_terminal_app_i.h also changed **//
  3. #include "../uart_terminal_app_i.h"
  4. // For each command, define whether additional arguments are needed
  5. // (enabling text input to fill them out), and whether the console
  6. // text box should focus at the start of the output or the end
  7. typedef enum { NO_ARGS = 0, INPUT_ARGS, TOGGLE_ARGS } InputArgs;
  8. typedef enum { FOCUS_CONSOLE_END = 0, FOCUS_CONSOLE_START, FOCUS_CONSOLE_TOGGLE } FocusConsole;
  9. #define SHOW_STOPSCAN_TIP (true)
  10. #define NO_TIP (false)
  11. #define MAX_OPTIONS (9)
  12. typedef struct {
  13. const char* item_string;
  14. const char* options_menu[MAX_OPTIONS];
  15. int num_options_menu;
  16. const char* actual_commands[MAX_OPTIONS];
  17. InputArgs needs_keyboard;
  18. FocusConsole focus_console;
  19. bool show_stopscan_tip;
  20. } UART_TerminalItem;
  21. // NUM_MENU_ITEMS defined in uart_terminal_app_i.h - if you add an entry here, increment it!
  22. const UART_TerminalItem items[NUM_MENU_ITEMS] = {
  23. {"Console",
  24. {"9600", "19200", "57600", "115200"},
  25. 4,
  26. {"", ""},
  27. NO_ARGS,
  28. FOCUS_CONSOLE_TOGGLE,
  29. NO_TIP},
  30. {"Send command", {""}, 1, {""}, INPUT_ARGS, FOCUS_CONSOLE_END, NO_TIP},
  31. {"Reboot", {""}, 1, {"reboot"}, NO_ARGS, FOCUS_CONSOLE_END, NO_TIP},
  32. {"Help", {""}, 1, {"help"}, NO_ARGS, FOCUS_CONSOLE_START, SHOW_STOPSCAN_TIP},
  33. };
  34. static void uart_terminal_scene_start_var_list_enter_callback(void* context, uint32_t index) {
  35. furi_assert(context);
  36. UART_TerminalApp* app = context;
  37. furi_assert(index < NUM_MENU_ITEMS);
  38. const UART_TerminalItem* item = &items[index];
  39. const int selected_option_index = app->selected_option_index[index];
  40. furi_assert(selected_option_index < item->num_options_menu);
  41. app->selected_tx_string = item->actual_commands[selected_option_index];
  42. app->is_command = (1 <= index);
  43. app->is_custom_tx_string = false;
  44. app->selected_menu_index = index;
  45. app->focus_console_start = (item->focus_console == FOCUS_CONSOLE_TOGGLE) ?
  46. (selected_option_index == 0) :
  47. item->focus_console;
  48. app->show_stopscan_tip = item->show_stopscan_tip;
  49. bool needs_keyboard = (item->needs_keyboard == TOGGLE_ARGS) ? (selected_option_index != 0) :
  50. item->needs_keyboard;
  51. if(needs_keyboard) {
  52. view_dispatcher_send_custom_event(app->view_dispatcher, UART_TerminalEventStartKeyboard);
  53. } else {
  54. view_dispatcher_send_custom_event(app->view_dispatcher, UART_TerminalEventStartConsole);
  55. }
  56. }
  57. static void uart_terminal_scene_start_var_list_change_callback(VariableItem* item) {
  58. furi_assert(item);
  59. UART_TerminalApp* app = variable_item_get_context(item);
  60. furi_assert(app);
  61. const UART_TerminalItem* menu_item = &items[app->selected_menu_index];
  62. uint8_t item_index = variable_item_get_current_value_index(item);
  63. furi_assert(item_index < menu_item->num_options_menu);
  64. variable_item_set_current_value_text(item, menu_item->options_menu[item_index]);
  65. app->selected_option_index[app->selected_menu_index] = item_index;
  66. }
  67. void uart_terminal_scene_start_on_enter(void* context) {
  68. UART_TerminalApp* app = context;
  69. VariableItemList* var_item_list = app->var_item_list;
  70. variable_item_list_set_enter_callback(
  71. var_item_list, uart_terminal_scene_start_var_list_enter_callback, app);
  72. VariableItem* item;
  73. for(int i = 0; i < NUM_MENU_ITEMS; ++i) {
  74. item = variable_item_list_add(
  75. var_item_list,
  76. items[i].item_string,
  77. items[i].num_options_menu,
  78. uart_terminal_scene_start_var_list_change_callback,
  79. app);
  80. variable_item_set_current_value_index(item, app->selected_option_index[i]);
  81. variable_item_set_current_value_text(
  82. item, items[i].options_menu[app->selected_option_index[i]]);
  83. }
  84. variable_item_list_set_selected_item(
  85. var_item_list, scene_manager_get_scene_state(app->scene_manager, UART_TerminalSceneStart));
  86. view_dispatcher_switch_to_view(app->view_dispatcher, UART_TerminalAppViewVarItemList);
  87. }
  88. bool uart_terminal_scene_start_on_event(void* context, SceneManagerEvent event) {
  89. UNUSED(context);
  90. UART_TerminalApp* app = context;
  91. bool consumed = false;
  92. if(event.type == SceneManagerEventTypeCustom) {
  93. if(event.event == UART_TerminalEventStartKeyboard) {
  94. scene_manager_set_scene_state(
  95. app->scene_manager, UART_TerminalSceneStart, app->selected_menu_index);
  96. scene_manager_next_scene(app->scene_manager, UART_TerminalAppViewTextInput);
  97. } else if(event.event == UART_TerminalEventStartConsole) {
  98. scene_manager_set_scene_state(
  99. app->scene_manager, UART_TerminalSceneStart, app->selected_menu_index);
  100. scene_manager_next_scene(app->scene_manager, UART_TerminalAppViewConsoleOutput);
  101. }
  102. consumed = true;
  103. } else if(event.type == SceneManagerEventTypeTick) {
  104. app->selected_menu_index = variable_item_list_get_selected_item_index(app->var_item_list);
  105. consumed = true;
  106. }
  107. return consumed;
  108. }
  109. void uart_terminal_scene_start_on_exit(void* context) {
  110. UART_TerminalApp* app = context;
  111. variable_item_list_reset(app->var_item_list);
  112. }