uart_terminal_scene_start.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 {
  6. NO_ARGS = 0,
  7. INPUT_ARGS,
  8. TOGGLE_ARGS
  9. } InputArgs;
  10. typedef enum {
  11. FOCUS_CONSOLE_END = 0,
  12. FOCUS_CONSOLE_START,
  13. FOCUS_CONSOLE_TOGGLE
  14. } FocusConsole;
  15. #define SHOW_STOPSCAN_TIP (true)
  16. #define NO_TIP (false)
  17. #define MAX_OPTIONS (9)
  18. typedef struct {
  19. const char* item_string;
  20. const char* options_menu[MAX_OPTIONS];
  21. int num_options_menu;
  22. const char* actual_commands[MAX_OPTIONS];
  23. InputArgs needs_keyboard;
  24. FocusConsole focus_console;
  25. bool show_stopscan_tip;
  26. } UART_TerminalItem;
  27. // NUM_MENU_ITEMS defined in uart_terminal_app_i.h - if you add an entry here, increment it!
  28. const UART_TerminalItem items[NUM_MENU_ITEMS] = {
  29. {"New custom message", {""}, 1, {""}, INPUT_ARGS, FOCUS_CONSOLE_END, NO_TIP},
  30. {"Quick message",
  31. {"SOS", "CQD", "VVV", "Eureka", "E.T ph...", "what h...", "Mayhem", "Flipper"},
  32. 8,
  33. {"sos",
  34. "cqd",
  35. "vvv",
  36. "eureka",
  37. "e.t. phone home",
  38. "what hath god wrought!",
  39. "let the mayhem begin",
  40. "flipper zero in da housa"},
  41. NO_ARGS,
  42. FOCUS_CONSOLE_END,
  43. NO_TIP},
  44. {"Help", {""}, 1, {""}, NO_ARGS, FOCUS_CONSOLE_START, SHOW_STOPSCAN_TIP},
  45. };
  46. static void uart_terminal_scene_start_var_list_enter_callback(void* context, uint32_t index) {
  47. furi_assert(context);
  48. UART_TerminalApp* app = context;
  49. furi_assert(index < NUM_MENU_ITEMS);
  50. const UART_TerminalItem* item = &items[index];
  51. const int selected_option_index = app->selected_option_index[index];
  52. furi_assert(selected_option_index < item->num_options_menu);
  53. app->selected_tx_string = item->actual_commands[selected_option_index];
  54. app->is_command = (1 <= index);
  55. app->is_custom_tx_string = false;
  56. app->selected_menu_index = index;
  57. app->focus_console_start = (item->focus_console == FOCUS_CONSOLE_TOGGLE) ?
  58. (selected_option_index == 0) :
  59. item->focus_console;
  60. app->show_stopscan_tip = item->show_stopscan_tip;
  61. bool needs_keyboard = (item->needs_keyboard == TOGGLE_ARGS) ? (selected_option_index != 0) :
  62. item->needs_keyboard;
  63. if(needs_keyboard) {
  64. view_dispatcher_send_custom_event(app->view_dispatcher, UART_TerminalEventStartKeyboard);
  65. } else {
  66. view_dispatcher_send_custom_event(app->view_dispatcher, UART_TerminalEventStartConsole);
  67. }
  68. }
  69. static void uart_terminal_scene_start_var_list_change_callback(VariableItem* item) {
  70. furi_assert(item);
  71. UART_TerminalApp* app = variable_item_get_context(item);
  72. furi_assert(app);
  73. const UART_TerminalItem* menu_item = &items[app->selected_menu_index];
  74. uint8_t item_index = variable_item_get_current_value_index(item);
  75. furi_assert(item_index < menu_item->num_options_menu);
  76. variable_item_set_current_value_text(item, menu_item->options_menu[item_index]);
  77. app->selected_option_index[app->selected_menu_index] = item_index;
  78. }
  79. void uart_terminal_scene_start_on_enter(void* context) {
  80. UART_TerminalApp* app = context;
  81. VariableItemList* var_item_list = app->var_item_list;
  82. variable_item_list_set_enter_callback(
  83. var_item_list, uart_terminal_scene_start_var_list_enter_callback, app);
  84. VariableItem* item;
  85. for(int i = 0; i < NUM_MENU_ITEMS; ++i) {
  86. item = variable_item_list_add(
  87. var_item_list,
  88. items[i].item_string,
  89. items[i].num_options_menu,
  90. uart_terminal_scene_start_var_list_change_callback,
  91. app);
  92. variable_item_set_current_value_index(item, app->selected_option_index[i]);
  93. variable_item_set_current_value_text(
  94. item, items[i].options_menu[app->selected_option_index[i]]);
  95. }
  96. variable_item_list_set_selected_item(
  97. var_item_list, scene_manager_get_scene_state(app->scene_manager, UART_TerminalSceneStart));
  98. view_dispatcher_switch_to_view(app->view_dispatcher, UART_TerminalAppViewVarItemList);
  99. }
  100. bool uart_terminal_scene_start_on_event(void* context, SceneManagerEvent event) {
  101. UNUSED(context);
  102. UART_TerminalApp* app = context;
  103. bool consumed = false;
  104. if(event.type == SceneManagerEventTypeCustom) {
  105. if(event.event == UART_TerminalEventStartKeyboard) {
  106. scene_manager_set_scene_state(
  107. app->scene_manager, UART_TerminalSceneStart, app->selected_menu_index);
  108. scene_manager_next_scene(app->scene_manager, UART_TerminalAppViewTextInput);
  109. } else if(event.event == UART_TerminalEventStartConsole) {
  110. scene_manager_set_scene_state(
  111. app->scene_manager, UART_TerminalSceneStart, app->selected_menu_index);
  112. scene_manager_next_scene(app->scene_manager, UART_TerminalAppViewConsoleOutput);
  113. }
  114. consumed = true;
  115. } else if(event.type == SceneManagerEventTypeTick) {
  116. app->selected_menu_index = variable_item_list_get_selected_item_index(app->var_item_list);
  117. consumed = true;
  118. }
  119. return consumed;
  120. }
  121. void uart_terminal_scene_start_on_exit(void* context) {
  122. UART_TerminalApp* app = context;
  123. variable_item_list_reset(app->var_item_list);
  124. }