esubghz_chat_freq_input.c 2.9 KB

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