rcwl_0516.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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(
  20. canvas, 60, 32, AlignCenter, AlignTop,"Motion Detection");
  21. notification_message(app->notifications, &sequence_display_backlight_on);
  22. }else {
  23. elements_multiline_text_aligned(
  24. canvas, 60, 32, AlignCenter, AlignTop,"No Motion");
  25. notification_message(app->notifications, &sequence_display_backlight_off);
  26. furi_hal_light_set(LightBlue, 255);
  27. furi_hal_light_set(LightRed, 0);
  28. furi_hal_light_set(LightGreen, 0);
  29. }
  30. }
  31. static void rcwl_0516_input_callback(InputEvent* input_event, void* ctx) {
  32. furi_assert(ctx);
  33. FuriMessageQueue* event_queue = ctx;
  34. furi_message_queue_put(event_queue, input_event, FuriWaitForever);
  35. }
  36. Rcwl_0516* rcwl_0516_alloc() {
  37. Rcwl_0516* app = malloc(sizeof(Rcwl_0516));
  38. app->view_port = view_port_alloc();
  39. app->event_queue = furi_message_queue_alloc(8, sizeof(InputEvent));
  40. view_port_draw_callback_set(app->view_port, rcwl_0516_draw_callback, app);
  41. view_port_input_callback_set(app->view_port, rcwl_0516_input_callback, app->event_queue);
  42. app->gui = furi_record_open(RECORD_GUI);
  43. gui_add_view_port(app->gui, app->view_port, GuiLayerFullscreen);
  44. app->notifications = furi_record_open(RECORD_NOTIFICATION);
  45. app->input_pin = &gpio_ext_pa7;
  46. app->output_pin = &gpio_ext_pa6;
  47. furi_hal_gpio_init(app->input_pin, GpioModeInput, GpioPullUp, GpioSpeedLow);
  48. furi_hal_gpio_init(app->output_pin, GpioModeOutputPushPull, GpioPullNo, GpioSpeedVeryHigh);
  49. furi_hal_gpio_write(app->output_pin, false);
  50. return app;
  51. }
  52. void rcwl_0516_free(Rcwl_0516* app) {
  53. furi_assert(app);
  54. view_port_enabled_set(app->view_port, false);
  55. gui_remove_view_port(app->gui, app->view_port);
  56. view_port_free(app->view_port);
  57. furi_message_queue_free(app->event_queue);
  58. furi_record_close(RECORD_GUI);
  59. furi_record_close(RECORD_NOTIFICATION);
  60. furi_hal_light_set(LightBlue, 0);
  61. furi_hal_light_set(LightRed, 0);
  62. furi_hal_light_set(LightGreen, 0);
  63. }
  64. int32_t rcwl_0516(void* p) {
  65. UNUSED(p);
  66. Rcwl_0516* app = rcwl_0516_alloc();
  67. InputEvent event;
  68. while(1) {
  69. app->input_value = furi_hal_gpio_read(app->input_pin);
  70. if(furi_message_queue_get(app->event_queue, &event, 100) == FuriStatusOk) {
  71. if(event.key == InputKeyBack) {
  72. if(event.type == InputTypePress) break;
  73. }
  74. }
  75. }
  76. rcwl_0516_free(app);
  77. return 0;
  78. }