radar_scanner.c 5.3 KB

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