nrf24channelscanner.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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] = {0};
  70. snprintf(strfreq, sizeof(strfreq), "%d MHZ", freq);
  71. canvas_draw_str(canvas, 40, 8, strfreq);
  72. } else {
  73. //show delay
  74. int dly = delayPerChan;
  75. char strdel[10] = {0};
  76. snprintf(strdel, sizeof(strdel), "%d us", dly);
  77. canvas_draw_str(canvas, 40, 8, strdel);
  78. }
  79. }
  80. //draw the chart
  81. for(int i = 0; i < num_channels; ++i) {
  82. int h = 64 - nrf24values[i];
  83. if(h < 11) h = 12;
  84. canvas_draw_line(canvas, i, h, i, 64);
  85. }
  86. }
  87. static void input_callback(InputEvent* input_event, void* ctx) {
  88. furi_assert(ctx);
  89. FuriMessageQueue* event_queue = ctx;
  90. Event event = {.type = EventTypeKey, .input = *input_event};
  91. furi_message_queue_put(event_queue, &event, FuriWaitForever);
  92. }
  93. static int32_t scanner(void* context) {
  94. UNUSED(context);
  95. isScanning = true;
  96. stopNrfScan = false;
  97. threadStoppedsoFree = false;
  98. uint8_t tmp = 0;
  99. currCh = 0;
  100. nrf24_set_rx_mode(nrf24_HANDLE, false);
  101. nrf24_write_reg(nrf24_HANDLE, REG_EN_AA, 0x0);
  102. nrf24_write_reg(nrf24_HANDLE, REG_RF_SETUP, 0x0f);
  103. while(true) { //scan until stopped somehow
  104. if(stopNrfScan) break;
  105. for(uint8_t i = 0; i < num_channels; i++) {
  106. if(stopNrfScan) break;
  107. currCh = i;
  108. nrf24_write_reg(nrf24_HANDLE, REG_RF_CH, i);
  109. nrf24_set_rx_mode(nrf24_HANDLE, true);
  110. for(uint8_t ii = 0; ii < 3; ++ii) {
  111. nrf24_flush_rx(nrf24_HANDLE);
  112. furi_delay_us(delayPerChan);
  113. tmp = nrf24_get_rdp(nrf24_HANDLE);
  114. if(tmp > 0 && nrf24values[i] < 65) {
  115. nrf24values[i]++; //don't overrun it
  116. }
  117. if(nrf24values[i] > 50 && !isInfiniteScan) {
  118. stopNrfScan = true; //stop, bc maxed, but only when not infinite
  119. }
  120. }
  121. }
  122. furi_delay_ms(1);
  123. //for screen refresh.
  124. }
  125. //cleanup
  126. nrf24_set_idle(nrf24_HANDLE);
  127. isScanning = false;
  128. threadStoppedsoFree = true;
  129. currCh = 0;
  130. return 0;
  131. }
  132. void ChangeFreq(int delta) {
  133. currCh += delta;
  134. if(currCh > num_channels) currCh = 0;
  135. showFreq = true;
  136. }
  137. void ChangeDelay(int delta) {
  138. delayPerChan += delta;
  139. if(delayPerChan > 4000) delayPerChan = 4000;
  140. if(delayPerChan < 120) delayPerChan = 120;
  141. if(delayPerChan == 170) delayPerChan = 150; //rounding for the next
  142. showFreq = false;
  143. }
  144. // Main entry of the application
  145. int32_t nrf24channelscanner_main(void* p) {
  146. UNUSED(p);
  147. Event event;
  148. FuriMessageQueue* event_queue = furi_message_queue_alloc(8, sizeof(Event));
  149. //turn on 5v for some modules
  150. uint8_t attempts = 0;
  151. while(!furi_hal_power_is_otg_enabled() && attempts++ < 5) {
  152. furi_hal_power_enable_otg();
  153. furi_delay_ms(10);
  154. }
  155. nrf24_init();
  156. ViewPort* view_port = view_port_alloc();
  157. view_port_draw_callback_set(view_port, draw_callback, NULL);
  158. view_port_input_callback_set(view_port, input_callback, event_queue);
  159. Gui* gui = furi_record_open(RECORD_GUI);
  160. gui_add_view_port(gui, view_port, GuiLayerFullscreen);
  161. NotificationApp* notification = furi_record_open(RECORD_NOTIFICATION);
  162. while(true) {
  163. furi_check(furi_message_queue_get(event_queue, &event, FuriWaitForever) == FuriStatusOk);
  164. if(event.type == EventTypeKey) {
  165. szuz = false; //hit any button, so hide welcome screen
  166. if(event.input.type == InputTypeLong && event.input.key == InputKeyBack) {
  167. if(isScanning) {
  168. stopNrfScan = true; //if running, stop it.
  169. notification_message(notification, &sequence_blink_yellow_100);
  170. furi_thread_join(thread);
  171. furi_thread_free(thread);
  172. }
  173. break;
  174. }
  175. //isInfiniteScan
  176. if((event.input.type == InputTypeShort || event.input.type == InputTypeLong) &&
  177. event.input.key == InputKeyOk) {
  178. if(isScanning) {
  179. notification_message(notification, &sequence_blink_yellow_100);
  180. stopNrfScan = true;
  181. furi_thread_join(thread);
  182. furi_thread_free(thread);
  183. threadStoppedsoFree = false; //to prevent double free
  184. continue;
  185. }
  186. memset(nrf24values, 0, sizeof(nrf24values));
  187. if(nrf24_check_connected(nrf24_HANDLE)) {
  188. threadStoppedsoFree = false;
  189. ifNotFoundNrf = false;
  190. notification_message(notification, &sequence_blink_green_100);
  191. isInfiniteScan = (event.input.type == InputTypeLong);
  192. thread = furi_thread_alloc();
  193. furi_thread_set_name(thread, "nrfscannerth");
  194. furi_thread_set_stack_size(thread, 1024);
  195. furi_thread_set_callback(thread, scanner);
  196. furi_thread_start(thread);
  197. } else {
  198. ifNotFoundNrf = true;
  199. notification_message(notification, &sequence_error);
  200. }
  201. }
  202. //change the delay
  203. if(event.input.type == InputTypeShort && event.input.key == InputKeyUp) {
  204. ChangeDelay(50);
  205. }
  206. if(event.input.type == InputTypeShort && event.input.key == InputKeyDown) {
  207. ChangeDelay(-50);
  208. }
  209. if(!isScanning) {
  210. if(event.input.type == InputTypeLong && event.input.key == InputKeyLeft)
  211. ChangeFreq(-10);
  212. if(event.input.type == InputTypeShort && event.input.key == InputKeyLeft)
  213. ChangeFreq(-1);
  214. if(event.input.type == InputTypeLong && event.input.key == InputKeyRight)
  215. ChangeFreq(10);
  216. if(event.input.type == InputTypeShort && event.input.key == InputKeyRight)
  217. ChangeFreq(1);
  218. }
  219. }
  220. if(threadStoppedsoFree) {
  221. threadStoppedsoFree = false;
  222. furi_thread_join(thread);
  223. furi_thread_free(thread);
  224. }
  225. }
  226. nrf24_deinit();
  227. furi_message_queue_free(event_queue);
  228. gui_remove_view_port(gui, view_port);
  229. view_port_free(view_port);
  230. furi_record_close(RECORD_GUI);
  231. //turn off 5v
  232. if(furi_hal_power_is_otg_enabled()) {
  233. furi_hal_power_disable_otg();
  234. }
  235. return 0;
  236. }