malveke_pin_test.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. #include "malveke_pin_test.h"
  2. #include <stdio.h> // Para sprintf
  3. #include <string.h> // Para strlen
  4. static void gb_live_camera_view_draw_callback(Canvas* canvas, void* _model) {
  5. UartDumpModel* model = _model;
  6. UNUSED(model);
  7. // Prepare canvas
  8. char show_pin[20];
  9. snprintf(show_pin, sizeof(show_pin), "%d", model->pin);
  10. canvas_set_color(canvas, ColorBlack);
  11. canvas_set_font(canvas, FontSecondary);
  12. canvas_draw_str(canvas, 37, 35, "PIN:");
  13. canvas_set_font(canvas, FontBigNumbers);
  14. canvas_draw_str(canvas, 57, 38, show_pin);
  15. }
  16. static uint32_t gb_live_camera_exit(void* context) {
  17. UNUSED(context);
  18. return VIEW_NONE;
  19. }
  20. static void gb_live_camera_app_free(UartEchoApp* app) {
  21. furi_assert(app);
  22. // furi_thread_flags_set(furi_thread_get_id(app->worker_thread), WorkerEventStop);
  23. // furi_thread_join(app->worker_thread);
  24. // furi_thread_free(app->worker_thread);
  25. // furi_hal_uart_set_irq_cb(FuriHalUartIdUSART1, NULL, NULL);
  26. // furi_hal_uart_set_irq_cb(FuriHalUartIdLPUART1, NULL, NULL);
  27. // furi_hal_uart_deinit(FuriHalUartIdLPUART1);
  28. furi_hal_serial_deinit(app->serial_handle);
  29. furi_hal_serial_control_release(app->serial_handle);
  30. // Free views
  31. view_dispatcher_remove_view(app->view_dispatcher, 0);
  32. view_free(app->view);
  33. view_dispatcher_free(app->view_dispatcher);
  34. // Close gui record
  35. furi_record_close(RECORD_GUI);
  36. furi_record_close(RECORD_NOTIFICATION);
  37. app->gui = NULL;
  38. // furi_stream_buffer_free(app->rx_stream);
  39. // Free rest
  40. free(app);
  41. }
  42. static bool gb_live_camera_view_input_callback(InputEvent* event, void* context) {
  43. UartEchoApp* app = context;
  44. if(event->type == InputTypePress) {
  45. if(event->key == InputKeyRight) {
  46. with_view_model(
  47. app->view,
  48. UartDumpModel * model,
  49. {
  50. if(model->pin + 1 <= 31) {
  51. model->pin += 1;
  52. char gbpin_start_command[80]; // A reasonably sized buffer.
  53. snprintf(
  54. gbpin_start_command,
  55. sizeof(gbpin_start_command),
  56. "gbpin -p %d\n",
  57. model->pin);
  58. furi_hal_serial_tx(
  59. app->serial_handle,
  60. (uint8_t*)gbpin_start_command,
  61. strlen(gbpin_start_command));
  62. }
  63. },
  64. true);
  65. } else if(event->key == InputKeyLeft) {
  66. with_view_model(
  67. app->view,
  68. UartDumpModel * model,
  69. {
  70. if(model->pin - 1 >= 2) {
  71. model->pin -= 1;
  72. char gbpin_start_command[80]; // A reasonably sized buffer.
  73. snprintf(
  74. gbpin_start_command,
  75. sizeof(gbpin_start_command),
  76. "gbpin -p %d\n",
  77. model->pin);
  78. furi_hal_serial_tx(
  79. app->serial_handle,
  80. (uint8_t*)gbpin_start_command,
  81. strlen(gbpin_start_command));
  82. }
  83. },
  84. true);
  85. } else if(event->key == InputKeyOk) {
  86. for(int i = 2; i <= 31; i++) {
  87. with_view_model(
  88. app->view,
  89. UartDumpModel * model,
  90. {
  91. model->pin = i;
  92. char gbpin_start_command[80]; // A reasonably sized buffer.
  93. snprintf(
  94. gbpin_start_command,
  95. sizeof(gbpin_start_command),
  96. "gbpin -p %d\n",
  97. model->pin);
  98. furi_hal_serial_tx(
  99. app->serial_handle,
  100. (uint8_t*)gbpin_start_command,
  101. strlen(gbpin_start_command));
  102. },
  103. true);
  104. furi_delay_ms(600);
  105. }
  106. }
  107. }
  108. return false;
  109. }
  110. static UartEchoApp* gb_live_camera_app_alloc() {
  111. UartEchoApp* app = malloc(sizeof(UartEchoApp));
  112. // app->rx_stream = furi_stream_buffer_alloc(2048, 1);
  113. // Gui
  114. app->gui = furi_record_open(RECORD_GUI);
  115. // View dispatcher
  116. app->view_dispatcher = view_dispatcher_alloc();
  117. view_dispatcher_attach_to_gui(app->view_dispatcher, app->gui, ViewDispatcherTypeFullscreen);
  118. // Views
  119. app->view = view_alloc();
  120. view_set_context(app->view, app);
  121. view_set_draw_callback(app->view, gb_live_camera_view_draw_callback);
  122. view_set_input_callback(app->view, gb_live_camera_view_input_callback);
  123. view_allocate_model(app->view, ViewModelTypeLocking, sizeof(UartDumpModel));
  124. with_view_model(app->view, UartDumpModel * model, { model->pin = 2; }, true);
  125. view_set_previous_callback(app->view, gb_live_camera_exit);
  126. view_dispatcher_add_view(app->view_dispatcher, 0, app->view);
  127. view_dispatcher_switch_to_view(app->view_dispatcher, 0);
  128. // Enable uart listener (UART)
  129. app->serial_handle = furi_hal_serial_control_acquire(FuriHalSerialIdUsart);
  130. if(!app->serial_handle) {
  131. furi_delay_ms(5000);
  132. }
  133. furi_check(app->serial_handle);
  134. furi_hal_serial_init(app->serial_handle, 115200);
  135. furi_hal_power_disable_otg();
  136. // furi_hal_power_suppress_charge_enter();
  137. furi_delay_ms(1);
  138. return app;
  139. }
  140. int32_t malveke_pin_test_app(void* p) {
  141. UNUSED(p);
  142. UartEchoApp* app = gb_live_camera_app_alloc();
  143. view_dispatcher_run(app->view_dispatcher);
  144. gb_live_camera_app_free(app);
  145. furi_hal_power_disable_otg();
  146. return 0;
  147. }