iot-firmware/src/espmega_iot_timer.cpp

43 lines
1.3 KiB
C++
Raw Normal View History

2023-09-29 17:37:31 +00:00
#include "espmega_iot_timer.hpp"
2023-09-29 17:56:25 +00:00
void ESPMega_Timer::loop() {
2023-09-29 17:37:31 +00:00
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();
}
}
2023-09-29 17:56:25 +00:00
ESPMega_Timer::ESPMega_Timer(uint8_t hour,uint8_t minute,void(*timer_callback)(), uint32_t fram_address) {
2023-09-29 17:37:31 +00:00
this->hr = hour;
this->min = minute;
this->timer_callback = timer_callback;
this->fram_address = fram_address;
2023-09-29 17:56:25 +00:00
}
void ESPMega_Timer::begin() {
rtctime_t curtime = ESPMega_getTime();
this-> today = curtime.day;
2023-09-29 17:37:31 +00:00
this-> timer_ran_today = ESPMega_FRAM.read8(fram_address);
loop();
}
2023-09-29 17:56:25 +00:00
void ESPMega_Timer::set(uint8_t hour,uint8_t minute) {
2023-09-29 17:37:31 +00:00
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;
}