radar_scanner.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. canvas_set_font(canvas, FontBatteryPercent);
  65. if(altPinout) {
  66. elements_multiline_text_aligned(
  67. canvas, 64, 42, AlignCenter, AlignTop, "Alt-Pinout Enabled");
  68. elements_multiline_text_aligned(
  69. canvas, 64, 49, AlignCenter, AlignTop, "VIN -> 5v :: GND -> Pin 3");
  70. elements_multiline_text_aligned(
  71. canvas, 64, 56, AlignCenter, AlignTop, "OUT -> Pin 2 (A7)");
  72. } else if(!altPinout) {
  73. elements_multiline_text_aligned(
  74. canvas, 64, 42, AlignCenter, AlignTop, "Alt-Pinout Disabled");
  75. elements_multiline_text_aligned(
  76. canvas, 64, 49, AlignCenter, AlignTop, "VIN -> 5v :: GND -> GND");
  77. elements_multiline_text_aligned(
  78. canvas, 64, 56, AlignCenter, AlignTop, "OUT -> Pin 7 (C3)");
  79. }
  80. }
  81. static void input_callback(InputEvent* input_event, void* ctx) {
  82. furi_assert(ctx);
  83. FuriMessageQueue* event_queue = ctx;
  84. furi_message_queue_put(event_queue, input_event, FuriWaitForever);
  85. }
  86. static void get_reading() {
  87. if(altPinout) {
  88. continuous = furi_hal_gpio_read(altRadarPin);
  89. } else {
  90. continuous = furi_hal_gpio_read(radarPin);
  91. }
  92. }
  93. int32_t app_radar_scanner(void* p) {
  94. UNUSED(p);
  95. FuriMessageQueue* event_queue = furi_message_queue_alloc(8, sizeof(InputEvent));
  96. // I'm keeping the forced backlight as you will likely be away from Flipper
  97. NotificationApp* notifications = furi_record_open(RECORD_NOTIFICATION);
  98. notification_message_block(notifications, &sequence_display_backlight_enforce_on);
  99. ViewPort* view_port = view_port_alloc();
  100. view_port_draw_callback_set(view_port, draw_callback, view_port);
  101. view_port_input_callback_set(view_port, input_callback, event_queue);
  102. Gui* gui = furi_record_open(RECORD_GUI);
  103. gui_add_view_port(gui, view_port, GuiLayerFullscreen);
  104. view_port_update(view_port);
  105. stop_feedback(notifications);
  106. // set input to be low; RCWL-0516 outputs High (3v) on detection
  107. furi_hal_gpio_init(radarPin, GpioModeInput, GpioPullDown, GpioSpeedVeryHigh);
  108. furi_hal_gpio_init(altRadarPin, GpioModeInput, GpioPullDown, GpioSpeedVeryHigh);
  109. furi_hal_gpio_init(altGroundPin, GpioModeOutputPushPull, GpioPullNo, GpioSpeedVeryHigh);
  110. furi_hal_gpio_write(altGroundPin, false);
  111. // Auto 5v- Thanks Willy!!
  112. uint8_t attempts = 0;
  113. bool otg_was_enabled = furi_hal_power_is_otg_enabled();
  114. while(!furi_hal_power_is_otg_enabled() && attempts++ < 5) {
  115. furi_hal_power_enable_otg();
  116. furi_delay_ms(10);
  117. }
  118. bool alarming = false; // Sensor begins in-active until user starts
  119. bool running = true; // to prevent unwanted false positives
  120. while(running) {
  121. if(active) {
  122. // start and stop feedback if sensor state is active
  123. get_reading();
  124. if(continuous && !alarming) {
  125. presenceDetected = true;
  126. start_feedback(notifications);
  127. } else if(!continuous && alarming) {
  128. presenceDetected = false;
  129. stop_feedback(notifications); // Green LED if clear/no presence
  130. notification_message_block(notifications, &sequence_set_only_green_255);
  131. }
  132. alarming = continuous;
  133. }
  134. // Exit on back key
  135. InputEvent event;
  136. if(furi_message_queue_get(event_queue, &event, EVENT_PERIOD_MS) == FuriStatusOk) {
  137. if(event.type == InputTypePress) {
  138. if(event.key == InputKeyBack) {
  139. break;
  140. }
  141. if(event.key == InputKeyOk) {
  142. active = !active; // Toggle the value of 'active'
  143. stop_feedback(notifications);
  144. }
  145. if(event.key == InputKeyDown) {
  146. muted = !muted; // Toggle the value of 'muted'
  147. stop_feedback(notifications);
  148. }
  149. if(event.key == InputKeyRight) {
  150. altPinout = !altPinout; // Toggle alternate pinout
  151. }
  152. }
  153. }
  154. }
  155. // return control of the LED, beeper, backlight, and stop vibration
  156. stop_feedback(notifications);
  157. notification_message_block(notifications, &sequence_display_backlight_enforce_auto);
  158. // Disable 5v power
  159. if(furi_hal_power_is_otg_enabled() && !otg_was_enabled) {
  160. furi_hal_power_disable_otg();
  161. }
  162. view_port_enabled_set(view_port, false);
  163. gui_remove_view_port(gui, view_port);
  164. view_port_free(view_port);
  165. furi_message_queue_free(event_queue);
  166. furi_record_close(RECORD_GUI);
  167. furi_record_close(RECORD_NOTIFICATION);
  168. return 0;
  169. }