uart_terminal_scene_start.c 5.3 KB

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