nrf24channelscanner.c 8.7 KB

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