From 23a1c3b18c45abf82fd9b13393497d83c4496056 Mon Sep 17 00:00:00 2001 From: Sandeep Mistry Date: Mon, 14 Dec 2015 10:34:41 -0500 Subject: [PATCH 1/9] Remove bundled time library --- libraries/Time/DateStrings.cpp | 97 ------ libraries/Time/Readme.txt | 131 ------- libraries/Time/Time.cpp | 321 ------------------ libraries/Time/Time.h | 1 - libraries/Time/TimeLib.h | 144 -------- .../examples/TimeCurieRTC/TimeCurieRTC.ino | 54 --- .../TimeCurieRTCLog/TimeCurieRTCLog.ino | 105 ------ .../TimeCurieRTCSet/TimeCurieRTCSet.ino | 75 ---- libraries/Time/examples/TimeGPS/TimeGPS.ino | 87 ----- libraries/Time/examples/TimeNTP/TimeNTP.ino | 135 -------- .../TimeNTP_ESP8266WiFi.ino | 143 -------- libraries/Time/examples/TimeRTC/TimeRTC.ino | 55 --- .../Time/examples/TimeRTCLog/TimeRTCLog.ino | 107 ------ .../Time/examples/TimeRTCSet/TimeRTCSet.ino | 80 ----- .../Time/examples/TimeSerial/TimeSerial.ino | 81 ----- .../TimeSerialDateStrings.ino | 108 ------ libraries/Time/keywords.txt | 34 -- libraries/Time/library.json | 22 -- libraries/Time/library.properties | 10 - 19 files changed, 1790 deletions(-) delete mode 100644 libraries/Time/DateStrings.cpp delete mode 100644 libraries/Time/Readme.txt delete mode 100644 libraries/Time/Time.cpp delete mode 100644 libraries/Time/Time.h delete mode 100644 libraries/Time/TimeLib.h delete mode 100644 libraries/Time/examples/TimeCurieRTC/TimeCurieRTC.ino delete mode 100644 libraries/Time/examples/TimeCurieRTCLog/TimeCurieRTCLog.ino delete mode 100644 libraries/Time/examples/TimeCurieRTCSet/TimeCurieRTCSet.ino delete mode 100644 libraries/Time/examples/TimeGPS/TimeGPS.ino delete mode 100644 libraries/Time/examples/TimeNTP/TimeNTP.ino delete mode 100644 libraries/Time/examples/TimeNTP_ESP8266WiFi/TimeNTP_ESP8266WiFi.ino delete mode 100644 libraries/Time/examples/TimeRTC/TimeRTC.ino delete mode 100644 libraries/Time/examples/TimeRTCLog/TimeRTCLog.ino delete mode 100644 libraries/Time/examples/TimeRTCSet/TimeRTCSet.ino delete mode 100644 libraries/Time/examples/TimeSerial/TimeSerial.ino delete mode 100644 libraries/Time/examples/TimeSerialDateStrings/TimeSerialDateStrings.ino delete mode 100644 libraries/Time/keywords.txt delete mode 100644 libraries/Time/library.json delete mode 100644 libraries/Time/library.properties diff --git a/libraries/Time/DateStrings.cpp b/libraries/Time/DateStrings.cpp deleted file mode 100644 index 489bb150..00000000 --- a/libraries/Time/DateStrings.cpp +++ /dev/null @@ -1,97 +0,0 @@ -/* DateStrings.cpp - * Definitions for date strings for use with the Time library - * - * Updated for Arduino 1.5.7 18 July 2014 - * - * No memory is consumed in the sketch if your code does not call any of the string methods - * You can change the text of the strings, make sure the short strings are each exactly 3 characters - * the long strings can be any length up to the constant dt_MAX_STRING_LEN defined in Time.h - * - */ - -#if defined(__AVR__) -#include -#else -// for compatiblity with Arduino Due and Teensy 3.0 and maybe others? -#define PROGMEM -#define PGM_P const char * -#define pgm_read_byte(addr) (*(const unsigned char *)(addr)) -#define pgm_read_word(addr) (*(const unsigned char **)(addr)) -#define strcpy_P(dest, src) strcpy((dest), (src)) -#endif -#include // for strcpy_P or strcpy -#include "Time.h" - -// the short strings for each day or month must be exactly dt_SHORT_STR_LEN -#define dt_SHORT_STR_LEN 3 // the length of short strings - -static char buffer[dt_MAX_STRING_LEN+1]; // must be big enough for longest string and the terminating null - -const char monthStr0[] PROGMEM = ""; -const char monthStr1[] PROGMEM = "January"; -const char monthStr2[] PROGMEM = "February"; -const char monthStr3[] PROGMEM = "March"; -const char monthStr4[] PROGMEM = "April"; -const char monthStr5[] PROGMEM = "May"; -const char monthStr6[] PROGMEM = "June"; -const char monthStr7[] PROGMEM = "July"; -const char monthStr8[] PROGMEM = "August"; -const char monthStr9[] PROGMEM = "September"; -const char monthStr10[] PROGMEM = "October"; -const char monthStr11[] PROGMEM = "November"; -const char monthStr12[] PROGMEM = "December"; - -const PROGMEM char * const PROGMEM monthNames_P[] = -{ - monthStr0,monthStr1,monthStr2,monthStr3,monthStr4,monthStr5,monthStr6, - monthStr7,monthStr8,monthStr9,monthStr10,monthStr11,monthStr12 -}; - -const char monthShortNames_P[] PROGMEM = "ErrJanFebMarAprMayJunJulAugSepOctNovDec"; - -const char dayStr0[] PROGMEM = "Err"; -const char dayStr1[] PROGMEM = "Sunday"; -const char dayStr2[] PROGMEM = "Monday"; -const char dayStr3[] PROGMEM = "Tuesday"; -const char dayStr4[] PROGMEM = "Wednesday"; -const char dayStr5[] PROGMEM = "Thursday"; -const char dayStr6[] PROGMEM = "Friday"; -const char dayStr7[] PROGMEM = "Saturday"; - -const PROGMEM char * const PROGMEM dayNames_P[] = -{ - dayStr0,dayStr1,dayStr2,dayStr3,dayStr4,dayStr5,dayStr6,dayStr7 -}; - -const char dayShortNames_P[] PROGMEM = "ErrSunMonTueWedThuFriSat"; - -/* functions to return date strings */ - -char* monthStr(uint8_t month) -{ - strcpy_P(buffer, (PGM_P)pgm_read_word(&(monthNames_P[month]))); - return buffer; -} - -char* monthShortStr(uint8_t month) -{ - for (int i=0; i < dt_SHORT_STR_LEN; i++) - buffer[i] = pgm_read_byte(&(monthShortNames_P[i+ (month*dt_SHORT_STR_LEN)])); - buffer[dt_SHORT_STR_LEN] = 0; - return buffer; -} - -char* dayStr(uint8_t day) -{ - strcpy_P(buffer, (PGM_P)pgm_read_word(&(dayNames_P[day]))); - return buffer; -} - -char* dayShortStr(uint8_t day) -{ - uint8_t index = day*dt_SHORT_STR_LEN; - for (int i=0; i < dt_SHORT_STR_LEN; i++) - buffer[i] = pgm_read_byte(&(dayShortNames_P[index + i])); - buffer[dt_SHORT_STR_LEN] = 0; - return buffer; -} diff --git a/libraries/Time/Readme.txt b/libraries/Time/Readme.txt deleted file mode 100644 index 67b148ec..00000000 --- a/libraries/Time/Readme.txt +++ /dev/null @@ -1,131 +0,0 @@ -Readme file for Arduino Time Library - -Time is a library that provides timekeeping functionality for Arduino. - -The code is derived from the Playground DateTime library but is updated -to provide an API that is more flexable and easier to use. - -A primary goal was to enable date and time functionality that can be used with -a variety of external time sources with minimum differences required in sketch logic. - -Example sketches illustrate how similar sketch code can be used with: a Real Time Clock, -internet NTP time service, GPS time data, and Serial time messages from a computer -for time synchronization. - -The functions available in the library include: - -hour(); // the hour now (0-23) -minute(); // the minute now (0-59) -second(); // the second now (0-59) -day(); // the day now (1-31) -weekday(); // day of the week, Sunday is day 0 -month(); // the month now (1-12) -year(); // the full four digit year: (2009, 2010 etc) - -there are also functions to return the hour in 12 hour format -hourFormat12(); // the hour now in 12 hour format -isAM(); // returns true if time now is AM -isPM(); // returns true if time now is PM - -now(); // returns the current time as seconds since Jan 1 1970 - -The time and date functions can take an optional parameter for the time. This prevents -errors if the time rolls over between elements. For example, if a new minute begins -between getting the minute and second, the values will be inconsistent. Using the -following functions eliminates this probglem - time_t t = now(); // store the current time in time variable t - hour(t); // returns the hour for the given time t - minute(t); // returns the minute for the given time t - second(t); // returns the second for the given time t - day(t); // the day for the given time t - weekday(t); // day of the week for the given time t - month(t); // the month for the given time t - year(t); // the year for the given time t - - -Functions for managing the timer services are: -setTime(t); // set the system time to the give time t -setTime(hr,min,sec,day,mnth,yr); // alternative to above, yr is 2 or 4 digit yr (2010 or 10 sets year to 2010) -adjustTime(adjustment); // adjust system time by adding the adjustment value - -timeStatus(); // indicates if time has been set and recently synchronized - // returns one of the following enumerations: - timeNotSet // the time has never been set, the clock started at Jan 1 1970 - timeNeedsSync // the time had been set but a sync attempt did not succeed - timeSet // the time is set and is synced -Time and Date values are not valid if the status is timeNotSet. Otherwise values can be used but -the returned time may have drifted if the status is timeNeedsSync. - -setSyncProvider(getTimeFunction); // set the external time provider -setSyncInterval(interval); // set the number of seconds between re-sync - - -There are many convenience macros in the time.h file for time constants and conversion of time units. - -To use the library, copy the download to the Library directory. - -The Time directory contains the Time library and some example sketches -illustrating how the library can be used with various time sources: - -- TimeSerial.pde shows Arduino as a clock without external hardware. - It is synchronized by time messages sent over the serial port. - A companion Processing sketch will automatically provide these messages - if it is running and connected to the Arduino serial port. - -- TimeSerialDateStrings.pde adds day and month name strings to the sketch above - Short (3 character) and long strings are available to print the days of - the week and names of the months. - -- TimeRTC uses a DS1307 real time clock to provide time synchronization. - A basic RTC library named DS1307RTC is included in the download. - To run this sketch the DS1307RTC library must be installed. - -- TimeRTCSet is similar to the above and adds the ability to set the Real Time Clock - -- TimeRTCLog demonstrates how to calculate the difference between times. - It is a vary simple logger application that monitors events on digtial pins - and prints (to the serial port) the time of an event and the time period since the previous event. - -- TimeNTP uses the Arduino Ethernet shield to access time using the internet NTP time service. - The NTP protocol uses UDP and the UdpBytewise library is required, see: - http://bitbucket.org/bjoern/arduino_osc/src/14667490521f/libraries/Ethernet/ - -- TimeGPS gets time from a GPS - This requires the TinyGPS library from Mikal Hart: - http://arduiniana.org/libraries/TinyGPS - -Differences between this code and the playground DateTime library -although the Time library is based on the DateTime codebase, the API has changed. -Changes in the Time library API: -- time elements are functions returning int (they are variables in DateTime) -- Years start from 1970 -- days of the week and months start from 1 (they start from 0 in DateTime) -- DateStrings do not require a seperate library -- time elements can be accessed non-atomically (in DateTime they are always atomic) -- function added to automatically sync time with extrnal source -- localTime and maketime parameters changed, localTime renamed to breakTime - -Technical notes: - -Internal system time is based on the standard Unix time_t. -The value is the number of seconds since Jan 1 1970. -System time begins at zero when the sketch starts. - -The internal time can be automatically synchronized at regular intervals to an external time source. -This is enabled by calling the setSyncProvider(provider) function - the provider argument is -the address of a function that returns the current time as a time_t. -See the sketches in the examples directory for usage. - -The default interval for re-syncing the time is 5 minutes but can be changed by calling the -setSyncInterval( interval) method to set the number of seconds between re-sync attempts. - -The Time library defines a structure for holding time elements that is a compact version of the C tm structure. -All the members of the Arduino tm structure are bytes and the year is offset from 1970. -Convenience macros provide conversion to and from the Arduino format. - -Low level functions to convert between system time and individual time elements are provided: - breakTime( time, &tm); // break time_t into elements stored in tm struct - makeTime( &tm); // return time_t from elements stored in tm struct - -The DS1307RTC library included in the download provides an example of how a time provider -can use the low level functions to interface with the Time library. diff --git a/libraries/Time/Time.cpp b/libraries/Time/Time.cpp deleted file mode 100644 index b12baece..00000000 --- a/libraries/Time/Time.cpp +++ /dev/null @@ -1,321 +0,0 @@ -/* - time.c - low level time and date functions - Copyright (c) Michael Margolis 2009-2014 - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Lesser General Public - License as published by the Free Software Foundation; either - version 2.1 of the License, or (at your option) any later version. - - This library is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public - License along with this library; if not, write to the Free Software - Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA - - 1.0 6 Jan 2010 - initial release - 1.1 12 Feb 2010 - fixed leap year calculation error - 1.2 1 Nov 2010 - fixed setTime bug (thanks to Korman for this) - 1.3 24 Mar 2012 - many edits by Paul Stoffregen: fixed timeStatus() to update - status, updated examples for Arduino 1.0, fixed ARM - compatibility issues, added TimeArduinoDue and TimeTeensy3 - examples, add error checking and messages to RTC examples, - add examples to DS1307RTC library. - 1.4 5 Sep 2014 - compatibility with Arduino 1.5.7 -*/ - -#if ARDUINO >= 100 -#include -#else -#include -#endif - -#include "Time.h" - -static tmElements_t tm; // a cache of time elements -static time_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) { - if (t != cacheTime) { - breakTime(t, tm); - cacheTime = t; - } -} - -int hour() { // the hour now - return hour(now()); -} - -int hour(time_t t) { // the hour for the given time - refreshCache(t); - return tm.Hour; -} - -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 - refreshCache(t); - if( tm.Hour == 0 ) - return 12; // 12 midnight - else if( tm.Hour > 12) - return tm.Hour - 12 ; - else - return tm.Hour ; -} - -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 - return !isPM(t); -} - -uint8_t isPM() { // returns true if PM - return isPM(now()); -} - -uint8_t isPM(time_t t) { // returns true if PM - return (hour(t) >= 12); -} - -int minute() { - return minute(now()); -} - -int minute(time_t t) { // the minute for the given time - refreshCache(t); - return tm.Minute; -} - -int second() { - return second(now()); -} - -int second(time_t t) { // the second for the given time - refreshCache(t); - return tm.Second; -} - -int day(){ - return(day(now())); -} - -int day(time_t t) { // the day for the given time (0-6) - refreshCache(t); - return tm.Day; -} - -int weekday() { // Sunday is day 1 - return weekday(now()); -} - -int weekday(time_t t) { - refreshCache(t); - return tm.Wday; -} - -int month(){ - return month(now()); -} - -int month(time_t t) { // the month for the given time - refreshCache(t); - return tm.Month; -} - -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 - refreshCache(t); - return tmYearToCalendar(tm.Year); -} - -/*============================================================================*/ -/* functions to convert to and from system time */ -/* These are for interfacing with time serivces and are not normally needed in a sketch */ - -// leap year calulator expects year argument as years offset from 1970 -#define LEAP_YEAR(Y) ( ((1970+Y)>0) && !((1970+Y)%4) && ( ((1970+Y)%100) || !((1970+Y)%400) ) ) - -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 -// this is a more compact version of the C library localtime function -// note that year is offset from 1970 !!! - - uint8_t year; - uint8_t month, monthLength; - uint32_t time; - unsigned long days; - - time = (uint32_t)timeInput; - tm.Second = time % 60; - time /= 60; // now it is minutes - tm.Minute = time % 60; - time /= 60; // now it is hours - tm.Hour = time % 24; - time /= 24; // now it is days - tm.Wday = ((time + 4) % 7) + 1; // Sunday is day 1 - - year = 0; - days = 0; - while((unsigned)(days += (LEAP_YEAR(year) ? 366 : 365)) <= time) { - year++; - } - tm.Year = year; // year is offset from 1970 - - days -= LEAP_YEAR(year) ? 366 : 365; - time -= days; // now it is days in this year, starting at 0 - - days=0; - month=0; - monthLength=0; - for (month=0; month<12; month++) { - if (month==1) { // february - if (LEAP_YEAR(year)) { - monthLength=29; - } else { - monthLength=28; - } - } else { - monthLength = monthDays[month]; - } - - if (time >= monthLength) { - time -= monthLength; - } else { - break; - } - } - tm.Month = month + 1; // jan is month 1 - tm.Day = time + 1; // day of month -} - -time_t makeTime(tmElements_t &tm){ -// assemble time elements into time_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 - - int i; - uint32_t seconds; - - // seconds from 1970 till 1 jan 00:00:00 of the given year - seconds= tm.Year*(SECS_PER_DAY * 365); - for (i = 0; i < tm.Year; i++) { - if (LEAP_YEAR(i)) { - seconds += SECS_PER_DAY; // add extra days for leap years - } - } - - // add days for this year, months start from 1 - for (i = 1; i < tm.Month; i++) { - if ( (i == 2) && LEAP_YEAR(tm.Year)) { - seconds += SECS_PER_DAY * 29; - } else { - seconds += SECS_PER_DAY * monthDays[i-1]; //monthDay array starts from 0 - } - } - seconds+= (tm.Day-1) * SECS_PER_DAY; - seconds+= tm.Hour * SECS_PER_HOUR; - seconds+= tm.Minute * SECS_PER_MIN; - seconds+= tm.Second; - return (time_t)seconds; -} -/*=====================================================*/ -/* Low level system time functions */ - -static uint32_t sysTime = 0; -static uint32_t prevMillis = 0; -static uint32_t nextSyncTime = 0; -static timeStatus_t Status = timeNotSet; - -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 -#endif - - -time_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 - sysTime++; - prevMillis += 1000; -#ifdef TIME_DRIFT_INFO - sysUnsyncedTime++; // this can be compared to the synced time to measure long term drift -#endif - } - if (nextSyncTime <= sysTime) { - if (getTimePtr != 0) { - time_t t = getTimePtr(); - if (t != 0) { - setTime(t); - } else { - nextSyncTime = sysTime + syncInterval; - Status = (Status == timeNotSet) ? timeNotSet : timeNeedsSync; - } - } - } - return (time_t)sysTime; -} - -void setTime(time_t t) { -#ifdef TIME_DRIFT_INFO - if(sysUnsyncedTime == 0) - sysUnsyncedTime = t; // store the time of the first call to set a valid Time -#endif - - sysTime = (uint32_t)t; - nextSyncTime = (uint32_t)t + syncInterval; - Status = timeSet; - prevMillis = millis(); // restart counting from now (thanks to Korman for this fix) -} - -void setTime(int hr,int min,int sec,int dy, int mnth, int yr){ - // year can be given as full four digit year or two digts (2010 or 10 for 2010); - //it is converted to years since 1970 - if( yr > 99) - yr = yr - 1970; - else - yr += 30; - tm.Year = yr; - tm.Month = mnth; - tm.Day = dy; - tm.Hour = hr; - tm.Minute = min; - tm.Second = sec; - setTime(makeTime(tm)); -} - -void adjustTime(long adjustment) { - sysTime += adjustment; -} - -// indicates if time has been set and recently synchronized -timeStatus_t timeStatus() { - now(); // required to actually update the status - return Status; -} - -void setSyncProvider( getExternalTime getTimeFunction){ - getTimePtr = getTimeFunction; - nextSyncTime = sysTime; - now(); // this will sync the clock -} - -void setSyncInterval(time_t interval){ // set the number of seconds between re-sync - syncInterval = (uint32_t)interval; - nextSyncTime = sysTime + syncInterval; -} diff --git a/libraries/Time/Time.h b/libraries/Time/Time.h deleted file mode 100644 index a79b0801..00000000 --- a/libraries/Time/Time.h +++ /dev/null @@ -1 +0,0 @@ -#include "TimeLib.h" diff --git a/libraries/Time/TimeLib.h b/libraries/Time/TimeLib.h deleted file mode 100644 index 61519f7d..00000000 --- a/libraries/Time/TimeLib.h +++ /dev/null @@ -1,144 +0,0 @@ -/* - time.h - low level time and date functions -*/ - -/* - July 3 2011 - fixed elapsedSecsThisWeek macro (thanks Vincent Valdy for this) - - fixed daysToTime_t macro (thanks maniacbug) -*/ - -#ifndef _Time_h -#ifdef __cplusplus -#define _Time_h - -#include -#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 -// have that file at all). On Mac and Windows, the compiler will find this -// "Time.h" instead of the C library "time.h", so we may cause other weird -// and unpredictable effects by conflicting with the C library header "time.h", -// but at least this hack lets us define C++ functions as intended. Hopefully -// nothing too terrible will result from overriding the C library header?! -extern "C++" { -typedef enum {timeNotSet, timeNeedsSync, timeSet -} timeStatus_t ; - -typedef enum { - dowInvalid, dowSunday, dowMonday, dowTuesday, dowWednesday, dowThursday, dowFriday, dowSaturday -} timeDayOfWeek_t; - -typedef enum { - tmSecond, tmMinute, tmHour, tmWday, tmDay,tmMonth, tmYear, tmNbrFields -} tmByteFields; - -typedef struct { - uint8_t Second; - uint8_t Minute; - uint8_t Hour; - uint8_t Wday; // day of week, sunday is day 1 - uint8_t Day; - uint8_t Month; - uint8_t Year; // offset from 1970; -} tmElements_t, TimeElements, *tmElementsPtr_t; - -//convenience macros to convert to and from tm years -#define tmYearToCalendar(Y) ((Y) + 1970) // full four digit year -#define CalendarYrToTm(Y) ((Y) - 1970) -#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 - - -/*==============================================================================*/ -/* Useful Constants */ -#define SECS_PER_MIN (60UL) -#define SECS_PER_HOUR (3600UL) -#define SECS_PER_DAY (SECS_PER_HOUR * 24UL) -#define DAYS_PER_WEEK (7UL) -#define SECS_PER_WEEK (SECS_PER_DAY * DAYS_PER_WEEK) -#define SECS_PER_YEAR (SECS_PER_WEEK * 52UL) -#define SECS_YR_2000 (946684800UL) // the time at the start of y2k - -/* Useful Macros for getting elapsed time */ -#define numberOfSeconds(_time_) (_time_ % SECS_PER_MIN) -#define numberOfMinutes(_time_) ((_time_ / SECS_PER_MIN) % SECS_PER_MIN) -#define numberOfHours(_time_) (( _time_% SECS_PER_DAY) / SECS_PER_HOUR) -#define dayOfWeek(_time_) ((( _time_ / SECS_PER_DAY + 4) % DAYS_PER_WEEK)+1) // 1 = Sunday -#define elapsedDays(_time_) ( _time_ / SECS_PER_DAY) // this is number of days since Jan 1 1970 -#define elapsedSecsToday(_time_) (_time_ % SECS_PER_DAY) // the number of seconds since last midnight -// The following macros are used in calculating alarms and assume the clock is set to a date later than Jan 1 1971 -// Always set the correct time before settting alarms -#define previousMidnight(_time_) (( _time_ / SECS_PER_DAY) * SECS_PER_DAY) // time at the start of the given day -#define nextMidnight(_time_) ( previousMidnight(_time_) + SECS_PER_DAY ) // time at the end of the given day -#define elapsedSecsThisWeek(_time_) (elapsedSecsToday(_time_) + ((dayOfWeek(_time_)-1) * SECS_PER_DAY) ) // note that week starts on day 1 -#define previousSunday(_time_) (_time_ - elapsedSecsThisWeek(_time_)) // time at the start of the week for the given time -#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 */ -#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 -#define weeksToTime_t ((W)) ( (W) * SECS_PER_WEEK) - -/*============================================================================*/ -/* time and date functions */ -int hour(); // the hour now -int hour(time_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 -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 isPM(); // returns true if time now is PM -uint8_t isPM(time_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 second(); // the second now -int second(time_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 weekday(); // the weekday now (Sunday is day 1) -int weekday(time_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 year(); // the full four digit year: (2009, 2010 etc) -int year(time_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); -void setTime(int hr,int min,int sec,int day, int month, int yr); -void adjustTime(long adjustment); - -/* date strings */ -#define dt_MAX_STRING_LEN 9 // length of longest date string (excluding terminating null) -char* monthStr(uint8_t month); -char* dayStr(uint8_t day); -char* monthShortStr(uint8_t month); -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 - -/* 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 - -} // extern "C++" -#endif // __cplusplus -#endif /* _Time_h */ - diff --git a/libraries/Time/examples/TimeCurieRTC/TimeCurieRTC.ino b/libraries/Time/examples/TimeCurieRTC/TimeCurieRTC.ino deleted file mode 100644 index d805ff55..00000000 --- a/libraries/Time/examples/TimeCurieRTC/TimeCurieRTC.ino +++ /dev/null @@ -1,54 +0,0 @@ -/* - * TimeRTC.pde - * example code illustrating Time library with Real Time Clock. - * - */ - -#include -#include -#include // a basic Curie library that returns time as a time_t - -void setup() { - Serial.begin(115200); - while (!Serial) ; // wait until Arduino Serial Monitor opens - setSyncProvider(RTC.get); // the function to get the time from the RTC - if(timeStatus()!= timeSet) - Serial.println("Unable to sync with the RTC"); - else - Serial.println("Curie RTC has set the system time"); -} - -void loop() -{ - if (timeStatus() == timeSet) { - digitalClockDisplay(); - } else { - Serial.println("The time has not been set. Please run the Time"); - Serial.println("TimeRTCSet example, or CurieRTC SetTime example."); - Serial.println(); - delay(4000); - } - delay(1000); -} - -void digitalClockDisplay(){ - // digital clock display of the time - Serial.print(hour()); - printDigits(minute()); - printDigits(second()); - Serial.print(" "); - Serial.print(day()); - Serial.print(" "); - Serial.print(month()); - Serial.print(" "); - Serial.print(year()); - Serial.println(); -} - -void printDigits(int digits){ - // utility function for digital clock display: prints preceding colon and leading 0 - Serial.print(":"); - if(digits < 10) - Serial.print('0'); - Serial.print(digits); -} diff --git a/libraries/Time/examples/TimeCurieRTCLog/TimeCurieRTCLog.ino b/libraries/Time/examples/TimeCurieRTCLog/TimeCurieRTCLog.ino deleted file mode 100644 index 53af5138..00000000 --- a/libraries/Time/examples/TimeCurieRTCLog/TimeCurieRTCLog.ino +++ /dev/null @@ -1,105 +0,0 @@ -/* - * TimeCurieRTCLogger.ino - * example code illustrating adding and subtracting Time. - * - * this sketch logs pin state change events - * the time of the event and time since the previous event is calculated and sent to the serial port. - */ - -#include -#include // a basic CurieRTC library that returns time as a time_t - -const int nbrInputPins = 6; // monitor 6 digital pins -const int inputPins[nbrInputPins] = {2,3,4,5,6,7}; // pins to monitor -boolean state[nbrInputPins] ; // the state of the monitored pins -time_t prevEventTime[nbrInputPins] ; // the time of the previous event - -void setup() { - Serial.begin(9600); - setSyncProvider(RTC.get); // the function to sync the time from the RTC - for(int i=0; i < nbrInputPins; i++){ - pinMode( inputPins[i], INPUT); - // uncomment these lines if pull-up resistors are wanted - // pinMode( inputPins[i], INPUT_PULLUP); - // state[i] = HIGH; - } -} - -void loop() -{ - for(int i=0; i < nbrInputPins; i++) - { - boolean val = digitalRead(inputPins[i]); - if(val != state[i]) - { - time_t duration = 0; // the time since the previous event - state[i] = val; - time_t timeNow = now(); - if(prevEventTime[i] > 0) - // if this was not the first state change, calculate the time from the previous change - duration = duration = timeNow - prevEventTime[i]; - logEvent(inputPins[i], val, timeNow, duration ); // log the event - prevEventTime[i] = timeNow; // store the time for this event - } - } -} - -void logEvent( int pin, boolean state, time_t timeNow, time_t duration) -{ - Serial.print("Pin "); - Serial.print(pin); - if( state == HIGH) - Serial.print(" went High at "); - else - Serial.print(" went Low at "); - showTime(timeNow); - if(duration > 0){ - // only display duration if greater than 0 - Serial.print(", Duration was "); - showDuration(duration); - } - Serial.println(); -} - - -void showTime(time_t t){ - // display the given time - Serial.print(hour(t)); - printDigits(minute(t)); - printDigits(second(t)); - Serial.print(" "); - Serial.print(day(t)); - Serial.print(" "); - Serial.print(month(t)); - Serial.print(" "); - Serial.print(year(t)); -} - -void printDigits(int digits){ - // utility function for digital clock display: prints preceding colon and leading 0 - Serial.print(":"); - if(digits < 10) - Serial.print('0'); - Serial.print(digits); -} - -void showDuration(time_t duration){ -// prints the duration in days, hours, minutes and seconds - if(duration >= SECS_PER_DAY){ - Serial.print(duration / SECS_PER_DAY); - Serial.print(" day(s) "); - duration = duration % SECS_PER_DAY; - } - if(duration >= SECS_PER_HOUR){ - Serial.print(duration / SECS_PER_HOUR); - Serial.print(" hour(s) "); - duration = duration % SECS_PER_HOUR; - } - if(duration >= SECS_PER_MIN){ - Serial.print(duration / SECS_PER_MIN); - Serial.print(" minute(s) "); - duration = duration % SECS_PER_MIN; - } - Serial.print(duration); - Serial.print(" second(s) "); -} diff --git a/libraries/Time/examples/TimeCurieRTCSet/TimeCurieRTCSet.ino b/libraries/Time/examples/TimeCurieRTCSet/TimeCurieRTCSet.ino deleted file mode 100644 index 73705597..00000000 --- a/libraries/Time/examples/TimeCurieRTCSet/TimeCurieRTCSet.ino +++ /dev/null @@ -1,75 +0,0 @@ -/* - * TimeCurieRTCSet.ino - * example code illustrating Time library with the Curie Real Time Clock. - * - * RTC clock is set in response to serial port time message - * A Processing example sketch to set the time is included in the download - * On Linux, you can use "date +T%s > /dev/ttyACM0" (UTC time zone) - */ - -#include -#include // a basic CurieRTC library that returns time as a time_t - - -void setup() { - Serial.begin(115200); - while (!Serial) ; // Needed for Leonardo only - setSyncProvider(RTC.get); // the function to get the time from the RTC - if (timeStatus() != timeSet) - Serial.println("Unable to sync with the RTC"); - else - Serial.println("RTC has set the system time"); -} - -void loop() -{ - if (Serial.available()) { - time_t t = processSyncMessage(); - if (t != 0) { - RTC.set(t); // set the RTC and the system time to the received value - setTime(t); - } - } - digitalClockDisplay(); - delay(1000); -} - -void digitalClockDisplay(){ - // digital clock display of the time - Serial.print(hour()); - printDigits(minute()); - printDigits(second()); - Serial.print(" "); - Serial.print(day()); - Serial.print(" "); - Serial.print(month()); - Serial.print(" "); - Serial.print(year()); - Serial.println(); -} - -void printDigits(int digits){ - // utility function for digital clock display: prints preceding colon and leading 0 - Serial.print(":"); - if(digits < 10) - Serial.print('0'); - Serial.print(digits); -} - -/* code to process time sync messages from the serial port */ -#define TIME_HEADER "T" // Header tag for serial time sync message - -unsigned long processSyncMessage() { - unsigned long pctime = 0L; - const unsigned long DEFAULT_TIME = 1357041600; // Jan 1 2013 - - if(Serial.find(TIME_HEADER)) { - pctime = Serial.parseInt(); - return pctime; - if( pctime < DEFAULT_TIME) { // check the value is a valid time (greater than Jan 1 2013) - pctime = 0L; // return 0 to indicate that the time is not valid - } - } - return pctime; -} - diff --git a/libraries/Time/examples/TimeGPS/TimeGPS.ino b/libraries/Time/examples/TimeGPS/TimeGPS.ino deleted file mode 100644 index fea96988..00000000 --- a/libraries/Time/examples/TimeGPS/TimeGPS.ino +++ /dev/null @@ -1,87 +0,0 @@ -/* - * TimeGPS.pde - * example code illustrating time synced from a GPS - * - */ - -#include -#include // http://arduiniana.org/libraries/TinyGPS/ -#include -// TinyGPS and SoftwareSerial libraries are the work of Mikal Hart - -SoftwareSerial SerialGPS = SoftwareSerial(10, 11); // receive on pin 10 -TinyGPS gps; - -// To use a hardware serial port, which is far more efficient than -// SoftwareSerial, uncomment this line and remove SoftwareSerial -//#define SerialGPS Serial1 - -// Offset hours from gps time (UTC) -const int offset = 1; // Central European Time -//const int offset = -5; // Eastern Standard Time (USA) -//const int offset = -4; // Eastern Daylight Time (USA) -//const int offset = -8; // Pacific Standard Time (USA) -//const int offset = -7; // Pacific Daylight Time (USA) - -// Ideally, it should be possible to learn the time zone -// based on the GPS position data. However, that would -// require a complex library, probably incorporating some -// sort of database using Eric Muller's time zone shape -// maps, at http://efele.net/maps/tz/ - -time_t prevDisplay = 0; // when the digital clock was displayed - -void setup() -{ - Serial.begin(9600); - while (!Serial) ; // Needed for Leonardo only - SerialGPS.begin(4800); - Serial.println("Waiting for GPS time ... "); -} - -void loop() -{ - while (SerialGPS.available()) { - if (gps.encode(SerialGPS.read())) { // process gps messages - // when TinyGPS reports new data... - unsigned long age; - int Year; - byte Month, Day, Hour, Minute, Second; - gps.crack_datetime(&Year, &Month, &Day, &Hour, &Minute, &Second, NULL, &age); - if (age < 500) { - // set the Time to the latest GPS reading - setTime(Hour, Minute, Second, Day, Month, Year); - adjustTime(offset * SECS_PER_HOUR); - } - } - } - if (timeStatus()!= timeNotSet) { - if (now() != prevDisplay) { //update the display only if the time has changed - prevDisplay = now(); - digitalClockDisplay(); - } - } -} - -void digitalClockDisplay(){ - // digital clock display of the time - Serial.print(hour()); - printDigits(minute()); - printDigits(second()); - Serial.print(" "); - Serial.print(day()); - Serial.print(" "); - Serial.print(month()); - Serial.print(" "); - Serial.print(year()); - Serial.println(); -} - -void printDigits(int digits) { - // utility function for digital clock display: prints preceding colon and leading 0 - Serial.print(":"); - if(digits < 10) - Serial.print('0'); - Serial.print(digits); -} - diff --git a/libraries/Time/examples/TimeNTP/TimeNTP.ino b/libraries/Time/examples/TimeNTP/TimeNTP.ino deleted file mode 100644 index 17a908f8..00000000 --- a/libraries/Time/examples/TimeNTP/TimeNTP.ino +++ /dev/null @@ -1,135 +0,0 @@ -/* - * Time_NTP.pde - * Example showing time sync to NTP time source - * - * This sketch uses the Ethernet library - */ - -#include -#include -#include -#include - -byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; -// NTP Servers: -IPAddress timeServer(132, 163, 4, 101); // time-a.timefreq.bldrdoc.gov -// IPAddress timeServer(132, 163, 4, 102); // time-b.timefreq.bldrdoc.gov -// IPAddress timeServer(132, 163, 4, 103); // time-c.timefreq.bldrdoc.gov - - -const int timeZone = 1; // Central European Time -//const int timeZone = -5; // Eastern Standard Time (USA) -//const int timeZone = -4; // Eastern Daylight Time (USA) -//const int timeZone = -8; // Pacific Standard Time (USA) -//const int timeZone = -7; // Pacific Daylight Time (USA) - - -EthernetUDP Udp; -unsigned int localPort = 8888; // local port to listen for UDP packets - -void setup() -{ - Serial.begin(9600); - while (!Serial) ; // Needed for Leonardo only - delay(250); - Serial.println("TimeNTP Example"); - if (Ethernet.begin(mac) == 0) { - // no point in carrying on, so do nothing forevermore: - while (1) { - Serial.println("Failed to configure Ethernet using DHCP"); - delay(10000); - } - } - Serial.print("IP number assigned by DHCP is "); - Serial.println(Ethernet.localIP()); - Udp.begin(localPort); - Serial.println("waiting for sync"); - setSyncProvider(getNtpTime); -} - -time_t prevDisplay = 0; // when the digital clock was displayed - -void loop() -{ - if (timeStatus() != timeNotSet) { - if (now() != prevDisplay) { //update the display only if time has changed - prevDisplay = now(); - digitalClockDisplay(); - } - } -} - -void digitalClockDisplay(){ - // digital clock display of the time - Serial.print(hour()); - printDigits(minute()); - printDigits(second()); - Serial.print(" "); - Serial.print(day()); - Serial.print(" "); - Serial.print(month()); - Serial.print(" "); - Serial.print(year()); - Serial.println(); -} - -void printDigits(int digits){ - // utility for digital clock display: prints preceding colon and leading 0 - Serial.print(":"); - if(digits < 10) - Serial.print('0'); - Serial.print(digits); -} - -/*-------- NTP code ----------*/ - -const int NTP_PACKET_SIZE = 48; // NTP time is in the first 48 bytes of message -byte packetBuffer[NTP_PACKET_SIZE]; //buffer to hold incoming & outgoing packets - -time_t getNtpTime() -{ - while (Udp.parsePacket() > 0) ; // discard any previously received packets - Serial.println("Transmit NTP Request"); - sendNTPpacket(timeServer); - uint32_t beginWait = millis(); - while (millis() - beginWait < 1500) { - int size = Udp.parsePacket(); - if (size >= NTP_PACKET_SIZE) { - Serial.println("Receive NTP Response"); - Udp.read(packetBuffer, NTP_PACKET_SIZE); // read packet into the buffer - unsigned long secsSince1900; - // convert four bytes starting at location 40 to a long integer - secsSince1900 = (unsigned long)packetBuffer[40] << 24; - secsSince1900 |= (unsigned long)packetBuffer[41] << 16; - secsSince1900 |= (unsigned long)packetBuffer[42] << 8; - secsSince1900 |= (unsigned long)packetBuffer[43]; - return secsSince1900 - 2208988800UL + timeZone * SECS_PER_HOUR; - } - } - Serial.println("No NTP Response :-("); - return 0; // return 0 if unable to get the time -} - -// send an NTP request to the time server at the given address -void sendNTPpacket(IPAddress &address) -{ - // set all bytes in the buffer to 0 - memset(packetBuffer, 0, NTP_PACKET_SIZE); - // Initialize values needed to form NTP request - // (see URL above for details on the packets) - packetBuffer[0] = 0b11100011; // LI, Version, Mode - packetBuffer[1] = 0; // Stratum, or type of clock - packetBuffer[2] = 6; // Polling Interval - packetBuffer[3] = 0xEC; // Peer Clock Precision - // 8 bytes of zero for Root Delay & Root Dispersion - packetBuffer[12] = 49; - packetBuffer[13] = 0x4E; - packetBuffer[14] = 49; - packetBuffer[15] = 52; - // all NTP fields have been given values, now - // you can send a packet requesting a timestamp: - Udp.beginPacket(address, 123); //NTP requests are to port 123 - Udp.write(packetBuffer, NTP_PACKET_SIZE); - Udp.endPacket(); -} - diff --git a/libraries/Time/examples/TimeNTP_ESP8266WiFi/TimeNTP_ESP8266WiFi.ino b/libraries/Time/examples/TimeNTP_ESP8266WiFi/TimeNTP_ESP8266WiFi.ino deleted file mode 100644 index cab64883..00000000 --- a/libraries/Time/examples/TimeNTP_ESP8266WiFi/TimeNTP_ESP8266WiFi.ino +++ /dev/null @@ -1,143 +0,0 @@ -/* - * Time_NTP.pde - * Example showing time sync to NTP time source - * - * This sketch uses the ESP8266WiFi library - */ - -#include -#include -#include - -const char ssid[] = "*************"; // your network SSID (name) -const char pass[] = "********"; // your network password - -// NTP Servers: -IPAddress timeServer(132, 163, 4, 101); // time-a.timefreq.bldrdoc.gov -// IPAddress timeServer(132, 163, 4, 102); // time-b.timefreq.bldrdoc.gov -// IPAddress timeServer(132, 163, 4, 103); // time-c.timefreq.bldrdoc.gov - - -const int timeZone = 1; // Central European Time -//const int timeZone = -5; // Eastern Standard Time (USA) -//const int timeZone = -4; // Eastern Daylight Time (USA) -//const int timeZone = -8; // Pacific Standard Time (USA) -//const int timeZone = -7; // Pacific Daylight Time (USA) - - -WiFiUDP Udp; -unsigned int localPort = 8888; // local port to listen for UDP packets - -void setup() -{ - Serial.begin(9600); - while (!Serial) ; // Needed for Leonardo only - delay(250); - Serial.println("TimeNTP Example"); - Serial.print("Connecting to "); - Serial.println(ssid); - WiFi.begin(ssid, pass); - - while (WiFi.status() != WL_CONNECTED) { - delay(500); - Serial.print("."); - } - - - Serial.print("IP number assigned by DHCP is "); - Serial.println(WiFi.localIP()); - Serial.println("Starting UDP"); - Udp.begin(localPort); - Serial.print("Local port: "); - Serial.println(Udp.localPort()); - Serial.println("waiting for sync"); - setSyncProvider(getNtpTime); -} - -time_t prevDisplay = 0; // when the digital clock was displayed - -void loop() -{ - if (timeStatus() != timeNotSet) { - if (now() != prevDisplay) { //update the display only if time has changed - prevDisplay = now(); - digitalClockDisplay(); - } - } -} - -void digitalClockDisplay(){ - // digital clock display of the time - Serial.print(hour()); - printDigits(minute()); - printDigits(second()); - Serial.print(" "); - Serial.print(day()); - Serial.print("."); - Serial.print(month()); - Serial.print("."); - Serial.print(year()); - Serial.println(); -} - - - -void printDigits(int digits){ - // utility for digital clock display: prints preceding colon and leading 0 - Serial.print(":"); - if(digits < 10) - Serial.print('0'); - Serial.print(digits); -} - -/*-------- NTP code ----------*/ - -const int NTP_PACKET_SIZE = 48; // NTP time is in the first 48 bytes of message -byte packetBuffer[NTP_PACKET_SIZE]; //buffer to hold incoming & outgoing packets - -time_t getNtpTime() -{ - while (Udp.parsePacket() > 0) ; // discard any previously received packets - Serial.println("Transmit NTP Request"); - sendNTPpacket(timeServer); - uint32_t beginWait = millis(); - while (millis() - beginWait < 1500) { - int size = Udp.parsePacket(); - if (size >= NTP_PACKET_SIZE) { - Serial.println("Receive NTP Response"); - Udp.read(packetBuffer, NTP_PACKET_SIZE); // read packet into the buffer - unsigned long secsSince1900; - // convert four bytes starting at location 40 to a long integer - secsSince1900 = (unsigned long)packetBuffer[40] << 24; - secsSince1900 |= (unsigned long)packetBuffer[41] << 16; - secsSince1900 |= (unsigned long)packetBuffer[42] << 8; - secsSince1900 |= (unsigned long)packetBuffer[43]; - return secsSince1900 - 2208988800UL + timeZone * SECS_PER_HOUR; - } - } - Serial.println("No NTP Response :-("); - return 0; // return 0 if unable to get the time -} - -// send an NTP request to the time server at the given address -void sendNTPpacket(IPAddress &address) -{ - // set all bytes in the buffer to 0 - memset(packetBuffer, 0, NTP_PACKET_SIZE); - // Initialize values needed to form NTP request - // (see URL above for details on the packets) - packetBuffer[0] = 0b11100011; // LI, Version, Mode - packetBuffer[1] = 0; // Stratum, or type of clock - packetBuffer[2] = 6; // Polling Interval - packetBuffer[3] = 0xEC; // Peer Clock Precision - // 8 bytes of zero for Root Delay & Root Dispersion - packetBuffer[12] = 49; - packetBuffer[13] = 0x4E; - packetBuffer[14] = 49; - packetBuffer[15] = 52; - // all NTP fields have been given values, now - // you can send a packet requesting a timestamp: - Udp.beginPacket(address, 123); //NTP requests are to port 123 - Udp.write(packetBuffer, NTP_PACKET_SIZE); - Udp.endPacket(); -} diff --git a/libraries/Time/examples/TimeRTC/TimeRTC.ino b/libraries/Time/examples/TimeRTC/TimeRTC.ino deleted file mode 100644 index fa10ff6f..00000000 --- a/libraries/Time/examples/TimeRTC/TimeRTC.ino +++ /dev/null @@ -1,55 +0,0 @@ -/* - * TimeRTC.pde - * example code illustrating Time library with Real Time Clock. - * - */ - -#include -#include -#include // a basic DS1307 library that returns time as a time_t - -void setup() { - Serial.begin(9600); - while (!Serial) ; // wait until Arduino Serial Monitor opens - setSyncProvider(RTC.get); // the function to get the time from the RTC - if(timeStatus()!= timeSet) - Serial.println("Unable to sync with the RTC"); - else - Serial.println("RTC has set the system time"); -} - -void loop() -{ - if (timeStatus() == timeSet) { - digitalClockDisplay(); - } else { - Serial.println("The time has not been set. Please run the Time"); - Serial.println("TimeRTCSet example, or DS1307RTC SetTime example."); - Serial.println(); - delay(4000); - } - delay(1000); -} - -void digitalClockDisplay(){ - // digital clock display of the time - Serial.print(hour()); - printDigits(minute()); - printDigits(second()); - Serial.print(" "); - Serial.print(day()); - Serial.print(" "); - Serial.print(month()); - Serial.print(" "); - Serial.print(year()); - Serial.println(); -} - -void printDigits(int digits){ - // utility function for digital clock display: prints preceding colon and leading 0 - Serial.print(":"); - if(digits < 10) - Serial.print('0'); - Serial.print(digits); -} - diff --git a/libraries/Time/examples/TimeRTCLog/TimeRTCLog.ino b/libraries/Time/examples/TimeRTCLog/TimeRTCLog.ino deleted file mode 100644 index 0b250c20..00000000 --- a/libraries/Time/examples/TimeRTCLog/TimeRTCLog.ino +++ /dev/null @@ -1,107 +0,0 @@ -/* - * TimeRTCLogger.pde - * example code illustrating adding and subtracting Time. - * - * this sketch logs pin state change events - * the time of the event and time since the previous event is calculated and sent to the serial port. - */ - -#include -#include -#include // a basic DS1307 library that returns time as a time_t - -const int nbrInputPins = 6; // monitor 6 digital pins -const int inputPins[nbrInputPins] = {2,3,4,5,6,7}; // pins to monitor -boolean state[nbrInputPins] ; // the state of the monitored pins -time_t prevEventTime[nbrInputPins] ; // the time of the previous event - -void setup() { - Serial.begin(9600); - setSyncProvider(RTC.get); // the function to sync the time from the RTC - for(int i=0; i < nbrInputPins; i++){ - pinMode( inputPins[i], INPUT); - // uncomment these lines if pull-up resistors are wanted - // pinMode( inputPins[i], INPUT_PULLUP); - // state[i] = HIGH; - } -} - -void loop() -{ - for(int i=0; i < nbrInputPins; i++) - { - boolean val = digitalRead(inputPins[i]); - if(val != state[i]) - { - time_t duration = 0; // the time since the previous event - state[i] = val; - time_t timeNow = now(); - if(prevEventTime[i] > 0) - // if this was not the first state change, calculate the time from the previous change - duration = duration = timeNow - prevEventTime[i]; - logEvent(inputPins[i], val, timeNow, duration ); // log the event - prevEventTime[i] = timeNow; // store the time for this event - } - } -} - -void logEvent( int pin, boolean state, time_t timeNow, time_t duration) -{ - Serial.print("Pin "); - Serial.print(pin); - if( state == HIGH) - Serial.print(" went High at "); - else - Serial.print(" went Low at "); - showTime(timeNow); - if(duration > 0){ - // only display duration if greater than 0 - Serial.print(", Duration was "); - showDuration(duration); - } - Serial.println(); -} - - -void showTime(time_t t){ - // display the given time - Serial.print(hour(t)); - printDigits(minute(t)); - printDigits(second(t)); - Serial.print(" "); - Serial.print(day(t)); - Serial.print(" "); - Serial.print(month(t)); - Serial.print(" "); - Serial.print(year(t)); -} - -void printDigits(int digits){ - // utility function for digital clock display: prints preceding colon and leading 0 - Serial.print(":"); - if(digits < 10) - Serial.print('0'); - Serial.print(digits); -} - -void showDuration(time_t duration){ -// prints the duration in days, hours, minutes and seconds - if(duration >= SECS_PER_DAY){ - Serial.print(duration / SECS_PER_DAY); - Serial.print(" day(s) "); - duration = duration % SECS_PER_DAY; - } - if(duration >= SECS_PER_HOUR){ - Serial.print(duration / SECS_PER_HOUR); - Serial.print(" hour(s) "); - duration = duration % SECS_PER_HOUR; - } - if(duration >= SECS_PER_MIN){ - Serial.print(duration / SECS_PER_MIN); - Serial.print(" minute(s) "); - duration = duration % SECS_PER_MIN; - } - Serial.print(duration); - Serial.print(" second(s) "); -} - diff --git a/libraries/Time/examples/TimeRTCSet/TimeRTCSet.ino b/libraries/Time/examples/TimeRTCSet/TimeRTCSet.ino deleted file mode 100644 index 49c49f37..00000000 --- a/libraries/Time/examples/TimeRTCSet/TimeRTCSet.ino +++ /dev/null @@ -1,80 +0,0 @@ -/* - * TimeRTCSet.pde - * example code illustrating Time library with Real Time Clock. - * - * RTC clock is set in response to serial port time message - * A Processing example sketch to set the time is included in the download - * On Linux, you can use "date +T%s > /dev/ttyACM0" (UTC time zone) - */ - -#include -#include -#include // a basic DS1307 library that returns time as a time_t - - -void setup() { - Serial.begin(9600); - while (!Serial) ; // Needed for Leonardo only - setSyncProvider(RTC.get); // the function to get the time from the RTC - if (timeStatus() != timeSet) - Serial.println("Unable to sync with the RTC"); - else - Serial.println("RTC has set the system time"); -} - -void loop() -{ - if (Serial.available()) { - time_t t = processSyncMessage(); - if (t != 0) { - RTC.set(t); // set the RTC and the system time to the received value - setTime(t); - } - } - digitalClockDisplay(); - delay(1000); -} - -void digitalClockDisplay(){ - // digital clock display of the time - Serial.print(hour()); - printDigits(minute()); - printDigits(second()); - Serial.print(" "); - Serial.print(day()); - Serial.print(" "); - Serial.print(month()); - Serial.print(" "); - Serial.print(year()); - Serial.println(); -} - -void printDigits(int digits){ - // utility function for digital clock display: prints preceding colon and leading 0 - Serial.print(":"); - if(digits < 10) - Serial.print('0'); - Serial.print(digits); -} - -/* code to process time sync messages from the serial port */ -#define TIME_HEADER "T" // Header tag for serial time sync message - -unsigned long processSyncMessage() { - unsigned long pctime = 0L; - const unsigned long DEFAULT_TIME = 1357041600; // Jan 1 2013 - - if(Serial.find(TIME_HEADER)) { - pctime = Serial.parseInt(); - return pctime; - if( pctime < DEFAULT_TIME) { // check the value is a valid time (greater than Jan 1 2013) - pctime = 0L; // return 0 to indicate that the time is not valid - } - } - return pctime; -} - - - - - diff --git a/libraries/Time/examples/TimeSerial/TimeSerial.ino b/libraries/Time/examples/TimeSerial/TimeSerial.ino deleted file mode 100644 index 07e609fd..00000000 --- a/libraries/Time/examples/TimeSerial/TimeSerial.ino +++ /dev/null @@ -1,81 +0,0 @@ -/* - * TimeSerial.pde - * example code illustrating Time library set through serial port messages. - * - * Messages consist of the letter T followed by ten digit time (as seconds since Jan 1 1970) - * you can send the text on the next line using Serial Monitor to set the clock to noon Jan 1 2013 - T1357041600 - * - * A Processing example sketch to automatically send the messages is included in the download - * On Linux, you can use "date +T%s\n > /dev/ttyACM0" (UTC time zone) - */ - -#include - -#define TIME_HEADER "T" // Header tag for serial time sync message -#define TIME_REQUEST 7 // ASCII bell character requests a time sync message - -void setup() { - Serial.begin(9600); - while (!Serial) ; // Needed for Leonardo only - pinMode(13, OUTPUT); - setSyncProvider( requestSync); //set function to call when sync required - Serial.println("Waiting for sync message"); -} - -void loop(){ - if (Serial.available()) { - processSyncMessage(); - } - if (timeStatus()!= timeNotSet) { - digitalClockDisplay(); - } - if (timeStatus() == timeSet) { - digitalWrite(13, HIGH); // LED on if synced - } else { - digitalWrite(13, LOW); // LED off if needs refresh - } - delay(1000); -} - -void digitalClockDisplay(){ - // digital clock display of the time - Serial.print(hour()); - printDigits(minute()); - printDigits(second()); - Serial.print(" "); - Serial.print(day()); - Serial.print(" "); - Serial.print(month()); - Serial.print(" "); - Serial.print(year()); - Serial.println(); -} - -void printDigits(int digits){ - // utility function for digital clock display: prints preceding colon and leading 0 - Serial.print(":"); - if(digits < 10) - Serial.print('0'); - Serial.print(digits); -} - - -void processSyncMessage() { - unsigned long pctime; - const unsigned long DEFAULT_TIME = 1357041600; // Jan 1 2013 - - if(Serial.find(TIME_HEADER)) { - pctime = Serial.parseInt(); - if( pctime >= DEFAULT_TIME) { // check the integer is a valid time (greater than Jan 1 2013) - setTime(pctime); // Sync Arduino clock to the time received on the serial port - } - } -} - -time_t requestSync() -{ - Serial.write(TIME_REQUEST); - return 0; // the time will be sent later in response to serial mesg -} - diff --git a/libraries/Time/examples/TimeSerialDateStrings/TimeSerialDateStrings.ino b/libraries/Time/examples/TimeSerialDateStrings/TimeSerialDateStrings.ino deleted file mode 100644 index 95d2568c..00000000 --- a/libraries/Time/examples/TimeSerialDateStrings/TimeSerialDateStrings.ino +++ /dev/null @@ -1,108 +0,0 @@ -/* - * TimeSerialDateStrings.pde - * example code illustrating Time library date strings - * - * This sketch adds date string functionality to TimeSerial sketch - * Also shows how to handle different messages - * - * A message starting with a time header sets the time - * A Processing example sketch to automatically send the messages is inclided in the download - * On Linux, you can use "date +T%s\n > /dev/ttyACM0" (UTC time zone) - * - * A message starting with a format header sets the date format - - * send: Fs\n for short date format - * send: Fl\n for long date format - */ - -#include - -// single character message tags -#define TIME_HEADER 'T' // Header tag for serial time sync message -#define FORMAT_HEADER 'F' // Header tag indicating a date format message -#define FORMAT_SHORT 's' // short month and day strings -#define FORMAT_LONG 'l' // (lower case l) long month and day strings - -#define TIME_REQUEST 7 // ASCII bell character requests a time sync message - -static boolean isLongFormat = true; - -void setup() { - Serial.begin(9600); - while (!Serial) ; // Needed for Leonardo only - setSyncProvider( requestSync); //set function to call when sync required - Serial.println("Waiting for sync message"); -} - -void loop(){ - if (Serial.available() > 1) { // wait for at least two characters - char c = Serial.read(); - if( c == TIME_HEADER) { - processSyncMessage(); - } - else if( c== FORMAT_HEADER) { - processFormatMessage(); - } - } - if (timeStatus()!= timeNotSet) { - digitalClockDisplay(); - } - delay(1000); -} - -void digitalClockDisplay() { - // digital clock display of the time - Serial.print(hour()); - printDigits(minute()); - printDigits(second()); - Serial.print(" "); - if(isLongFormat) - Serial.print(dayStr(weekday())); - else - Serial.print(dayShortStr(weekday())); - Serial.print(" "); - Serial.print(day()); - Serial.print(" "); - if(isLongFormat) - Serial.print(monthStr(month())); - else - Serial.print(monthShortStr(month())); - Serial.print(" "); - Serial.print(year()); - Serial.println(); -} - -void printDigits(int digits) { - // utility function for digital clock display: prints preceding colon and leading 0 - Serial.print(":"); - if(digits < 10) - Serial.print('0'); - Serial.print(digits); -} - -void processFormatMessage() { - char c = Serial.read(); - if( c == FORMAT_LONG){ - isLongFormat = true; - Serial.println(F("Setting long format")); - } - else if( c == FORMAT_SHORT) { - isLongFormat = false; - Serial.println(F("Setting short format")); - } -} - -void processSyncMessage() { - unsigned long pctime; - const unsigned long DEFAULT_TIME = 1357041600; // Jan 1 2013 - paul, perhaps we define in time.h? - - pctime = Serial.parseInt(); - if( pctime >= DEFAULT_TIME) { // check the integer is a valid time (greater than Jan 1 2013) - setTime(pctime); // Sync Arduino clock to the time received on the serial port - } -} - -time_t requestSync() { - Serial.write(TIME_REQUEST); - return 0; // the time will be sent later in response to serial mesg -} diff --git a/libraries/Time/keywords.txt b/libraries/Time/keywords.txt deleted file mode 100644 index 073f8f88..00000000 --- a/libraries/Time/keywords.txt +++ /dev/null @@ -1,34 +0,0 @@ -####################################### -# Syntax Coloring Map For Time -####################################### - -####################################### -# Datatypes (KEYWORD1) -####################################### -time_t KEYWORD1 -####################################### -# Methods and Functions (KEYWORD2) -####################################### -now KEYWORD2 -second KEYWORD2 -minute KEYWORD2 -hour KEYWORD2 -day KEYWORD2 -month KEYWORD2 -year KEYWORD2 -isAM KEYWORD2 -isPM KEYWORD2 -weekday KEYWORD2 -setTime KEYWORD2 -adjustTime KEYWORD2 -setSyncProvider KEYWORD2 -setSyncInterval KEYWORD2 -timeStatus KEYWORD2 -TimeLib KEYWORD2 -####################################### -# Instances (KEYWORD2) -####################################### - -####################################### -# Constants (LITERAL1) -####################################### diff --git a/libraries/Time/library.json b/libraries/Time/library.json deleted file mode 100644 index 071e74c9..00000000 --- a/libraries/Time/library.json +++ /dev/null @@ -1,22 +0,0 @@ -{ -"name": "Time", -"frameworks": "Arduino", -"keywords": "Time, date, hour, minute, second, day, week, month, year, RTC", -"description": "Time keeping library", -"url": "http://playground.arduino.cc/Code/Time", -"authors": -[ -{ - "name": "Michael Margolis" -}, -{ - "name": "Paul Stoffregen" -} -], -"repository": -{ - "type": "git", - "url": "https://github.com/PaulStoffregen/Time" -} -} - diff --git a/libraries/Time/library.properties b/libraries/Time/library.properties deleted file mode 100644 index 49b1e2a1..00000000 --- a/libraries/Time/library.properties +++ /dev/null @@ -1,10 +0,0 @@ -name=Time -version=1.5 -author=Michael Margolis -maintainer=Paul Stoffregen -sentence=Timekeeping functionality for Arduino -paragraph=Date and Time functions, with provisions to synchronize to external time sources like GPS and NTP (Internet). This library is often used together with TimeAlarms and DS1307RTC. -category=Timing -url=http://playground.arduino.cc/code/time -architectures=* - From 7bb2c23d0db4503d59cfeab00b7f12be8c8ff32c Mon Sep 17 00:00:00 2001 From: Sandeep Mistry Date: Mon, 14 Dec 2015 11:06:09 -0500 Subject: [PATCH 2/9] Change CurieRTC lib to have Processing style time API's based on the RTC value --- .../CurieRTC/examples/ReadTest/ReadTest.ino | 24 +++--- .../CurieRTC/examples/SetTime/SetTime.ino | 73 ++++++++++------- libraries/CurieRTC/keywords.txt | 27 +++++++ libraries/CurieRTC/src/CurieRTC.cpp | 80 ++++++++++++++++--- libraries/CurieRTC/src/CurieRTC.h | 27 +++---- 5 files changed, 159 insertions(+), 72 deletions(-) create mode 100644 libraries/CurieRTC/keywords.txt diff --git a/libraries/CurieRTC/examples/ReadTest/ReadTest.ino b/libraries/CurieRTC/examples/ReadTest/ReadTest.ino index d538cf02..8ee72c2a 100644 --- a/libraries/CurieRTC/examples/ReadTest/ReadTest.ino +++ b/libraries/CurieRTC/examples/ReadTest/ReadTest.ino @@ -1,30 +1,26 @@ -#include #include void setup() { - Serial.begin(115200); - delay(200); + while(!Serial); + Serial.begin(9600); + Serial.println("CurieRTC Read Test"); Serial.println("-------------------"); } -void loop() { - tmElements_t tm; - time_t t = RTC.get(); - breakTime(t, tm); - +void loop() { Serial.print("Ok, Time = "); - print2digits(tm.Hour); + print2digits(hour()); Serial.write(':'); - print2digits(tm.Minute); + print2digits(minute()); Serial.write(':'); - print2digits(tm.Second); + print2digits(second()); Serial.print(", Date (D/M/Y) = "); - Serial.print(tm.Day); + Serial.print(day()); Serial.write('/'); - Serial.print(tm.Month); + Serial.print(month()); Serial.write('/'); - Serial.print(tmYearToCalendar(tm.Year)); + Serial.print(year()); Serial.println(); delay(1000); } diff --git a/libraries/CurieRTC/examples/SetTime/SetTime.ino b/libraries/CurieRTC/examples/SetTime/SetTime.ino index 63d09041..fa3bcaed 100644 --- a/libraries/CurieRTC/examples/SetTime/SetTime.ino +++ b/libraries/CurieRTC/examples/SetTime/SetTime.ino @@ -1,4 +1,3 @@ -#include #include const char *monthName[12] = { @@ -6,29 +5,21 @@ const char *monthName[12] = { "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; -tmElements_t tm; +int Hour, Min, Sec; +int Day, Month, Year; void setup() { - bool parse=false; - bool config=false; - time_t t; + while(!Serial); + Serial.begin(9600); // get the date and time the compiler was run if (getDate(__DATE__) && getTime(__TIME__)) { - t = makeTime(tm); - RTC.set(t); - parse = true; - config = true; - } - - Serial.begin(115200); - delay(3000); - - if (parse && config) { Serial.print("Curie configured Time="); Serial.print(__TIME__); Serial.print(", Date="); Serial.println(__DATE__); + + setTime(Hour, Min, Sec, Day, Month, Year); } else { Serial.print("Could not parse info from the compiler, Time=\""); Serial.print(__TIME__); @@ -39,33 +30,53 @@ void setup() { } void loop() { + Serial.print("Time now is: "); + + print2digits(hour()); + Serial.print(":"); + print2digits(minute()); + Serial.print(":"); + print2digits(second()); + + Serial.print(" "); + + Serial.print(day()); + Serial.print("/"); + Serial.print(month()); + Serial.print("/"); + Serial.print(year()); + + Serial.println(); + + delay(1000); } bool getTime(const char *str) { - int Hour, Min, Sec; - if (sscanf(str, "%d:%d:%d", &Hour, &Min, &Sec) != 3) return false; - tm.Hour = Hour; - tm.Minute = Min; - tm.Second = Sec; return true; } bool getDate(const char *str) { - char Month[12]; - int Day, Year; - uint8_t monthIndex; + char monthString[12]; - if (sscanf(str, "%s %d %d", Month, &Day, &Year) != 3) return false; - for (monthIndex = 0; monthIndex < 12; monthIndex++) { - if (strcmp(Month, monthName[monthIndex]) == 0) break; + if (sscanf(str, "%s %d %d", monthString, &Day, &Year) != 3) { + return false; } - if (monthIndex >= 12) return false; - tm.Day = Day; - tm.Month = monthIndex + 1; - tm.Year = CalendarYrToTm(Year); - return true; + + for (Month = 1; Month <= 12; Month++) { + if (strcmp(monthString, monthName[Month - 1]) == 0) { + break; + } + } + + return (Month <= 12); } +void print2digits(int number) { + if (number >= 0 && number < 10) { + Serial.print('0'); + } + Serial.print(number); +} diff --git a/libraries/CurieRTC/keywords.txt b/libraries/CurieRTC/keywords.txt new file mode 100644 index 00000000..7bc0c896 --- /dev/null +++ b/libraries/CurieRTC/keywords.txt @@ -0,0 +1,27 @@ +####################################### +# Syntax Coloring Map For Time +####################################### + +####################################### +# Datatypes (KEYWORD1) +####################################### + +####################################### +# Methods and Functions (KEYWORD2) +####################################### +now KEYWORD2 +second KEYWORD2 +minute KEYWORD2 +hour KEYWORD2 +day KEYWORD2 +month KEYWORD2 +year KEYWORD2 +setTime KEYWORD2 + +####################################### +# Instances (KEYWORD2) +####################################### + +####################################### +# Constants (LITERAL1) +####################################### diff --git a/libraries/CurieRTC/src/CurieRTC.cpp b/libraries/CurieRTC/src/CurieRTC.cpp index 7bdb4d5e..2fc2c33a 100644 --- a/libraries/CurieRTC/src/CurieRTC.cpp +++ b/libraries/CurieRTC/src/CurieRTC.cpp @@ -20,23 +20,85 @@ */ +#include + #include "CurieRTC.h" -CurieRTC::CurieRTC() +#define YEAR_OFFSET 1900 +#define MONTH_OFFSET 1 + +unsigned long now() { + return *(int*)RTC_CCVR; } + +struct tm* nowTm() { + time_t t = now(); -// PUBLIC FUNCTIONS -time_t CurieRTC::get() + return gmtime(&t); +} + +int year() +{ + struct tm* tm = nowTm(); + + return (tm->tm_year + YEAR_OFFSET); +} + +int month() +{ + struct tm* tm = nowTm(); + + return (tm->tm_mon + MONTH_OFFSET); +} + +int day() { - // Read the value of the RTC_CCVR register - return *(int*)RTC_CCVR; + struct tm* tm = nowTm(); + + return tm->tm_mday; } -void CurieRTC::set(time_t t) +int hour() { - // Write t into the RTC_CLR register - *(int*)RTC_CLR = t; + struct tm* tm = nowTm(); + + return tm->tm_hour; } -CurieRTC RTC = CurieRTC(); // create an instance for the user +int minute() +{ + struct tm* tm = nowTm(); + + return tm->tm_min; +} + +int second() +{ + struct tm* tm = nowTm(); + + return tm->tm_sec; +} + +void setTime(unsigned long t) +{ + *(int*)RTC_CLR = t; +} + +void setTime(int hour, int minute, int second, int day, int month, int year) +{ + struct tm tm; + time_t t; + + tm.tm_year = year - YEAR_OFFSET; + tm.tm_mon = month - MONTH_OFFSET; + tm.tm_mday = day; + tm.tm_hour = hour; + tm.tm_min = minute; + tm.tm_sec = second; + tm.tm_isdst = -1; + + t = mktime(&tm); + + setTime(t); +} diff --git a/libraries/CurieRTC/src/CurieRTC.h b/libraries/CurieRTC/src/CurieRTC.h index 3187a56b..554eeaba 100644 --- a/libraries/CurieRTC/src/CurieRTC.h +++ b/libraries/CurieRTC/src/CurieRTC.h @@ -1,6 +1,5 @@ /* * CurieRTC.h - RTC library for Arduino101 - * This library is intended to be uses with Arduino Time.h library functions */ #ifndef CurieRTC_h @@ -14,24 +13,16 @@ #define RTC_RSTAT 0xb0000414 // Interrupt Raw Status Register #define RTC_EOI 0xb0000418 // End of Interrupt Register -#include +unsigned long now(); // current time as seconds since Jan 1 1970 -// library interface description -class CurieRTC -{ - // user-accessible "public" interface - public: - CurieRTC(); - static time_t get(); - static void set(time_t t); -}; +int year(); // current year as an integer +int month(); // current month as an integer (1 - 12) +int day(); // current day as an integer (1 - 31) +int hour(); // current hour as an integer (0 - 23) +int minute(); // current minute as an integer (0 - 59) +int second(); // current second as an integer (0 - 59) -#ifdef RTC -#undef RTC // workaround for Arduino Due, which defines "RTC"... -#endif - -extern CurieRTC RTC; +void setTime(int hour, int minute, int second, int day, int month, int year); // set the current time +void setTime(unsigned long t); // set the current time from seconds since Jan 1 1970 #endif - - From ed69e4f8c0f57db4c910b84d65d93d67e716766b Mon Sep 17 00:00:00 2001 From: Sandeep Mistry Date: Mon, 14 Dec 2015 11:27:42 -0500 Subject: [PATCH 3/9] Auto format --- libraries/CurieRTC/examples/ReadTest/ReadTest.ino | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/libraries/CurieRTC/examples/ReadTest/ReadTest.ino b/libraries/CurieRTC/examples/ReadTest/ReadTest.ino index 8ee72c2a..12a1b719 100644 --- a/libraries/CurieRTC/examples/ReadTest/ReadTest.ino +++ b/libraries/CurieRTC/examples/ReadTest/ReadTest.ino @@ -1,14 +1,14 @@ #include void setup() { - while(!Serial); + while (!Serial); Serial.begin(9600); Serial.println("CurieRTC Read Test"); Serial.println("-------------------"); } -void loop() { +void loop() { Serial.print("Ok, Time = "); print2digits(hour()); Serial.write(':'); From b872431e1df13cebb07cef8b2d85bc0906202462 Mon Sep 17 00:00:00 2001 From: Sandeep Mistry Date: Mon, 14 Dec 2015 11:48:01 -0500 Subject: [PATCH 4/9] Parse date and time strings using Arduino string class instead of sscanf --- .../CurieRTC/examples/SetTime/SetTime.ino | 62 +++++++++++++++++-- 1 file changed, 56 insertions(+), 6 deletions(-) diff --git a/libraries/CurieRTC/examples/SetTime/SetTime.ino b/libraries/CurieRTC/examples/SetTime/SetTime.ino index fa3bcaed..194fb47a 100644 --- a/libraries/CurieRTC/examples/SetTime/SetTime.ino +++ b/libraries/CurieRTC/examples/SetTime/SetTime.ino @@ -9,9 +9,9 @@ int Hour, Min, Sec; int Day, Month, Year; void setup() { - while(!Serial); + while (!Serial); Serial.begin(9600); - + // get the date and time the compiler was run if (getDate(__DATE__) && getTime(__TIME__)) { Serial.print("Curie configured Time="); @@ -53,24 +53,74 @@ void loop() { bool getTime(const char *str) { - if (sscanf(str, "%d:%d:%d", &Hour, &Min, &Sec) != 3) return false; + // Get time from string of format HH:MM:SS + String s = str; + + int firstColonIndex = s.indexOf(":"); + int lastColonIndex = s.lastIndexOf(":"); + + if (firstColonIndex == -1) { + // first colon not found + return false; + } + + if (lastColonIndex == -1) { + // last colon not found + return false; + } + + if (firstColonIndex == lastColonIndex) { + // only one colon, first and last index are the same + return false; + } + + String hourString = s.substring(0, firstColonIndex); + String minuteString = s.substring(firstColonIndex + 1, lastColonIndex); + String secondString = s.substring(lastColonIndex + 1); + + Hour = hourString.toInt(); + Min = minuteString.toInt(); + Sec = secondString.toInt(); + return true; } bool getDate(const char *str) { - char monthString[12]; + // Get Date from string of format MMM DD YYYY + String s = str; + + int firstSpaceIndex = s.indexOf(" "); + int lastSpaceIndex = s.lastIndexOf(" "); + + if (firstSpaceIndex == -1) { + // first space not found + return false; + } - if (sscanf(str, "%s %d %d", monthString, &Day, &Year) != 3) { + if (lastSpaceIndex == -1) { + // last space not found return false; } + if (firstSpaceIndex == lastSpaceIndex) { + // only one space, first and last index are the same + return false; + } + + String monthString = s.substring(0, firstSpaceIndex); + String dayString = s.substring(firstSpaceIndex + 1, lastSpaceIndex); + String yearString = s.substring(lastSpaceIndex + 1); + for (Month = 1; Month <= 12; Month++) { - if (strcmp(monthString, monthName[Month - 1]) == 0) { + if (monthString.equals(monthName[Month - 1])) { break; } } + Day = dayString.toInt(); + Year = yearString.toInt(); + return (Month <= 12); } From 4d9c07eb44178b0afb185f247de561da87d8aa3d Mon Sep 17 00:00:00 2001 From: Sandeep Mistry Date: Mon, 14 Dec 2015 12:06:45 -0500 Subject: [PATCH 5/9] Make current count value and counter load registers volatile --- libraries/CurieRTC/src/CurieRTC.cpp | 4 ++-- libraries/CurieRTC/src/CurieRTC.h | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/libraries/CurieRTC/src/CurieRTC.cpp b/libraries/CurieRTC/src/CurieRTC.cpp index 2fc2c33a..cb07b1cf 100644 --- a/libraries/CurieRTC/src/CurieRTC.cpp +++ b/libraries/CurieRTC/src/CurieRTC.cpp @@ -29,7 +29,7 @@ unsigned long now() { - return *(int*)RTC_CCVR; + return *RTC_CCVR; } struct tm* nowTm() { @@ -82,7 +82,7 @@ int second() void setTime(unsigned long t) { - *(int*)RTC_CLR = t; + *RTC_CLR = t; } void setTime(int hour, int minute, int second, int day, int month, int year) diff --git a/libraries/CurieRTC/src/CurieRTC.h b/libraries/CurieRTC/src/CurieRTC.h index 554eeaba..bc50dbbd 100644 --- a/libraries/CurieRTC/src/CurieRTC.h +++ b/libraries/CurieRTC/src/CurieRTC.h @@ -5,9 +5,9 @@ #ifndef CurieRTC_h #define CurieRTC_h -#define RTC_CCVR 0xb0000400 // Current Counter Value Register +#define RTC_CCVR (volatile int*)0xb0000400 // Current Counter Value Register #define RTC_CMR 0xb0000404 // Counter Match Register -#define RTC_CLR 0xb0000408 // Counter Load Register +#define RTC_CLR (volatile int*)0xb0000408 // Counter Load Register #define RTC_CCR 0xb000040C // Counter Control Register #define RTC_STAT 0xb0000410 // Interrupt Status Register #define RTC_RSTAT 0xb0000414 // Interrupt Raw Status Register From 304c733e07f4306c89533d5a36bc714cb43683ba Mon Sep 17 00:00:00 2001 From: Sandeep Mistry Date: Mon, 14 Dec 2015 14:03:13 -0500 Subject: [PATCH 6/9] Rename CurieRTC to CurieTime --- libraries/CurieRTC/readme.txt | 7 ------- .../{CurieRTC => CurieTime}/examples/ReadTest/ReadTest.ino | 4 ++-- .../{CurieRTC => CurieTime}/examples/SetTime/SetTime.ino | 2 +- libraries/{CurieRTC => CurieTime}/keywords.txt | 0 libraries/{CurieRTC => CurieTime}/library.properties | 4 ++-- libraries/CurieTime/readme.txt | 7 +++++++ .../src/CurieRTC.cpp => CurieTime/src/CurieTime.cpp} | 4 ++-- .../{CurieRTC/src/CurieRTC.h => CurieTime/src/CurieTime.h} | 6 +++--- 8 files changed, 17 insertions(+), 17 deletions(-) delete mode 100644 libraries/CurieRTC/readme.txt rename libraries/{CurieRTC => CurieTime}/examples/ReadTest/ReadTest.ino (89%) rename libraries/{CurieRTC => CurieTime}/examples/SetTime/SetTime.ino (99%) rename libraries/{CurieRTC => CurieTime}/keywords.txt (100%) rename libraries/{CurieRTC => CurieTime}/library.properties (73%) create mode 100644 libraries/CurieTime/readme.txt rename libraries/{CurieRTC/src/CurieRTC.cpp => CurieTime/src/CurieTime.cpp} (96%) rename libraries/{CurieRTC/src/CurieRTC.h => CurieTime/src/CurieTime.h} (92%) diff --git a/libraries/CurieRTC/readme.txt b/libraries/CurieRTC/readme.txt deleted file mode 100644 index c299b3a7..00000000 --- a/libraries/CurieRTC/readme.txt +++ /dev/null @@ -1,7 +0,0 @@ -Readme file for CurieRTC Library - -The CurieRTC library is provided to demonstrate the Arduino101 Time library. - -See the TimeCurieRTC example sketches provided with the Arduino101 Time library download for usage - - diff --git a/libraries/CurieRTC/examples/ReadTest/ReadTest.ino b/libraries/CurieTime/examples/ReadTest/ReadTest.ino similarity index 89% rename from libraries/CurieRTC/examples/ReadTest/ReadTest.ino rename to libraries/CurieTime/examples/ReadTest/ReadTest.ino index 12a1b719..8490ac7e 100644 --- a/libraries/CurieRTC/examples/ReadTest/ReadTest.ino +++ b/libraries/CurieTime/examples/ReadTest/ReadTest.ino @@ -1,10 +1,10 @@ -#include +#include void setup() { while (!Serial); Serial.begin(9600); - Serial.println("CurieRTC Read Test"); + Serial.println("CurieTime Read Test"); Serial.println("-------------------"); } diff --git a/libraries/CurieRTC/examples/SetTime/SetTime.ino b/libraries/CurieTime/examples/SetTime/SetTime.ino similarity index 99% rename from libraries/CurieRTC/examples/SetTime/SetTime.ino rename to libraries/CurieTime/examples/SetTime/SetTime.ino index 194fb47a..6e629d02 100644 --- a/libraries/CurieRTC/examples/SetTime/SetTime.ino +++ b/libraries/CurieTime/examples/SetTime/SetTime.ino @@ -1,4 +1,4 @@ -#include +#include const char *monthName[12] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", diff --git a/libraries/CurieRTC/keywords.txt b/libraries/CurieTime/keywords.txt similarity index 100% rename from libraries/CurieRTC/keywords.txt rename to libraries/CurieTime/keywords.txt diff --git a/libraries/CurieRTC/library.properties b/libraries/CurieTime/library.properties similarity index 73% rename from libraries/CurieRTC/library.properties rename to libraries/CurieTime/library.properties index 2e5f0357..1925fa35 100644 --- a/libraries/CurieRTC/library.properties +++ b/libraries/CurieTime/library.properties @@ -1,8 +1,8 @@ -name=CurieRTC +name=CurieTime version=1.0 author=Intel maintainer=Intel -sentence=Curie RTC library. +sentence=Curie Time library. paragraph= category=Timing url= diff --git a/libraries/CurieTime/readme.txt b/libraries/CurieTime/readme.txt new file mode 100644 index 00000000..8ca2de2c --- /dev/null +++ b/libraries/CurieTime/readme.txt @@ -0,0 +1,7 @@ +Readme file for CurieTime Library + +The CurieTime library is provided to demonstrate the Arduino101 Time library. + +See the CurieTime example sketches provided with the Arduino101 Time library download for usage + + diff --git a/libraries/CurieRTC/src/CurieRTC.cpp b/libraries/CurieTime/src/CurieTime.cpp similarity index 96% rename from libraries/CurieRTC/src/CurieRTC.cpp rename to libraries/CurieTime/src/CurieTime.cpp index cb07b1cf..efd553b6 100644 --- a/libraries/CurieRTC/src/CurieRTC.cpp +++ b/libraries/CurieTime/src/CurieTime.cpp @@ -1,5 +1,5 @@ /* - * CurieRTC.c - RTC library for Arduino101 + * CurieTime.cpp - Time library for Arduino101 Copyright (c) 2015 Intel Corporation. All rights reserved This library is intended to be uses with Arduino Time.h library functions @@ -22,7 +22,7 @@ #include -#include "CurieRTC.h" +#include "CurieTime.h" #define YEAR_OFFSET 1900 #define MONTH_OFFSET 1 diff --git a/libraries/CurieRTC/src/CurieRTC.h b/libraries/CurieTime/src/CurieTime.h similarity index 92% rename from libraries/CurieRTC/src/CurieRTC.h rename to libraries/CurieTime/src/CurieTime.h index bc50dbbd..24f8f60e 100644 --- a/libraries/CurieRTC/src/CurieRTC.h +++ b/libraries/CurieTime/src/CurieTime.h @@ -1,9 +1,9 @@ /* - * CurieRTC.h - RTC library for Arduino101 + * CurieTime.h - Time library for Arduino101 */ -#ifndef CurieRTC_h -#define CurieRTC_h +#ifndef CurieTime_h +#define CurieTime_h #define RTC_CCVR (volatile int*)0xb0000400 // Current Counter Value Register #define RTC_CMR 0xb0000404 // Counter Match Register From e5072e27a530a68acda9fe84fb96abaf1f31faac Mon Sep 17 00:00:00 2001 From: Sandeep Mistry Date: Mon, 14 Dec 2015 14:27:15 -0500 Subject: [PATCH 7/9] Add API's to convert epoch time to date/time component --- libraries/CurieTime/src/CurieTime.cpp | 61 +++++++++++++++++++++++---- libraries/CurieTime/src/CurieTime.h | 21 ++++++--- 2 files changed, 67 insertions(+), 15 deletions(-) diff --git a/libraries/CurieTime/src/CurieTime.cpp b/libraries/CurieTime/src/CurieTime.cpp index efd553b6..8f6bdd00 100644 --- a/libraries/CurieTime/src/CurieTime.cpp +++ b/libraries/CurieTime/src/CurieTime.cpp @@ -32,50 +32,93 @@ unsigned long now() return *RTC_CCVR; } -struct tm* nowTm() { - time_t t = now(); +struct tm* tTm(unsigned long t) +{ + time_t time = t; - return gmtime(&t); + return gmtime(&time); } int year() { - struct tm* tm = nowTm(); + unsigned long t = now(); + + return year(t); +} + +int year(unsigned long t) +{ + struct tm* tm = tTm(t); return (tm->tm_year + YEAR_OFFSET); } int month() { - struct tm* tm = nowTm(); + unsigned long t = now(); + + return month(t); +} + +int month(unsigned long t) +{ + struct tm* tm = tTm(t); return (tm->tm_mon + MONTH_OFFSET); } int day() { - struct tm* tm = nowTm(); + unsigned long t = now(); + + return day(t); +} + +int day(unsigned long t) +{ + struct tm* tm = tTm(t); return tm->tm_mday; } int hour() { - struct tm* tm = nowTm(); + unsigned long t = now(); + + return hour(t); +} + +int hour(unsigned long t) +{ + struct tm* tm = tTm(t); return tm->tm_hour; } int minute() { - struct tm* tm = nowTm(); + unsigned long t = now(); + + return minute(t); +} + +int minute(unsigned long t) +{ + struct tm* tm = tTm(t); return tm->tm_min; } int second() { - struct tm* tm = nowTm(); + unsigned long t = now(); + + return second(t); +} + +int second(unsigned long t) +{ + struct tm* tm = tTm(t); return tm->tm_sec; } diff --git a/libraries/CurieTime/src/CurieTime.h b/libraries/CurieTime/src/CurieTime.h index 24f8f60e..c217916b 100644 --- a/libraries/CurieTime/src/CurieTime.h +++ b/libraries/CurieTime/src/CurieTime.h @@ -13,14 +13,23 @@ #define RTC_RSTAT 0xb0000414 // Interrupt Raw Status Register #define RTC_EOI 0xb0000418 // End of Interrupt Register +// The following API is based on Paul Stoffregen's Arduino Time Library: +// https://github.com/PaulStoffregen/Time + unsigned long now(); // current time as seconds since Jan 1 1970 -int year(); // current year as an integer -int month(); // current month as an integer (1 - 12) -int day(); // current day as an integer (1 - 31) -int hour(); // current hour as an integer (0 - 23) -int minute(); // current minute as an integer (0 - 59) -int second(); // current second as an integer (0 - 59) +int year(); // current year as an integer +int year(unsigned long t); // year of t as an integer +int month(); // current month as an integer (1 - 12) +int month(unsigned long t); // month of t as an integer (1 - 12) +int day(); // current day as an integer (1 - 31) +int day(unsigned long t); // day of t as an integer (1 - 31) +int hour(); // current hour as an integer (0 - 23) +int hour(unsigned long t); // hour of t as an integer (0 - 23) +int minute(); // current minute as an integer (0 - 59) +int minute(unsigned long t); // minute of t as an integer (0 - 59) +int second(); // current second as an integer (0 - 59) +int second(unsigned long t); // second of t as an integer (0 - 59) void setTime(int hour, int minute, int second, int day, int month, int year); // set the current time void setTime(unsigned long t); // set the current time from seconds since Jan 1 1970 From d0c2800286ecf575fa900d9210d7473c9e68ba04 Mon Sep 17 00:00:00 2001 From: Sandeep Mistry Date: Mon, 14 Dec 2015 14:30:26 -0500 Subject: [PATCH 8/9] Simplify SetTime example, use hard coded time --- .../CurieTime/examples/SetTime/SetTime.ino | 98 +------------------ 1 file changed, 2 insertions(+), 96 deletions(-) diff --git a/libraries/CurieTime/examples/SetTime/SetTime.ino b/libraries/CurieTime/examples/SetTime/SetTime.ino index 6e629d02..d63d2bb0 100644 --- a/libraries/CurieTime/examples/SetTime/SetTime.ino +++ b/libraries/CurieTime/examples/SetTime/SetTime.ino @@ -1,32 +1,11 @@ #include -const char *monthName[12] = { - "Jan", "Feb", "Mar", "Apr", "May", "Jun", - "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" -}; - -int Hour, Min, Sec; -int Day, Month, Year; - void setup() { while (!Serial); Serial.begin(9600); - // get the date and time the compiler was run - if (getDate(__DATE__) && getTime(__TIME__)) { - Serial.print("Curie configured Time="); - Serial.print(__TIME__); - Serial.print(", Date="); - Serial.println(__DATE__); - - setTime(Hour, Min, Sec, Day, Month, Year); - } else { - Serial.print("Could not parse info from the compiler, Time=\""); - Serial.print(__TIME__); - Serial.print("\", Date=\""); - Serial.print(__DATE__); - Serial.println("\""); - } + // set the current time to 14:27:00, December 14th, 2015 + setTime(14, 27, 00, 14, 12, 2015); } void loop() { @@ -51,79 +30,6 @@ void loop() { delay(1000); } -bool getTime(const char *str) -{ - // Get time from string of format HH:MM:SS - String s = str; - - int firstColonIndex = s.indexOf(":"); - int lastColonIndex = s.lastIndexOf(":"); - - if (firstColonIndex == -1) { - // first colon not found - return false; - } - - if (lastColonIndex == -1) { - // last colon not found - return false; - } - - if (firstColonIndex == lastColonIndex) { - // only one colon, first and last index are the same - return false; - } - - String hourString = s.substring(0, firstColonIndex); - String minuteString = s.substring(firstColonIndex + 1, lastColonIndex); - String secondString = s.substring(lastColonIndex + 1); - - Hour = hourString.toInt(); - Min = minuteString.toInt(); - Sec = secondString.toInt(); - - return true; -} - -bool getDate(const char *str) -{ - // Get Date from string of format MMM DD YYYY - String s = str; - - int firstSpaceIndex = s.indexOf(" "); - int lastSpaceIndex = s.lastIndexOf(" "); - - if (firstSpaceIndex == -1) { - // first space not found - return false; - } - - if (lastSpaceIndex == -1) { - // last space not found - return false; - } - - if (firstSpaceIndex == lastSpaceIndex) { - // only one space, first and last index are the same - return false; - } - - String monthString = s.substring(0, firstSpaceIndex); - String dayString = s.substring(firstSpaceIndex + 1, lastSpaceIndex); - String yearString = s.substring(lastSpaceIndex + 1); - - for (Month = 1; Month <= 12; Month++) { - if (monthString.equals(monthName[Month - 1])) { - break; - } - } - - Day = dayString.toInt(); - Year = yearString.toInt(); - - return (Month <= 12); -} - void print2digits(int number) { if (number >= 0 && number < 10) { Serial.print('0'); From b3273bd9cec3f721e9958a768456965c3a7fe5b2 Mon Sep 17 00:00:00 2001 From: Sandeep Mistry Date: Mon, 14 Dec 2015 19:26:13 -0500 Subject: [PATCH 9/9] Update name in keywords.txt --- libraries/CurieTime/keywords.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/CurieTime/keywords.txt b/libraries/CurieTime/keywords.txt index 7bc0c896..84e1982a 100644 --- a/libraries/CurieTime/keywords.txt +++ b/libraries/CurieTime/keywords.txt @@ -1,5 +1,5 @@ ####################################### -# Syntax Coloring Map For Time +# Syntax Coloring Map For CurieTime ####################################### #######################################