#include <cstdint>
#include <cstdio>
// Timebase A: SysTick (1 kHz)
struct SysTickTimer {
static std::uint32_t ticks() {
return 100;
}
static std::uint32_t freq_hz() {
return 1000;
}
};
// Timebase B: RTC (32 Hz)
struct RtcTimer {
static std::uint32_t ticks() {
return 4;
}
static std::uint32_t freq_hz() {
return 32;
}
};
// Scheduler requires a timebase type
template<typename Timer>
class Scheduler {
public:
std::uint32_t elapsed_ms() const {
return (Timer::ticks() * 1000U) / Timer::freq_hz();
}
};
using SelectedTimer = SysTickTimer;
int main() {
Scheduler <SelectedTimer> scheduler;
std::printf("elapsed_ms=%u\n",
static_cast<unsigned>(scheduler.elapsed_ms()));
return 0;
}
Expected Output
elapsed_ms=100