uart_terminal_scene_start.c 5.3 KB

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