nrf24channelscanner.c 9.3 KB

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