esubghz_chat_freq_input.c 2.9 KB

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