weather_station_app_i.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. #include "weather_station_app_i.h"
  2. #define TAG "WeatherStation"
  3. #include <flipper_format/flipper_format_i.h>
  4. void ws_preset_init(
  5. void* context,
  6. const char* preset_name,
  7. uint32_t frequency,
  8. uint8_t* preset_data,
  9. size_t preset_data_size) {
  10. furi_assert(context);
  11. WeatherStationApp* app = context;
  12. furi_string_set(app->txrx->preset->name, preset_name);
  13. app->txrx->preset->frequency = frequency;
  14. app->txrx->preset->data = preset_data;
  15. app->txrx->preset->data_size = preset_data_size;
  16. }
  17. bool ws_set_preset(WeatherStationApp* app, const char* preset) {
  18. if(!strcmp(preset, "FuriHalSubGhzPresetOok270Async")) {
  19. furi_string_set(app->txrx->preset->name, "AM270");
  20. } else if(!strcmp(preset, "FuriHalSubGhzPresetOok650Async")) {
  21. furi_string_set(app->txrx->preset->name, "AM650");
  22. } else if(!strcmp(preset, "FuriHalSubGhzPreset2FSKDev238Async")) {
  23. furi_string_set(app->txrx->preset->name, "FM238");
  24. } else if(!strcmp(preset, "FuriHalSubGhzPreset2FSKDev476Async")) {
  25. furi_string_set(app->txrx->preset->name, "FM476");
  26. } else if(!strcmp(preset, "FuriHalSubGhzPresetCustom")) {
  27. furi_string_set(app->txrx->preset->name, "CUSTOM");
  28. } else {
  29. FURI_LOG_E(TAG, "Unknown preset");
  30. return false;
  31. }
  32. return true;
  33. }
  34. void ws_get_frequency_modulation(
  35. WeatherStationApp* app,
  36. FuriString* frequency,
  37. FuriString* modulation) {
  38. furi_assert(app);
  39. if(frequency != NULL) {
  40. furi_string_printf(
  41. frequency,
  42. "%03ld.%02ld",
  43. app->txrx->preset->frequency / 1000000 % 1000,
  44. app->txrx->preset->frequency / 10000 % 100);
  45. }
  46. if(modulation != NULL) {
  47. furi_string_printf(modulation, "%.2s", furi_string_get_cstr(app->txrx->preset->name));
  48. }
  49. }
  50. void ws_begin(WeatherStationApp* app, uint8_t* preset_data) {
  51. furi_assert(app);
  52. UNUSED(preset_data);
  53. furi_hal_subghz_reset();
  54. furi_hal_subghz_idle();
  55. furi_hal_subghz_load_custom_preset(preset_data);
  56. furi_hal_gpio_init(&gpio_cc1101_g0, GpioModeInput, GpioPullNo, GpioSpeedLow);
  57. app->txrx->txrx_state = WSTxRxStateIDLE;
  58. }
  59. uint32_t ws_rx(WeatherStationApp* app, uint32_t frequency) {
  60. furi_assert(app);
  61. if(!furi_hal_subghz_is_frequency_valid(frequency)) {
  62. furi_crash("WeatherStation: Incorrect RX frequency.");
  63. }
  64. furi_assert(
  65. app->txrx->txrx_state != WSTxRxStateRx && app->txrx->txrx_state != WSTxRxStateSleep);
  66. furi_hal_subghz_idle();
  67. uint32_t value = furi_hal_subghz_set_frequency_and_path(frequency);
  68. furi_hal_gpio_init(&gpio_cc1101_g0, GpioModeInput, GpioPullNo, GpioSpeedLow);
  69. furi_hal_subghz_flush_rx();
  70. furi_hal_subghz_rx();
  71. furi_hal_subghz_start_async_rx(subghz_worker_rx_callback, app->txrx->worker);
  72. subghz_worker_start(app->txrx->worker);
  73. app->txrx->txrx_state = WSTxRxStateRx;
  74. return value;
  75. }
  76. void ws_idle(WeatherStationApp* app) {
  77. furi_assert(app);
  78. furi_assert(app->txrx->txrx_state != WSTxRxStateSleep);
  79. furi_hal_subghz_idle();
  80. app->txrx->txrx_state = WSTxRxStateIDLE;
  81. }
  82. void ws_rx_end(WeatherStationApp* app) {
  83. furi_assert(app);
  84. furi_assert(app->txrx->txrx_state == WSTxRxStateRx);
  85. if(subghz_worker_is_running(app->txrx->worker)) {
  86. subghz_worker_stop(app->txrx->worker);
  87. furi_hal_subghz_stop_async_rx();
  88. }
  89. furi_hal_subghz_idle();
  90. app->txrx->txrx_state = WSTxRxStateIDLE;
  91. }
  92. void ws_sleep(WeatherStationApp* app) {
  93. furi_assert(app);
  94. furi_hal_subghz_sleep();
  95. app->txrx->txrx_state = WSTxRxStateSleep;
  96. }
  97. void ws_hopper_update(WeatherStationApp* app) {
  98. furi_assert(app);
  99. switch(app->txrx->hopper_state) {
  100. case WSHopperStateOFF:
  101. case WSHopperStatePause:
  102. return;
  103. case WSHopperStateRSSITimeOut:
  104. if(app->txrx->hopper_timeout != 0) {
  105. app->txrx->hopper_timeout--;
  106. return;
  107. }
  108. break;
  109. default:
  110. break;
  111. }
  112. float rssi = -127.0f;
  113. if(app->txrx->hopper_state != WSHopperStateRSSITimeOut) {
  114. // See RSSI Calculation timings in CC1101 17.3 RSSI
  115. rssi = furi_hal_subghz_get_rssi();
  116. // Stay if RSSI is high enough
  117. if(rssi > -90.0f) {
  118. app->txrx->hopper_timeout = 10;
  119. app->txrx->hopper_state = WSHopperStateRSSITimeOut;
  120. return;
  121. }
  122. } else {
  123. app->txrx->hopper_state = WSHopperStateRunnig;
  124. }
  125. // Select next frequency
  126. if(app->txrx->hopper_idx_frequency <
  127. subghz_setting_get_hopper_frequency_count(app->setting) - 1) {
  128. app->txrx->hopper_idx_frequency++;
  129. } else {
  130. app->txrx->hopper_idx_frequency = 0;
  131. }
  132. if(app->txrx->txrx_state == WSTxRxStateRx) {
  133. ws_rx_end(app);
  134. };
  135. if(app->txrx->txrx_state == WSTxRxStateIDLE) {
  136. subghz_receiver_reset(app->txrx->receiver);
  137. app->txrx->preset->frequency =
  138. subghz_setting_get_hopper_frequency(app->setting, app->txrx->hopper_idx_frequency);
  139. ws_rx(app, app->txrx->preset->frequency);
  140. }
  141. }