timer demo

This commit is contained in:
Siwat Sirichai 2023-09-30 00:56:25 +07:00
parent 86d4d3f3d1
commit 576abc1c90
4 changed files with 26 additions and 27 deletions

View File

@ -1,6 +1,6 @@
#include "espmega_iot_timer.hpp" #include "espmega_iot_timer.hpp"
void Timer::loop() { void ESPMega_Timer::loop() {
rtctime_t curtime = ESPMega_getTime(); rtctime_t curtime = ESPMega_getTime();
if(today!=curtime.day) { if(today!=curtime.day) {
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) { ESPMega_Timer::ESPMega_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->hr = hour;
this->min = minute; this->min = minute;
this->timer_callback = timer_callback; this->timer_callback = timer_callback;
this->fram_address = fram_address; 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); this-> timer_ran_today = ESPMega_FRAM.read8(fram_address);
loop(); 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(); rtctime_t curtime = ESPMega_getTime();
if ((hr < curtime.hours || (hr == curtime.hours && min <= curtime.minutes))) { if ((hr < curtime.hours || (hr == curtime.hours && min <= curtime.minutes))) {
this->timer_ran_today = true; this->timer_ran_today = true;

View File

@ -1,11 +1,12 @@
#pragma once #pragma once
#include <ESPMegaPRO.h> #include <ESPMegaPRO.h>
class Timer { class ESPMega_Timer {
public: public:
void loop(); 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 set(uint8_t hour,uint8_t minute);
void begin();
private: private:
uint8_t today; uint8_t today;
uint8_t timer_ran_today; uint8_t timer_ran_today;

View File

@ -1,19 +1,21 @@
#include <user_code.hpp> #include <user_code.hpp>
int today = 0;
bool timer1_ran_today = false; void timer1_callback(){
uint8_t timer1_hr = 0; for(int i=0;i<16;i++){
uint8_t timer1_min = 15; pwm_set_state(i,1);
uint8_t today = 0; }
}
ESPMega_Timer timer1(0,50,timer1_callback,15001);
/* /*
This Code will run right after ESPMega PRO's This Code will run right after ESPMega PRO's
Peripheral Initialization Routine 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;
} }
/* /*
@ -21,6 +23,7 @@ This code will run after every component is initialized
*/ */
void user_init() void user_init()
{ {
timer1.begin();
} }
/* /*
@ -42,16 +45,7 @@ This code will run every 15 seconds
*/ */
void timer_tick_callback() void timer_tick_callback()
{ {
rtctime_t curtime = ESPMega_getTime(); if(standalone){
if(today!=curtime.day) { timer1.loop();
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);
} }
} }

View File

@ -36,3 +36,4 @@ extern void ac_set_state(int mode, int temperature, int fan_speed);
extern uint8_t ac_get_temperature(); extern uint8_t ac_get_temperature();
extern uint8_t ac_get_mode(); extern uint8_t ac_get_mode();
extern uint8_t ac_get_fan_speed(); extern uint8_t ac_get_fan_speed();
extern bool standalone;