diff --git a/I2CRTC.cpp b/I2CRTC.cpp index f3e1f9ed..6832a558 100644 --- a/I2CRTC.cpp +++ b/I2CRTC.cpp @@ -58,14 +58,14 @@ bool I2CRTC::detect() } // PUBLIC FUNCTIONS -time_t I2CRTC::get() // Aquire data from buffer and convert to time_t +time_os_t I2CRTC::get() // Aquire data from buffer and convert to time_os_t { tmElements_t tm; read(tm); return(makeTime(tm)); } -void I2CRTC::set(time_t t) +void I2CRTC::set(time_os_t t) { tmElements_t tm; breakTime(t, tm); diff --git a/I2CRTC.h b/I2CRTC.h index 7dac2433..ca575fea 100644 --- a/I2CRTC.h +++ b/I2CRTC.h @@ -11,6 +11,7 @@ #define MCP7940_ADDR 0x6F #define PCF8563_ADDR 0x51 +#include "types.h" #include "TimeLib.h" // library interface description @@ -19,8 +20,8 @@ class I2CRTC // user-accessible "public" interface public: I2CRTC(); - static time_t get(); - static void set(time_t t); + static time_os_t get(); + static void set(time_os_t t); static void read(tmElements_t &tm); static void write(tmElements_t &tm); static bool detect(); diff --git a/OpenSprinkler.cpp b/OpenSprinkler.cpp index 748258f9..27b8b98e 100644 --- a/OpenSprinkler.cpp +++ b/OpenSprinkler.cpp @@ -41,21 +41,21 @@ byte OpenSprinkler::station_bits[MAX_NUM_BOARDS]; byte OpenSprinkler::engage_booster; uint16_t OpenSprinkler::baseline_current; -time_t OpenSprinkler::sensor1_on_timer; -time_t OpenSprinkler::sensor1_off_timer; -time_t OpenSprinkler::sensor1_active_lasttime; -time_t OpenSprinkler::sensor2_on_timer; -time_t OpenSprinkler::sensor2_off_timer; -time_t OpenSprinkler::sensor2_active_lasttime; -time_t OpenSprinkler::raindelay_on_lasttime; +time_os_t OpenSprinkler::sensor1_on_timer; +time_os_t OpenSprinkler::sensor1_off_timer; +time_os_t OpenSprinkler::sensor1_active_lasttime; +time_os_t OpenSprinkler::sensor2_on_timer; +time_os_t OpenSprinkler::sensor2_off_timer; +time_os_t OpenSprinkler::sensor2_active_lasttime; +time_os_t OpenSprinkler::raindelay_on_lasttime; ulong OpenSprinkler::pause_timer; ulong OpenSprinkler::flowcount_log_start; ulong OpenSprinkler::flowcount_rt; byte OpenSprinkler::button_timeout; -time_t OpenSprinkler::checkwt_lasttime; -time_t OpenSprinkler::checkwt_success_lasttime; -time_t OpenSprinkler::powerup_lasttime; +time_os_t OpenSprinkler::checkwt_lasttime; +time_os_t OpenSprinkler::checkwt_success_lasttime; +time_os_t OpenSprinkler::powerup_lasttime; uint8_t OpenSprinkler::last_reboot_cause = REBOOT_CAUSE_NONE; byte OpenSprinkler::weather_update_flag; @@ -415,7 +415,7 @@ static const char days_str[] PROGMEM = "Sun\0"; /** Calculate local time (UTC time plus time zone offset) */ -time_t OpenSprinkler::now_tz() { +time_os_t OpenSprinkler::now_tz() { return now()+(int32_t)3600/4*(int32_t)(iopts[IOPT_TIMEZONE]-48); } @@ -467,6 +467,7 @@ byte OpenSprinkler::start_network() { if (start_ether()) { useEth = true; + WiFi.mode(WIFI_OFF); } else { useEth = false; } @@ -583,25 +584,26 @@ byte OpenSprinkler::start_ether() { lcd_print_line_clear_pgm(PSTR("Start wired link"), 1); lcd_print_line_clear_pgm(eth.isW5500 ? PSTR(" (w5500) ") : PSTR(" (enc28j60) "), 2); - ulong timeout = millis()+30000; // 30 seconds time out - while (!eth.connected()) { + ulong timeout = millis()+60000; // 60 seconds time out + while (!eth.connected() && millis()timeout) return 0; } - - DEBUG_PRINTLN(); - DEBUG_PRINT("eth.ip:"); - DEBUG_PRINTLN(eth.localIP()); - DEBUG_PRINT("eth.dns:"); - DEBUG_PRINTLN(WiFi.dnsIP()); - - if (iopts[IOPT_USE_DHCP]) { - memcpy(iopts+IOPT_STATIC_IP1, &(eth.localIP()[0]), 4); - memcpy(iopts+IOPT_GATEWAY_IP1, &(eth.gatewayIP()[0]),4); - memcpy(iopts+IOPT_DNS_IP1, &(WiFi.dnsIP()[0]), 4); // todo: lwip need dns ip - memcpy(iopts+IOPT_SUBNET_MASK1, &(eth.subnetMask()[0]), 4); - iopts_save(); + // if wired connection is not done at this point, return directly + if(eth.connected()) { + DEBUG_PRINTLN(); + DEBUG_PRINT("eth.ip:"); + DEBUG_PRINTLN(eth.localIP()); + DEBUG_PRINT("eth.dns:"); + DEBUG_PRINTLN(WiFi.dnsIP()); + + if (iopts[IOPT_USE_DHCP]) { + memcpy(iopts+IOPT_STATIC_IP1, &(eth.localIP()[0]), 4); + memcpy(iopts+IOPT_GATEWAY_IP1, &(eth.gatewayIP()[0]),4); + memcpy(iopts+IOPT_DNS_IP1, &(WiFi.dnsIP()[0]), 4); // todo: lwip need dns ip + memcpy(iopts+IOPT_SUBNET_MASK1, &(eth.subnetMask()[0]), 4); + iopts_save(); + } } return 1; @@ -634,7 +636,8 @@ byte OpenSprinkler::start_ether() { bool OpenSprinkler::network_connected(void) { #if defined (ESP8266) - if(useEth) return true; // todo: lwip currently does not have a way to check link status + if(useEth) + return eth.connected(); else return (get_wifi_mode()==WIFI_MODE_STA && WiFi.status()==WL_CONNECTED && state==OS_STATE_CONNECTED); #else @@ -1044,7 +1047,7 @@ pinModeExt(PIN_BUTTON_3, INPUT_PULLUP); RTC.detect(); #else - DEBUG_PRINTLN(get_runtime_path()); + //DEBUG_PRINTLN(get_runtime_path()); #endif } @@ -1295,7 +1298,7 @@ void OpenSprinkler::apply_all_station_bits() { // we refresh the station that's next in line static byte next_sid_to_refresh = MAX_NUM_STATIONS>>1; static byte lastnow = 0; - time_t curr_time = now_tz(); + time_os_t curr_time = now_tz(); byte _now = (curr_time & 0xFF); if (lastnow != _now) { // perform this no more than once per second lastnow = _now; @@ -1320,7 +1323,7 @@ void OpenSprinkler::apply_all_station_bits() { } /** Read rain sensor status */ -void OpenSprinkler::detect_binarysensor_status(time_t curr_time) { +void OpenSprinkler::detect_binarysensor_status(time_os_t curr_time) { // sensor_type: 0 if normally closed, 1 if normally open if(iopts[IOPT_SENSOR1_TYPE]==SENSOR_TYPE_RAIN || iopts[IOPT_SENSOR1_TYPE]==SENSOR_TYPE_SOIL) { if(hw_rev>=2) pinModeExt(PIN_SENSOR1, INPUT_PULLUP); // this seems necessary for OS 3.2 @@ -1384,7 +1387,7 @@ void OpenSprinkler::detect_binarysensor_status(time_t curr_time) { } /** Return program switch status */ -byte OpenSprinkler::detect_programswitch_status(time_t curr_time) { +byte OpenSprinkler::detect_programswitch_status(time_os_t curr_time) { byte ret = 0; if(iopts[IOPT_SENSOR1_TYPE]==SENSOR_TYPE_PSWITCH) { static byte sensor1_hist = 0; @@ -2477,7 +2480,7 @@ void OpenSprinkler::lcd_print_2digit(int v) } /** print time to a given line */ -void OpenSprinkler::lcd_print_time(time_t t) +void OpenSprinkler::lcd_print_time(time_os_t t) { #if defined(USE_SSD1306) lcd.setAutoDisplay(false); diff --git a/OpenSprinkler.h b/OpenSprinkler.h index d2ce013a..b3d425e4 100644 --- a/OpenSprinkler.h +++ b/OpenSprinkler.h @@ -25,6 +25,7 @@ #ifndef _OPENSPRINKLER_H #define _OPENSPRINKLER_H +#include "types.h" #include "defines.h" #include "utils.h" #include "gpio.h" @@ -241,21 +242,21 @@ class OpenSprinkler { static byte masters[NUM_MASTER_ZONES][NUM_MASTER_OPTS]; // variables for time keeping - static time_t sensor1_on_timer; // time when sensor1 is detected on last time - static time_t sensor1_off_timer; // time when sensor1 is detected off last time - static time_t sensor1_active_lasttime; // most recent time sensor1 is activated - static time_t sensor2_on_timer; // time when sensor2 is detected on last time - static time_t sensor2_off_timer; // time when sensor2 is detected off last time - static time_t sensor2_active_lasttime; // most recent time sensor1 is activated - static time_t raindelay_on_lasttime; // time when the most recent rain delay started + static time_os_t sensor1_on_timer; // time when sensor1 is detected on last time + static time_os_t sensor1_off_timer; // time when sensor1 is detected off last time + static time_os_t sensor1_active_lasttime; // most recent time sensor1 is activated + static time_os_t sensor2_on_timer; // time when sensor2 is detected on last time + static time_os_t sensor2_off_timer; // time when sensor2 is detected off last time + static time_os_t sensor2_active_lasttime; // most recent time sensor1 is activated + static time_os_t raindelay_on_lasttime; // time when the most recent rain delay started static ulong pause_timer; // count down timer in paused state static ulong flowcount_rt; // flow count (for computing real-time flow rate) static ulong flowcount_log_start; // starting flow count (for logging) static byte button_timeout; // button timeout - static time_t checkwt_lasttime; // time when weather was checked - static time_t checkwt_success_lasttime; // time when weather check was successful - static time_t powerup_lasttime; // time when controller is powered up most recently + static time_os_t checkwt_lasttime; // time when weather was checked + static time_os_t checkwt_success_lasttime; // time when weather check was successful + static time_os_t powerup_lasttime; // time when controller is powered up most recently static uint8_t last_reboot_cause; // last reboot cause static byte weather_update_flag; // member functions @@ -267,7 +268,7 @@ class OpenSprinkler { static byte start_ether(); // initialize ethernet with the given mac and port static bool network_connected(); // check if the network is up static bool load_hardware_mac(byte* buffer, bool wired=false); // read hardware mac address - static time_t now_tz(); + static time_os_t now_tz(); // -- station names and attributes static void get_station_data(byte sid, StationData* data); // get station data static void set_station_data(byte sid, StationData* data); // set station data @@ -313,8 +314,8 @@ class OpenSprinkler { static void disable(); // disable controller operation, all stations will be closed immediately static void raindelay_start(); // start raindelay static void raindelay_stop(); // stop rain delay - static void detect_binarysensor_status(time_t curr_time);// update binary (rain, soil) sensor status - static byte detect_programswitch_status(time_t curr_time); // get program switch status + static void detect_binarysensor_status(time_os_t curr_time);// update binary (rain, soil) sensor status + static byte detect_programswitch_status(time_os_t curr_time); // get program switch status static void sensor_resetall(); static uint16_t read_current(); // read current sensing value @@ -333,7 +334,7 @@ class OpenSprinkler { static int8_t send_http_request(char* server_with_port, char* p, void(*callback)(char*)=NULL, uint16_t timeout=5000); // -- LCD functions #if defined(USE_DISPLAY) - static void lcd_print_time(time_t t); // print current time + static void lcd_print_time(time_os_t t); // print current time static void lcd_print_ip(const byte *ip, byte endian); // print ip static void lcd_print_mac(const byte *mac); // print mac static void lcd_print_screen(char c); // print station bits of the board selected by display_board diff --git a/TimeLib.cpp b/TimeLib.cpp index bf3faf0f..b985a024 100644 --- a/TimeLib.cpp +++ b/TimeLib.cpp @@ -34,12 +34,13 @@ #endif #include "TimeLib.h" +#include "types.h" static tmElements_t tm; // a cache of time elements -static time_t cacheTime; // the time the cache was updated +static time_os_t cacheTime; // the time the cache was updated static uint32_t syncInterval = 300; // time sync will be attempted after this many seconds -void refreshCache(time_t t) { +void refreshCache(time_os_t t) { if (t != cacheTime) { breakTime(t, tm); cacheTime = t; @@ -50,7 +51,7 @@ int hour() { // the hour now return hour(now()); } -int hour(time_t t) { // the hour for the given time +int hour(time_os_t t) { // the hour for the given time refreshCache(t); return tm.Hour; } @@ -59,7 +60,7 @@ int hourFormat12() { // the hour now in 12 hour format return hourFormat12(now()); } -int hourFormat12(time_t t) { // the hour for the given time in 12 hour format +int hourFormat12(time_os_t t) { // the hour for the given time in 12 hour format refreshCache(t); if( tm.Hour == 0 ) return 12; // 12 midnight @@ -73,7 +74,7 @@ uint8_t isAM() { // returns true if time now is AM return !isPM(now()); } -uint8_t isAM(time_t t) { // returns true if given time is AM +uint8_t isAM(time_os_t t) { // returns true if given time is AM return !isPM(t); } @@ -81,7 +82,7 @@ uint8_t isPM() { // returns true if PM return isPM(now()); } -uint8_t isPM(time_t t) { // returns true if PM +uint8_t isPM(time_os_t t) { // returns true if PM return (hour(t) >= 12); } @@ -89,7 +90,7 @@ int minute() { return minute(now()); } -int minute(time_t t) { // the minute for the given time +int minute(time_os_t t) { // the minute for the given time refreshCache(t); return tm.Minute; } @@ -98,7 +99,7 @@ int second() { return second(now()); } -int second(time_t t) { // the second for the given time +int second(time_os_t t) { // the second for the given time refreshCache(t); return tm.Second; } @@ -107,7 +108,7 @@ int day(){ return(day(now())); } -int day(time_t t) { // the day for the given time (0-6) +int day(time_os_t t) { // the day for the given time (0-6) refreshCache(t); return tm.Day; } @@ -116,7 +117,7 @@ int weekday() { // Sunday is day 1 return weekday(now()); } -int weekday(time_t t) { +int weekday(time_os_t t) { refreshCache(t); return tm.Wday; } @@ -125,7 +126,7 @@ int month(){ return month(now()); } -int month(time_t t) { // the month for the given time +int month(time_os_t t) { // the month for the given time refreshCache(t); return tm.Month; } @@ -134,7 +135,7 @@ int year() { // as in Processing, the full four digit year: (2009, 2010 etc) return year(now()); } -int year(time_t t) { // the year for the given time +int year(time_os_t t) { // the year for the given time refreshCache(t); return tmYearToCalendar(tm.Year); } @@ -148,8 +149,8 @@ int year(time_t t) { // the year for the given time static const uint8_t monthDays[]={31,28,31,30,31,30,31,31,30,31,30,31}; // API starts months from 1, this array starts from 0 -void breakTime(time_t timeInput, tmElements_t &tm){ -// break the given time_t into time components +void breakTime(time_os_t timeInput, tmElements_t &tm){ +// break the given time_os_t into time components // this is a more compact version of the C library localtime function // note that year is offset from 1970 !!! @@ -201,8 +202,8 @@ void breakTime(time_t timeInput, tmElements_t &tm){ tm.Day = time + 1; // day of month } -time_t makeTime(tmElements_t &tm){ -// assemble time elements into time_t +time_os_t makeTime(tmElements_t &tm){ +// assemble time elements into time_os_t // note year argument is offset from 1970 (see macros in time.h to convert to other formats) // previous version used full four digit year (or digits since 2000),i.e. 2009 was 2009 or 9 @@ -229,7 +230,7 @@ time_t makeTime(tmElements_t &tm){ seconds+= tm.Hour * SECS_PER_HOUR; seconds+= tm.Minute * SECS_PER_MIN; seconds+= tm.Second; - return (time_t)seconds; + return (time_os_t)seconds; } /*=====================================================*/ /* Low level system time functions */ @@ -243,11 +244,11 @@ getExternalTime getTimePtr; // pointer to external sync function //setExternalTime setTimePtr; // not used in this version #ifdef TIME_DRIFT_INFO // define this to get drift data -time_t sysUnsyncedTime = 0; // the time sysTime unadjusted by sync +time_os_t sysUnsyncedTime = 0; // the time sysTime unadjusted by sync #endif -time_t now() { +time_os_t now() { // calculate number of seconds passed since last call to now() while (millis() - prevMillis >= 1000) { // millis() and prevMillis are both unsigned ints thus the subtraction will always be the absolute value of the difference @@ -259,7 +260,7 @@ time_t now() { } if (nextSyncTime <= sysTime) { if (getTimePtr != 0) { - time_t t = getTimePtr(); + time_os_t t = getTimePtr(); if (t != 0) { setTime(t); } else { @@ -268,10 +269,10 @@ time_t now() { } } } - return (time_t)sysTime; + return (time_os_t)sysTime; } -void setTime(time_t t) { +void setTime(time_os_t t) { #ifdef TIME_DRIFT_INFO if(sysUnsyncedTime == 0) sysUnsyncedTime = t; // store the time of the first call to set a valid Time @@ -315,7 +316,7 @@ void setSyncProvider( getExternalTime getTimeFunction){ now(); // this will sync the clock } -void setSyncInterval(time_t interval){ // set the number of seconds between re-sync +void setSyncInterval(time_os_t interval){ // set the number of seconds between re-sync syncInterval = (uint32_t)interval; nextSyncTime = sysTime + syncInterval; } diff --git a/TimeLib.h b/TimeLib.h index bbb1a83e..348b85f3 100644 --- a/TimeLib.h +++ b/TimeLib.h @@ -12,16 +12,11 @@ #define _Time_h #include +#include "types.h" #ifndef __AVR__ #include // for __time_t_defined, but avr libc lacks sys/types.h #endif - -#if !defined(__time_t_defined) // avoid conflict with newlib or other posix libc -typedef unsigned long time_t; -#endif - - // This ugly hack allows us to define C++ overloaded functions, when included // from within an extern "C", as newlib's sys/stat.h does. Actually it is // intended to include "time.h" from the C library (on ARM, but AVR does not @@ -58,8 +53,8 @@ typedef struct { #define tmYearToY2k(Y) ((Y) - 30) // offset is from 2000 #define y2kYearToTm(Y) ((Y) + 30) -typedef time_t(*getExternalTime)(); -//typedef void (*setExternalTime)(const time_t); // not used in this version +typedef time_os_t(*getExternalTime)(); +//typedef void (*setExternalTime)(const time_os_t); // not used in this version /*==============================================================================*/ @@ -88,7 +83,7 @@ typedef time_t(*getExternalTime)(); #define nextSunday(_time_) ( previousSunday(_time_)+SECS_PER_WEEK) // time at the end of the week for the given time -/* Useful Macros for converting elapsed time to a time_t */ +/* Useful Macros for converting elapsed time to a time_os_t */ #define minutesToTime_t ((M)) ( (M) * SECS_PER_MIN) #define hoursToTime_t ((H)) ( (H) * SECS_PER_HOUR) #define daysToTime_t ((D)) ( (D) * SECS_PER_DAY) // fixed on Jul 22 2011 @@ -97,28 +92,28 @@ typedef time_t(*getExternalTime)(); /*============================================================================*/ /* time and date functions */ int hour(); // the hour now -int hour(time_t t); // the hour for the given time +int hour(time_os_t t); // the hour for the given time int hourFormat12(); // the hour now in 12 hour format -int hourFormat12(time_t t); // the hour for the given time in 12 hour format +int hourFormat12(time_os_t t); // the hour for the given time in 12 hour format uint8_t isAM(); // returns true if time now is AM -uint8_t isAM(time_t t); // returns true the given time is AM +uint8_t isAM(time_os_t t); // returns true the given time is AM uint8_t isPM(); // returns true if time now is PM -uint8_t isPM(time_t t); // returns true the given time is PM +uint8_t isPM(time_os_t t); // returns true the given time is PM int minute(); // the minute now -int minute(time_t t); // the minute for the given time +int minute(time_os_t t); // the minute for the given time int second(); // the second now -int second(time_t t); // the second for the given time +int second(time_os_t t); // the second for the given time int day(); // the day now -int day(time_t t); // the day for the given time +int day(time_os_t t); // the day for the given time int weekday(); // the weekday now (Sunday is day 1) -int weekday(time_t t); // the weekday for the given time +int weekday(time_os_t t); // the weekday for the given time int month(); // the month now (Jan is month 1) -int month(time_t t); // the month for the given time +int month(time_os_t t); // the month for the given time int year(); // the full four digit year: (2009, 2010 etc) -int year(time_t t); // the year for the given time +int year(time_os_t t); // the year for the given time -time_t now(); // return the current time as seconds since Jan 1 1970 -void setTime(time_t t); +time_os_t now(); // return the current time as seconds since Jan 1 1970 +void setTime(time_os_t t); void setTime(int hr,int min,int sec,int day, int month, int yr); void adjustTime(long adjustment); @@ -132,11 +127,11 @@ char* dayShortStr(uint8_t day); /* time sync functions */ timeStatus_t timeStatus(); // indicates if time has been set and recently synchronized void setSyncProvider( getExternalTime getTimeFunction); // identify the external time provider -void setSyncInterval(time_t interval); // set the number of seconds between re-sync +void setSyncInterval(time_os_t interval); // set the number of seconds between re-sync /* low level functions to convert to and from system time */ -void breakTime(time_t time, tmElements_t &tm); // break time_t into elements -time_t makeTime(tmElements_t &tm); // convert time elements into time_t +void breakTime(time_os_t time, tmElements_t &tm); // break time_os_t into elements +time_os_t makeTime(tmElements_t &tm); // convert time elements into time_os_t } // extern "C++" #endif // __cplusplus diff --git a/defines.h b/defines.h index ef7be3a1..aa270f16 100755 --- a/defines.h +++ b/defines.h @@ -36,7 +36,7 @@ typedef unsigned long ulong; // if this number is different from the one stored in non-volatile memory // a device reset will be automatically triggered -#define OS_FW_MINOR 4 // Firmware minor version +#define OS_FW_MINOR 5 // Firmware minor version /** Hardware version base numbers */ #define OS_HW_VERSION_BASE 0x00 // OpenSprinkler @@ -64,6 +64,7 @@ typedef unsigned long ulong; #define STN_TYPE_REMOTE 0x02 // Remote OpenSprinkler station #define STN_TYPE_GPIO 0x03 // direct GPIO station #define STN_TYPE_HTTP 0x04 // HTTP station +#define STN_TYPE_HTTPS 0x05 // HTTPS station #define STN_TYPE_OTHER 0xFF /** Notification macro defines */ @@ -472,12 +473,14 @@ enum { #define DEBUG_BEGIN(x) {Serial.begin(x);} #define DEBUG_PRINT(x) {Serial.print(x);} #define DEBUG_PRINTLN(x) {Serial.println(x);} + #define DEBUG_PRINTF(msg, ...) {Serial.printf(msg, ##__VA_ARGS__);} #else #include #define DEBUG_BEGIN(x) {} /** Serial debug functions */ inline void DEBUG_PRINT(int x) {printf("%d", x);} inline void DEBUG_PRINT(const char*s) {printf("%s", s);} #define DEBUG_PRINTLN(x) {DEBUG_PRINT(x);printf("\n");} + #define DEBUG_PRINTF(msg, ...) {printf(msg, ##__VA_ARGS__);} #endif #else @@ -485,6 +488,7 @@ enum { #define DEBUG_BEGIN(x) {} #define DEBUG_PRINT(x) {} #define DEBUG_PRINTLN(x) {} + #define DEBUG_PRINTF(x, ...) {} #endif @@ -501,6 +505,7 @@ enum { #define PSTR(x) x #define F(x) x #define strcat_P strcat + #define strncat_P strncat #define strcpy_P strcpy #define sprintf_P sprintf #include diff --git a/main.cpp b/main.cpp index a6aa916b..ac608a03 100644 --- a/main.cpp +++ b/main.cpp @@ -23,6 +23,7 @@ #include +#include "types.h" #include "OpenSprinkler.h" #include "program.h" #include "weather.h" @@ -470,7 +471,7 @@ void do_loop() } } - static time_t last_time = 0; + static time_os_t last_time = 0; static ulong last_minute = 0; byte bid, sid, s, pid, qid, gid, bitvalue; @@ -478,7 +479,7 @@ void do_loop() os.status.mas = os.iopts[IOPT_MASTER_STATION]; os.status.mas2= os.iopts[IOPT_MASTER_STATION_2]; - time_t curr_time = os.now_tz(); + time_os_t curr_time = os.now_tz(); // ====== Process Ethernet packets ====== #if defined(ARDUINO) // Process Ethernet packets for Arduino @@ -874,7 +875,7 @@ void do_loop() // check through runtime queue, calculate the last stop time of sequential stations memset(pd.last_seq_stop_times, 0, sizeof(ulong)*NUM_SEQ_GROUPS); - time_t sst; + time_os_t sst; byte re=os.iopts[IOPT_REMOTE_EXT_MODE]; q = pd.queue; for(;q os.checkwt_success_lasttime + CHECK_WEATHER_SUCCESS_TIMEOUT)) { // if last successful weather call timestamp is more than allowed threshold // and if the selected adjustment method is not one of the manual methods @@ -1098,9 +1099,9 @@ void turn_on_station(byte sid, ulong duration) { } // after removing element q, update remaining stations in its group -void handle_shift_remaining_stations(RuntimeQueueStruct* q, byte gid, time_t curr_time) { +void handle_shift_remaining_stations(RuntimeQueueStruct* q, byte gid, time_os_t curr_time) { RuntimeQueueStruct *s = pd.queue; - time_t q_end_time = q->st + q->dur; + time_os_t q_end_time = q->st + q->dur; ulong remainder = 0; if (q_end_time > curr_time) { // remainder is non-zero @@ -1128,7 +1129,7 @@ void handle_shift_remaining_stations(RuntimeQueueStruct* q, byte gid, time_t cur * writes a log record and determines if * the station should be removed from the queue */ -void turn_off_station(byte sid, time_t curr_time, byte shift) { +void turn_off_station(byte sid, time_os_t curr_time, byte shift) { byte qid = pd.station_qid[sid]; // ignore request if trying to turn off a zone that's not even in the queue @@ -1197,7 +1198,7 @@ void turn_off_station(byte sid, time_t curr_time, byte shift) { * such as rain delay, rain sensing * and turn off stations accordingly */ -void process_dynamic_events(time_t curr_time) { +void process_dynamic_events(time_os_t curr_time) { // check if rain is detected bool sn1 = false; bool sn2 = false; @@ -1246,7 +1247,7 @@ void process_dynamic_events(time_t curr_time) { * this function determines the appropriate start and dequeue times * of stations bound to master stations with on and off adjustments */ -void handle_master_adjustments(time_t curr_time, RuntimeQueueStruct *q) { +void handle_master_adjustments(time_os_t curr_time, RuntimeQueueStruct *q) { int16_t start_adj = 0; int16_t dequeue_adj = 0; @@ -1278,7 +1279,7 @@ void handle_master_adjustments(time_t curr_time, RuntimeQueueStruct *q) { * This function loops through the queue * and schedules the start time of each station */ -void schedule_all_stations(time_t curr_time) { +void schedule_all_stations(time_os_t curr_time) { ulong con_start_time = curr_time + 1; // concurrent start time // if the queue is paused, make sure the start time is after the scheduled pause ends if (os.status.pause_state) { @@ -1635,7 +1636,7 @@ static const char log_type_names[] PROGMEM = "cu\0"; /** write run record to log on SD card */ -void write_log(byte type, time_t curr_time) { +void write_log(byte type, time_os_t curr_time) { if (!os.iopts[IOPT_ENABLE_LOGGING]) return; @@ -1761,10 +1762,10 @@ void write_log(byte type, time_t curr_time) { #if defined(ESP8266) bool delete_log_oldest() { Dir dir = LittleFS.openDir(LOG_PREFIX); - time_t oldest_t = ULONG_MAX; + time_os_t oldest_t = ULONG_MAX; String oldest_fn; while (dir.next()) { - time_t t = dir.fileCreationTime(); + time_os_t t = dir.fileCreationTime(); if(t - #define DEBUG_PRINTF(msg, ...) {printf(msg, ##__VA_ARGS__);} - #define DEBUG_TIMESTAMP() {char tstr[21]; time_t t = time(NULL); struct tm *tm = localtime(&t); strftime(tstr, 21, "%y-%m-%d %H:%M:%S - ", tm);printf("%s", tstr);} + #define DEBUG_TIMESTAMP() {char tstr[21]; time_os_t t = time(NULL); struct tm *tm = localtime(&t); strftime(tstr, 21, "%y-%m-%d %H:%M:%S - ", tm);printf("%s", tstr);} #endif #define DEBUG_LOGF(msg, ...) {DEBUG_TIMESTAMP(); DEBUG_PRINTF(msg, ##__VA_ARGS__);} static unsigned long _lastMillis = 0; // Holds the timestamp associated with the last call to DEBUG_DURATION() inline unsigned long DEBUG_DURATION() {unsigned long dur = millis() - _lastMillis; _lastMillis = millis(); return dur;} #else - #define DEBUG_PRINTF(msg, ...) {} #define DEBUG_LOGF(msg, ...) {} #define DEBUG_DURATION() {} #endif diff --git a/opensprinkler_server.cpp b/opensprinkler_server.cpp index 8d825373..02075557 100644 --- a/opensprinkler_server.cpp +++ b/opensprinkler_server.cpp @@ -21,6 +21,7 @@ * . */ +#include "types.h" #include "OpenSprinkler.h" #include "program.h" #include "opensprinkler_server.h" @@ -1095,7 +1096,7 @@ function rst_wsp() {document.getElementById('wsp').value='$S';})"), void server_json_controller_main(OTF_PARAMS_DEF) { byte bid, sid; - time_t curr_time = os.now_tz(); + time_os_t curr_time = os.now_tz(); bfill.emit_p(PSTR("\"devt\":$L,\"nbrd\":$D,\"en\":$D,\"sn1\":$D,\"sn2\":$D,\"rd\":$D,\"rdst\":$L," "\"sunrise\":$D,\"sunset\":$D,\"eip\":$L,\"lwc\":$L,\"lswc\":$L," "\"lupt\":$L,\"lrbtc\":$D,\"lrun\":[$D,$D,$D,$L],\"pq\":$D,\"pt\":$L,\"nq\":$D,"), diff --git a/platformio.ini b/platformio.ini index 82ad5dd3..8ca2903f 100644 --- a/platformio.ini +++ b/platformio.ini @@ -16,7 +16,7 @@ src_dir = . include_dir = . [env:d1_mini] -platform = espressif8266@4.2.0 +platform = espressif8266@4.2.1 board = d1_mini framework = arduino lib_ldf_mode = deep diff --git a/program.cpp b/program.cpp index 418aa866..2e1042bd 100644 --- a/program.cpp +++ b/program.cpp @@ -37,7 +37,7 @@ byte ProgramData::nqueue = 0; RuntimeQueueStruct ProgramData::queue[RUNTIME_QUEUE_SIZE]; byte ProgramData::station_qid[MAX_NUM_STATIONS]; LogStruct ProgramData::lastrun; -time_t ProgramData::last_seq_stop_times[NUM_SEQ_GROUPS]; +time_os_t ProgramData::last_seq_stop_times[NUM_SEQ_GROUPS]; extern char tmp_buffer[]; @@ -139,7 +139,7 @@ void ProgramData::toggle_pause(ulong delay) { void ProgramData::set_pause() { RuntimeQueueStruct *q = queue; - time_t curr_t = os.now_tz(); + time_os_t curr_t = os.now_tz(); for (; q < queue + nqueue; q++) { @@ -225,14 +225,14 @@ int16_t ProgramStruct::starttime_decode(int16_t t) { } /** Check if a given time matches the program's start day */ -byte ProgramStruct::check_day_match(time_t t) { +byte ProgramStruct::check_day_match(time_os_t t) { #if defined(ARDUINO) // get current time from Arduino byte weekday_t = weekday(t); // weekday ranges from [0,6] within Sunday being 1 byte day_t = day(t); byte month_t = month(t); #else // get current time from RPI/BBB - time_t ct = t; + time_os_t ct = t; struct tm *ti = gmtime(&ct); byte weekday_t = (ti->tm_wday+1)%7; // tm_wday ranges from [0,6] with Sunday being 0 byte day_t = ti->tm_mday; @@ -294,7 +294,7 @@ byte ProgramStruct::check_day_match(time_t t) { // Check if a given time matches program's start time // this also checks for programs that started the previous // day and ran over night -byte ProgramStruct::check_match(time_t t) { +byte ProgramStruct::check_match(time_os_t t) { // check program enable status if (!enabled) return 0; diff --git a/program.h b/program.h index 39a18f3d..a50a7961 100644 --- a/program.h +++ b/program.h @@ -31,6 +31,7 @@ #define RUNTIME_QUEUE_SIZE MAX_NUM_STATIONS #define PROGRAMSTRUCT_SIZE sizeof(ProgramStruct) #include "OpenSprinkler.h" +#include "types.h" /** Log data structure */ struct LogStruct { @@ -103,12 +104,12 @@ class ProgramStruct { char name[PROGRAM_NAME_SIZE]; int16_t daterange[2] = {MIN_ENCODED_DATE, MAX_ENCODED_DATE}; // date range: start date, end date - byte check_match(time_t t); + byte check_match(time_os_t t); int16_t starttime_decode(int16_t t); protected: - byte check_day_match(time_t t); + byte check_day_match(time_os_t t); }; @@ -116,11 +117,11 @@ extern OpenSprinkler os; class RuntimeQueueStruct { public: - time_t st; // start time + time_os_t st; // start time uint16_t dur; // water time byte sid; byte pid; - time_t deque_time; // deque time, which can be larger than st+dur to allow positive master off adjustment time + time_os_t deque_time; // deque time, which can be larger than st+dur to allow positive master off adjustment time }; class ProgramData { @@ -130,7 +131,7 @@ class ProgramData { static byte station_qid[]; // this array stores the queue element index for each scheduled station static byte nprograms; // number of programs static LogStruct lastrun; - static time_t last_seq_stop_times[]; // the last stop time of a sequential station (for each sequential group respectively) + static time_os_t last_seq_stop_times[]; // the last stop time of a sequential station (for each sequential group respectively) static void toggle_pause(ulong delay); static void set_pause(); diff --git a/types.h b/types.h new file mode 100644 index 00000000..d03aee03 --- /dev/null +++ b/types.h @@ -0,0 +1,12 @@ +#ifndef TYPES_H +#define TYPES_H +#include + +#if defined(ARDUINO) +typedef unsigned long time_os_t; +#else +#include +typedef time_t time_os_t; +#endif + +#endif \ No newline at end of file diff --git a/utils.cpp b/utils.cpp index afa8d95e..785486d9 100644 --- a/utils.cpp +++ b/utils.cpp @@ -22,6 +22,7 @@ */ #include "utils.h" +#include "types.h" #include "OpenSprinkler.h" extern OpenSprinkler os; diff --git a/weather.cpp b/weather.cpp index 3be40cf5..26ac5672 100644 --- a/weather.cpp +++ b/weather.cpp @@ -27,6 +27,7 @@ #include "opensprinkler_server.h" #include "weather.h" #include "main.h" +#include "types.h" extern OpenSprinkler os; // OpenSprinkler object extern char tmp_buffer[]; @@ -198,13 +199,13 @@ void load_wt_monthly(char* wto) { } } -void apply_monthly_adjustment(time_t curr_time) { +void apply_monthly_adjustment(time_os_t curr_time) { // ====== Check monthly water percentage ====== if(os.iopts[IOPT_USE_WEATHER]==WEATHER_METHOD_MONTHLY) { #if defined(ARDUINO) byte m = month(curr_time)-1; #else - time_t ct = curr_time; + time_os_t ct = curr_time; struct tm *ti = gmtime(&ct); byte m = ti->tm_mon; // tm_mon ranges from [0,11] #endif diff --git a/weather.h b/weather.h index 145dffc2..31e743f3 100644 --- a/weather.h +++ b/weather.h @@ -38,5 +38,5 @@ extern char wt_rawData[]; extern int wt_errCode; extern byte wt_monthly[]; void load_wt_monthly(char* wto); -void apply_monthly_adjustment(time_t curr_time); +void apply_monthly_adjustment(time_os_t curr_time); #endif // _WEATHER_H