pc_monitor.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. uint8_t spacing = app->lines_count ? SCREEN_HEIGHT / app->lines_count : 0;
  11. uint8_t margin_top = spacing ? (spacing - LINE_HEIGHT) / 2 : 0;
  12. char str[32];
  13. if(app->data.cpu_usage <= 100) {
  14. canvas_draw_str(canvas, 1, margin_top + line * spacing + 9, "CPU");
  15. snprintf(str, 32, "%d%%", app->data.cpu_usage);
  16. elements_progress_bar_with_text(
  17. canvas,
  18. BAR_X,
  19. margin_top + line * spacing,
  20. BAR_WIDTH,
  21. app->data.cpu_usage / 100.0f,
  22. str);
  23. line++;
  24. }
  25. if(app->data.ram_usage <= 100) {
  26. canvas_draw_str(canvas, 1, margin_top + line * spacing + 9, "RAM");
  27. snprintf(
  28. str,
  29. 32,
  30. "%.1f/%.1f %s",
  31. (double)(app->data.ram_max * 0.1f * app->data.ram_usage * 0.01f),
  32. (double)(app->data.ram_max * 0.1f),
  33. app->data.ram_unit);
  34. elements_progress_bar_with_text(
  35. canvas,
  36. BAR_X,
  37. margin_top + line * spacing,
  38. BAR_WIDTH,
  39. app->data.ram_usage * 0.01f,
  40. str);
  41. line++;
  42. }
  43. if(app->data.gpu_usage <= 100) {
  44. canvas_draw_str(canvas, 1, margin_top + line * spacing + 9, "GPU");
  45. snprintf(str, 32, "%d%%", app->data.gpu_usage);
  46. elements_progress_bar_with_text(
  47. canvas,
  48. BAR_X,
  49. margin_top + line * spacing,
  50. BAR_WIDTH,
  51. app->data.gpu_usage / 100.0f,
  52. str);
  53. line++;
  54. }
  55. if(app->data.vram_usage <= 100) {
  56. canvas_draw_str(canvas, 1, margin_top + line * spacing + 9, "VRAM");
  57. snprintf(
  58. str,
  59. 32,
  60. "%.1f/%.1f %s",
  61. (double)(app->data.vram_max * 0.1f * app->data.vram_usage * 0.01f),
  62. (double)(app->data.vram_max * 0.1f),
  63. app->data.vram_unit);
  64. elements_progress_bar_with_text(
  65. canvas,
  66. BAR_X,
  67. margin_top + line * spacing,
  68. BAR_WIDTH,
  69. app->data.vram_usage * 0.01f,
  70. str);
  71. line++;
  72. }
  73. if(line == 0) app->bt_state = BtStateNoData;
  74. app->lines_count = line;
  75. } else {
  76. canvas_draw_str_aligned(
  77. canvas,
  78. 64,
  79. 32,
  80. AlignCenter,
  81. AlignCenter,
  82. app->bt_state == BtStateChecking ? "Checking BLE..." :
  83. app->bt_state == BtStateInactive ? "BLE inactive!" :
  84. app->bt_state == BtStateWaiting ? "Waiting for data..." :
  85. app->bt_state == BtStateLost ? "Connection lost!" :
  86. "No data!");
  87. }
  88. }
  89. static void input_callback(InputEvent* input_event, void* ctx) {
  90. furi_assert(ctx);
  91. FuriMessageQueue* event_queue = ctx;
  92. furi_message_queue_put(event_queue, input_event, FuriWaitForever);
  93. }
  94. static uint16_t bt_serial_callback(SerialServiceEvent event, void* ctx) {
  95. furi_assert(ctx);
  96. PcMonitorApp* app = ctx;
  97. if(event.event == SerialServiceEventTypeDataReceived) {
  98. FURI_LOG_D(
  99. TAG,
  100. "SerialServiceEventTypeDataReceived. Size: %u/%u. Data: %s",
  101. event.data.size,
  102. sizeof(DataStruct),
  103. (char*)event.data.buffer);
  104. if(event.data.size == sizeof(DataStruct)) {
  105. memcpy(&app->data, event.data.buffer, sizeof(DataStruct));
  106. app->bt_state = BtStateRecieving;
  107. app->last_packet = furi_hal_rtc_get_timestamp();
  108. }
  109. }
  110. return 0;
  111. }
  112. static PcMonitorApp* pc_monitor_alloc() {
  113. PcMonitorApp* app = malloc(sizeof(PcMonitorApp));
  114. app->view_port = view_port_alloc();
  115. app->event_queue = furi_message_queue_alloc(8, sizeof(InputEvent));
  116. app->notification = furi_record_open(RECORD_NOTIFICATION);
  117. app->gui = furi_record_open(RECORD_GUI);
  118. app->bt = furi_record_open(RECORD_BT);
  119. app->data.cpu_usage = UINT8_MAX;
  120. app->data.ram_usage = UINT8_MAX;
  121. app->data.gpu_usage = UINT8_MAX;
  122. app->data.vram_usage = UINT8_MAX;
  123. gui_add_view_port(app->gui, app->view_port, GuiLayerFullscreen);
  124. view_port_draw_callback_set(app->view_port, render_callback, app);
  125. view_port_input_callback_set(app->view_port, input_callback, app->event_queue);
  126. return app;
  127. }
  128. static void pc_monitor_free(PcMonitorApp* app) {
  129. gui_remove_view_port(app->gui, app->view_port);
  130. view_port_free(app->view_port);
  131. furi_message_queue_free(app->event_queue);
  132. furi_record_close(RECORD_NOTIFICATION);
  133. furi_record_close(RECORD_GUI);
  134. furi_record_close(RECORD_BT);
  135. free(app);
  136. }
  137. int32_t pc_monitor_app(void* p) {
  138. UNUSED(p);
  139. PcMonitorApp* app = pc_monitor_alloc();
  140. if(furi_hal_bt_is_active()) {
  141. furi_hal_bt_serial_set_event_callback(BT_SERIAL_BUFFER_SIZE, bt_serial_callback, app);
  142. furi_hal_bt_start_advertising();
  143. app->bt_state = BtStateWaiting;
  144. FURI_LOG_D(TAG, "Bluetooth is active!");
  145. } else {
  146. app->bt_state = BtStateInactive;
  147. FURI_LOG_W(TAG, "Please, enable the Bluetooth and restart the app");
  148. }
  149. // Main loop
  150. InputEvent event;
  151. while(true) {
  152. if(furi_message_queue_get(app->event_queue, &event, 1) == FuriStatusOk) {
  153. if(event.type == InputTypeShort && event.key == InputKeyBack) break;
  154. }
  155. if(app->bt_state == BtStateRecieving &&
  156. (furi_hal_rtc_get_timestamp() - app->last_packet > 5))
  157. app->bt_state = BtStateLost;
  158. }
  159. furi_hal_bt_serial_set_event_callback(0, NULL, NULL);
  160. pc_monitor_free(app);
  161. return 0;
  162. }