send_view.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. #include "send_view.h"
  2. #include <furi.h>
  3. #include <gui/elements.h>
  4. #include <notification/notification.h>
  5. #include <notification/notification_messages.h>
  6. #include <furi_hal_uart.h>
  7. #define MODULE_CONTROL_COMMAND_SEND 's'
  8. #define FLIPPERZERO_SERIAL_BAUD 115200
  9. typedef enum ESerialCommand
  10. {
  11. ESerialCommand_Send
  12. } ESerialCommand;
  13. struct SendView {
  14. View* view;
  15. };
  16. typedef struct {
  17. bool right_pressed;
  18. bool connected;
  19. } SendViewModel;
  20. static void Shake(void) {
  21. NotificationApp* notification = furi_record_open(RECORD_NOTIFICATION);
  22. notification_message(notification, &sequence_single_vibro);
  23. furi_record_close(RECORD_NOTIFICATION);
  24. }
  25. void send_serial_command_send(ESerialCommand command)
  26. {
  27. uint8_t data[1] = { 0 };
  28. switch(command)
  29. {
  30. case ESerialCommand_Send:
  31. data[0] = MODULE_CONTROL_COMMAND_SEND;
  32. break;
  33. default:
  34. return;
  35. };
  36. furi_hal_uart_tx(FuriHalUartIdUSART1, data, 1);
  37. }
  38. static void send_view_draw_callback(Canvas* canvas, void* context) {
  39. furi_assert(context);
  40. SendViewModel* model = context;
  41. canvas_clear(canvas);
  42. canvas_set_color(canvas, ColorBlack);
  43. canvas_draw_str_aligned(canvas, 64, 0, AlignCenter, AlignTop, "SEND MODULE");
  44. canvas_draw_line(canvas, 0, 10, 128, 10);
  45. canvas_draw_str_aligned(canvas, 64, 15, AlignCenter, AlignTop, "Press right to send IFTTT");
  46. canvas_draw_str_aligned(canvas, 64, 25, AlignCenter, AlignTop, "command or press and hold");
  47. canvas_draw_str_aligned(canvas, 64, 35, AlignCenter, AlignTop, "back to return to the menu");
  48. // Right
  49. if(model->right_pressed) {
  50. }
  51. }
  52. static void send_view_process(SendView* send_view, InputEvent* event) {
  53. with_view_model(
  54. send_view->view, (SendViewModel * model) {
  55. if(event->type == InputTypePress) {
  56. if(event->key == InputKeyUp) {
  57. } else if(event->key == InputKeyDown) {
  58. } else if(event->key == InputKeyLeft) {
  59. } else if(event->key == InputKeyRight) {
  60. model->right_pressed = true;
  61. Shake();
  62. send_serial_command_send(ESerialCommand_Send);
  63. } else if(event->key == InputKeyOk) {
  64. } else if(event->key == InputKeyBack) {
  65. }
  66. } else if(event->type == InputTypeRelease) {
  67. if(event->key == InputKeyUp) {
  68. } else if(event->key == InputKeyDown) {
  69. } else if(event->key == InputKeyLeft) {
  70. } else if(event->key == InputKeyRight) {
  71. model->right_pressed = false;
  72. } else if(event->key == InputKeyOk) {
  73. } else if(event->key == InputKeyBack) {
  74. }
  75. } else if(event->type == InputTypeShort) {
  76. if(event->key == InputKeyBack) {
  77. }
  78. }
  79. return true;
  80. });
  81. }
  82. static bool send_view_input_callback(InputEvent* event, void* context) {
  83. furi_assert(context);
  84. SendView* send_view = context;
  85. bool consumed = false;
  86. if(event->type == InputTypeLong && event->key == InputKeyBack) {
  87. } else {
  88. send_view_process(send_view, event);
  89. consumed = true;
  90. }
  91. return consumed;
  92. }
  93. SendView* send_view_alloc() {
  94. SendView* send_view = malloc(sizeof(SendView));
  95. send_view->view = view_alloc();
  96. view_set_context(send_view->view, send_view);
  97. view_allocate_model(send_view->view, ViewModelTypeLocking, sizeof(SendViewModel));
  98. view_set_draw_callback(send_view->view, send_view_draw_callback);
  99. view_set_input_callback(send_view->view, send_view_input_callback);
  100. furi_hal_uart_set_br(FuriHalUartIdUSART1, FLIPPERZERO_SERIAL_BAUD);
  101. return send_view;
  102. }
  103. void send_view_free(SendView* send_view) {
  104. furi_assert(send_view);
  105. view_free(send_view->view);
  106. free(send_view);
  107. }
  108. View* send_view_get_view(SendView* send_view) {
  109. furi_assert(send_view);
  110. return send_view->view;
  111. }
  112. void send_view_set_data(SendView* send_view, bool connected) {
  113. furi_assert(send_view);
  114. with_view_model(
  115. send_view->view, (SendViewModel * model) {
  116. model->connected = connected;
  117. return true;
  118. });
  119. }