weather_station_app_i.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. subghz_devices_reset(app->txrx->radio_device);
  53. subghz_devices_idle(app->txrx->radio_device);
  54. subghz_devices_load_preset(app->txrx->radio_device, FuriHalSubGhzPresetCustom, preset_data);
  55. app->txrx->txrx_state = WSTxRxStateIDLE;
  56. }
  57. uint32_t ws_rx(WeatherStationApp* app, uint32_t frequency) {
  58. furi_assert(app);
  59. if(!subghz_devices_is_frequency_valid(app->txrx->radio_device, frequency)) {
  60. furi_crash("WeatherStation: Incorrect RX frequency.");
  61. }
  62. furi_assert(
  63. app->txrx->txrx_state != WSTxRxStateRx && app->txrx->txrx_state != WSTxRxStateSleep);
  64. subghz_devices_idle(app->txrx->radio_device);
  65. uint32_t value = subghz_devices_set_frequency(app->txrx->radio_device, frequency);
  66. subghz_devices_flush_rx(app->txrx->radio_device);
  67. subghz_devices_set_rx(app->txrx->radio_device);
  68. subghz_devices_start_async_rx(
  69. app->txrx->radio_device, subghz_worker_rx_callback, app->txrx->worker);
  70. subghz_worker_start(app->txrx->worker);
  71. app->txrx->txrx_state = WSTxRxStateRx;
  72. return value;
  73. }
  74. void ws_idle(WeatherStationApp* app) {
  75. furi_assert(app);
  76. furi_assert(app->txrx->txrx_state != WSTxRxStateSleep);
  77. subghz_devices_idle(app->txrx->radio_device);
  78. app->txrx->txrx_state = WSTxRxStateIDLE;
  79. }
  80. void ws_rx_end(WeatherStationApp* app) {
  81. furi_assert(app);
  82. furi_assert(app->txrx->txrx_state == WSTxRxStateRx);
  83. if(subghz_worker_is_running(app->txrx->worker)) {
  84. subghz_worker_stop(app->txrx->worker);
  85. subghz_devices_stop_async_rx(app->txrx->radio_device);
  86. }
  87. subghz_devices_idle(app->txrx->radio_device);
  88. app->txrx->txrx_state = WSTxRxStateIDLE;
  89. }
  90. void ws_sleep(WeatherStationApp* app) {
  91. furi_assert(app);
  92. subghz_devices_sleep(app->txrx->radio_device);
  93. app->txrx->txrx_state = WSTxRxStateSleep;
  94. }
  95. void ws_hopper_update(WeatherStationApp* app) {
  96. furi_assert(app);
  97. switch(app->txrx->hopper_state) {
  98. case WSHopperStateOFF:
  99. case WSHopperStatePause:
  100. return;
  101. case WSHopperStateRSSITimeOut:
  102. if(app->txrx->hopper_timeout != 0) {
  103. app->txrx->hopper_timeout--;
  104. return;
  105. }
  106. break;
  107. default:
  108. break;
  109. }
  110. float rssi = -127.0f;
  111. if(app->txrx->hopper_state != WSHopperStateRSSITimeOut) {
  112. // See RSSI Calculation timings in CC1101 17.3 RSSI
  113. rssi = subghz_devices_get_rssi(app->txrx->radio_device);
  114. // Stay if RSSI is high enough
  115. if(rssi > -90.0f) {
  116. app->txrx->hopper_timeout = 10;
  117. app->txrx->hopper_state = WSHopperStateRSSITimeOut;
  118. return;
  119. }
  120. } else {
  121. app->txrx->hopper_state = WSHopperStateRunnig;
  122. }
  123. // Select next frequency
  124. if(app->txrx->hopper_idx_frequency <
  125. subghz_setting_get_hopper_frequency_count(app->setting) - 1) {
  126. app->txrx->hopper_idx_frequency++;
  127. } else {
  128. app->txrx->hopper_idx_frequency = 0;
  129. }
  130. if(app->txrx->txrx_state == WSTxRxStateRx) {
  131. ws_rx_end(app);
  132. };
  133. if(app->txrx->txrx_state == WSTxRxStateIDLE) {
  134. subghz_receiver_reset(app->txrx->receiver);
  135. app->txrx->preset->frequency =
  136. subghz_setting_get_hopper_frequency(app->setting, app->txrx->hopper_idx_frequency);
  137. ws_rx(app, app->txrx->preset->frequency);
  138. }
  139. }