#include "perf_monitor.h" #include static uint32_t refr_count = 0; // refreshes that actually rendered something static uint32_t refr_time_us = 0; // time spent in those refreshes static uint32_t busy_us = 0; // time spent inside lv_timer_handler() static void refr_event_cb(lv_event_t *e) { static uint32_t refr_start_us = 0; static bool rendered = false; switch (lv_event_get_code(e)) { case LV_EVENT_REFR_START: refr_start_us = micros(); rendered = false; break; case LV_EVENT_RENDER_START: rendered = true; break; case LV_EVENT_REFR_READY: if (rendered) { refr_count++; refr_time_us += micros() - refr_start_us; } break; default: break; } } void perf_monitor_begin(lv_display_t *disp) { lv_display_add_event_cb(disp, refr_event_cb, LV_EVENT_ALL, NULL); } uint32_t perf_timer_handler() { uint32_t t0 = micros(); uint32_t next = lv_timer_handler(); busy_us += micros() - t0; return next; } void perf_monitor_print() { static uint32_t last_us = 0; uint32_t now = micros(); uint32_t elapsed = now - last_us; if (elapsed < 1000000) return; uint32_t other_us = busy_us > refr_time_us ? busy_us - refr_time_us : 0; Serial.printf("FPS: %.1f refresh: %.1f ms other: %.1f ms cpu: %.0f%%\n", refr_count * 1e6f / elapsed, refr_count ? refr_time_us / 1000.0f / refr_count : 0.0f, refr_count ? other_us / 1000.0f / refr_count : 0.0f, busy_us * 100.0f / elapsed); refr_count = 0; refr_time_us = 0; busy_us = 0; last_us = now; }