Skip to content

Instantly share code, notes, and snippets.

@kinoshita-lab
Created April 9, 2023 02:00
Show Gist options
  • Save kinoshita-lab/0d235ebc739bf3988c16ced732b51893 to your computer and use it in GitHub Desktop.
Save kinoshita-lab/0d235ebc739bf3988c16ced732b51893 to your computer and use it in GitHub Desktop.
/**
* @file FakeTimerInterrupt.h
* @brief
*
* @author Kazuki Saita <saita@kinoshita-lab.com>
*
* Copyright (c) 2022 Kinoshita Lab. All rights reserved.
*
*/
#pragma once
#ifndef FAKETIMERINTERRUPT_H
#define FAKETIMERINTERRUPT_H
#include <stdint.h>
#include <functional>
class FakeTimerInterrupt {
public:
FakeTimerInterrupt(const float sampling_rate) : sampling_rate_(sampling_rate) {
}
void setCallback(std::function<void()> f) {
callback = f;
}
void tick() {
if (max_count_ == 0) {
return;
}
count_++;
if (count_ < max_count_) {
return;
}
//Serial.println("setcallback::timer exceeded");
count_ = 0;
if (!callback) {
return;
}
callback();
}
void setIntervalMsec(const size_t msec) {
max_count_ = (msec * sampling_rate_) / 1000.f;
}
void start() {
count_ = 0;
}
protected:
size_t count_ = 0;
size_t max_count_ = 0;
size_t sampling_rate_ = 0;
std::function<void()> callback = nullptr;
private:
FakeTimerInterrupt(const FakeTimerInterrupt&) {}
};
#endif // FAKETIMERINTERRUPT_H
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment