pc_monitor.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. #include "pc_monitor.h"
  2. static void render_callback(Canvas* canvas, void* ctx) {
  3. furi_assert(ctx);
  4. PcMonitorApp* app = ctx;
  5. switch(app->bt_state) {
  6. case BtStateWaiting:
  7. draw_connect_view(canvas);
  8. break;
  9. case BtStateRecieving:
  10. draw_bars_view(canvas, app);
  11. break;
  12. default:
  13. draw_status_view(canvas, app);
  14. break;
  15. }
  16. }
  17. static void input_callback(InputEvent* input_event, void* ctx) {
  18. furi_assert(ctx);
  19. FuriMessageQueue* event_queue = ctx;
  20. furi_message_queue_put(event_queue, input_event, FuriWaitForever);
  21. }
  22. static uint16_t bt_serial_callback(SerialServiceEvent event, void* ctx) {
  23. furi_assert(ctx);
  24. PcMonitorApp* app = ctx;
  25. if(event.event == SerialServiceEventTypeDataReceived) {
  26. FURI_LOG_D(
  27. TAG,
  28. "SerialServiceEventTypeDataReceived. Size: %u/%u. Data: %s",
  29. event.data.size,
  30. sizeof(DataStruct),
  31. (char*)event.data.buffer);
  32. if(event.data.size == sizeof(DataStruct)) {
  33. memcpy(&app->data, event.data.buffer, sizeof(DataStruct));
  34. app->bt_state = BtStateRecieving;
  35. app->last_packet = furi_hal_rtc_get_timestamp();
  36. // Elegant solution, the backlight is only on when there is continuous communication
  37. notification_message(app->notification, &sequence_display_backlight_on);
  38. notification_message(app->notification, &sequence_blink_blue_10);
  39. }
  40. }
  41. return 0;
  42. }
  43. static PcMonitorApp* pc_monitor_alloc() {
  44. PcMonitorApp* app = malloc(sizeof(PcMonitorApp));
  45. app->view_port = view_port_alloc();
  46. app->event_queue = furi_message_queue_alloc(8, sizeof(InputEvent));
  47. app->notification = furi_record_open(RECORD_NOTIFICATION);
  48. app->gui = furi_record_open(RECORD_GUI);
  49. app->bt = furi_record_open(RECORD_BT);
  50. gui_add_view_port(app->gui, app->view_port, GuiLayerFullscreen);
  51. view_port_draw_callback_set(app->view_port, render_callback, app);
  52. view_port_input_callback_set(app->view_port, input_callback, app->event_queue);
  53. return app;
  54. }
  55. static void pc_monitor_free(PcMonitorApp* app) {
  56. gui_remove_view_port(app->gui, app->view_port);
  57. view_port_free(app->view_port);
  58. furi_message_queue_free(app->event_queue);
  59. furi_record_close(RECORD_NOTIFICATION);
  60. furi_record_close(RECORD_GUI);
  61. furi_record_close(RECORD_BT);
  62. free(app);
  63. }
  64. int32_t pc_monitor_app(void* p) {
  65. UNUSED(p);
  66. PcMonitorApp* app = pc_monitor_alloc();
  67. bt_disconnect(app->bt);
  68. // Wait 2nd core to update nvm storage
  69. furi_delay_ms(200);
  70. bt_keys_storage_set_storage_path(app->bt, APP_DATA_PATH(".bt_serial.keys"));
  71. BleProfileSerialParams params = {
  72. .device_name_prefix = "PC Mon",
  73. .mac_xor = 0x0002,
  74. };
  75. app->ble_serial_profile = bt_profile_start(app->bt, ble_profile_serial, &params);
  76. furi_check(app->ble_serial_profile);
  77. ble_profile_serial_set_event_callback(
  78. app->ble_serial_profile, BT_SERIAL_BUFFER_SIZE, bt_serial_callback, app);
  79. furi_hal_bt_start_advertising();
  80. app->bt_state = BtStateWaiting;
  81. FURI_LOG_D(TAG, "Bluetooth is active!");
  82. // Main loop
  83. InputEvent event;
  84. while(true) {
  85. if(furi_message_queue_get(app->event_queue, &event, 1) == FuriStatusOk) {
  86. if(event.type == InputTypeShort && event.key == InputKeyBack) break;
  87. }
  88. if(app->bt_state == BtStateRecieving &&
  89. (furi_hal_rtc_get_timestamp() - app->last_packet > 5))
  90. app->bt_state = BtStateLost;
  91. }
  92. ble_profile_serial_set_event_callback(app->ble_serial_profile, 0, NULL, NULL);
  93. bt_disconnect(app->bt);
  94. // Wait 2nd core to update nvm storage
  95. furi_delay_ms(200);
  96. bt_keys_storage_set_default_path(app->bt);
  97. furi_check(bt_profile_restore_default(app->bt));
  98. pc_monitor_free(app);
  99. return 0;
  100. }