module_view.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. #include "module_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_CONFIG_ON 'c'
  8. #define MODULE_CONTROL_COMMAND_CONFIG_OFF 'd'
  9. #define FLIPPERZERO_SERIAL_BAUD 115200
  10. bool configState;
  11. typedef enum ESerialCommand { ESerialCommand_Config_On, ESerialCommand_Config_Off } ESerialCommand;
  12. struct ModuleView {
  13. View* view;
  14. };
  15. typedef struct {
  16. bool right_pressed;
  17. bool connected;
  18. } ModuleViewModel;
  19. /*
  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. */
  26. void send_serial_command_module(ESerialCommand command) {
  27. uint8_t data[1] = {0};
  28. switch(command) {
  29. case ESerialCommand_Config_On:
  30. data[0] = MODULE_CONTROL_COMMAND_CONFIG_ON;
  31. break;
  32. case ESerialCommand_Config_Off:
  33. data[0] = MODULE_CONTROL_COMMAND_CONFIG_OFF;
  34. break;
  35. default:
  36. return;
  37. };
  38. furi_hal_uart_tx(FuriHalUartIdUSART1, data, 1);
  39. }
  40. static void module_view_draw_callback(Canvas* canvas, void* context) {
  41. furi_assert(context);
  42. ModuleViewModel* model = context;
  43. canvas_clear(canvas);
  44. canvas_set_color(canvas, ColorBlack);
  45. canvas_draw_str_aligned(canvas, 64, 0, AlignCenter, AlignTop, "CONFIGURE MODULE");
  46. canvas_draw_line(canvas, 0, 10, 128, 10);
  47. canvas_draw_str_aligned(canvas, 64, 15, AlignCenter, AlignTop, "Press right to start the");
  48. canvas_draw_str_aligned(canvas, 64, 25, AlignCenter, AlignTop, "configurator and connect");
  49. canvas_draw_str_aligned(canvas, 64, 35, AlignCenter, AlignTop, "to ip 192.168.4.1 or press");
  50. canvas_draw_str_aligned(canvas, 64, 45, AlignCenter, AlignTop, "and hold back to return");
  51. canvas_draw_str_aligned(canvas, 64, 55, AlignCenter, AlignTop, "to the menu");
  52. if(configState == false) {
  53. send_serial_command_module(ESerialCommand_Config_On);
  54. configState = true;
  55. }
  56. // Right
  57. if(model->right_pressed) {
  58. }
  59. }
  60. static void module_view_process(ModuleView* module_view, InputEvent* event) {
  61. with_view_model(
  62. module_view->view,
  63. ModuleViewModel * model,
  64. {
  65. if(event->type == InputTypePress) {
  66. if(event->key == InputKeyUp) {
  67. } else if(event->key == InputKeyDown) {
  68. } else if(event->key == InputKeyLeft) {
  69. } else if(event->key == InputKeyRight) {
  70. model->right_pressed = true;
  71. } else if(event->key == InputKeyOk) {
  72. } else if(event->key == InputKeyBack) {
  73. }
  74. } else if(event->type == InputTypeRelease) {
  75. if(event->key == InputKeyUp) {
  76. } else if(event->key == InputKeyDown) {
  77. } else if(event->key == InputKeyLeft) {
  78. } else if(event->key == InputKeyRight) {
  79. model->right_pressed = false;
  80. } else if(event->key == InputKeyOk) {
  81. } else if(event->key == InputKeyBack) {
  82. }
  83. } else if(event->type == InputTypeShort) {
  84. if(event->key == InputKeyBack) {
  85. }
  86. }
  87. },
  88. true);
  89. }
  90. static bool module_view_input_callback(InputEvent* event, void* context) {
  91. furi_assert(context);
  92. ModuleView* module_view = context;
  93. bool consumed = false;
  94. if(event->type == InputTypeLong && event->key == InputKeyBack) {
  95. send_serial_command_module(ESerialCommand_Config_Off);
  96. configState = false;
  97. } else {
  98. module_view_process(module_view, event);
  99. consumed = true;
  100. }
  101. return consumed;
  102. }
  103. ModuleView* module_view_alloc() {
  104. ModuleView* module_view = malloc(sizeof(ModuleView));
  105. module_view->view = view_alloc();
  106. view_set_context(module_view->view, module_view);
  107. view_allocate_model(module_view->view, ViewModelTypeLocking, sizeof(ModuleViewModel));
  108. view_set_draw_callback(module_view->view, module_view_draw_callback);
  109. view_set_input_callback(module_view->view, module_view_input_callback);
  110. furi_hal_uart_set_br(FuriHalUartIdUSART1, FLIPPERZERO_SERIAL_BAUD);
  111. configState = false;
  112. return module_view;
  113. }
  114. void module_view_free(ModuleView* module_view) {
  115. furi_assert(module_view);
  116. view_free(module_view->view);
  117. free(module_view);
  118. }
  119. View* module_view_get_view(ModuleView* module_view) {
  120. furi_assert(module_view);
  121. return module_view->view;
  122. }
  123. void module_view_set_data(ModuleView* module_view, bool connected) {
  124. furi_assert(module_view);
  125. with_view_model(
  126. module_view->view, ModuleViewModel * model, { model->connected = connected; }, true);
  127. }