malveke_pin_test.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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(gbpin_start_command, sizeof(gbpin_start_command), "gbpin -p %d\n", model->pin);
  54. furi_hal_serial_tx(app->serial_handle,
  55. (uint8_t*)gbpin_start_command,
  56. strlen(gbpin_start_command));
  57. }
  58. },
  59. true);
  60. }
  61. else if (event->key == InputKeyLeft){
  62. with_view_model(
  63. app->view,
  64. UartDumpModel * model,
  65. {
  66. if(model->pin -1 >= 2) {
  67. model->pin -= 1;
  68. char gbpin_start_command[80]; // A reasonably sized buffer.
  69. snprintf(gbpin_start_command, sizeof(gbpin_start_command), "gbpin -p %d\n", model->pin);
  70. furi_hal_serial_tx(app->serial_handle,
  71. (uint8_t*)gbpin_start_command,
  72. strlen(gbpin_start_command));
  73. }
  74. },
  75. true);
  76. } else if (event->key == InputKeyOk){
  77. for(int i = 2; i <= 31; i++) {
  78. with_view_model(
  79. app->view,
  80. UartDumpModel * model,
  81. {
  82. model->pin = i;
  83. char gbpin_start_command[80]; // A reasonably sized buffer.
  84. snprintf(gbpin_start_command, sizeof(gbpin_start_command), "gbpin -p %d\n", model->pin);
  85. furi_hal_serial_tx(app->serial_handle,
  86. (uint8_t*)gbpin_start_command,
  87. strlen(gbpin_start_command));
  88. }, true);
  89. furi_delay_ms(600);
  90. }
  91. }
  92. }
  93. return false;
  94. }
  95. static UartEchoApp* gb_live_camera_app_alloc() {
  96. UartEchoApp* app = malloc(sizeof(UartEchoApp));
  97. // app->rx_stream = furi_stream_buffer_alloc(2048, 1);
  98. // Gui
  99. app->gui = furi_record_open(RECORD_GUI);
  100. // View dispatcher
  101. app->view_dispatcher = view_dispatcher_alloc();
  102. view_dispatcher_enable_queue(app->view_dispatcher);
  103. view_dispatcher_attach_to_gui(app->view_dispatcher, app->gui, ViewDispatcherTypeFullscreen);
  104. // Views
  105. app->view = view_alloc();
  106. view_set_context(app->view, app);
  107. view_set_draw_callback(app->view, gb_live_camera_view_draw_callback);
  108. view_set_input_callback(app->view, gb_live_camera_view_input_callback);
  109. view_allocate_model(app->view, ViewModelTypeLocking, sizeof(UartDumpModel));
  110. with_view_model(
  111. app->view,
  112. UartDumpModel * model,
  113. {
  114. model->pin = 2;
  115. },
  116. true);
  117. view_set_previous_callback(app->view, gb_live_camera_exit);
  118. view_dispatcher_add_view(app->view_dispatcher, 0, app->view);
  119. view_dispatcher_switch_to_view(app->view_dispatcher, 0);
  120. // Enable uart listener (UART)
  121. app->serial_handle = furi_hal_serial_control_acquire(FuriHalSerialIdUsart);
  122. if(!app->serial_handle) {
  123. furi_delay_ms(5000);
  124. }
  125. furi_check(app->serial_handle);
  126. furi_hal_serial_init(app->serial_handle, 115200);
  127. furi_hal_power_disable_otg();
  128. // furi_hal_power_suppress_charge_enter();
  129. furi_delay_ms(1);
  130. return app;
  131. }
  132. int32_t malveke_pin_test_app(void* p) {
  133. UNUSED(p);
  134. UartEchoApp* app = gb_live_camera_app_alloc();
  135. view_dispatcher_run(app->view_dispatcher);
  136. gb_live_camera_app_free(app);
  137. furi_hal_power_disable_otg();
  138. return 0;
  139. }