malveke_pin_test.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. // Free views
  29. view_dispatcher_remove_view(app->view_dispatcher, 0);
  30. view_free(app->view);
  31. view_dispatcher_free(app->view_dispatcher);
  32. // Close gui record
  33. furi_record_close(RECORD_GUI);
  34. furi_record_close(RECORD_NOTIFICATION);
  35. app->gui = NULL;
  36. // furi_stream_buffer_free(app->rx_stream);
  37. // Free rest
  38. free(app);
  39. }
  40. static bool gb_live_camera_view_input_callback(InputEvent* event, void* context) {
  41. UartEchoApp* app = context;
  42. if (event->type == InputTypePress){
  43. if (event->key == InputKeyRight){
  44. with_view_model(
  45. app->view,
  46. UartDumpModel * model,
  47. {
  48. if(model->pin + 1 <= 31) {
  49. model->pin += 1;
  50. char gbpin_start_command[80]; // A reasonably sized buffer.
  51. snprintf(gbpin_start_command, sizeof(gbpin_start_command), "gbpin -p %d\n", model->pin);
  52. furi_hal_uart_tx(FuriHalUartIdUSART1, (uint8_t*)gbpin_start_command, strlen(gbpin_start_command));
  53. }
  54. },
  55. true);
  56. }
  57. else if (event->key == InputKeyLeft){
  58. with_view_model(
  59. app->view,
  60. UartDumpModel * model,
  61. {
  62. if(model->pin -1 >= 2) {
  63. model->pin -= 1;
  64. char gbpin_start_command[80]; // A reasonably sized buffer.
  65. snprintf(gbpin_start_command, sizeof(gbpin_start_command), "gbpin -p %d\n", model->pin);
  66. furi_hal_uart_tx(FuriHalUartIdUSART1, (uint8_t*)gbpin_start_command, strlen(gbpin_start_command));
  67. }
  68. },
  69. true);
  70. } else if (event->key == InputKeyOk){
  71. for(int i = 2; i <= 31; i++) {
  72. with_view_model(
  73. app->view,
  74. UartDumpModel * model,
  75. {
  76. model->pin = i;
  77. char gbpin_start_command[80]; // A reasonably sized buffer.
  78. snprintf(gbpin_start_command, sizeof(gbpin_start_command), "gbpin -p %d\n", model->pin);
  79. furi_hal_uart_tx(FuriHalUartIdUSART1, (uint8_t*)gbpin_start_command, strlen(gbpin_start_command));
  80. }, true);
  81. furi_delay_ms(600);
  82. }
  83. }
  84. }
  85. return false;
  86. }
  87. static UartEchoApp* gb_live_camera_app_alloc() {
  88. UartEchoApp* app = malloc(sizeof(UartEchoApp));
  89. // app->rx_stream = furi_stream_buffer_alloc(2048, 1);
  90. // Gui
  91. app->gui = furi_record_open(RECORD_GUI);
  92. // View dispatcher
  93. app->view_dispatcher = view_dispatcher_alloc();
  94. view_dispatcher_enable_queue(app->view_dispatcher);
  95. view_dispatcher_attach_to_gui(app->view_dispatcher, app->gui, ViewDispatcherTypeFullscreen);
  96. // Views
  97. app->view = view_alloc();
  98. view_set_context(app->view, app);
  99. view_set_draw_callback(app->view, gb_live_camera_view_draw_callback);
  100. view_set_input_callback(app->view, gb_live_camera_view_input_callback);
  101. view_allocate_model(app->view, ViewModelTypeLocking, sizeof(UartDumpModel));
  102. with_view_model(
  103. app->view,
  104. UartDumpModel * model,
  105. {
  106. model->pin = 2;
  107. },
  108. true);
  109. view_set_previous_callback(app->view, gb_live_camera_exit);
  110. view_dispatcher_add_view(app->view_dispatcher, 0, app->view);
  111. view_dispatcher_switch_to_view(app->view_dispatcher, 0);
  112. // app->worker_thread = furi_thread_alloc_ex("UsbUartWorker", 2048, gb_live_camera_worker, app);
  113. // furi_thread_start(app->worker_thread);
  114. // Enable uart listener (UART & UART1)
  115. // furi_hal_console_disable();
  116. furi_hal_uart_set_br(FuriHalUartIdUSART1, 115200);
  117. // furi_hal_uart_set_irq_cb(FuriHalUartIdLPUART1, gb_live_camera_on_irq_cb, app);
  118. furi_hal_power_disable_otg();
  119. // furi_hal_power_suppress_charge_enter();
  120. furi_delay_ms(1);
  121. return app;
  122. }
  123. int32_t malveke_pin_test_app(void* p) {
  124. UNUSED(p);
  125. UartEchoApp* app = gb_live_camera_app_alloc();
  126. view_dispatcher_run(app->view_dispatcher);
  127. gb_live_camera_app_free(app);
  128. furi_hal_power_disable_otg();
  129. return 0;
  130. }