nrf24channelscanner.c 8.3 KB

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