From 86d4d3f3d1e744e77affd96f5918441787f5e78d Mon Sep 17 00:00:00 2001 From: Siwat Sirichai Date: Sat, 30 Sep 2023 00:37:31 +0700 Subject: [PATCH] timer function --- src/espmega_iot_core.cpp | 2 +- src/espmega_iot_timer.cpp | 40 +++++++++++++++++++++++++++++++++++++ src/espmega_iot_timer.hpp | 16 +++++++++++++++ src/user_code.cpp | 42 +++++++++++++++++++++++++++++---------- src/user_code.hpp | 4 +--- 5 files changed, 89 insertions(+), 15 deletions(-) create mode 100644 src/espmega_iot_timer.cpp create mode 100644 src/espmega_iot_timer.hpp diff --git a/src/espmega_iot_core.cpp b/src/espmega_iot_core.cpp index 3dcaaf3..f162c9b 100644 --- a/src/espmega_iot_core.cpp +++ b/src/espmega_iot_core.cpp @@ -336,7 +336,7 @@ void thread_initialization() eeprom_pwm_updater.onRun(eeprom_pwm_update); eeprom_pwm_updater.setInterval(1000); user_timer_tick.onRun(timer_tick_callback); - user_timer_tick.setInterval(5000); + user_timer_tick.setInterval(15000); } void pwm_state_callback(String topic, String message) diff --git a/src/espmega_iot_timer.cpp b/src/espmega_iot_timer.cpp new file mode 100644 index 0000000..f437978 --- /dev/null +++ b/src/espmega_iot_timer.cpp @@ -0,0 +1,40 @@ +#include "espmega_iot_timer.hpp" + +void 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(); + } +} + +Timer::Timer(uint8_t hour,uint8_t minute,void(*timer_callback)(), uint32_t fram_address) { + rtctime_t curtime = ESPMega_getTime(); + this-> today = curtime.day; + this->hr = hour; + this->min = minute; + this->timer_callback = timer_callback; + this->fram_address = fram_address; + this-> timer_ran_today = ESPMega_FRAM.read8(fram_address); + loop(); +} + +void 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; +} \ No newline at end of file diff --git a/src/espmega_iot_timer.hpp b/src/espmega_iot_timer.hpp new file mode 100644 index 0000000..9fef3f4 --- /dev/null +++ b/src/espmega_iot_timer.hpp @@ -0,0 +1,16 @@ +#pragma once +#include + +class Timer { + public: + void loop(); + Timer(uint8_t hour,uint8_t minute,void(*timer_callback)(), uint32_t fram_address); + void set(uint8_t hour,uint8_t minute); + private: + uint8_t today; + uint8_t timer_ran_today; + uint8_t hr; + uint8_t min; + uint32_t fram_address; + void (*timer_callback)(); +}; \ No newline at end of file diff --git a/src/user_code.cpp b/src/user_code.cpp index 0e841ee..9d970d7 100644 --- a/src/user_code.cpp +++ b/src/user_code.cpp @@ -1,37 +1,57 @@ #include +int today = 0; +bool timer1_ran_today = false; +uint8_t timer1_hr = 0; +uint8_t timer1_min = 15; +uint8_t today = 0; /* This Code will run right after ESPMega PRO's Peripheral Initialization Routine */ -void user_pre_init() { - +void user_pre_init() +{ + timer1_ran_today = ESPMega_FRAM.read8(15000); + rtctime_t curtime = ESPMega_getTime(); + today = curtime.day; } /* This code will run after every component is initialized */ -void user_init() { - +void user_init() +{ } /* This code will run once every event loop */ -void user_loop() { - +void user_loop() +{ } /* This code will run when an input pin changed state */ -void virtual_interrupt_user_callback(int pin, int state) { - +void virtual_interrupt_user_callback(int pin, int state) +{ } /* -This code will run every 5 seconds +This code will run every 15 seconds */ -void timer_tick_callback() { - +void timer_tick_callback() +{ + rtctime_t curtime = ESPMega_getTime(); + if(today!=curtime.day) { + today=curtime.day; + timer1_ran_today = false; + } + if (!timer1_ran_today && (timer1_hr < curtime.hours || (timer1_hr == curtime.hours && timer1_min <= curtime.minutes))) + { + timer1_ran_today = true; + ESPMega_FRAM.write8(15000,timer1_ran_today); + pwm_set_state(1,true); + pwm_set_value(1,2000); + } } diff --git a/src/user_code.hpp b/src/user_code.hpp index ff77f21..c150e20 100644 --- a/src/user_code.hpp +++ b/src/user_code.hpp @@ -1,9 +1,7 @@ #pragma once #include #include - - - +#include "espmega_iot_timer.hpp" // External LCD Configuration #define ENABLE_EXTERNAL_LCD