From 576abc1c901fc20af0a89fbe11023765cb87996f Mon Sep 17 00:00:00 2001 From: Siwat Sirichai Date: Sat, 30 Sep 2023 00:56:25 +0700 Subject: [PATCH] timer demo --- src/espmega_iot_timer.cpp | 13 ++++++++----- src/espmega_iot_timer.hpp | 5 +++-- src/user_code.cpp | 32 +++++++++++++------------------- src/user_code.hpp | 3 ++- 4 files changed, 26 insertions(+), 27 deletions(-) diff --git a/src/espmega_iot_timer.cpp b/src/espmega_iot_timer.cpp index f437978..eb25edd 100644 --- a/src/espmega_iot_timer.cpp +++ b/src/espmega_iot_timer.cpp @@ -1,6 +1,6 @@ #include "espmega_iot_timer.hpp" -void Timer::loop() { +void ESPMega_Timer::loop() { rtctime_t curtime = ESPMega_getTime(); if(today!=curtime.day) { today=curtime.day; @@ -14,18 +14,21 @@ void Timer::loop() { } } -Timer::Timer(uint8_t hour,uint8_t minute,void(*timer_callback)(), uint32_t fram_address) { - rtctime_t curtime = ESPMega_getTime(); - this-> today = curtime.day; +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 Timer::set(uint8_t hour,uint8_t minute) { +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; diff --git a/src/espmega_iot_timer.hpp b/src/espmega_iot_timer.hpp index 9fef3f4..395c6e8 100644 --- a/src/espmega_iot_timer.hpp +++ b/src/espmega_iot_timer.hpp @@ -1,11 +1,12 @@ #pragma once #include -class Timer { +class ESPMega_Timer { public: void loop(); - Timer(uint8_t hour,uint8_t minute,void(*timer_callback)(), uint32_t fram_address); + ESPMega_Timer(uint8_t hour,uint8_t minute,void(*timer_callback)(), uint32_t fram_address); void set(uint8_t hour,uint8_t minute); + void begin(); private: uint8_t today; uint8_t timer_ran_today; diff --git a/src/user_code.cpp b/src/user_code.cpp index 9d970d7..4c93cf9 100644 --- a/src/user_code.cpp +++ b/src/user_code.cpp @@ -1,19 +1,21 @@ #include -int today = 0; -bool timer1_ran_today = false; -uint8_t timer1_hr = 0; -uint8_t timer1_min = 15; -uint8_t today = 0; + +void timer1_callback(){ + for(int i=0;i<16;i++){ + pwm_set_state(i,1); + } +} + +ESPMega_Timer timer1(0,50,timer1_callback,15001); /* This Code will run right after ESPMega PRO's Peripheral Initialization Routine */ + void user_pre_init() { - timer1_ran_today = ESPMega_FRAM.read8(15000); - rtctime_t curtime = ESPMega_getTime(); - today = curtime.day; + } /* @@ -21,6 +23,7 @@ This code will run after every component is initialized */ void user_init() { + timer1.begin(); } /* @@ -42,16 +45,7 @@ This code will run every 15 seconds */ 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); + if(standalone){ + timer1.loop(); } } diff --git a/src/user_code.hpp b/src/user_code.hpp index c150e20..4c81694 100644 --- a/src/user_code.hpp +++ b/src/user_code.hpp @@ -35,4 +35,5 @@ extern bool input_get_state(int id); extern void ac_set_state(int mode, int temperature, int fan_speed); extern uint8_t ac_get_temperature(); extern uint8_t ac_get_mode(); -extern uint8_t ac_get_fan_speed(); \ No newline at end of file +extern uint8_t ac_get_fan_speed(); +extern bool standalone; \ No newline at end of file