nrf24channelscanner.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. #include <stdio.h>
  2. #include <furi.h>
  3. #include <furi_hal_power.h>
  4. #include <gui/gui.h>
  5. #include <input/input.h>
  6. #include <gui/elements.h>
  7. #include <notification/notification_messages.h>
  8. #include <nrf24.h>
  9. #include "nrf24channelscanner_icons.h"
  10. #include <assets_icons.h>
  11. const uint8_t num_channels = 128;
  12. static uint8_t nrf24values[128] = {0}; //to store channel data
  13. bool ifNotFoundNrf = false; //to show error message
  14. bool szuz = true; //to show welcome screen
  15. static bool isScanning = false; //to track the progress
  16. static bool stopNrfScan = false; //to exit thread
  17. static bool isInfiniteScan = false; //to prevent stop scan when OK long pressed
  18. static bool threadStoppedsoFree = false; //indicate if I can free the thread from ram.
  19. static uint8_t currCh = 0; //for the progress bar or the channel selector
  20. static int delayPerChan = 150; //can set via up / down.
  21. bool showFreq = true;
  22. FuriThread* thread;
  23. typedef enum {
  24. EventTypeKey,
  25. EventTypeTick,
  26. } EventType;
  27. typedef struct {
  28. EventType type;
  29. InputEvent input;
  30. } Event;
  31. static void draw_callback(Canvas* canvas, void* ctx) {
  32. UNUSED(ctx);
  33. canvas_clear(canvas);
  34. canvas_set_bitmap_mode(canvas, true);
  35. canvas_draw_icon(canvas, 100, 0, &I_Pin_back_arrow_10x8);
  36. canvas_set_font(canvas, FontSecondary);
  37. canvas_draw_str(canvas, 112, 8, "Exit");
  38. canvas_draw_icon(canvas, 1, 0, &I_Ok_btn_9x9);
  39. canvas_set_font(canvas, FontSecondary);
  40. if(isScanning) {
  41. canvas_draw_str(canvas, 12, 8, "Stop");
  42. } else {
  43. canvas_draw_str(canvas, 12, 8, "Scan");
  44. }
  45. canvas_draw_line(canvas, 0, 11, 127, 11);
  46. if(ifNotFoundNrf) {
  47. canvas_set_font(canvas, FontPrimary);
  48. canvas_draw_str(canvas, 23, 35, "NRF24 not found!");
  49. return;
  50. }
  51. canvas_draw_line(canvas, currCh, 12, currCh, 13); //draw the current channel
  52. //draw hello mesage
  53. if(szuz) {
  54. canvas_set_font(canvas, FontSecondary);
  55. canvas_draw_str(canvas, 1, 22, "OK: scan / stop. Long: infinite.");
  56. canvas_draw_str(canvas, 1, 33, "Up / Down to change channel time.");
  57. canvas_draw_str(canvas, 1, 44, "Left / Right to select channel");
  58. canvas_draw_str(canvas, 1, 56, " to get it's frequency");
  59. }
  60. //draw freq ir the progress
  61. canvas_set_font(canvas, FontSecondary);
  62. if(isScanning) {
  63. if(isInfiniteScan)
  64. canvas_draw_str(canvas, 37, 8, "scanning...");
  65. else
  66. canvas_draw_str(canvas, 37, 8, "scanning");
  67. } else {
  68. if(showFreq) {
  69. int freq = 2400 + currCh;
  70. char strfreq[10] = {0};
  71. snprintf(strfreq, sizeof(strfreq), "%d MHZ", freq);
  72. canvas_draw_str(canvas, 40, 8, strfreq);
  73. } else {
  74. //show delay
  75. int dly = delayPerChan;
  76. char strdel[10] = {0};
  77. snprintf(strdel, sizeof(strdel), "%d us", dly);
  78. canvas_draw_str(canvas, 40, 8, strdel);
  79. }
  80. }
  81. //draw the chart
  82. for(int i = 0; i < num_channels; ++i) {
  83. int h = 64 - nrf24values[i];
  84. if(h < 11) h = 12;
  85. canvas_draw_line(canvas, i, h, i, 64);
  86. }
  87. }
  88. static void input_callback(InputEvent* input_event, void* ctx) {
  89. furi_assert(ctx);
  90. FuriMessageQueue* event_queue = ctx;
  91. Event event = {.type = EventTypeKey, .input = *input_event};
  92. furi_message_queue_put(event_queue, &event, FuriWaitForever);
  93. }
  94. static int32_t scanner(void* context) {
  95. UNUSED(context);
  96. isScanning = true;
  97. stopNrfScan = false;
  98. threadStoppedsoFree = false;
  99. uint8_t tmp = 0;
  100. currCh = 0;
  101. nrf24_set_rx_mode(nrf24_HANDLE, false);
  102. nrf24_write_reg(nrf24_HANDLE, REG_EN_AA, 0x0);
  103. nrf24_write_reg(nrf24_HANDLE, REG_RF_SETUP, 0x0f);
  104. while(true) { //scan until stopped somehow
  105. if(stopNrfScan) break;
  106. for(uint8_t i = 0; i < num_channels; i++) {
  107. if(stopNrfScan) break;
  108. currCh = i;
  109. nrf24_write_reg(nrf24_HANDLE, REG_RF_CH, i);
  110. nrf24_set_rx_mode(nrf24_HANDLE, true);
  111. for(uint8_t ii = 0; ii < 3; ++ii) {
  112. nrf24_flush_rx(nrf24_HANDLE);
  113. furi_delay_us(delayPerChan);
  114. tmp = nrf24_get_rdp(nrf24_HANDLE);
  115. if(tmp > 0 && nrf24values[i] < 65) {
  116. nrf24values[i]++; //don't overrun it
  117. }
  118. if(nrf24values[i] > 50 && !isInfiniteScan) {
  119. stopNrfScan = true; //stop, bc maxed, but only when not infinite
  120. }
  121. }
  122. }
  123. furi_delay_ms(1);
  124. //for screen refresh.
  125. }
  126. //cleanup
  127. nrf24_set_idle(nrf24_HANDLE);
  128. isScanning = false;
  129. threadStoppedsoFree = true;
  130. currCh = 0;
  131. return 0;
  132. }
  133. void ChangeFreq(int delta) {
  134. currCh += delta;
  135. if(currCh > num_channels) currCh = 0;
  136. showFreq = true;
  137. }
  138. void ChangeDelay(int delta) {
  139. delayPerChan += delta;
  140. if(delayPerChan > 4000) delayPerChan = 4000;
  141. if(delayPerChan < 120) delayPerChan = 120;
  142. if(delayPerChan == 170) delayPerChan = 150; //rounding for the next
  143. showFreq = false;
  144. }
  145. // Main entry of the application
  146. int32_t nrf24channelscanner_main(void* p) {
  147. UNUSED(p);
  148. Event event;
  149. FuriMessageQueue* event_queue = furi_message_queue_alloc(8, sizeof(Event));
  150. //turn on 5v for some modules
  151. uint8_t attempts = 0;
  152. bool otg_was_enabled = furi_hal_power_is_otg_enabled();
  153. while(!furi_hal_power_is_otg_enabled() && attempts++ < 5) {
  154. furi_hal_power_enable_otg();
  155. furi_delay_ms(10);
  156. }
  157. nrf24_init();
  158. ViewPort* view_port = view_port_alloc();
  159. view_port_draw_callback_set(view_port, draw_callback, NULL);
  160. view_port_input_callback_set(view_port, input_callback, event_queue);
  161. Gui* gui = furi_record_open(RECORD_GUI);
  162. gui_add_view_port(gui, view_port, GuiLayerFullscreen);
  163. NotificationApp* notification = furi_record_open(RECORD_NOTIFICATION);
  164. while(true) {
  165. furi_check(furi_message_queue_get(event_queue, &event, FuriWaitForever) == FuriStatusOk);
  166. if(event.type == EventTypeKey) {
  167. szuz = false; //hit any button, so hide welcome screen
  168. if((event.input.type == InputTypeShort || event.input.type == InputTypeLong) &&
  169. event.input.key == InputKeyBack) {
  170. if(isScanning) {
  171. stopNrfScan = true; //if running, stop it.
  172. notification_message(notification, &sequence_blink_yellow_100);
  173. furi_thread_join(thread);
  174. furi_thread_free(thread);
  175. }
  176. break;
  177. }
  178. //isInfiniteScan
  179. if((event.input.type == InputTypeShort || event.input.type == InputTypeLong) &&
  180. event.input.key == InputKeyOk) {
  181. if(isScanning) {
  182. notification_message(notification, &sequence_blink_yellow_100);
  183. stopNrfScan = true;
  184. furi_thread_join(thread);
  185. furi_thread_free(thread);
  186. threadStoppedsoFree = false; //to prevent double free
  187. continue;
  188. }
  189. memset(nrf24values, 0, sizeof(nrf24values));
  190. if(nrf24_check_connected(nrf24_HANDLE)) {
  191. threadStoppedsoFree = false;
  192. ifNotFoundNrf = false;
  193. notification_message(notification, &sequence_blink_green_100);
  194. isInfiniteScan = (event.input.type == InputTypeLong);
  195. thread = furi_thread_alloc();
  196. furi_thread_set_name(thread, "nrfscannerth");
  197. furi_thread_set_stack_size(thread, 1024);
  198. furi_thread_set_callback(thread, scanner);
  199. furi_thread_start(thread);
  200. } else {
  201. ifNotFoundNrf = true;
  202. notification_message(notification, &sequence_error);
  203. }
  204. }
  205. //change the delay
  206. if(event.input.type == InputTypeShort && event.input.key == InputKeyUp) {
  207. ChangeDelay(50);
  208. }
  209. if(event.input.type == InputTypeShort && event.input.key == InputKeyDown) {
  210. ChangeDelay(-50);
  211. }
  212. if(!isScanning) {
  213. if(event.input.type == InputTypeLong && event.input.key == InputKeyLeft)
  214. ChangeFreq(-10);
  215. if(event.input.type == InputTypeShort && event.input.key == InputKeyLeft)
  216. ChangeFreq(-1);
  217. if(event.input.type == InputTypeLong && event.input.key == InputKeyRight)
  218. ChangeFreq(10);
  219. if(event.input.type == InputTypeShort && event.input.key == InputKeyRight)
  220. ChangeFreq(1);
  221. }
  222. }
  223. if(threadStoppedsoFree) {
  224. threadStoppedsoFree = false;
  225. furi_thread_join(thread);
  226. furi_thread_free(thread);
  227. }
  228. }
  229. nrf24_deinit();
  230. if(furi_hal_power_is_otg_enabled() && !otg_was_enabled) {
  231. furi_hal_power_disable_otg();
  232. }
  233. furi_message_queue_free(event_queue);
  234. gui_remove_view_port(gui, view_port);
  235. view_port_free(view_port);
  236. furi_record_close(RECORD_GUI);
  237. return 0;
  238. }