@@ -50,6 +50,22 @@ static float clampUsagePercent(float usage) {
5050 return usage;
5151}
5252
53+ template <typename Callback, typename ... Args>
54+ static void invokeCpuMonitorCallback (const Callback &callback, Args... args) noexcept {
55+ if (!callback) {
56+ return ;
57+ }
58+
59+ #if defined(__cpp_exceptions)
60+ try {
61+ callback (args...);
62+ } catch (...) {
63+ }
64+ #else
65+ callback (args...);
66+ #endif
67+ }
68+
5369void ESPCpuMonitor::resetState (const CpuMonitorConfig &cfg) {
5470 config_ = cfg;
5571 if (!isValidSmoothingMode (config_.smoothingMode )) {
@@ -388,7 +404,7 @@ bool ESPCpuMonitor::sampleNow(CpuUsageSample &out) {
388404 bool ready = captureSample (out, callbacks);
389405 for (const auto &cb : callbacks) {
390406 if (cb) {
391- cb ( out);
407+ invokeCpuMonitorCallback (cb, out);
392408 }
393409 }
394410 return ready;
@@ -427,7 +443,7 @@ void ESPCpuMonitor::timerCallback(void *arg) {
427443 if (self->captureSample (sample, callbacks)) {
428444 for (const auto &cb : callbacks) {
429445 if (cb) {
430- cb ( sample);
446+ invokeCpuMonitorCallback (cb, sample);
431447 }
432448 }
433449 }
@@ -482,7 +498,8 @@ bool ESPCpuMonitor::computeSampleLocked(CpuUsageSample &out) {
482498 baseline = expectedIdle;
483499 }
484500 }
485- const float idleRatio = baseline > 0 .0f ? static_cast <float >(deltaIdle) / baseline : 0 .0f ;
501+ const float idleRatio =
502+ baseline > 0 .0f ? static_cast <float >(deltaIdle) / baseline : 0 .0f ;
486503 const float usage = clampUsagePercent (100 .0f * (1 .0f - idleRatio));
487504 perCoreUsage[i] = usage;
488505 avg += usage;
@@ -496,8 +513,7 @@ bool ESPCpuMonitor::computeSampleLocked(CpuUsageSample &out) {
496513 calibrationSamplesDone_++;
497514 if (calibrationSamplesDone_ >= calibrationSamplesNeeded_) {
498515 const float calibrationWindowUs = static_cast <float >(calibrationWindowUs_);
499- const float fallbackWindowUs =
500- static_cast <float >(config_.sampleIntervalMs ) * 1000 .0f ;
516+ const float fallbackWindowUs = static_cast <float >(config_.sampleIntervalMs ) * 1000 .0f ;
501517 for (int i = 0 ; i < portNUM_PROCESSORS; ++i) {
502518 idleBaseline_[i] /= static_cast <float >(calibrationSamplesNeeded_);
503519 if (idleBaseline_[i] < 1 .0f ) {
0 commit comments