esubghz_chat_freq_input.c 3.3 KB

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