Move components to lib folder
This commit is contained in:
parent
ae3c20b59d
commit
b9b68f08cb
8 changed files with 1 additions and 0 deletions
38
lib/Timer/Timer.cpp
Normal file
38
lib/Timer/Timer.cpp
Normal file
|
@ -0,0 +1,38 @@
|
|||
#ifndef Timer_cpp
|
||||
#define Timer_cpp
|
||||
|
||||
#include <Arduino.h>
|
||||
#include <functional>
|
||||
|
||||
class Timer{
|
||||
private:
|
||||
unsigned long _interval, _lastTick;
|
||||
std::function<void()> _callback;
|
||||
bool _running;
|
||||
|
||||
public:
|
||||
Timer(std::function<void()> callback){
|
||||
_callback = callback;
|
||||
_running = false;
|
||||
}
|
||||
|
||||
void reset(unsigned long interval){
|
||||
_interval = interval;
|
||||
_lastTick = millis();
|
||||
_running = true;
|
||||
}
|
||||
|
||||
void loop(){
|
||||
if (!_running)
|
||||
return;
|
||||
|
||||
unsigned long tick = millis();
|
||||
|
||||
if (tick - _lastTick >= _interval){
|
||||
_callback();
|
||||
_running = false;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
#endif
|
32
lib/Timer/TimerManager.cpp
Normal file
32
lib/Timer/TimerManager.cpp
Normal file
|
@ -0,0 +1,32 @@
|
|||
#ifndef TimerManager_cpp
|
||||
#define TimerManager_cpp
|
||||
|
||||
#include <map>
|
||||
#include <functional>
|
||||
#include "Timer.cpp"
|
||||
|
||||
class TimerManager{
|
||||
std::map<int, Timer*> _timers;
|
||||
|
||||
public:
|
||||
|
||||
void add(int id, std::function<void()> callback){
|
||||
_timers[id] = new Timer(callback);
|
||||
}
|
||||
|
||||
void reset(int id, unsigned long interval){
|
||||
if (!_timers.count(id))
|
||||
return;
|
||||
_timers.find(id)->second->reset(interval);
|
||||
}
|
||||
|
||||
void loop(){
|
||||
for(auto iterator = _timers.begin(); iterator != _timers.end(); ++iterator)
|
||||
{
|
||||
auto timer = iterator->second;
|
||||
timer->loop();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
#endif
|
Loading…
Add table
Add a link
Reference in a new issue