iot-firmware/src/espmega_iot_timer.cpp

43 lines
1.3 KiB
C++

#include "espmega_iot_timer.hpp"
void ESPMega_Timer::loop() {
rtctime_t curtime = ESPMega_getTime();
if(today!=curtime.day) {
today=curtime.day;
timer_ran_today = false;
ESPMega_FRAM.write8(fram_address,timer_ran_today);
}
if (!timer_ran_today && (hr < curtime.hours || (hr == curtime.hours && min <= curtime.minutes))) {
timer_ran_today = true;
ESPMega_FRAM.write8(fram_address,timer_ran_today);
timer_callback();
}
}
ESPMega_Timer::ESPMega_Timer(uint8_t hour,uint8_t minute,void(*timer_callback)(), uint32_t fram_address) {
this->hr = hour;
this->min = minute;
this->timer_callback = timer_callback;
this->fram_address = fram_address;
}
void ESPMega_Timer::begin() {
rtctime_t curtime = ESPMega_getTime();
this-> today = curtime.day;
this-> timer_ran_today = ESPMega_FRAM.read8(fram_address);
loop();
}
void ESPMega_Timer::set(uint8_t hour,uint8_t minute) {
rtctime_t curtime = ESPMega_getTime();
if ((hr < curtime.hours || (hr == curtime.hours && min <= curtime.minutes))) {
this->timer_ran_today = true;
ESPMega_FRAM.write8(fram_address,timer_ran_today);
} else {
this->timer_ran_today = false;
ESPMega_FRAM.write8(fram_address,timer_ran_today);
}
hr = hour;
min = minute;
}