libDaisy
Hardware Library for Daisy
Loading...
Searching...
No Matches
CpuLoadMeter.h
Go to the documentation of this file.
1#pragma once
2
3#include "sys/system.h"
4#include <cmath>
5
6namespace daisy
7{
19{
20 public:
22
29 void Init(float sampleRateInHz,
31 float smoothingFilterCutoffHz = 1.0f)
32 {
34 const auto ticksPerS = float(System::GetTickFreq());
35 ticksPerBlockInv_ = 1.0f / (ticksPerS * secPerBlock);
36
37 // update filter coefficient for smoothing filter (1pole lowpass)
39 const auto cutoffNormalized
40 = smoothingFilterCutoffHz * 2.0f * 3.141592653f / blockRateInHz;
41 // according to
42 // https://en.wikipedia.org/wiki/Low-pass_filter#Simple_infinite_impulse_response_filter
43 smoothingConstant_ = cutoffNormalized / (cutoffNormalized + 1.0f);
44
45 Reset();
46 }
47
49 void OnBlockStart() { currentBlockStartTicks_ = System::GetTick(); }
50
53 {
54 const auto end = System::GetTick();
55 const auto ticksPassed = end - currentBlockStartTicks_;
56 const auto currentBlockLoad
57 = float(ticksPassed) * ticksPerBlockInv_; // usPassed / usPerBlock
58
59 if(firstCycle_)
60 {
61 max_ = min_ = avg_ = currentBlockLoad;
62 firstCycle_ = false;
63 }
64 else
65 {
66 if(currentBlockLoad > max_)
67 max_ = currentBlockLoad;
68 if(currentBlockLoad < min_)
69 min_ = currentBlockLoad;
70
71 avg_ = smoothingConstant_ * currentBlockLoad
72 + (1.0f - smoothingConstant_) * avg_;
73 }
74 }
75
77 float GetAvgCpuLoad() const { return avg_; }
79 float GetMinCpuLoad() const { return min_; }
81 float GetMaxCpuLoad() const { return max_; }
82
84 void Reset()
85 {
86 firstCycle_ = true;
87 avg_ = max_ = min_ = NAN;
88 }
89
90 private:
91 bool firstCycle_;
92 float ticksPerBlockInv_;
93 uint32_t currentBlockStartTicks_;
94 float min_;
95 float max_;
96 float avg_;
97 float smoothingConstant_;
98
99 CpuLoadMeter(const CpuLoadMeter&) = delete;
100 CpuLoadMeter& operator=(const CpuLoadMeter&) = delete;
101};
102} // namespace daisy
Definition CpuLoadMeter.h:19
float GetAvgCpuLoad() const
Definition CpuLoadMeter.h:77
void Reset()
Definition CpuLoadMeter.h:84
void Init(float sampleRateInHz, int blockSizeInSamples, float smoothingFilterCutoffHz=1.0f)
Definition CpuLoadMeter.h:29
CpuLoadMeter()
Definition CpuLoadMeter.h:21
float GetMaxCpuLoad() const
Definition CpuLoadMeter.h:81
void OnBlockStart()
Definition CpuLoadMeter.h:49
void OnBlockEnd()
Definition CpuLoadMeter.h:52
float GetMinCpuLoad() const
Definition CpuLoadMeter.h:79
Definition leddriver.h:33
static uint32_t GetTick()
static uint32_t GetTickFreq()
Hardware defines and helpers for daisy field platform.
Definition index.h:2