fix: use light timer callback

This commit is contained in:
alban 2023-11-02 21:12:18 +01:00
parent fd24e602e5
commit fd087b4a80

View File

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