Mandatory Build-Time Timebase Selection

#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();
    }
};

int main() {
    Scheduler<SysTickTimer> scheduler;   // must not compile
    std::printf("elapsed_ms=%u\n",
                static_cast<unsigned>(scheduler.elapsed_ms()));
    return 0;
}

Solving Approach

 

 

 

 

 

Upvote
Downvote
Loading...

Input

Expected Output

elapsed_ms=100