malveke_pin_test.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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), "PIN: %d", model->pin );
  10. canvas_set_color(canvas, ColorBlack);
  11. canvas_draw_frame(canvas, 0, 0, FRAME_WIDTH, FRAME_HEIGTH);
  12. canvas_draw_str(canvas, 8, 28, show_pin);
  13. // canvas_draw_str(canvas, 60, 28,(char*) model->pin);
  14. }
  15. static uint32_t gb_live_camera_exit(void* context) {
  16. UNUSED(context);
  17. return VIEW_NONE;
  18. }
  19. static void gb_live_camera_app_free(UartEchoApp* app) {
  20. furi_assert(app);
  21. // furi_thread_flags_set(furi_thread_get_id(app->worker_thread), WorkerEventStop);
  22. // furi_thread_join(app->worker_thread);
  23. // furi_thread_free(app->worker_thread);
  24. // furi_hal_uart_set_irq_cb(FuriHalUartIdUSART1, NULL, NULL);
  25. // furi_hal_uart_set_irq_cb(FuriHalUartIdLPUART1, NULL, NULL);
  26. // furi_hal_uart_deinit(FuriHalUartIdLPUART1);
  27. // Free views
  28. view_dispatcher_remove_view(app->view_dispatcher, 0);
  29. view_free(app->view);
  30. view_dispatcher_free(app->view_dispatcher);
  31. // Close gui record
  32. furi_record_close(RECORD_GUI);
  33. furi_record_close(RECORD_NOTIFICATION);
  34. app->gui = NULL;
  35. // furi_stream_buffer_free(app->rx_stream);
  36. // Free rest
  37. free(app);
  38. }
  39. static bool gb_live_camera_view_input_callback(InputEvent* event, void* context) {
  40. UartEchoApp* app = context;
  41. if (event->type == InputTypePress){
  42. if (event->key == InputKeyRight){
  43. with_view_model(
  44. app->view,
  45. UartDumpModel * model,
  46. {
  47. if(model->pin + 1 <= 31) {
  48. model->pin += 1;
  49. char gbpin_start_command[80]; // A reasonably sized buffer.
  50. snprintf(gbpin_start_command, sizeof(gbpin_start_command), "gbpin -p %d\n", model->pin);
  51. furi_hal_uart_tx(FuriHalUartIdUSART1, (uint8_t*)gbpin_start_command, strlen(gbpin_start_command));
  52. }
  53. },
  54. true);
  55. }
  56. else if (event->key == InputKeyLeft){
  57. with_view_model(
  58. app->view,
  59. UartDumpModel * model,
  60. {
  61. if(model->pin -1 >= 2) {
  62. model->pin -= 1;
  63. char gbpin_start_command[80]; // A reasonably sized buffer.
  64. snprintf(gbpin_start_command, sizeof(gbpin_start_command), "gbpin -p %d\n", model->pin);
  65. furi_hal_uart_tx(FuriHalUartIdUSART1, (uint8_t*)gbpin_start_command, strlen(gbpin_start_command));
  66. }
  67. },
  68. true);
  69. }
  70. }
  71. return false;
  72. }
  73. static UartEchoApp* gb_live_camera_app_alloc() {
  74. UartEchoApp* app = malloc(sizeof(UartEchoApp));
  75. // app->rx_stream = furi_stream_buffer_alloc(2048, 1);
  76. // Gui
  77. app->gui = furi_record_open(RECORD_GUI);
  78. // View dispatcher
  79. app->view_dispatcher = view_dispatcher_alloc();
  80. view_dispatcher_enable_queue(app->view_dispatcher);
  81. view_dispatcher_attach_to_gui(app->view_dispatcher, app->gui, ViewDispatcherTypeFullscreen);
  82. // Views
  83. app->view = view_alloc();
  84. view_set_context(app->view, app);
  85. view_set_draw_callback(app->view, gb_live_camera_view_draw_callback);
  86. view_set_input_callback(app->view, gb_live_camera_view_input_callback);
  87. view_allocate_model(app->view, ViewModelTypeLocking, sizeof(UartDumpModel));
  88. with_view_model(
  89. app->view,
  90. UartDumpModel * model,
  91. {
  92. model->pin = 2;
  93. },
  94. true);
  95. view_set_previous_callback(app->view, gb_live_camera_exit);
  96. view_dispatcher_add_view(app->view_dispatcher, 0, app->view);
  97. view_dispatcher_switch_to_view(app->view_dispatcher, 0);
  98. // app->worker_thread = furi_thread_alloc_ex("UsbUartWorker", 2048, gb_live_camera_worker, app);
  99. // furi_thread_start(app->worker_thread);
  100. // Enable uart listener (UART & UART1)
  101. // furi_hal_console_disable();
  102. furi_hal_uart_set_br(FuriHalUartIdUSART1, 115200);
  103. // furi_hal_uart_set_irq_cb(FuriHalUartIdLPUART1, gb_live_camera_on_irq_cb, app);
  104. furi_hal_power_disable_otg();
  105. // furi_hal_power_suppress_charge_enter();
  106. furi_delay_ms(1);
  107. return app;
  108. }
  109. int32_t malveke_pin_test_app(void* p) {
  110. UNUSED(p);
  111. UartEchoApp* app = gb_live_camera_app_alloc();
  112. view_dispatcher_run(app->view_dispatcher);
  113. gb_live_camera_app_free(app);
  114. furi_hal_power_disable_otg();
  115. return 0;
  116. }