uart_terminal_scene_start.c 5.9 KB

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