nrf24channelscanner.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. FuriThread* thread;
  18. typedef enum {
  19. EventTypeKey,
  20. EventTypeTick,
  21. } EventType;
  22. typedef struct {
  23. EventType type;
  24. InputEvent input;
  25. } Event;
  26. static void draw_callback(Canvas* canvas, void* ctx) {
  27. UNUSED(ctx);
  28. canvas_clear(canvas);
  29. canvas_set_bitmap_mode(canvas, 1);
  30. canvas_draw_icon(canvas, 100, 0, &I_Pin_back_arrow_10x8);
  31. canvas_set_font(canvas, FontSecondary);
  32. canvas_draw_str(canvas, 112, 8, "Exit");
  33. canvas_draw_icon(canvas, 1, 0, &I_Ok_btn_9x9);
  34. canvas_set_font(canvas, FontSecondary);
  35. if(isScanning) {
  36. canvas_draw_str(canvas, 12, 8, "Stop");
  37. } else {
  38. canvas_draw_str(canvas, 12, 8, "Scan");
  39. }
  40. canvas_draw_line(canvas, 0, 11, 127, 11);
  41. if(ifNotFoundNrf) {
  42. canvas_set_font(canvas, FontPrimary);
  43. canvas_draw_str(canvas, 23, 35, "NRF24 not found!");
  44. return;
  45. }
  46. canvas_draw_line(canvas, currCh, 12, currCh, 13); //draw the current channel
  47. //draw hello mesage
  48. if(szuz) {
  49. canvas_set_font(canvas, FontSecondary);
  50. canvas_draw_str(canvas, 1, 36, "Left / right to select channel,");
  51. canvas_set_font(canvas, FontSecondary);
  52. canvas_draw_str(canvas, 1, 48, "to get it's frequency");
  53. }
  54. //draw freq ir the progress
  55. canvas_set_font(canvas, FontSecondary);
  56. if(isScanning) {
  57. canvas_draw_str(canvas, 37, 8, "scanning");
  58. } else {
  59. int freq = 2400 + currCh;
  60. char strfreq[10] = {32};
  61. itoa(freq, strfreq, 10);
  62. strfreq[4] = ' ';
  63. strfreq[5] = 'M';
  64. strfreq[6] = 'H';
  65. strfreq[7] = 'Z';
  66. strfreq[8] = 0;
  67. canvas_draw_str(canvas, 40, 8, strfreq);
  68. }
  69. //draw the chart
  70. for(int i = 0; i < num_channels; ++i) {
  71. int h = 64 - nrf24values[i];
  72. if(h < 11) h = 12;
  73. canvas_draw_line(canvas, i, h, i, 64);
  74. }
  75. }
  76. static void input_callback(InputEvent* input_event, void* ctx) {
  77. furi_assert(ctx);
  78. FuriMessageQueue* event_queue = ctx;
  79. Event event = {.type = EventTypeKey, .input = *input_event};
  80. furi_message_queue_put(event_queue, &event, FuriWaitForever);
  81. }
  82. static int32_t scanner(void* context) {
  83. UNUSED(context);
  84. isScanning = true;
  85. stopNrfScan = false;
  86. threadStoppedsoFree = false;
  87. uint8_t tmp = 0;
  88. currCh = 0;
  89. nrf24_set_rx_mode(nrf24_HANDLE, false);
  90. nrf24_write_reg(nrf24_HANDLE, REG_EN_AA, 0x0);
  91. nrf24_write_reg(nrf24_HANDLE, REG_RF_SETUP, 0x0f);
  92. for(uint8_t j = 0; j < 10; j++) {
  93. if(stopNrfScan) break;
  94. for(uint8_t i = 0; i < num_channels; i++) {
  95. if(stopNrfScan) break;
  96. currCh = i;
  97. nrf24_write_reg(nrf24_HANDLE, REG_RF_CH, i);
  98. nrf24_set_rx_mode(nrf24_HANDLE, true);
  99. for(uint8_t ii = 0; ii < 5; ++ii) {
  100. nrf24_flush_rx(nrf24_HANDLE);
  101. furi_delay_ms(5);
  102. tmp = nrf24_get_rdp(nrf24_HANDLE);
  103. if(tmp > 0) nrf24values[i]++;
  104. }
  105. }
  106. }
  107. nrf24_set_idle(nrf24_HANDLE);
  108. isScanning = false;
  109. threadStoppedsoFree = true;
  110. currCh = 0;
  111. return 0;
  112. }
  113. // Main entry of the application
  114. int32_t nrf24channelscanner_main(void* p) {
  115. UNUSED(p);
  116. Event event;
  117. FuriMessageQueue* event_queue = furi_message_queue_alloc(8, sizeof(Event));
  118. nrf24_init();
  119. ViewPort* view_port = view_port_alloc();
  120. view_port_draw_callback_set(view_port, draw_callback, NULL);
  121. view_port_input_callback_set(view_port, input_callback, event_queue);
  122. Gui* gui = furi_record_open(RECORD_GUI);
  123. gui_add_view_port(gui, view_port, GuiLayerFullscreen);
  124. NotificationApp* notification = furi_record_open(RECORD_NOTIFICATION);
  125. while(true) {
  126. furi_check(furi_message_queue_get(event_queue, &event, FuriWaitForever) == FuriStatusOk);
  127. if(event.type == EventTypeKey) {
  128. szuz = false; //hit any button, so hide welcome screen
  129. if(event.input.type == InputTypeLong && event.input.key == InputKeyBack) {
  130. if(isScanning) {
  131. stopNrfScan = true; //if running, stop it.
  132. notification_message(notification, &sequence_blink_yellow_100);
  133. furi_thread_join(thread);
  134. furi_thread_free(thread);
  135. }
  136. break;
  137. }
  138. if(event.input.type == InputTypeShort && event.input.key == InputKeyOk) {
  139. if(isScanning) {
  140. notification_message(notification, &sequence_blink_yellow_100);
  141. stopNrfScan = true;
  142. furi_thread_join(thread);
  143. furi_thread_free(thread);
  144. threadStoppedsoFree = false; //to prevent double free
  145. continue;
  146. }
  147. memset(nrf24values, 0, sizeof(nrf24values));
  148. if(nrf24_check_connected(nrf24_HANDLE)) {
  149. threadStoppedsoFree = false;
  150. ifNotFoundNrf = false;
  151. notification_message(notification, &sequence_blink_green_100);
  152. thread = furi_thread_alloc();
  153. furi_thread_set_name(thread, "nrfscannerth");
  154. furi_thread_set_stack_size(thread, 1024);
  155. furi_thread_set_callback(thread, scanner);
  156. furi_thread_start(thread);
  157. } else {
  158. ifNotFoundNrf = true;
  159. notification_message(notification, &sequence_error);
  160. }
  161. }
  162. if(!isScanning) {
  163. uint8_t num = 0;
  164. if(event.input.type == InputTypeLong) num = 10;
  165. if(event.input.type == InputTypeShort) num = 1;
  166. if(event.input.key == InputKeyLeft) {
  167. currCh -= num;
  168. }
  169. if(event.input.key == InputKeyRight) {
  170. currCh += num;
  171. }
  172. }
  173. }
  174. if(threadStoppedsoFree) {
  175. threadStoppedsoFree = false;
  176. furi_thread_join(thread);
  177. furi_thread_free(thread);
  178. }
  179. }
  180. nrf24_deinit();
  181. furi_message_queue_free(event_queue);
  182. gui_remove_view_port(gui, view_port);
  183. view_port_free(view_port);
  184. furi_record_close(RECORD_GUI);
  185. return 0;
  186. }