Skip to content

Commit

Permalink
added time types to types.h
Browse files Browse the repository at this point in the history
  • Loading branch information
arfrie22 committed Jun 18, 2024
1 parent 8dd0efe commit 1002d52
Show file tree
Hide file tree
Showing 16 changed files with 129 additions and 123 deletions.
4 changes: 2 additions & 2 deletions I2CRTC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,14 @@ bool I2CRTC::detect()
}

// PUBLIC FUNCTIONS
tm_t I2CRTC::get() // Aquire data from buffer and convert to tm_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(tm_t t)
void I2CRTC::set(time_os_t t)
{
tmElements_t tm;
breakTime(t, tm);
Expand Down
5 changes: 3 additions & 2 deletions I2CRTC.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#define MCP7940_ADDR 0x6F
#define PCF8563_ADDR 0x51

#include "types.h"
#include "TimeLib.h"

// library interface description
Expand All @@ -19,8 +20,8 @@ class I2CRTC
// user-accessible "public" interface
public:
I2CRTC();
static tm_t get();
static void set(tm_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();
Expand Down
30 changes: 15 additions & 15 deletions OpenSprinkler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,21 +41,21 @@ byte OpenSprinkler::station_bits[MAX_NUM_BOARDS];
byte OpenSprinkler::engage_booster;
uint16_t OpenSprinkler::baseline_current;

tm_t OpenSprinkler::sensor1_on_timer;
tm_t OpenSprinkler::sensor1_off_timer;
tm_t OpenSprinkler::sensor1_active_lasttime;
tm_t OpenSprinkler::sensor2_on_timer;
tm_t OpenSprinkler::sensor2_off_timer;
tm_t OpenSprinkler::sensor2_active_lasttime;
tm_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;
tm_t OpenSprinkler::checkwt_lasttime;
tm_t OpenSprinkler::checkwt_success_lasttime;
tm_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;

Expand Down Expand Up @@ -412,7 +412,7 @@ static const char days_str[] PROGMEM =
"Sun\0";

/** Calculate local time (UTC time plus time zone offset) */
tm_t OpenSprinkler::now_tz() {
time_os_t OpenSprinkler::now_tz() {
return now()+(int32_t)3600/4*(int32_t)(iopts[IOPT_TIMEZONE]-48);
}

Expand Down Expand Up @@ -1288,7 +1288,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;
tm_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;
Expand All @@ -1313,7 +1313,7 @@ void OpenSprinkler::apply_all_station_bits() {
}

/** Read rain sensor status */
void OpenSprinkler::detect_binarysensor_status(tm_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
Expand Down Expand Up @@ -1377,7 +1377,7 @@ void OpenSprinkler::detect_binarysensor_status(tm_t curr_time) {
}

/** Return program switch status */
byte OpenSprinkler::detect_programswitch_status(tm_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;
Expand Down Expand Up @@ -2446,7 +2446,7 @@ void OpenSprinkler::lcd_print_2digit(int v)
}

/** print time to a given line */
void OpenSprinkler::lcd_print_time(tm_t t)
void OpenSprinkler::lcd_print_time(time_os_t t)
{
#if defined(ESP8266)
lcd.setAutoDisplay(false);
Expand Down
30 changes: 15 additions & 15 deletions OpenSprinkler.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
#ifndef _OPENSPRINKLER_H
#define _OPENSPRINKLER_H

#include "TimeLib.h"
#include "types.h"
#include "defines.h"
#include "utils.h"
#include "gpio.h"
Expand Down Expand Up @@ -235,21 +235,21 @@ class OpenSprinkler {
static byte masters[NUM_MASTER_ZONES][NUM_MASTER_OPTS];

// variables for time keeping
static tm_t sensor1_on_timer; // time when sensor1 is detected on last time
static tm_t sensor1_off_timer; // time when sensor1 is detected off last time
static tm_t sensor1_active_lasttime; // most recent time sensor1 is activated
static tm_t sensor2_on_timer; // time when sensor2 is detected on last time
static tm_t sensor2_off_timer; // time when sensor2 is detected off last time
static tm_t sensor2_active_lasttime; // most recent time sensor1 is activated
static tm_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 tm_t checkwt_lasttime; // time when weather was checked
static tm_t checkwt_success_lasttime; // time when weather check was successful
static tm_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
Expand All @@ -261,7 +261,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 tm_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
Expand Down Expand Up @@ -307,8 +307,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(tm_t curr_time);// update binary (rain, soil) sensor status
static byte detect_programswitch_status(tm_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
Expand All @@ -334,7 +334,7 @@ class OpenSprinkler {
static void lcd_print_pgm(PGM_P PROGMEM str); // print a program memory string
static void lcd_print_line_clear_pgm(PGM_P PROGMEM str, byte line);
#endif
static void lcd_print_time(tm_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
Expand Down
47 changes: 24 additions & 23 deletions TimeLib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,13 @@
#endif

#include "TimeLib.h"
#include "types.h"

static tmElements_t tm; // a cache of time elements
static tm_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(tm_t t) {
void refreshCache(time_os_t t) {
if (t != cacheTime) {
breakTime(t, tm);
cacheTime = t;
Expand All @@ -50,7 +51,7 @@ int hour() { // the hour now
return hour(now());
}

int hour(tm_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;
}
Expand All @@ -59,7 +60,7 @@ int hourFormat12() { // the hour now in 12 hour format
return hourFormat12(now());
}

int hourFormat12(tm_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
Expand All @@ -73,23 +74,23 @@ uint8_t isAM() { // returns true if time now is AM
return !isPM(now());
}

uint8_t isAM(tm_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);
}

uint8_t isPM() { // returns true if PM
return isPM(now());
}

uint8_t isPM(tm_t t) { // returns true if PM
uint8_t isPM(time_os_t t) { // returns true if PM
return (hour(t) >= 12);
}

int minute() {
return minute(now());
}

int minute(tm_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;
}
Expand All @@ -98,7 +99,7 @@ int second() {
return second(now());
}

int second(tm_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;
}
Expand All @@ -107,7 +108,7 @@ int day(){
return(day(now()));
}

int day(tm_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;
}
Expand All @@ -116,7 +117,7 @@ int weekday() { // Sunday is day 1
return weekday(now());
}

int weekday(tm_t t) {
int weekday(time_os_t t) {
refreshCache(t);
return tm.Wday;
}
Expand All @@ -125,7 +126,7 @@ int month(){
return month(now());
}

int month(tm_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;
}
Expand All @@ -134,7 +135,7 @@ int year() { // as in Processing, the full four digit year: (2009, 2010 etc)
return year(now());
}

int year(tm_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);
}
Expand All @@ -148,8 +149,8 @@ int year(tm_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(tm_t timeInput, tmElements_t &tm){
// break the given tm_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 !!!

Expand Down Expand Up @@ -201,8 +202,8 @@ void breakTime(tm_t timeInput, tmElements_t &tm){
tm.Day = time + 1; // day of month
}

tm_t makeTime(tmElements_t &tm){
// assemble time elements into tm_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

Expand All @@ -229,7 +230,7 @@ tm_t makeTime(tmElements_t &tm){
seconds+= tm.Hour * SECS_PER_HOUR;
seconds+= tm.Minute * SECS_PER_MIN;
seconds+= tm.Second;
return (tm_t)seconds;
return (time_os_t)seconds;
}
/*=====================================================*/
/* Low level system time functions */
Expand All @@ -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
tm_t sysUnsyncedTime = 0; // the time sysTime unadjusted by sync
time_os_t sysUnsyncedTime = 0; // the time sysTime unadjusted by sync
#endif


tm_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
Expand All @@ -259,7 +260,7 @@ tm_t now() {
}
if (nextSyncTime <= sysTime) {
if (getTimePtr != 0) {
tm_t t = getTimePtr();
time_os_t t = getTimePtr();
if (t != 0) {
setTime(t);
} else {
Expand All @@ -268,10 +269,10 @@ tm_t now() {
}
}
}
return (tm_t)sysTime;
return (time_os_t)sysTime;
}

void setTime(tm_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
Expand Down Expand Up @@ -315,7 +316,7 @@ void setSyncProvider( getExternalTime getTimeFunction){
now(); // this will sync the clock
}

void setSyncInterval(tm_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;
}
Loading

0 comments on commit 1002d52

Please sign in to comment.