rcwl_0516.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. #include "rcwl_0516.h"
  2. #include <furi.h>
  3. #include <furi_hal.h>
  4. #include <gui/gui.h>
  5. #include <gui/elements.h>
  6. #include <input/input.h>
  7. #include <notification/notification_messages.h>
  8. static void rcwl_0516_draw_callback(Canvas* canvas, void* ctx) {
  9. furi_assert(ctx);
  10. Rcwl_0516* app = ctx;
  11. canvas_clear(canvas);
  12. canvas_set_font(canvas, FontPrimary);
  13. elements_multiline_text_aligned(canvas, 60, 17, AlignCenter, AlignTop, "Motion Status");
  14. canvas_set_font(canvas, FontSecondary);
  15. if(app->input_value) {
  16. furi_hal_light_set(LightRed, 255);
  17. furi_hal_light_set(LightBlue, 0);
  18. furi_hal_light_set(LightGreen, 0);
  19. elements_multiline_text_aligned(canvas, 60, 32, AlignCenter, AlignTop, "Motion Detection");
  20. notification_message(app->notifications, &sequence_display_backlight_on);
  21. } else {
  22. elements_multiline_text_aligned(canvas, 60, 32, AlignCenter, AlignTop, "No Motion");
  23. notification_message(app->notifications, &sequence_display_backlight_off);
  24. furi_hal_light_set(LightBlue, 255);
  25. furi_hal_light_set(LightRed, 0);
  26. furi_hal_light_set(LightGreen, 0);
  27. }
  28. }
  29. static void rcwl_0516_input_callback(InputEvent* input_event, void* ctx) {
  30. furi_assert(ctx);
  31. FuriMessageQueue* event_queue = ctx;
  32. furi_message_queue_put(event_queue, input_event, FuriWaitForever);
  33. }
  34. Rcwl_0516* rcwl_0516_alloc() {
  35. Rcwl_0516* app = malloc(sizeof(Rcwl_0516));
  36. app->view_port = view_port_alloc();
  37. app->event_queue = furi_message_queue_alloc(8, sizeof(InputEvent));
  38. view_port_draw_callback_set(app->view_port, rcwl_0516_draw_callback, app);
  39. view_port_input_callback_set(app->view_port, rcwl_0516_input_callback, app->event_queue);
  40. app->gui = furi_record_open(RECORD_GUI);
  41. gui_add_view_port(app->gui, app->view_port, GuiLayerFullscreen);
  42. app->notifications = furi_record_open(RECORD_NOTIFICATION);
  43. app->input_pin = &gpio_ext_pa7;
  44. app->output_pin = &gpio_ext_pa6;
  45. furi_hal_gpio_init(app->input_pin, GpioModeInput, GpioPullUp, GpioSpeedLow);
  46. furi_hal_gpio_init(app->output_pin, GpioModeOutputPushPull, GpioPullNo, GpioSpeedVeryHigh);
  47. furi_hal_gpio_write(app->output_pin, false);
  48. return app;
  49. }
  50. void rcwl_0516_free(Rcwl_0516* app) {
  51. furi_assert(app);
  52. view_port_enabled_set(app->view_port, false);
  53. gui_remove_view_port(app->gui, app->view_port);
  54. view_port_free(app->view_port);
  55. furi_message_queue_free(app->event_queue);
  56. furi_record_close(RECORD_GUI);
  57. furi_record_close(RECORD_NOTIFICATION);
  58. furi_hal_light_set(LightBlue, 0);
  59. furi_hal_light_set(LightRed, 0);
  60. furi_hal_light_set(LightGreen, 0);
  61. }
  62. int32_t rcwl_0516(void* p) {
  63. UNUSED(p);
  64. Rcwl_0516* app = rcwl_0516_alloc();
  65. InputEvent event;
  66. while(1) {
  67. app->input_value = furi_hal_gpio_read(app->input_pin);
  68. if(furi_message_queue_get(app->event_queue, &event, 100) == FuriStatusOk) {
  69. if(event.key == InputKeyBack) {
  70. if(event.type == InputTypePress) break;
  71. }
  72. }
  73. }
  74. rcwl_0516_free(app);
  75. return 0;
  76. }