radar_scanner.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. // Created by @MatthewKuKanich for use with the RCWL-0516
  2. // Design inspired from @unixispower Wire Tester
  3. #include <furi.h>
  4. #include <furi_hal.h>
  5. #include <furi_hal_gpio.h>
  6. #include <furi_hal_power.h>
  7. #include <gui/gui.h>
  8. #include <input/input.h>
  9. #include <notification/notification.h>
  10. #include <notification/notification_messages.h>
  11. #include <gui/elements.h>
  12. #include <stdlib.h>
  13. static const uint32_t EVENT_PERIOD_MS = 10;
  14. static const float BEEP_FREQ = 1000.0f;
  15. static const float BEEP_VOL = 0.9f;
  16. static const GpioPin* const radarPin = &gpio_ext_pc3; // Pin 7
  17. bool presenceDetected = false;
  18. bool muted = false;
  19. bool active = false;
  20. static void start_feedback(NotificationApp* notifications) {
  21. // Set LED to red for detection
  22. notification_message_block(notifications, &sequence_set_only_red_255);
  23. // Set vibration
  24. notification_message_block(notifications, &sequence_double_vibro);
  25. if(!muted) {
  26. // Start beep if not muted
  27. if(furi_hal_speaker_acquire(1000)) {
  28. furi_hal_speaker_start(BEEP_FREQ, BEEP_VOL);
  29. }
  30. }
  31. }
  32. static void stop_feedback(NotificationApp* notifications) {
  33. // Clear LED
  34. notification_message_block(notifications, &sequence_reset_rgb);
  35. // Reset vibration
  36. notification_message_block(notifications, &sequence_reset_vibro);
  37. // Stop beeping
  38. if(furi_hal_speaker_is_mine()) {
  39. furi_hal_speaker_stop();
  40. furi_hal_speaker_release();
  41. }
  42. }
  43. static void draw_callback(Canvas* canvas, void* ctx) {
  44. furi_assert(ctx);
  45. canvas_clear(canvas);
  46. canvas_set_font(canvas, FontPrimary);
  47. elements_multiline_text_aligned(canvas, 64, 2, AlignCenter, AlignTop, "Microwave Radar");
  48. if(active) {
  49. elements_multiline_text_aligned(canvas, 64, 12, AlignCenter, AlignTop, "Active");
  50. } else {
  51. elements_multiline_text_aligned(canvas, 64, 12, AlignCenter, AlignTop, "On Standby");
  52. }
  53. // Display presence status
  54. if(presenceDetected) {
  55. elements_multiline_text_aligned(
  56. canvas, 64, 25, AlignCenter, AlignTop, "Presence Detected");
  57. } else {
  58. elements_multiline_text_aligned(canvas, 64, 25, AlignCenter, AlignTop, "No Presence");
  59. }
  60. if(muted) {
  61. elements_multiline_text_aligned(canvas, 64, 35, AlignCenter, AlignTop, "Muted");
  62. }
  63. canvas_set_font(canvas, FontSecondary);
  64. elements_multiline_text_aligned(
  65. canvas, 64, 45, AlignCenter, AlignTop, "RCWL-0516 :: OUT -> Pin7");
  66. elements_multiline_text_aligned(
  67. canvas, 64, 55, AlignCenter, AlignTop, "VIN -> 5v :: GND -> GND");
  68. }
  69. static void input_callback(InputEvent* input_event, void* ctx) {
  70. furi_assert(ctx);
  71. FuriMessageQueue* event_queue = ctx;
  72. furi_message_queue_put(event_queue, input_event, FuriWaitForever);
  73. }
  74. int32_t app_radar_scanner(void* p) {
  75. UNUSED(p);
  76. FuriMessageQueue* event_queue = furi_message_queue_alloc(8, sizeof(InputEvent));
  77. // I'm keeping the forced backlight as you will likely be away from Flipper
  78. NotificationApp* notifications = furi_record_open(RECORD_NOTIFICATION);
  79. notification_message_block(notifications, &sequence_display_backlight_enforce_on);
  80. ViewPort* view_port = view_port_alloc();
  81. view_port_draw_callback_set(view_port, draw_callback, view_port);
  82. view_port_input_callback_set(view_port, input_callback, event_queue);
  83. Gui* gui = furi_record_open(RECORD_GUI);
  84. gui_add_view_port(gui, view_port, GuiLayerFullscreen);
  85. view_port_update(view_port);
  86. stop_feedback(notifications);
  87. // set input to be low; RCWL-0516 outputs High (3v) on detection
  88. furi_hal_gpio_init(radarPin, GpioModeInput, GpioPullDown, GpioSpeedVeryHigh);
  89. bool alarming = false; // Sensor begins in-active until user starts
  90. bool running = true; // to prevent unwanted false positives
  91. while(running) {
  92. if(active) {
  93. // start and stop feedback if sensor state is active
  94. bool continuous = furi_hal_gpio_read(radarPin);
  95. if(continuous && !alarming) {
  96. presenceDetected = true;
  97. start_feedback(notifications);
  98. } else if(!continuous && alarming) {
  99. presenceDetected = false;
  100. stop_feedback(notifications); // Green LED if clear/no presence
  101. notification_message_block(notifications, &sequence_set_only_green_255);
  102. }
  103. alarming = continuous;
  104. }
  105. // Exit on back key
  106. InputEvent event;
  107. if(furi_message_queue_get(event_queue, &event, EVENT_PERIOD_MS) == FuriStatusOk) {
  108. if(event.type == InputTypePress) {
  109. if(event.key == InputKeyBack) {
  110. break;
  111. }
  112. if(event.key == InputKeyOk) {
  113. active = !active; // Toggle the value of 'active'
  114. stop_feedback(notifications);
  115. }
  116. if(event.key == InputKeyDown) {
  117. muted = !muted; // Toggle the value of 'muted'
  118. stop_feedback(notifications);
  119. }
  120. }
  121. }
  122. }
  123. // return control of the LED, beeper, backlight, and stop vibration
  124. stop_feedback(notifications);
  125. notification_message_block(notifications, &sequence_display_backlight_enforce_auto);
  126. view_port_enabled_set(view_port, false);
  127. gui_remove_view_port(gui, view_port);
  128. view_port_free(view_port);
  129. furi_message_queue_free(event_queue);
  130. furi_record_close(RECORD_GUI);
  131. furi_record_close(RECORD_NOTIFICATION);
  132. return 0;
  133. }