esubghz_chat_freq_input.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. #include "../esubghz_chat_i.h"
  2. /* Sends FreqEntered event to scene manager and displays the frequency in the
  3. * text box. */
  4. static void freq_input_cb(void *context)
  5. {
  6. furi_assert(context);
  7. ESubGhzChatState* state = context;
  8. furi_string_cat_printf(state->chat_box_store, "Frequency: %lu",
  9. state->frequency);
  10. view_dispatcher_send_custom_event(state->view_dispatcher,
  11. ESubGhzChatEvent_FreqEntered);
  12. }
  13. /* Validates the entered frequency. */
  14. static bool freq_input_validator(const char *text, FuriString *error,
  15. void *context)
  16. {
  17. furi_assert(text);
  18. furi_assert(error);
  19. furi_assert(context);
  20. ESubGhzChatState* state = context;
  21. int ret = sscanf(text, "%lu", &(state->frequency));
  22. if (ret != 1) {
  23. furi_string_printf(error, "Please enter\nfrequency\nin Hz!");
  24. return false;
  25. }
  26. if (!subghz_devices_is_frequency_valid(state->subghz_device,
  27. state->frequency)) {
  28. furi_string_printf(error, "Frequency\n%lu\n is invalid!",
  29. state->frequency);
  30. return false;
  31. }
  32. #ifdef FW_ORIGIN_Official
  33. if (!furi_hal_region_is_frequency_allowed(state->frequency)) {
  34. #else /* FW_ORIGIN_Official */
  35. if (!furi_hal_subghz_is_tx_allowed(state->frequency)) {
  36. #endif /* FW_ORIGIN_Official */
  37. furi_string_printf(error, "TX forbidden\non frequency\n%lu!",
  38. state->frequency);
  39. return false;
  40. }
  41. return true;
  42. }
  43. /* Prepares the frequency input scene. */
  44. void scene_on_enter_freq_input(void* context)
  45. {
  46. FURI_LOG_T(APPLICATION_NAME, "scene_on_enter_freq_input");
  47. furi_assert(context);
  48. ESubGhzChatState* state = context;
  49. snprintf(state->text_input_store, TEXT_INPUT_STORE_SIZE, "%lu",
  50. (uint32_t) DEFAULT_FREQ);
  51. text_input_reset(state->text_input);
  52. text_input_set_result_callback(
  53. state->text_input,
  54. freq_input_cb,
  55. state,
  56. state->text_input_store,
  57. sizeof(state->text_input_store),
  58. true);
  59. text_input_set_validator(
  60. state->text_input,
  61. freq_input_validator,
  62. state);
  63. text_input_set_header_text(
  64. state->text_input,
  65. "Frequency");
  66. view_dispatcher_switch_to_view(state->view_dispatcher, ESubGhzChatView_Input);
  67. }
  68. /* Handles scene manager events for the frequency input scene. */
  69. bool scene_on_event_freq_input(void* context, SceneManagerEvent event)
  70. {
  71. FURI_LOG_T(APPLICATION_NAME, "scene_on_event_freq_input");
  72. furi_assert(context);
  73. ESubGhzChatState* state = context;
  74. bool consumed = false;
  75. switch(event.type) {
  76. case SceneManagerEventTypeCustom:
  77. switch(event.event) {
  78. /* switch to password input scene */
  79. case ESubGhzChatEvent_FreqEntered:
  80. scene_manager_next_scene(state->scene_manager,
  81. ESubGhzChatScene_KeyMenu);
  82. consumed = true;
  83. break;
  84. }
  85. break;
  86. case SceneManagerEventTypeBack:
  87. /* stop the application if the user presses back here */
  88. view_dispatcher_stop(state->view_dispatcher);
  89. consumed = true;
  90. break;
  91. default:
  92. consumed = false;
  93. break;
  94. }
  95. return consumed;
  96. }
  97. /* Cleans up the frequency input scene. */
  98. void scene_on_exit_freq_input(void* context)
  99. {
  100. FURI_LOG_T(APPLICATION_NAME, "scene_on_exit_freq_input");
  101. furi_assert(context);
  102. ESubGhzChatState* state = context;
  103. text_input_reset(state->text_input);
  104. }