module_view.c 4.8 KB

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