fix: use light timer callback
This commit is contained in:
parent
fd24e602e5
commit
fd087b4a80
@ -10,12 +10,12 @@
|
||||
|
||||
// Init const and global objects
|
||||
|
||||
// PREFRENCES
|
||||
// Preferences
|
||||
Preferences preferences;
|
||||
#define RW_MODE false
|
||||
#define RO_MODE true
|
||||
|
||||
// WIFI
|
||||
// Wifi
|
||||
const char* ssid = "Wokwi-GUEST";
|
||||
const char* password = "";
|
||||
const uint8_t wifi_loop_max = 10;
|
||||
@ -27,14 +27,14 @@ const char* ntpServer2 = "time.nist.gov";
|
||||
const long gmtOffset_sec = 2 * 3600; // GMT + 2
|
||||
const int daylightOffset_sec = 0;
|
||||
|
||||
// TIMER
|
||||
// Timer
|
||||
hw_timer_t*timer_main = NULL;
|
||||
uint32_t timer_interval = 3000000;
|
||||
uint16_t timer_interval_in_secs = timer_interval / 1000000;
|
||||
;
|
||||
|
||||
// SCHEDULER
|
||||
bool brun_scheduler = true;
|
||||
// Scheduler
|
||||
bool scheduler_flag = true;
|
||||
uint8_t current_hour = HOUR_DEFAULT;
|
||||
uint8_t current_minute = MINUTE_DEFAULT;
|
||||
uint8_t current_second = 0;
|
||||
@ -47,7 +47,7 @@ uint16_t* scheduler_list[] = {
|
||||
scheduler_2
|
||||
};
|
||||
|
||||
// PINS
|
||||
// Pins and Hardware
|
||||
unsigned int PIN_1 = 2;
|
||||
unsigned int PIN_2 = 4;
|
||||
unsigned int pin_list[2] = {
|
||||
@ -55,33 +55,31 @@ unsigned int pin_list[2] = {
|
||||
PIN_2
|
||||
};
|
||||
|
||||
// ----------------------------------------------------------------------
|
||||
// Local functions
|
||||
// ----------------------------------------------------------------------
|
||||
|
||||
// Time helper
|
||||
uint32_t get_timestamp( uint8_t hour, uint8_t minute, uint8_t second){
|
||||
return ( 3600 * hour + 60 * minute + second) % 86400;
|
||||
}
|
||||
|
||||
bool event_keep_waiting( uint32_t current_ts, uint32_t event_ts ){
|
||||
// Naive Timestamp based event igniter
|
||||
bool scheduler_event_fire( uint32_t current_ts, uint32_t event_ts ){
|
||||
|
||||
// basic case
|
||||
// ts: 11h 41m 11s
|
||||
// ev: 11h 44m 0s
|
||||
// ts < ev => TRUE
|
||||
// basic case = ts:11h41m11s versus ev:11h44m0s
|
||||
// ts < ev means wait
|
||||
if ( current_ts < event_ts ){
|
||||
return true;
|
||||
}
|
||||
// if time looped at midnight
|
||||
// ts: 23h 57m 21s
|
||||
// ev: 0h 1m 0s
|
||||
// ts > ev => TRUE (for high to very low values )
|
||||
if( (current_ts - event_ts) > 600 && event_ts < 300 ) {
|
||||
return true;
|
||||
}
|
||||
// ts: 11h 44m 11s
|
||||
// ev: 11h 44m 0s
|
||||
// ts > ev => FALSE
|
||||
return false;
|
||||
}
|
||||
// if time looped at midnight = ts:23h57m21s versus ev:0h1m0s
|
||||
// in case of high against very low values ts > ev means wait
|
||||
if( (current_ts - event_ts) > 600 && event_ts < 300 ) {
|
||||
return false;
|
||||
}
|
||||
// ts:11h44m11s versus ev:11h44m0s
|
||||
// ts > ev means fire event
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@ -95,10 +93,11 @@ void timeavailable(struct timeval *t)
|
||||
|
||||
// TIMER Callback
|
||||
void IRAM_ATTR onTimer(){
|
||||
brun_scheduler = true;
|
||||
scheduler_flag = true;
|
||||
}
|
||||
void run_scheduler(){
|
||||
// Serial.println("onTimer::run");
|
||||
|
||||
// updater local time variables for NTP and no FTP cases
|
||||
void scheduler_time_update(){
|
||||
// Get the current time via NTP, or downgrade
|
||||
if ( sntp_initialized ){
|
||||
struct tm timeinfo;
|
||||
@ -125,13 +124,19 @@ void run_scheduler(){
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
// Scheduler's main loop
|
||||
void scheduler_action(){
|
||||
|
||||
// Serial.println("onTimer::run");
|
||||
scheduler_time_update();
|
||||
|
||||
// If not expected to run exit
|
||||
current_ts = get_timestamp( current_hour, current_minute, current_second );
|
||||
Serial.printf("onTimer::check event %d %d\n", current_ts, next_event_ts);
|
||||
if( event_keep_waiting( current_ts, next_event_ts ) ){
|
||||
// Serial.printf("onTimer::check event %d %d\n", current_ts, next_event_ts);
|
||||
if( ! scheduler_event_fire( current_ts, next_event_ts ) ){
|
||||
return;
|
||||
}
|
||||
|
||||
@ -165,6 +170,10 @@ void run_scheduler(){
|
||||
return;
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------
|
||||
// Arduino functions
|
||||
// ----------------------------------------------------------------------
|
||||
|
||||
void setup(){
|
||||
|
||||
// HARDWARE
|
||||
@ -177,12 +186,14 @@ void setup(){
|
||||
// PREFERENCES
|
||||
Serial.println("Setup::PREFERENCES");
|
||||
preferences.begin(PREF_NAMESPACE, RW_MODE);
|
||||
bool pref_init = preferences.isKey("nvsInit");
|
||||
bool pref_init = preferences.isKey("init");
|
||||
if (pref_init == false) {
|
||||
preferences.putBool ("init", true);
|
||||
preferences.putBytes(RELAY_1_SCHEDULE, scheduler_1_default, sizeof(scheduler_1_default));
|
||||
preferences.putBytes(RELAY_2_SCHEDULE, scheduler_2_default, sizeof(scheduler_2_default));
|
||||
/// ... more to come
|
||||
}
|
||||
// huh this causes a crash
|
||||
preferences.getBytes(RELAY_1_SCHEDULE, scheduler_1, preferences.getBytesLength(RELAY_1_SCHEDULE));
|
||||
preferences.getBytes(RELAY_2_SCHEDULE, scheduler_2, preferences.getBytesLength(RELAY_2_SCHEDULE));
|
||||
preferences.end();
|
||||
@ -192,7 +203,7 @@ void setup(){
|
||||
Serial.printf("Connecting to %s ", ssid);
|
||||
WiFi.begin(ssid, password);
|
||||
uint8_t wifi_loop_count = 0;
|
||||
while (WiFi.status() != WL_CONNECTED or wifi_loop_count < wifi_loop_max ) {
|
||||
while (WiFi.status() != WL_CONNECTED && wifi_loop_count < wifi_loop_max ) {
|
||||
delay(500);
|
||||
wifi_loop_count++;
|
||||
Serial.print(".");
|
||||
@ -225,10 +236,9 @@ void setup(){
|
||||
}
|
||||
void loop(){
|
||||
|
||||
Serial.println("Loop::Enter");
|
||||
if( brun_scheduler == true ){
|
||||
brun_scheduler = false;
|
||||
run_scheduler();
|
||||
if( scheduler_flag == true ){
|
||||
scheduler_flag = false;
|
||||
scheduler_action();
|
||||
}
|
||||
|
||||
delay(1000);
|
||||
|
Loading…
Reference in New Issue
Block a user