radar_scanner.c 5.5 KB

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