fbs.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. #include "fbs.h"
  2. const uint16_t BT_SERIAL_BUFFER_SIZE = 128;
  3. void draw_callback(Canvas* canvas, void* ctx) {
  4. FBS* app = ctx;
  5. furi_check(furi_mutex_acquire(app->app_mutex, FuriWaitForever) == FuriStatusOk);
  6. canvas_draw_str_aligned(canvas, 64, 32, AlignCenter, AlignCenter, (char*)app->display_text);
  7. furi_mutex_release(app->app_mutex);
  8. }
  9. void input_callback(InputEvent* input, void* ctx) {
  10. FBS* app = ctx;
  11. furi_message_queue_put(app->event_queue, input, FuriWaitForever);
  12. }
  13. FBS* fbs_alloc() {
  14. FBS* app = malloc(sizeof(FBS));
  15. app->app_mutex = furi_mutex_alloc(FuriMutexTypeNormal);
  16. app->event_queue = furi_message_queue_alloc(8, sizeof(InputEvent));
  17. app->gui = furi_record_open(RECORD_GUI);
  18. app->view_port = view_port_alloc();
  19. view_port_draw_callback_set(app->view_port, draw_callback, app);
  20. view_port_input_callback_set(app->view_port, input_callback, app);
  21. gui_add_view_port(app->gui, app->view_port, GuiLayerFullscreen);
  22. app->bt_connected = false;
  23. app->bt = furi_record_open(RECORD_BT);
  24. return app;
  25. }
  26. void fbs_free(FBS* app) {
  27. view_port_enabled_set(app->view_port, false);
  28. gui_remove_view_port(app->gui, app->view_port);
  29. furi_record_close(RECORD_GUI);
  30. app->gui = NULL;
  31. view_port_free(app->view_port);
  32. free(app->display_text);
  33. furi_mutex_free(app->app_mutex);
  34. furi_message_queue_free(app->event_queue);
  35. furi_record_close(RECORD_BT);
  36. app->bt = NULL;
  37. free(app);
  38. }
  39. static uint16_t bt_serial_event_callback(SerialServiceEvent event, void* context) {
  40. furi_assert(context);
  41. Bt* bt = context;
  42. UNUSED(bt);
  43. uint16_t ret = 0;
  44. if(event.event == SerialServiceEventTypeDataReceived) {
  45. FURI_LOG_D(TAG, "SerialServiceEventTypeDataReceived");
  46. FURI_LOG_D(TAG, "Size: %u", event.data.size);
  47. FURI_LOG_D(TAG, "Data: ");
  48. for(size_t i = 0; i < event.data.size; i++) {
  49. printf("%X ", event.data.buffer[i]);
  50. }
  51. printf("\r\n");
  52. } else if(event.event == SerialServiceEventTypeDataSent) {
  53. FURI_LOG_D(TAG, "SerialServiceEventTypeDataSent");
  54. FURI_LOG_D(TAG, "Size: %u", event.data.size);
  55. FURI_LOG_D(TAG, "Data: ");
  56. for(size_t i = 0; i < event.data.size; i++) {
  57. printf("%X ", event.data.buffer[i]);
  58. }
  59. printf("\r\n");
  60. }
  61. return ret;
  62. }
  63. int32_t fbs_app(void* p) {
  64. UNUSED(p);
  65. FBS* app = fbs_alloc();
  66. if(furi_hal_bt_is_active()) {
  67. FURI_LOG_D(TAG, "BT is working, hijacking the serial connection...");
  68. furi_hal_bt_serial_set_event_callback(
  69. BT_SERIAL_BUFFER_SIZE, bt_serial_event_callback, app);
  70. furi_hal_bt_start_advertising();
  71. } else {
  72. FURI_LOG_D(TAG, "Please, enable the Bluetooth and restart the app");
  73. }
  74. InputEvent event;
  75. for(bool processing = true; processing;) {
  76. int status = furi_message_queue_get(app->event_queue, &event, 100);
  77. furi_check(furi_mutex_acquire(app->app_mutex, FuriWaitForever) == FuriStatusOk);
  78. if(status == FuriStatusOk && event.type == InputTypePress && event.key == InputKeyBack) {
  79. processing = false;
  80. }
  81. furi_mutex_release(app->app_mutex);
  82. view_port_update(app->view_port);
  83. }
  84. furi_hal_bt_serial_set_event_callback(0, NULL, NULL);
  85. fbs_free(app);
  86. FURI_LOG_D(TAG, "Released everything");
  87. return 0;
  88. }