radar_scanner.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. // Created by @MatthewKuKanich for use with the RCWL-0516
  2. // Design inspired from @unixispower Wire Tester
  3. #include <furi_hal.h>
  4. #include <gui/gui.h>
  5. #include <notification/notification_messages.h>
  6. #include <gui/elements.h>
  7. static const uint32_t EVENT_PERIOD_MS = 10;
  8. static const float BEEP_FREQ = 1000.0f;
  9. static const float BEEP_VOL = 0.9f;
  10. static const GpioPin* const radarPin = &gpio_ext_pc3; // Pin 7
  11. static const GpioPin* const altRadarPin = &gpio_ext_pa7; // Pin 2
  12. static const GpioPin* const altGroundPin = &gpio_ext_pa6; // Pin 3
  13. bool presenceDetected = false;
  14. bool muted = false;
  15. bool active = false;
  16. bool continuous = false; // Start with no signal from OUT
  17. bool altPinout; // Sets which GPIO pinout config to use
  18. static void start_feedback(NotificationApp* notifications) {
  19. // Set LED to red for detection
  20. notification_message_block(notifications, &sequence_set_only_red_255);
  21. // Set vibration
  22. notification_message_block(notifications, &sequence_double_vibro);
  23. if(!muted) {
  24. // Start beep if not muted
  25. if(furi_hal_speaker_acquire(1000)) {
  26. furi_hal_speaker_start(BEEP_FREQ, BEEP_VOL);
  27. }
  28. }
  29. }
  30. static void stop_feedback(NotificationApp* notifications) {
  31. // Clear LED
  32. notification_message_block(notifications, &sequence_reset_rgb);
  33. // Reset vibration
  34. notification_message_block(notifications, &sequence_reset_vibro);
  35. // Stop beeping
  36. if(furi_hal_speaker_is_mine()) {
  37. furi_hal_speaker_stop();
  38. furi_hal_speaker_release();
  39. }
  40. }
  41. static void draw_callback(Canvas* canvas, void* ctx) {
  42. furi_assert(ctx);
  43. canvas_clear(canvas);
  44. canvas_set_font(canvas, FontPrimary);
  45. elements_multiline_text_aligned(canvas, 64, 2, AlignCenter, AlignTop, "Microwave Radar");
  46. canvas_set_font(canvas, FontSecondary);
  47. if(active) {
  48. elements_multiline_text_aligned(canvas, 64, 12, AlignCenter, AlignTop, "Active");
  49. } else {
  50. elements_multiline_text_aligned(canvas, 64, 12, AlignCenter, AlignTop, "On Standby");
  51. }
  52. // Display presence status
  53. canvas_set_font(canvas, FontPrimary);
  54. if(presenceDetected) {
  55. elements_multiline_text_aligned(
  56. canvas, 64, 20, AlignCenter, AlignTop, "Presence Detected");
  57. } else {
  58. elements_multiline_text_aligned(canvas, 64, 20, AlignCenter, AlignTop, "No Presence");
  59. }
  60. canvas_set_font(canvas, FontSecondary);
  61. if(muted) {
  62. elements_multiline_text_aligned(canvas, 64, 32, AlignCenter, AlignTop, "Muted");
  63. }
  64. #ifdef FW_ORIGIN_Official
  65. canvas_set_font(canvas, FontKeyboard);
  66. if(altPinout) {
  67. elements_multiline_text_aligned(
  68. canvas, 64, 42, AlignCenter, AlignTop, "Alt-PinoutEnabled");
  69. elements_multiline_text_aligned(canvas, 64, 49, AlignCenter, AlignTop, "GND -> Pin 3");
  70. elements_multiline_text_aligned(canvas, 64, 56, AlignCenter, AlignTop, "OUT -> Pin 2(A7)");
  71. } else if(!altPinout) {
  72. elements_multiline_text_aligned(
  73. canvas, 64, 42, AlignCenter, AlignTop, "Alt-PinoutDisabled");
  74. elements_multiline_text_aligned(canvas, 64, 49, AlignCenter, AlignTop, "GND -> GND");
  75. elements_multiline_text_aligned(canvas, 64, 56, AlignCenter, AlignTop, "OUT -> Pin 7(C3)");
  76. }
  77. #else
  78. canvas_set_font(canvas, FontBatteryPercent);
  79. if(altPinout) {
  80. elements_multiline_text_aligned(
  81. canvas, 64, 42, AlignCenter, AlignTop, "Alt-Pinout Enabled");
  82. elements_multiline_text_aligned(
  83. canvas, 64, 49, AlignCenter, AlignTop, "VIN -> 5v :: GND -> Pin 3");
  84. elements_multiline_text_aligned(
  85. canvas, 64, 56, AlignCenter, AlignTop, "OUT -> Pin 2 (A7)");
  86. } else if(!altPinout) {
  87. elements_multiline_text_aligned(
  88. canvas, 64, 42, AlignCenter, AlignTop, "Alt-Pinout Disabled");
  89. elements_multiline_text_aligned(
  90. canvas, 64, 49, AlignCenter, AlignTop, "VIN -> 5v :: GND -> GND");
  91. elements_multiline_text_aligned(
  92. canvas, 64, 56, AlignCenter, AlignTop, "OUT -> Pin 7 (C3)");
  93. }
  94. #endif
  95. }
  96. static void input_callback(InputEvent* input_event, void* ctx) {
  97. furi_assert(ctx);
  98. FuriMessageQueue* event_queue = ctx;
  99. furi_message_queue_put(event_queue, input_event, FuriWaitForever);
  100. }
  101. static void get_reading() {
  102. if(altPinout) {
  103. continuous = furi_hal_gpio_read(altRadarPin);
  104. } else {
  105. continuous = furi_hal_gpio_read(radarPin);
  106. }
  107. }
  108. int32_t app_radar_scanner(void* p) {
  109. UNUSED(p);
  110. FuriMessageQueue* event_queue = furi_message_queue_alloc(8, sizeof(InputEvent));
  111. // I'm keeping the forced backlight as you will likely be away from Flipper
  112. NotificationApp* notifications = furi_record_open(RECORD_NOTIFICATION);
  113. notification_message_block(notifications, &sequence_display_backlight_enforce_on);
  114. ViewPort* view_port = view_port_alloc();
  115. view_port_draw_callback_set(view_port, draw_callback, view_port);
  116. view_port_input_callback_set(view_port, input_callback, event_queue);
  117. Gui* gui = furi_record_open(RECORD_GUI);
  118. gui_add_view_port(gui, view_port, GuiLayerFullscreen);
  119. view_port_update(view_port);
  120. stop_feedback(notifications);
  121. // set input to be low; RCWL-0516 outputs High (3v) on detection
  122. furi_hal_gpio_init(radarPin, GpioModeInput, GpioPullDown, GpioSpeedVeryHigh);
  123. furi_hal_gpio_init(altRadarPin, GpioModeInput, GpioPullDown, GpioSpeedVeryHigh);
  124. furi_hal_gpio_init(altGroundPin, GpioModeOutputPushPull, GpioPullNo, GpioSpeedVeryHigh);
  125. furi_hal_gpio_write(altGroundPin, false);
  126. // Auto 5v power
  127. uint8_t attempts = 0;
  128. bool otg_was_enabled = furi_hal_power_is_otg_enabled();
  129. while(!furi_hal_power_is_otg_enabled() && attempts++ < 5) {
  130. furi_hal_power_enable_otg();
  131. furi_delay_ms(10);
  132. }
  133. bool alarming = false; // Sensor begins in-active until user starts
  134. bool running = true; // to prevent unwanted false positives
  135. while(running) {
  136. if(active) {
  137. // start and stop feedback if sensor state is active
  138. get_reading();
  139. if(continuous && !alarming) {
  140. presenceDetected = true;
  141. start_feedback(notifications);
  142. } else if(!continuous && alarming) {
  143. presenceDetected = false;
  144. stop_feedback(notifications); // Green LED if clear/no presence
  145. notification_message_block(notifications, &sequence_set_only_green_255);
  146. }
  147. alarming = continuous;
  148. }
  149. // Exit on back key
  150. InputEvent event;
  151. if(furi_message_queue_get(event_queue, &event, EVENT_PERIOD_MS) == FuriStatusOk) {
  152. if(event.type == InputTypePress) {
  153. if(event.key == InputKeyBack) {
  154. break;
  155. }
  156. if(event.key == InputKeyOk) {
  157. active = !active; // Toggle the value of 'active'
  158. stop_feedback(notifications);
  159. }
  160. if(event.key == InputKeyDown) {
  161. muted = !muted; // Toggle the value of 'muted'
  162. stop_feedback(notifications);
  163. }
  164. if(event.key == InputKeyRight) {
  165. altPinout = !altPinout; // Toggle alternate pinout
  166. }
  167. }
  168. }
  169. }
  170. // return control of the LED, beeper, backlight, and stop vibration
  171. stop_feedback(notifications);
  172. notification_message_block(notifications, &sequence_display_backlight_enforce_auto);
  173. // Disable 5v power
  174. if(furi_hal_power_is_otg_enabled() && !otg_was_enabled) {
  175. furi_hal_power_disable_otg();
  176. }
  177. view_port_enabled_set(view_port, false);
  178. gui_remove_view_port(gui, view_port);
  179. view_port_free(view_port);
  180. furi_message_queue_free(event_queue);
  181. furi_record_close(RECORD_GUI);
  182. furi_record_close(RECORD_NOTIFICATION);
  183. return 0;
  184. }