pc_monitor.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. #include "pc_monitor.h"
  2. static void render_callback(Canvas* canvas, void* ctx) {
  3. furi_assert(ctx);
  4. PcMonitorApp* app = ctx;
  5. if(app->bt_state == BtStateRecieving) {
  6. canvas_clear(canvas);
  7. canvas_set_color(canvas, ColorBlack);
  8. canvas_set_font(canvas, FontKeyboard);
  9. uint8_t line = 0;
  10. char str[32];
  11. canvas_draw_str(canvas, 0, 10, "CPU");
  12. snprintf(str, 32, "%d%%", app->data.cpu_usage);
  13. elements_progress_bar_with_text(canvas, 20, 1, 108, app->data.cpu_usage / 100.0f, str);
  14. line = 1;
  15. canvas_draw_str(canvas, 0, 10 + 14 * line, "RAM");
  16. snprintf(
  17. str,
  18. 32,
  19. "%.1f/%.1f %s",
  20. app->data.ram_used / (double)10,
  21. app->data.ram_max / (double)10,
  22. app->data.ram_unit);
  23. elements_progress_bar_with_text(
  24. canvas, 20, 1 + 14 * line, 108, app->data.ram_used / (float)app->data.ram_max, str);
  25. line = 2;
  26. canvas_draw_str(canvas, 0, 10 + 14 * line, "GPU");
  27. snprintf(str, 32, "%d%%", app->data.gpu_usage);
  28. elements_progress_bar_with_text(
  29. canvas, 20, 1 + 14 * line, 108, app->data.gpu_usage / 100.0f, str);
  30. } else {
  31. canvas_draw_str_aligned(
  32. canvas,
  33. 64,
  34. 32,
  35. AlignCenter,
  36. AlignCenter,
  37. app->bt_state == BtStateChecking ? "Checking BLE..." :
  38. app->bt_state == BtStateInactive ? "BLE inactive!" :
  39. app->bt_state == BtStateWaiting ? "Waiting for data..." :
  40. "Connection lost!");
  41. }
  42. }
  43. static void input_callback(InputEvent* input_event, void* ctx) {
  44. furi_assert(ctx);
  45. FuriMessageQueue* event_queue = ctx;
  46. furi_message_queue_put(event_queue, input_event, FuriWaitForever);
  47. }
  48. static uint16_t bt_serial_callback(SerialServiceEvent event, void* ctx) {
  49. furi_assert(ctx);
  50. PcMonitorApp* app = ctx;
  51. if(event.event == SerialServiceEventTypeDataReceived) {
  52. app->bt_state = BtStateRecieving;
  53. FURI_LOG_D(
  54. TAG,
  55. "SerialServiceEventTypeDataReceived. Size: %u. Data: %s",
  56. event.data.size,
  57. (char*)event.data.buffer);
  58. uint8_t step = 0;
  59. // strtok is not in the API
  60. char temp_str[32] = {0};
  61. uint8_t lastIndex = 0;
  62. for(size_t i = 0; i <= strlen((char*)event.data.buffer); i++) {
  63. if(event.data.buffer[i] == ':' || event.data.buffer[i] == '\0') {
  64. char* sub_str = temp_str + lastIndex;
  65. printf("step: %d, sub_str: %s\r\n", step, sub_str);
  66. if(step == 0) app->data.cpu_usage = atoi(sub_str);
  67. if(step == 1) app->data.gpu_usage = atoi(sub_str);
  68. if(step == 2) app->data.ram_used = atoi(sub_str);
  69. if(step == 3) app->data.ram_max = atoi(sub_str);
  70. if(step == 4) strcpy(app->data.ram_unit, sub_str);
  71. step++;
  72. lastIndex = i + 1;
  73. //memset(temp_str, 0, 32);
  74. //for(size_t j = 0; j < 32; j++) temp_str[j] = 0;
  75. }
  76. temp_str[i] = event.data.buffer[i];
  77. }
  78. }
  79. return 0;
  80. }
  81. static PcMonitorApp* pc_monitor_alloc() {
  82. PcMonitorApp* app = malloc(sizeof(PcMonitorApp));
  83. app->view_port = view_port_alloc();
  84. app->event_queue = furi_message_queue_alloc(8, sizeof(InputEvent));
  85. app->notification = furi_record_open(RECORD_NOTIFICATION);
  86. app->gui = furi_record_open(RECORD_GUI);
  87. app->bt = furi_record_open(RECORD_BT);
  88. gui_add_view_port(app->gui, app->view_port, GuiLayerFullscreen);
  89. view_port_draw_callback_set(app->view_port, render_callback, app);
  90. view_port_input_callback_set(app->view_port, input_callback, app->event_queue);
  91. return app;
  92. }
  93. static void pc_monitor_free(PcMonitorApp* app) {
  94. gui_remove_view_port(app->gui, app->view_port);
  95. view_port_free(app->view_port);
  96. furi_message_queue_free(app->event_queue);
  97. furi_record_close(RECORD_NOTIFICATION);
  98. furi_record_close(RECORD_GUI);
  99. furi_record_close(RECORD_BT);
  100. free(app);
  101. }
  102. int32_t pc_monitor_app(void* p) {
  103. UNUSED(p);
  104. PcMonitorApp* app = pc_monitor_alloc();
  105. if(furi_hal_bt_is_active()) {
  106. furi_hal_bt_serial_set_event_callback(BT_SERIAL_BUFFER_SIZE, bt_serial_callback, app);
  107. furi_hal_bt_start_advertising();
  108. app->bt_state = BtStateWaiting;
  109. FURI_LOG_D(TAG, "Bluetooth is active!");
  110. } else {
  111. app->bt_state = BtStateInactive;
  112. FURI_LOG_W(TAG, "Please, enable the Bluetooth and restart the app");
  113. }
  114. InputEvent event;
  115. while(furi_message_queue_get(app->event_queue, &event, FuriWaitForever) == FuriStatusOk) {
  116. if(event.type == InputTypeShort && event.key == InputKeyBack) break;
  117. }
  118. furi_hal_bt_serial_set_event_callback(0, NULL, NULL);
  119. pc_monitor_free(app);
  120. return 0;
  121. }