app.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. #include <furi.h>
  2. #include <furi_hal.h>
  3. #include <lib/flipper_format/flipper_format.h>
  4. #include <input/input.h>
  5. #include <gui/gui.h>
  6. #include <stdlib.h>
  7. #include "app.h"
  8. #include "app_buffer.h"
  9. #define FREQ 433920000
  10. RawSamplesBuffer *RawSamples, *DetectedSamples;
  11. extern const SubGhzProtocolRegistry protoview_protocol_registry;
  12. /* Render the received signal.
  13. *
  14. * The screen of the flipper is 128 x 64. Even using 4 pixels per line
  15. * (where low level signal is one pixel high, high level is 4 pixels
  16. * high) and 4 pixels of spacing between the different lines, we can
  17. * plot comfortably 8 lines.
  18. *
  19. * The 'idx' argument is the first sample to render in the circular
  20. * buffer. */
  21. void render_signal(Canvas *const canvas, RawSamplesBuffer *buf, uint32_t idx) {
  22. canvas_set_color(canvas, ColorWhite);
  23. canvas_draw_box(canvas, 0, 0, 127, 63);
  24. canvas_set_color(canvas, ColorBlack);
  25. int rows = 8;
  26. uint32_t time_per_pixel = 100;
  27. bool level = 0;
  28. uint32_t dur = 0;
  29. for (int row = 0; row < rows ; row++) {
  30. for (int x = 0; x < 128; x++) {
  31. int y = 3 + row*8;
  32. if (dur < time_per_pixel/2) {
  33. /* Get more data. */
  34. raw_samples_get(buf, idx++, &level, &dur);
  35. }
  36. canvas_draw_line(canvas, x,y,x,y-(level*3));
  37. /* Remove from the current level duration the time we
  38. * just plot. */
  39. if (dur > time_per_pixel)
  40. dur -= time_per_pixel;
  41. else
  42. dur = 0;
  43. }
  44. }
  45. }
  46. /* Return the time difference between a and b, always >= 0 since
  47. * the absolute value is returned. */
  48. uint32_t duration_delta(uint32_t a, uint32_t b) {
  49. return a > b ? a - b : b -a;
  50. }
  51. /* This function starts scanning samples at offset idx looking for the
  52. * longest run of pulses, either high or low, that are among 10%
  53. * of each other, for a maximum of three classes. The classes are
  54. * counted separtely for high and low signals (RF on / off) because
  55. * many devices tend to have different pulse lenghts depending on
  56. * the level of the pulse.
  57. *
  58. * For instance Oregon2 sensors, in the case of protocol 2.1 will send
  59. * pulses of ~400us (RF on) VS ~580us (RF off). */
  60. #define SEARCH_CLASSES 3
  61. uint32_t search_coherent_signal(RawSamplesBuffer *s, uint32_t idx) {
  62. struct {
  63. uint32_t dur[2]; /* dur[0] = low, dur[1] = high */
  64. uint32_t count[2]; /* Associated observed frequency. */
  65. } classes[SEARCH_CLASSES];
  66. memset(classes,0,sizeof(classes));
  67. uint32_t minlen = 80, maxlen = 4000; /* Depends on data rate, here we
  68. allow for high and low. */
  69. uint32_t len = 0; /* Observed len of coherent samples. */
  70. s->short_pulse_dur = 0;
  71. for (uint32_t j = idx; j < idx+100; j++) {
  72. bool level;
  73. uint32_t dur;
  74. raw_samples_get(s, j, &level, &dur);
  75. if (dur < minlen || dur > maxlen) return len;
  76. /* Let's see if it matches a class we already have or if we
  77. * can populate a new (yet empty) class. */
  78. uint32_t k;
  79. for (k = 0; k < SEARCH_CLASSES; k++) {
  80. if (classes[k].count[level] == 0) {
  81. classes[k].dur[level] = dur;
  82. classes[k].count[level] = 1;
  83. break;
  84. } else {
  85. uint32_t classavg = classes[k].dur[level];
  86. uint32_t count = classes[k].count[level];
  87. uint32_t delta = duration_delta(dur,classavg);
  88. if (delta < classavg/10) {
  89. /* It is useful to compute the average of the class
  90. * we are observing. We know how many samples we got so
  91. * far, so we can recompute the average easily.
  92. * By always having a better estimate of the pulse len
  93. * we can avoid missing next samples in case the first
  94. * observed samples are too off. */
  95. classavg = ((classavg * count) + dur) / (count+1);
  96. classes[k].dur[level] = classavg;
  97. classes[k].count[level]++;
  98. break;
  99. }
  100. }
  101. }
  102. if (k == SEARCH_CLASSES) { /* No match, return. */
  103. return len;
  104. } else {
  105. /* Update the buffer setting the shortest pulse we found
  106. * among the three classes. This will be used when scaling
  107. * for visualization. */
  108. if (s->short_pulse_dur == 0 || dur < s->short_pulse_dur)
  109. s->short_pulse_dur = dur;
  110. }
  111. len++;
  112. }
  113. return len;
  114. }
  115. /* Search the buffer with the stored signal (last N samples received)
  116. * in order to find a coherent signal. If a signal that does not appear to
  117. * be just noise is found, it is set in DetectedSamples global signal
  118. * buffer, that is what is rendered on the screen. */
  119. void scan_for_signal(ProtoViewApp *app) {
  120. /* We need to work on a copy: the RawSamples buffer is populated
  121. * by the background thread receiving data. */
  122. RawSamplesBuffer *copy = raw_samples_alloc();
  123. raw_samples_copy(copy,RawSamples);
  124. /* Try to seek on data that looks to have a regular high low high low
  125. * pattern. */
  126. uint32_t minlen = 10; /* Min run of coherent samples. */
  127. for (uint32_t i = 0; i < copy->total-1; i++) {
  128. uint32_t thislen = search_coherent_signal(copy,i);
  129. if (thislen > minlen && thislen > app->signal_bestlen) {
  130. app->signal_bestlen = thislen;
  131. raw_samples_copy(DetectedSamples,copy);
  132. DetectedSamples->idx = (DetectedSamples->idx+i)%
  133. DetectedSamples->total;
  134. FURI_LOG_E(TAG, "Displayed sample updated");
  135. }
  136. }
  137. raw_samples_free(copy);
  138. }
  139. static void render_callback(Canvas *const canvas, void *ctx) {
  140. ProtoViewApp *app = ctx;
  141. scan_for_signal(app);
  142. render_signal(canvas, DetectedSamples, 0);
  143. }
  144. /* Here all we do is putting the events into the queue that will be handled
  145. * in the while() loop of the app entry point function. */
  146. static void input_callback(InputEvent* input_event, void* ctx)
  147. {
  148. ProtoViewApp *app = ctx;
  149. if (input_event->type == InputTypePress) {
  150. furi_message_queue_put(app->event_queue,input_event,FuriWaitForever);
  151. FURI_LOG_E(TAG, "INPUT CALLBACK %d", (int)input_event->key);
  152. }
  153. }
  154. ProtoViewApp* protoview_app_alloc() {
  155. ProtoViewApp *app = malloc(sizeof(ProtoViewApp));
  156. // Init shared data structures
  157. RawSamples = raw_samples_alloc();
  158. DetectedSamples = raw_samples_alloc();
  159. //init setting
  160. app->setting = subghz_setting_alloc();
  161. subghz_setting_load(app->setting, EXT_PATH("protoview/settings.txt"));
  162. // GUI
  163. app->gui = furi_record_open(RECORD_GUI);
  164. app->view_port = view_port_alloc();
  165. view_port_draw_callback_set(app->view_port, render_callback, app);
  166. view_port_input_callback_set(app->view_port, input_callback, app);
  167. gui_add_view_port(app->gui, app->view_port, GuiLayerFullscreen);
  168. app->event_queue = furi_message_queue_alloc(8, sizeof(InputEvent));
  169. // Signal found
  170. app->signal_bestlen = 0;
  171. //init Worker & Protocol
  172. app->txrx = malloc(sizeof(ProtoViewTxRx));
  173. app->txrx->preset = malloc(sizeof(SubGhzRadioPreset));
  174. app->txrx->preset->name = furi_string_alloc();
  175. /* Setup rx worker and environment. */
  176. app->txrx->worker = subghz_worker_alloc();
  177. app->txrx->environment = subghz_environment_alloc();
  178. subghz_environment_set_protocol_registry(
  179. app->txrx->environment, (void*)&protoview_protocol_registry);
  180. app->txrx->receiver = subghz_receiver_alloc_init(app->txrx->environment);
  181. subghz_receiver_set_filter(app->txrx->receiver, SubGhzProtocolFlag_Decodable);
  182. subghz_worker_set_overrun_callback(
  183. app->txrx->worker, (SubGhzWorkerOverrunCallback)subghz_receiver_reset);
  184. subghz_worker_set_pair_callback(
  185. app->txrx->worker, (SubGhzWorkerPairCallback)subghz_receiver_decode);
  186. subghz_worker_set_context(app->txrx->worker, app->txrx->receiver);
  187. furi_hal_power_suppress_charge_enter();
  188. app->running = 1;
  189. return app;
  190. }
  191. void protoview_app_free(ProtoViewApp *app) {
  192. furi_assert(app);
  193. //CC1101 off
  194. radio_sleep(app);
  195. // View
  196. view_port_enabled_set(app->view_port, false);
  197. gui_remove_view_port(app->gui, app->view_port);
  198. view_port_free(app->view_port);
  199. furi_record_close(RECORD_GUI);
  200. furi_message_queue_free(app->event_queue);
  201. app->gui = NULL;
  202. //setting
  203. subghz_setting_free(app->setting);
  204. //Worker
  205. subghz_receiver_free(app->txrx->receiver);
  206. subghz_environment_free(app->txrx->environment);
  207. subghz_worker_free(app->txrx->worker);
  208. furi_string_free(app->txrx->preset->name);
  209. free(app->txrx->preset);
  210. free(app->txrx);
  211. furi_hal_power_suppress_charge_exit();
  212. raw_samples_free(RawSamples);
  213. raw_samples_free(DetectedSamples);
  214. free(app);
  215. }
  216. int32_t protoview_app_entry(void* p) {
  217. UNUSED(p);
  218. ProtoViewApp *app = protoview_app_alloc();
  219. radio_begin(app);
  220. radio_rx(app, FREQ);
  221. InputEvent input;
  222. while(app->running) {
  223. FuriStatus qstat = furi_message_queue_get(app->event_queue, &input, 100);
  224. if (qstat == FuriStatusOk) {
  225. if (input.key == InputKeyBack) {
  226. app->running = 0;
  227. } else if (input.key == InputKeyOk) {
  228. app->signal_bestlen = 0;
  229. raw_samples_reset(DetectedSamples);
  230. }
  231. FURI_LOG_E(TAG, "Main Loop - Input: %u", input.key);
  232. } else {
  233. static int c = 0;
  234. c++;
  235. if (!(c % 20)) FURI_LOG_E(TAG, "Loop timeout");
  236. }
  237. view_port_update(app->view_port);
  238. }
  239. if (app->txrx->txrx_state == TxRxStateRx) {
  240. FURI_LOG_E(TAG, "Putting CC1101 to sleep before exiting.");
  241. radio_rx_end(app);
  242. radio_sleep(app);
  243. }
  244. protoview_app_free(app);
  245. return 0;
  246. }