From 944579172ce07faebad3c4eaf1faa8f67a57944c Mon Sep 17 00:00:00 2001 From: siddacious Date: Mon, 20 Apr 2020 11:08:58 -0700 Subject: [PATCH] actionified, formatted and doxy'd --- .github/workflows/githubci.yml | 32 ++ .travis.yml | 29 -- README.md | 35 +- RTClib.cpp | 711 +++++++++++++++++---------------- RTClib.h | 211 +++++----- library.properties | 3 +- LICENSE => license.txt | 0 7 files changed, 527 insertions(+), 494 deletions(-) create mode 100644 .github/workflows/githubci.yml delete mode 100644 .travis.yml rename LICENSE => license.txt (100%) diff --git a/.github/workflows/githubci.yml b/.github/workflows/githubci.yml new file mode 100644 index 0000000..79321e8 --- /dev/null +++ b/.github/workflows/githubci.yml @@ -0,0 +1,32 @@ +name: Arduino Library CI + +on: [pull_request, push, repository_dispatch] + +jobs: + build: + runs-on: ubuntu-latest + + steps: + - uses: actions/setup-python@v1 + with: + python-version: '3.x' + - uses: actions/checkout@v2 + - uses: actions/checkout@v2 + with: + repository: adafruit/ci-arduino + path: ci + + - name: pre-install + run: bash ci/actions_install.sh + + - name: test platforms + run: python3 ci/build_platform.py main_platforms + + - name: clang + run: python3 ci/run-clang-format.py -e "ci/*" -e "bin/*" -r . + + - name: doxygen + env: + GH_REPO_TOKEN: ${{ secrets.GH_REPO_TOKEN }} + PRETTYNAME : "RTClib" + run: bash ci/doxy_gen_and_deploy.sh diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index b170526..0000000 --- a/.travis.yml +++ /dev/null @@ -1,29 +0,0 @@ -language: c -sudo: false -# Blacklist -branches: - except: - - gh-pages -cache: - directories: - - ~/arduino_ide - - ~/.arduino15/packages/ -git: - depth: false - quiet: true -env: - global: - - PRETTYNAME="Adafruit RTClib" -# Optional, will default to "$TRAVIS_BUILD_DIR/Doxyfile" -# - DOXYFILE: $TRAVIS_BUILD_DIR/Doxyfile - -before_install: - - source <(curl -SLs https://raw.githubusercontent.com/adafruit/travis-ci-arduino/master/install.sh) - -script: - - build_main_platforms - -# Generate and deploy documentation -after_success: - - source <(curl -SLs https://raw.githubusercontent.com/adafruit/travis-ci-arduino/master/library_check.sh) - - source <(curl -SLs https://raw.githubusercontent.com/adafruit/travis-ci-arduino/master/doxy_gen_and_deploy.sh) diff --git a/README.md b/README.md index df8891e..b267927 100644 --- a/README.md +++ b/README.md @@ -1,19 +1,13 @@ -# Adafruit RTClib [![Build Status](https://travis-ci.com/adafruit/RTClib.svg?branch=master)](https://travis-ci.com/adafruit/RTClib) +# RTClib [![Build Status](https://github.com/adafruit/RTClib/workflows/Arduino%20Library%20CI/badge.svg)](https://github.com/adafruit/RTClib/actions) This is a fork of JeeLab's fantastic real time clock library for Arduino. -For a tutorial on using this library with an RTC module like the DS1307, PCF8523, or DS3231, see the guide at: https://learn.adafruit.com/ds1307-real-time-clock-breakout-board-kit/overview - -For the detailed API documentation, see https://adafruit.github.io/RTClib/html/index.html - Works great with Adafruit RTC breakouts: - [DS3231 Precision RTC](https://www.adafruit.com/product/3013) - [PCF8523 RTC](https://www.adafruit.com/product/3295) - [DS1307 RTC](https://www.adafruit.com/product/3296) -To install, use the Arduino Library Manager to search for "RTClib", find "RTClib by Adafruit" and install the library. - Please note that dayOfTheWeek() ranges from 0 to 6 inclusive with 0 being 'Sunday'. @@ -47,3 +41,30 @@ STM32F2 | | | X | * ATtiny85 @ 8MHz : Adafruit Gemma, Arduino Gemma, Adafruit Trinket 3V +Adafruit invests time and resources providing this open source code, please support Adafruit and open-source hardware by purchasing products from Adafruit! + +# Dependencies + * [Adafruit BusIO](https://github.com/adafruit/Adafruit_BusIO) + * [Adafruit Unified Sensor Driver](https://github.com/adafruit/Adafruit_Sensor) + * [Adafruit GFX Library](https://github.com/adafruit/Adafruit-GFX-Library) + +# Contributing + +Contributions are welcome! Please read our [Code of Conduct](https://github.com/adafruit/RTClib/blob/master/CODE_OF_CONDUCT.md>) +before contributing to help this project stay welcoming. + +## Documentation and doxygen +For the detailed API documentation, see https://adafruit.github.io/RTClib/html/index.html +Documentation is produced by doxygen. Contributions should include documentation for any new code added. + +Some examples of how to use doxygen can be found in these guide pages: + +https://learn.adafruit.com/the-well-automated-arduino-library/doxygen + +https://learn.adafruit.com/the-well-automated-arduino-library/doxygen-tips + +Written by JeeLabs +MIT license, check license.txt for more information +All text above must be included in any redistribution + +To install, use the Arduino Library Manager and search for "RTClib" and install the library. diff --git a/RTClib.cpp b/RTClib.cpp index 0fb192d..949bc7a 100644 --- a/RTClib.cpp +++ b/RTClib.cpp @@ -8,8 +8,9 @@ This is a fork of JeeLab's fantastic real time clock library for Arduino. - For details on using this library with an RTC module like the DS1307, PCF8523, or DS3231, - see the guide at: https://learn.adafruit.com/ds1307-real-time-clock-breakout-board-kit/overview + For details on using this library with an RTC module like the DS1307, PCF8523, + or DS3231, see the guide at: + https://learn.adafruit.com/ds1307-real-time-clock-breakout-board-kit/overview Adafruit invests time and resources providing this open source code, please support Adafruit and open-source hardware by purchasing @@ -34,46 +35,45 @@ @section license License - Original library by JeeLabs http://news.jeelabs.org/code/, released to the public domain. + Original library by JeeLabs http://news.jeelabs.org/code/, released to the + public domain. This version: MIT (see LICENSE) */ /**************************************************************************/ #ifdef __AVR_ATtiny85__ - #include - #define Wire TinyWireM +#include +#define Wire TinyWireM #else #include #endif #include "RTClib.h" #ifdef __AVR__ - #include +#include #elif defined(ESP8266) - #include +#include #elif defined(ARDUINO_ARCH_SAMD) // nothing special needed #elif defined(ARDUINO_SAM_DUE) - #define PROGMEM - #define pgm_read_byte(addr) (*(const unsigned char *)(addr)) - #define Wire Wire1 +#define PROGMEM +#define pgm_read_byte(addr) (*(const unsigned char *)(addr)) +#define Wire Wire1 #endif - - #if (ARDUINO >= 100) - #include // capital A so it is error prone on case-sensitive filesystems - // Macro to deal with the difference in I2C write functions from old and new Arduino versions. - #define _I2C_WRITE write ///< Modern I2C write - #define _I2C_READ read ///< Modern I2C read +#include // capital A so it is error prone on case-sensitive filesystems +// Macro to deal with the difference in I2C write functions from old and new +// Arduino versions. +#define _I2C_WRITE write ///< Modern I2C write +#define _I2C_READ read ///< Modern I2C read #else - #include - #define _I2C_WRITE send ///< Legacy I2C write - #define _I2C_READ receive ///< legacy I2C read +#include +#define _I2C_WRITE send ///< Legacy I2C write +#define _I2C_READ receive ///< legacy I2C read #endif - /**************************************************************************/ /*! @brief Read a byte from an I2C register @@ -106,7 +106,6 @@ static void write_i2c_register(uint8_t addr, uint8_t reg, uint8_t val) { Wire.endTransmission(); } - /**************************************************************************/ // utility code, some of this could be exposed in the DateTime API if needed /**************************************************************************/ @@ -116,7 +115,8 @@ static void write_i2c_register(uint8_t addr, uint8_t reg, uint8_t val) { needed. Omitting it avoids an incompatibility with Paul Stoffregen's Time library. C.f. https://github.com/adafruit/RTClib/issues/114 */ -const uint8_t daysInMonth [] PROGMEM = { 31,28,31,30,31,30,31,31,30,31,30 }; +const uint8_t daysInMonth[] PROGMEM = {31, 28, 31, 30, 31, 30, + 31, 31, 30, 31, 30}; /**************************************************************************/ /*! @@ -129,19 +129,20 @@ const uint8_t daysInMonth [] PROGMEM = { 31,28,31,30,31,30,31,31,30,31,30 }; */ /**************************************************************************/ static uint16_t date2days(uint16_t y, uint8_t m, uint8_t d) { - if (y >= 2000) - y -= 2000; - uint16_t days = d; - for (uint8_t i = 1; i < m; ++i) - days += pgm_read_byte(daysInMonth + i - 1); - if (m > 2 && y % 4 == 0) - ++days; - return days + 365 * y + (y + 3) / 4 - 1; + if (y >= 2000) + y -= 2000; + uint16_t days = d; + for (uint8_t i = 1; i < m; ++i) + days += pgm_read_byte(daysInMonth + i - 1); + if (m > 2 && y % 4 == 0) + ++days; + return days + 365 * y + (y + 3) / 4 - 1; } /**************************************************************************/ /*! - @brief Given a number of days, hours, minutes, and seconds, return the total seconds + @brief Given a number of days, hours, minutes, and seconds, return the + total seconds @param days Days @param h Hours @param m Minutes @@ -150,11 +151,9 @@ static uint16_t date2days(uint16_t y, uint8_t m, uint8_t d) { */ /**************************************************************************/ static uint32_t time2ulong(uint16_t days, uint8_t h, uint8_t m, uint8_t s) { - return ((days * 24UL + h) * 60 + m) * 60 + s; + return ((days * 24UL + h) * 60 + m) * 60 + s; } - - /**************************************************************************/ /*! @brief Constructor from @@ -183,8 +182,8 @@ static uint32_t time2ulong(uint16_t days, uint8_t h, uint8_t m, uint8_t s) { @param t Time elapsed in seconds since 1970-01-01 00:00:00. */ /**************************************************************************/ -DateTime::DateTime (uint32_t t) { - t -= SECONDS_FROM_1970_TO_2000; // bring to 2000 timestamp from 1970 +DateTime::DateTime(uint32_t t) { + t -= SECONDS_FROM_1970_TO_2000; // bring to 2000 timestamp from 1970 ss = t % 60; t /= 60; @@ -193,7 +192,7 @@ DateTime::DateTime (uint32_t t) { hh = t % 24; uint16_t days = t / 24; uint8_t leap; - for (yOff = 0; ; ++yOff) { + for (yOff = 0;; ++yOff) { leap = yOff % 4 == 0; if (days < 365 + leap) break; @@ -224,15 +223,16 @@ DateTime::DateTime (uint32_t t) { @param hour,min,sec Hour (0--23), minute (0--59) and second (0--59). */ /**************************************************************************/ -DateTime::DateTime (uint16_t year, uint8_t month, uint8_t day, uint8_t hour, uint8_t min, uint8_t sec) { - if (year >= 2000) - year -= 2000; - yOff = year; - m = month; - d = day; - hh = hour; - mm = min; - ss = sec; +DateTime::DateTime(uint16_t year, uint8_t month, uint8_t day, uint8_t hour, + uint8_t min, uint8_t sec) { + if (year >= 2000) + year -= 2000; + yOff = year; + m = month; + d = day; + hh = hour; + mm = min; + ss = sec; } /**************************************************************************/ @@ -241,26 +241,22 @@ DateTime::DateTime (uint16_t year, uint8_t month, uint8_t day, uint8_t hour, uin @param copy DateTime to copy. */ /**************************************************************************/ -DateTime::DateTime (const DateTime& copy): - yOff(copy.yOff), - m(copy.m), - d(copy.d), - hh(copy.hh), - mm(copy.mm), - ss(copy.ss) -{} +DateTime::DateTime(const DateTime ©) + : yOff(copy.yOff), m(copy.m), d(copy.d), hh(copy.hh), mm(copy.mm), + ss(copy.ss) {} /**************************************************************************/ /*! - @brief Convert a string containing two digits to uint8_t, e.g. "09" returns 9 + @brief Convert a string containing two digits to uint8_t, e.g. "09" returns + 9 @param p Pointer to a string containing two digits */ /**************************************************************************/ -static uint8_t conv2d(const char* p) { - uint8_t v = 0; - if ('0' <= *p && *p <= '9') - v = *p - '0'; - return 10 * v + *++p - '0'; +static uint8_t conv2d(const char *p) { + uint8_t v = 0; + if ('0' <= *p && *p <= '9') + v = *p - '0'; + return 10 * v + *++p - '0'; } /**************************************************************************/ @@ -282,23 +278,39 @@ static uint8_t conv2d(const char* p) { @param time Time string, e.g. "18:34:56". */ /**************************************************************************/ -DateTime::DateTime (const char* date, const char* time) { - yOff = conv2d(date + 9); - // Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec - switch (date[0]) { - case 'J': m = (date[1] == 'a') ? 1 : ((date[2] == 'n') ? 6 : 7); break; - case 'F': m = 2; break; - case 'A': m = date[2] == 'r' ? 4 : 8; break; - case 'M': m = date[2] == 'r' ? 3 : 5; break; - case 'S': m = 9; break; - case 'O': m = 10; break; - case 'N': m = 11; break; - case 'D': m = 12; break; - } - d = conv2d(date + 4); - hh = conv2d(time); - mm = conv2d(time + 3); - ss = conv2d(time + 6); +DateTime::DateTime(const char *date, const char *time) { + yOff = conv2d(date + 9); + // Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec + switch (date[0]) { + case 'J': + m = (date[1] == 'a') ? 1 : ((date[2] == 'n') ? 6 : 7); + break; + case 'F': + m = 2; + break; + case 'A': + m = date[2] == 'r' ? 4 : 8; + break; + case 'M': + m = date[2] == 'r' ? 3 : 5; + break; + case 'S': + m = 9; + break; + case 'O': + m = 10; + break; + case 'N': + m = 11; + break; + case 'D': + m = 12; + break; + } + d = conv2d(date + 4); + hh = conv2d(time); + mm = conv2d(time + 3); + ss = conv2d(time + 6); } /**************************************************************************/ @@ -316,26 +328,43 @@ DateTime::DateTime (const char* date, const char* time) { @param time Time PROGMEM string, e.g. F("18:34:56"). */ /**************************************************************************/ -DateTime::DateTime (const __FlashStringHelper* date, const __FlashStringHelper* time) { - char buff[11]; - memcpy_P(buff, date, 11); - yOff = conv2d(buff + 9); - // Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec - switch (buff[0]) { - case 'J': m = (buff[1] == 'a') ? 1 : ((buff[2] == 'n') ? 6 : 7); break; - case 'F': m = 2; break; - case 'A': m = buff[2] == 'r' ? 4 : 8; break; - case 'M': m = buff[2] == 'r' ? 3 : 5; break; - case 'S': m = 9; break; - case 'O': m = 10; break; - case 'N': m = 11; break; - case 'D': m = 12; break; - } - d = conv2d(buff + 4); - memcpy_P(buff, time, 8); - hh = conv2d(buff); - mm = conv2d(buff + 3); - ss = conv2d(buff + 6); +DateTime::DateTime(const __FlashStringHelper *date, + const __FlashStringHelper *time) { + char buff[11]; + memcpy_P(buff, date, 11); + yOff = conv2d(buff + 9); + // Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec + switch (buff[0]) { + case 'J': + m = (buff[1] == 'a') ? 1 : ((buff[2] == 'n') ? 6 : 7); + break; + case 'F': + m = 2; + break; + case 'A': + m = buff[2] == 'r' ? 4 : 8; + break; + case 'M': + m = buff[2] == 'r' ? 3 : 5; + break; + case 'S': + m = 9; + break; + case 'O': + m = 10; + break; + case 'N': + m = 11; + break; + case 'D': + m = 12; + break; + } + d = conv2d(buff + 4); + memcpy_P(buff, time, 8); + hh = conv2d(buff); + mm = conv2d(buff + 3); + ss = conv2d(buff + 6); } /**************************************************************************/ @@ -398,101 +427,94 @@ bool DateTime::isValid() const { */ /**************************************************************************/ -char* DateTime::toString(char* buffer){ - uint8_t apTag = (strstr(buffer, "ap") != nullptr) || (strstr(buffer, "AP") != nullptr); - uint8_t hourReformatted, isPM; - if(apTag) { //12 Hour Mode - if(hh == 0) { //midnight - isPM = false; - hourReformatted = 12; - } - else if(hh == 12) { //noon - isPM = true; - hourReformatted = 12; - } - else if(hh < 12) { //morning - isPM = false; - hourReformatted = hh; - } - else { //1 o'clock or after - isPM = true; - hourReformatted = hh - 12; - } +char *DateTime::toString(char *buffer) { + uint8_t apTag = + (strstr(buffer, "ap") != nullptr) || (strstr(buffer, "AP") != nullptr); + uint8_t hourReformatted, isPM; + if (apTag) { // 12 Hour Mode + if (hh == 0) { // midnight + isPM = false; + hourReformatted = 12; + } else if (hh == 12) { // noon + isPM = true; + hourReformatted = 12; + } else if (hh < 12) { // morning + isPM = false; + hourReformatted = hh; + } else { // 1 o'clock or after + isPM = true; + hourReformatted = hh - 12; } + } - for(int i=0;i 12) { //1 o'clock or later - return hh - 12; - } - else { //morning - return hh; - } + if (hh == 0 || hh == 12) { // midnight or noon + return 12; + } else if (hh > 12) { // 1 o'clock or later + return hh - 12; + } else { // morning + return hh; + } } /**************************************************************************/ @@ -521,8 +540,8 @@ uint8_t DateTime::twelveHour() const { */ /**************************************************************************/ uint8_t DateTime::dayOfTheWeek() const { - uint16_t day = date2days(yOff, m, d); - return (day + 6) % 7; // Jan 1, 2000 is a Saturday, i.e. returns 6 + uint16_t day = date2days(yOff, m, d); + return (day + 6) % 7; // Jan 1, 2000 is a Saturday, i.e. returns 6 } /**************************************************************************/ @@ -539,7 +558,7 @@ uint32_t DateTime::unixtime(void) const { uint32_t t; uint16_t days = date2days(yOff, m, d); t = time2ulong(days, hh, mm, ss); - t += SECONDS_FROM_1970_TO_2000; // seconds from 1970 to 2000 + t += SECONDS_FROM_1970_TO_2000; // seconds from 1970 to 2000 return t; } @@ -571,8 +590,8 @@ uint32_t DateTime::secondstime(void) const { @return New DateTime object with span added to it. */ /**************************************************************************/ -DateTime DateTime::operator+(const TimeSpan& span) { - return DateTime(unixtime()+span.totalseconds()); +DateTime DateTime::operator+(const TimeSpan &span) { + return DateTime(unixtime() + span.totalseconds()); } /**************************************************************************/ @@ -582,8 +601,8 @@ DateTime DateTime::operator+(const TimeSpan& span) { @return New DateTime object with span subtracted from it. */ /**************************************************************************/ -DateTime DateTime::operator-(const TimeSpan& span) { - return DateTime(unixtime()-span.totalseconds()); +DateTime DateTime::operator-(const TimeSpan &span) { + return DateTime(unixtime() - span.totalseconds()); } /**************************************************************************/ @@ -598,8 +617,8 @@ DateTime DateTime::operator-(const TimeSpan& span) { @return TimeSpan of the difference between DateTimes. */ /**************************************************************************/ -TimeSpan DateTime::operator-(const DateTime& right) { - return TimeSpan(unixtime()-right.unixtime()); +TimeSpan DateTime::operator-(const DateTime &right) { + return TimeSpan(unixtime() - right.unixtime()); } /**************************************************************************/ @@ -610,7 +629,7 @@ TimeSpan DateTime::operator-(const DateTime& right) { false otherwise. */ /**************************************************************************/ -bool DateTime::operator<(const DateTime& right) const { +bool DateTime::operator<(const DateTime &right) const { return unixtime() < right.unixtime(); } @@ -621,7 +640,7 @@ bool DateTime::operator<(const DateTime& right) const { @return True if both DateTime objects are the same, false otherwise. */ /**************************************************************************/ -bool DateTime::operator==(const DateTime& right) const { +bool DateTime::operator==(const DateTime &right) const { return unixtime() == right.unixtime(); } @@ -640,51 +659,49 @@ bool DateTime::operator==(const DateTime& right) const { @return Timestamp string, e.g. "2020-04-16T18:34:56". */ /**************************************************************************/ -String DateTime::timestamp(timestampOpt opt){ +String DateTime::timestamp(timestampOpt opt) { char buffer[20]; - //Generate timestamp according to opt - switch(opt){ - case TIMESTAMP_TIME: - //Only time + // Generate timestamp according to opt + switch (opt) { + case TIMESTAMP_TIME: + // Only time sprintf(buffer, "%02d:%02d:%02d", hh, mm, ss); break; - case TIMESTAMP_DATE: - //Only date - sprintf(buffer, "%d-%02d-%02d", 2000+yOff, m, d); + case TIMESTAMP_DATE: + // Only date + sprintf(buffer, "%d-%02d-%02d", 2000 + yOff, m, d); break; - default: - //Full - sprintf(buffer, "%d-%02d-%02dT%02d:%02d:%02d", 2000+yOff, m, d, hh, mm, ss); + default: + // Full + sprintf(buffer, "%d-%02d-%02dT%02d:%02d:%02d", 2000 + yOff, m, d, hh, mm, + ss); } return String(buffer); } - - /**************************************************************************/ /*! @brief Create a new TimeSpan object in seconds @param seconds Number of seconds */ /**************************************************************************/ -TimeSpan::TimeSpan (int32_t seconds): - _seconds(seconds) -{} +TimeSpan::TimeSpan(int32_t seconds) : _seconds(seconds) {} /**************************************************************************/ /*! - @brief Create a new TimeSpan object using a number of days/hours/minutes/seconds - e.g. Make a TimeSpan of 3 hours and 45 minutes: new TimeSpan(0, 3, 45, 0); + @brief Create a new TimeSpan object using a number of + days/hours/minutes/seconds e.g. Make a TimeSpan of 3 hours and 45 minutes: + new TimeSpan(0, 3, 45, 0); @param days Number of days @param hours Number of hours @param minutes Number of minutes @param seconds Number of seconds */ /**************************************************************************/ -TimeSpan::TimeSpan (int16_t days, int8_t hours, int8_t minutes, int8_t seconds): - _seconds((int32_t)days*86400L + (int32_t)hours*3600 + (int32_t)minutes*60 + seconds) -{} +TimeSpan::TimeSpan(int16_t days, int8_t hours, int8_t minutes, int8_t seconds) + : _seconds((int32_t)days * 86400L + (int32_t)hours * 3600 + + (int32_t)minutes * 60 + seconds) {} /**************************************************************************/ /*! @@ -692,9 +709,7 @@ TimeSpan::TimeSpan (int16_t days, int8_t hours, int8_t minutes, int8_t seconds): @param copy The TimeSpan to copy */ /**************************************************************************/ -TimeSpan::TimeSpan (const TimeSpan& copy): - _seconds(copy._seconds) -{} +TimeSpan::TimeSpan(const TimeSpan ©) : _seconds(copy._seconds) {} /**************************************************************************/ /*! @@ -703,8 +718,8 @@ TimeSpan::TimeSpan (const TimeSpan& copy): @return New TimeSpan object, sum of left and right */ /**************************************************************************/ -TimeSpan TimeSpan::operator+(const TimeSpan& right) { - return TimeSpan(_seconds+right._seconds); +TimeSpan TimeSpan::operator+(const TimeSpan &right) { + return TimeSpan(_seconds + right._seconds); } /**************************************************************************/ @@ -714,20 +729,19 @@ TimeSpan TimeSpan::operator+(const TimeSpan& right) { @return New TimeSpan object, right subtracted from left */ /**************************************************************************/ -TimeSpan TimeSpan::operator-(const TimeSpan& right) { - return TimeSpan(_seconds-right._seconds); +TimeSpan TimeSpan::operator-(const TimeSpan &right) { + return TimeSpan(_seconds - right._seconds); } - - /**************************************************************************/ /*! - @brief Convert a binary coded decimal value to binary. RTC stores time/date values as BCD. + @brief Convert a binary coded decimal value to binary. RTC stores time/date + values as BCD. @param val BCD value @return Binary value */ /**************************************************************************/ -static uint8_t bcd2bin (uint8_t val) { return val - 6 * (val >> 4); } +static uint8_t bcd2bin(uint8_t val) { return val - 6 * (val >> 4); } /**************************************************************************/ /*! @@ -736,9 +750,7 @@ static uint8_t bcd2bin (uint8_t val) { return val - 6 * (val >> 4); } @return BCD value */ /**************************************************************************/ -static uint8_t bin2bcd (uint8_t val) { return val + 6 * (val / 10); } - - +static uint8_t bin2bcd(uint8_t val) { return val + 6 * (val / 10); } /**************************************************************************/ /*! @@ -764,7 +776,7 @@ uint8_t RTC_DS1307::isrunning(void) { Wire.requestFrom(DS1307_ADDRESS, 1); uint8_t ss = Wire._I2C_READ(); - return !(ss>>7); + return !(ss >> 7); } /**************************************************************************/ @@ -773,7 +785,7 @@ uint8_t RTC_DS1307::isrunning(void) { @param dt DateTime object containing the desired date/time */ /**************************************************************************/ -void RTC_DS1307::adjust(const DateTime& dt) { +void RTC_DS1307::adjust(const DateTime &dt) { Wire.beginTransmission(DS1307_ADDRESS); Wire._I2C_WRITE((byte)0); // start at location 0 Wire._I2C_WRITE(bin2bcd(dt.second())); @@ -806,7 +818,7 @@ DateTime RTC_DS1307::now() { uint8_t m = bcd2bin(Wire._I2C_READ()); uint16_t y = bcd2bin(Wire._I2C_READ()) + 2000; - return DateTime (y, m, d, hh, mm, ss); + return DateTime(y, m, d, hh, mm, ss); } /**************************************************************************/ @@ -845,18 +857,19 @@ void RTC_DS1307::writeSqwPinMode(Ds1307SqwPinMode mode) { /**************************************************************************/ /*! @brief Read data from the DS1307's NVRAM - @param buf Pointer to a buffer to store the data - make sure it's large enough to hold size bytes + @param buf Pointer to a buffer to store the data - make sure it's large + enough to hold size bytes @param size Number of bytes to read @param address Starting NVRAM address, from 0 to 55 */ /**************************************************************************/ -void RTC_DS1307::readnvram(uint8_t* buf, uint8_t size, uint8_t address) { +void RTC_DS1307::readnvram(uint8_t *buf, uint8_t size, uint8_t address) { int addrByte = DS1307_NVRAM + address; Wire.beginTransmission(DS1307_ADDRESS); Wire._I2C_WRITE(addrByte); Wire.endTransmission(); - Wire.requestFrom((uint8_t) DS1307_ADDRESS, size); + Wire.requestFrom((uint8_t)DS1307_ADDRESS, size); for (uint8_t pos = 0; pos < size; ++pos) { buf[pos] = Wire._I2C_READ(); } @@ -870,7 +883,7 @@ void RTC_DS1307::readnvram(uint8_t* buf, uint8_t size, uint8_t address) { @param size Number of bytes in buf to write to NVRAM */ /**************************************************************************/ -void RTC_DS1307::writenvram(uint8_t address, uint8_t* buf, uint8_t size) { +void RTC_DS1307::writenvram(uint8_t address, uint8_t *buf, uint8_t size) { int addrByte = DS1307_NVRAM + address; Wire.beginTransmission(DS1307_ADDRESS); Wire._I2C_WRITE(addrByte); @@ -904,8 +917,6 @@ void RTC_DS1307::writenvram(uint8_t address, uint8_t data) { writenvram(address, &data, 1); } - - /** Alignment between the milis() timescale and the Unix timescale. These two variables are updated on each call to now(), which prevents rollover issues. Note that lastMillis is **not** the millis() value @@ -920,7 +931,7 @@ uint32_t RTC_Millis::lastUnix; @param dt DateTime object with the desired date and time */ /**************************************************************************/ -void RTC_Millis::adjust(const DateTime& dt) { +void RTC_Millis::adjust(const DateTime &dt) { lastMillis = millis(); lastUnix = dt.unixtime(); } @@ -940,9 +951,8 @@ DateTime RTC_Millis::now() { return lastUnix; } - - -/** Number of microseconds reported by micros() per "true" (calibrated) second. */ +/** Number of microseconds reported by micros() per "true" (calibrated) second. + */ uint32_t RTC_Micros::microsPerSecond = 1000000; /** The timing logic is identical to RTC_Millis. */ @@ -955,7 +965,7 @@ uint32_t RTC_Micros::lastUnix; @param dt DateTime object with the desired date and time */ /**************************************************************************/ -void RTC_Micros::adjust(const DateTime& dt) { +void RTC_Micros::adjust(const DateTime &dt) { lastMicros = micros(); lastUnix = dt.unixtime(); } @@ -967,9 +977,7 @@ void RTC_Micros::adjust(const DateTime& dt) { */ /**************************************************************************/ // A positive adjustment makes the clock faster. -void RTC_Micros::adjustDrift(int ppm) { - microsPerSecond = 1000000 - ppm; -} +void RTC_Micros::adjustDrift(int ppm) { microsPerSecond = 1000000 - ppm; } /**************************************************************************/ /*! @@ -984,8 +992,6 @@ DateTime RTC_Micros::now() { return lastUnix; } - - /**************************************************************************/ /*! @brief Start using the PCF8523 @@ -1001,7 +1007,8 @@ boolean RTC_PCF8523::begin(void) { /**************************************************************************/ /*! - @brief Check control register 3 to see if we've run adjust() yet (setting the date/time and battery switchover mode) + @brief Check control register 3 to see if we've run adjust() yet (setting + the date/time and battery switchover mode) @return True if the PCF8523 has been set up, false if not */ /**************************************************************************/ @@ -1021,7 +1028,7 @@ boolean RTC_PCF8523::initialized(void) { @param dt DateTime to set */ /**************************************************************************/ -void RTC_PCF8523::adjust(const DateTime& dt) { +void RTC_PCF8523::adjust(const DateTime &dt) { Wire.beginTransmission(PCF8523_ADDRESS); Wire._I2C_WRITE((byte)3); // start at location 3 Wire._I2C_WRITE(bin2bcd(dt.second())); @@ -1056,11 +1063,11 @@ DateTime RTC_PCF8523::now() { uint8_t mm = bcd2bin(Wire._I2C_READ()); uint8_t hh = bcd2bin(Wire._I2C_READ()); uint8_t d = bcd2bin(Wire._I2C_READ()); - Wire._I2C_READ(); // skip 'weekdays' + Wire._I2C_READ(); // skip 'weekdays' uint8_t m = bcd2bin(Wire._I2C_READ()); uint16_t y = bcd2bin(Wire._I2C_READ()) + 2000; - return DateTime (y, m, d, hh, mm, ss); + return DateTime(y, m, d, hh, mm, ss); } /**************************************************************************/ @@ -1103,12 +1110,14 @@ void RTC_PCF8523::writeSqwPinMode(Pcf8523SqwPinMode mode) { - Aging adjustment - Temperature compensation - Accuracy tuning - @param mode The offset mode to use, once every two hours or once every minute. See the Pcf8523OffsetMode enum. - @param offset Offset value from -64 to +63. See the datasheet for exact ppm values. + @param mode The offset mode to use, once every two hours or once every + minute. See the Pcf8523OffsetMode enum. + @param offset Offset value from -64 to +63. See the datasheet for exact ppm + values. */ /**************************************************************************/ void RTC_PCF8523::calibrate(Pcf8523OffsetMode mode, int8_t offset) { - uint8_t reg = (uint8_t) offset & 0x7F; + uint8_t reg = (uint8_t)offset & 0x7F; reg |= mode; Wire.beginTransmission(PCF8523_ADDRESS); @@ -1117,8 +1126,6 @@ void RTC_PCF8523::calibrate(Pcf8523OffsetMode mode, int8_t offset) { Wire.endTransmission(); } - - /**************************************************************************/ /*! @brief Start I2C for the DS3231 and test succesful connection @@ -1127,15 +1134,18 @@ void RTC_PCF8523::calibrate(Pcf8523OffsetMode mode, int8_t offset) { /**************************************************************************/ boolean RTC_DS3231::begin(void) { Wire.begin(); - Wire.beginTransmission (DS3231_ADDRESS); - if (Wire.endTransmission() == 0) return true; + Wire.beginTransmission(DS3231_ADDRESS); + if (Wire.endTransmission() == 0) + return true; return false; } /**************************************************************************/ /*! - @brief Check the status register Oscillator Stop Flag to see if the DS3231 stopped due to power loss - @return True if the bit is set (oscillator stopped) or false if it is running + @brief Check the status register Oscillator Stop Flag to see if the DS3231 + stopped due to power loss + @return True if the bit is set (oscillator stopped) or false if it is + running */ /**************************************************************************/ bool RTC_DS3231::lostPower(void) { @@ -1148,7 +1158,7 @@ bool RTC_DS3231::lostPower(void) { @param dt DateTime object containing the date/time to set */ /**************************************************************************/ -void RTC_DS3231::adjust(const DateTime& dt) { +void RTC_DS3231::adjust(const DateTime &dt) { Wire.beginTransmission(DS3231_ADDRESS); Wire._I2C_WRITE(DS3231_TIME); // start at location 0 Wire._I2C_WRITE(bin2bcd(dt.second())); @@ -1185,7 +1195,7 @@ DateTime RTC_DS3231::now() { uint8_t m = bcd2bin(Wire._I2C_READ()); uint16_t y = bcd2bin(Wire._I2C_READ()) + 2000; - return DateTime (y, m, d, hh, mm, ss); + return DateTime(y, m, d, hh, mm, ss); } /**************************************************************************/ @@ -1228,7 +1238,7 @@ void RTC_DS3231::writeSqwPinMode(Ds3231SqwPinMode mode) { } write_i2c_register(DS3231_ADDRESS, DS3231_CONTROL, ctrl); - //Serial.println( read_i2c_register(DS3231_ADDRESS, DS3231_CONTROL), HEX); + // Serial.println( read_i2c_register(DS3231_ADDRESS, DS3231_CONTROL), HEX); } /**************************************************************************/ @@ -1237,8 +1247,7 @@ void RTC_DS3231::writeSqwPinMode(Ds3231SqwPinMode mode) { @return Current temperature (float) */ /**************************************************************************/ -float RTC_DS3231::getTemperature() -{ +float RTC_DS3231::getTemperature() { uint8_t lsb; int8_t msb; Wire.beginTransmission(DS3231_ADDRESS); @@ -1249,118 +1258,120 @@ float RTC_DS3231::getTemperature() msb = Wire._I2C_READ(); lsb = Wire._I2C_READ(); -// Serial.print("msb="); -// Serial.print(msb,HEX); -// Serial.print(", lsb="); -// Serial.println(lsb,HEX); + // Serial.print("msb="); + // Serial.print(msb,HEX); + // Serial.print(", lsb="); + // Serial.println(lsb,HEX); - return (float) msb + (lsb >> 6) * 0.25f; + return (float)msb + (lsb >> 6) * 0.25f; } /**************************************************************************/ /*! @brief Set alarm 1 for DS3231 - @param dt DateTime object - @param alarm_mode Desired mode, see Ds3231Alarm1Mode enum + @param dt DateTime object + @param alarm_mode Desired mode, see Ds3231Alarm1Mode enum @return False if control register is not set, otherwise true */ /**************************************************************************/ -bool RTC_DS3231::setAlarm1(const DateTime& dt, Ds3231Alarm1Mode alarm_mode) { - uint8_t ctrl = read_i2c_register(DS3231_ADDRESS, DS3231_CONTROL); - if (!(ctrl & 0x04)) { - return false; - } +bool RTC_DS3231::setAlarm1(const DateTime &dt, Ds3231Alarm1Mode alarm_mode) { + uint8_t ctrl = read_i2c_register(DS3231_ADDRESS, DS3231_CONTROL); + if (!(ctrl & 0x04)) { + return false; + } - uint8_t A1M1 = (alarm_mode & 0x01) << 7; // Seconds bit 7. - uint8_t A1M2 = (alarm_mode & 0x02) << 6; // Minutes bit 7. - uint8_t A1M3 = (alarm_mode & 0x04) << 5; // Hour bit 7. - uint8_t A1M4 = (alarm_mode & 0x08) << 4; // Day/Date bit 7. - uint8_t DY_DT = (alarm_mode & 0x10) << 2; // Day/Date bit 6. Date when 0, day of week when 1. + uint8_t A1M1 = (alarm_mode & 0x01) << 7; // Seconds bit 7. + uint8_t A1M2 = (alarm_mode & 0x02) << 6; // Minutes bit 7. + uint8_t A1M3 = (alarm_mode & 0x04) << 5; // Hour bit 7. + uint8_t A1M4 = (alarm_mode & 0x08) << 4; // Day/Date bit 7. + uint8_t DY_DT = (alarm_mode & 0x10) + << 2; // Day/Date bit 6. Date when 0, day of week when 1. - Wire.beginTransmission(DS3231_ADDRESS); - Wire._I2C_WRITE(DS3231_ALARM1); - Wire._I2C_WRITE(bin2bcd(dt.second()) | A1M1); - Wire._I2C_WRITE(bin2bcd(dt.minute()) | A1M2); - Wire._I2C_WRITE(bin2bcd(dt.hour()) | A1M3); - if (DY_DT) { - Wire._I2C_WRITE(bin2bcd(dt.dayOfTheWeek()) | A1M4 | DY_DT); - } else { - Wire._I2C_WRITE(bin2bcd(dt.day()) | A1M4 | DY_DT); - } - Wire.endTransmission(); + Wire.beginTransmission(DS3231_ADDRESS); + Wire._I2C_WRITE(DS3231_ALARM1); + Wire._I2C_WRITE(bin2bcd(dt.second()) | A1M1); + Wire._I2C_WRITE(bin2bcd(dt.minute()) | A1M2); + Wire._I2C_WRITE(bin2bcd(dt.hour()) | A1M3); + if (DY_DT) { + Wire._I2C_WRITE(bin2bcd(dt.dayOfTheWeek()) | A1M4 | DY_DT); + } else { + Wire._I2C_WRITE(bin2bcd(dt.day()) | A1M4 | DY_DT); + } + Wire.endTransmission(); - ctrl |= 0x01; // AI1E - write_i2c_register(DS3231_ADDRESS, DS3231_CONTROL, ctrl); - return true; + ctrl |= 0x01; // AI1E + write_i2c_register(DS3231_ADDRESS, DS3231_CONTROL, ctrl); + return true; } /**************************************************************************/ /*! @brief Set alarm 2 for DS3231 - @param dt DateTime object - @param alarm_mode Desired mode, see Ds3231Alarm2Mode enum + @param dt DateTime object + @param alarm_mode Desired mode, see Ds3231Alarm2Mode enum @return False if control register is not set, otherwise true */ /**************************************************************************/ -bool RTC_DS3231::setAlarm2(const DateTime& dt, Ds3231Alarm2Mode alarm_mode) { - uint8_t ctrl = read_i2c_register(DS3231_ADDRESS, DS3231_CONTROL); - if (!(ctrl & 0x04)) { - return false; - } +bool RTC_DS3231::setAlarm2(const DateTime &dt, Ds3231Alarm2Mode alarm_mode) { + uint8_t ctrl = read_i2c_register(DS3231_ADDRESS, DS3231_CONTROL); + if (!(ctrl & 0x04)) { + return false; + } - uint8_t A2M2 = (alarm_mode & 0x01) << 7; // Minutes bit 7. - uint8_t A2M3 = (alarm_mode & 0x02) << 6; // Hour bit 7. - uint8_t A2M4 = (alarm_mode & 0x04) << 5; // Day/Date bit 7. - uint8_t DY_DT = (alarm_mode & 0x8) << 3; // Day/Date bit 6. Date when 0, day of week when 1. + uint8_t A2M2 = (alarm_mode & 0x01) << 7; // Minutes bit 7. + uint8_t A2M3 = (alarm_mode & 0x02) << 6; // Hour bit 7. + uint8_t A2M4 = (alarm_mode & 0x04) << 5; // Day/Date bit 7. + uint8_t DY_DT = (alarm_mode & 0x8) + << 3; // Day/Date bit 6. Date when 0, day of week when 1. - Wire.beginTransmission(DS3231_ADDRESS); - Wire._I2C_WRITE(DS3231_ALARM2); - Wire._I2C_WRITE(bin2bcd(dt.minute()) | A2M2); - Wire._I2C_WRITE(bin2bcd(dt.hour()) | A2M3); - if (DY_DT) { - Wire._I2C_WRITE(bin2bcd(dt.dayOfTheWeek()) | A2M4 | DY_DT); - } else { - Wire._I2C_WRITE(bin2bcd(dt.day()) | A2M4 | DY_DT); - } - Wire.endTransmission(); + Wire.beginTransmission(DS3231_ADDRESS); + Wire._I2C_WRITE(DS3231_ALARM2); + Wire._I2C_WRITE(bin2bcd(dt.minute()) | A2M2); + Wire._I2C_WRITE(bin2bcd(dt.hour()) | A2M3); + if (DY_DT) { + Wire._I2C_WRITE(bin2bcd(dt.dayOfTheWeek()) | A2M4 | DY_DT); + } else { + Wire._I2C_WRITE(bin2bcd(dt.day()) | A2M4 | DY_DT); + } + Wire.endTransmission(); - ctrl |= 0x02; // AI2E - write_i2c_register(DS3231_ADDRESS, DS3231_CONTROL, ctrl); - return true; + ctrl |= 0x02; // AI2E + write_i2c_register(DS3231_ADDRESS, DS3231_CONTROL, ctrl); + return true; } /**************************************************************************/ /*! @brief Disable alarm - @param alarm_num Alarm number to disable + @param alarm_num Alarm number to disable */ /**************************************************************************/ void RTC_DS3231::disableAlarm(uint8_t alarm_num) { - uint8_t ctrl = read_i2c_register(DS3231_ADDRESS, DS3231_CONTROL); - ctrl &= ~(1 << (alarm_num - 1)); - write_i2c_register(DS3231_ADDRESS, DS3231_CONTROL, ctrl); + uint8_t ctrl = read_i2c_register(DS3231_ADDRESS, DS3231_CONTROL); + ctrl &= ~(1 << (alarm_num - 1)); + write_i2c_register(DS3231_ADDRESS, DS3231_CONTROL, ctrl); } /**************************************************************************/ /*! @brief Clear status of alarm - @param alarm_num Alarm number to clear + @param alarm_num Alarm number to clear */ /**************************************************************************/ void RTC_DS3231::clearAlarm(uint8_t alarm_num) { - uint8_t status = read_i2c_register(DS3231_ADDRESS, DS3231_STATUSREG); - status &= ~(0x1 << (alarm_num - 1)); - write_i2c_register(DS3231_ADDRESS, DS3231_STATUSREG, status); + uint8_t status = read_i2c_register(DS3231_ADDRESS, DS3231_STATUSREG); + status &= ~(0x1 << (alarm_num - 1)); + write_i2c_register(DS3231_ADDRESS, DS3231_STATUSREG, status); } /**************************************************************************/ /*! @brief Get status of alarm - @param alarm_num Alarm number to check status of - @return True if alarm has been fired otherwise false + @param alarm_num Alarm number to check status of + @return True if alarm has been fired otherwise false */ /**************************************************************************/ bool RTC_DS3231::alarmFired(uint8_t alarm_num) { - uint8_t status = read_i2c_register(DS3231_ADDRESS, DS3231_STATUSREG); - return (status >> (alarm_num - 1)) & 0x1; + uint8_t status = read_i2c_register(DS3231_ADDRESS, DS3231_STATUSREG); + return (status >> (alarm_num - 1)) & 0x1; } diff --git a/RTClib.h b/RTClib.h index b1578cf..8d95832 100644 --- a/RTClib.h +++ b/RTClib.h @@ -2,14 +2,16 @@ /*! @file RTClib.h - Original library by JeeLabs http://news.jeelabs.org/code/, released to the public domain + Original library by JeeLabs http://news.jeelabs.org/code/, released to the + public domain License: MIT (see LICENSE) This is a fork of JeeLab's fantastic real time clock library for Arduino. - For details on using this library with an RTC module like the DS1307, PCF8523, or DS3231, - see the guide at: https://learn.adafruit.com/ds1307-real-time-clock-breakout-board-kit/overview + For details on using this library with an RTC module like the DS1307, PCF8523, + or DS3231, see the guide at: + https://learn.adafruit.com/ds1307-real-time-clock-breakout-board-kit/overview Adafruit invests time and resources providing this open source code, please support Adafruit and open-source hardware by purchasing @@ -24,27 +26,29 @@ class TimeSpan; /** Registers */ -#define PCF8523_ADDRESS 0x68 ///< I2C address for PCF8523 -#define PCF8523_CLKOUTCONTROL 0x0F ///< Timer and CLKOUT control register -#define PCF8523_CONTROL_3 0x02 ///< Control and status register 3 -#define PCF8523_OFFSET 0x0E ///< Offset register +#define PCF8523_ADDRESS 0x68 ///< I2C address for PCF8523 +#define PCF8523_CLKOUTCONTROL 0x0F ///< Timer and CLKOUT control register +#define PCF8523_CONTROL_3 0x02 ///< Control and status register 3 +#define PCF8523_OFFSET 0x0E ///< Offset register -#define DS1307_ADDRESS 0x68 ///< I2C address for DS1307 -#define DS1307_CONTROL 0x07 ///< Control register -#define DS1307_NVRAM 0x08 ///< Start of RAM registers - 56 bytes, 0x08 to 0x3f +#define DS1307_ADDRESS 0x68 ///< I2C address for DS1307 +#define DS1307_CONTROL 0x07 ///< Control register +#define DS1307_NVRAM 0x08 ///< Start of RAM registers - 56 bytes, 0x08 to 0x3f -#define DS3231_ADDRESS 0x68 ///< I2C address for DS3231 -#define DS3231_TIME 0x00 ///< Time register -#define DS3231_ALARM1 0x07 ///< Alarm 1 register -#define DS3231_ALARM2 0x0B ///< Alarm 2 register -#define DS3231_CONTROL 0x0E ///< Control register -#define DS3231_STATUSREG 0x0F ///< Status register -#define DS3231_TEMPERATUREREG 0x11 ///< Temperature register (high byte - low byte is at 0x12), 10-bit temperature value +#define DS3231_ADDRESS 0x68 ///< I2C address for DS3231 +#define DS3231_TIME 0x00 ///< Time register +#define DS3231_ALARM1 0x07 ///< Alarm 1 register +#define DS3231_ALARM2 0x0B ///< Alarm 2 register +#define DS3231_CONTROL 0x0E ///< Control register +#define DS3231_STATUSREG 0x0F ///< Status register +#define DS3231_TEMPERATUREREG \ + 0x11 ///< Temperature register (high byte - low byte is at 0x12), 10-bit + ///< temperature value /** Constants */ -#define SECONDS_PER_DAY 86400L ///< 60 * 60 * 24 -#define SECONDS_FROM_1970_TO_2000 946684800 ///< Unixtime for 2000-01-01 00:00:00, useful for initialization - +#define SECONDS_PER_DAY 86400L ///< 60 * 60 * 24 +#define SECONDS_FROM_1970_TO_2000 \ + 946684800 ///< Unixtime for 2000-01-01 00:00:00, useful for initialization /**************************************************************************/ /*! @@ -64,53 +68,51 @@ class TimeSpan; /**************************************************************************/ class DateTime { public: - DateTime (uint32_t t = SECONDS_FROM_1970_TO_2000); - DateTime (uint16_t year, uint8_t month, uint8_t day, - uint8_t hour = 0, uint8_t min = 0, uint8_t sec = 0); - DateTime (const DateTime& copy); - DateTime (const char* date, const char* time); - DateTime (const __FlashStringHelper* date, const __FlashStringHelper* time); - - bool isValid() const; - char* toString(char* buffer); + DateTime(uint32_t t = SECONDS_FROM_1970_TO_2000); + DateTime(uint16_t year, uint8_t month, uint8_t day, uint8_t hour = 0, + uint8_t min = 0, uint8_t sec = 0); + DateTime(const DateTime ©); + DateTime(const char *date, const char *time); + DateTime(const __FlashStringHelper *date, const __FlashStringHelper *time); + char *toString(char *buffer); /*! @brief Return the year. @return Year (range: 2000--2099). */ - uint16_t year() const { return 2000 + yOff; } + uint16_t year() const { return 2000 + yOff; } /*! @brief Return the month. @return Month number (1--12). */ - uint8_t month() const { return m; } + uint8_t month() const { return m; } /*! @brief Return the day of the month. @return Day of the month (1--31). */ - uint8_t day() const { return d; } + uint8_t day() const { return d; } /*! @brief Return the hour @return Hour (0--23). */ - uint8_t hour() const { return hh; } + uint8_t hour() const { return hh; } uint8_t twelveHour() const; /*! @brief Return whether the time is PM. @return 0 if the time is AM, 1 if it's PM. */ - uint8_t isPM() const { return hh >= 12; } + uint8_t isPM() const { return hh >= 12; } /*! @brief Return the minute. @return Minute (0--59). */ - uint8_t minute() const { return mm; } + uint8_t minute() const { return mm; } /*! @brief Return the second. @return Second (0--59). */ - uint8_t second() const { return ss; } + uint8_t second() const { return ss; } uint8_t dayOfTheWeek() const; @@ -124,56 +126,55 @@ public: Format of the ISO 8601 timestamp generated by `timestamp()`. Each option corresponds to a `toString()` format as follows: */ - enum timestampOpt{ + enum timestampOpt { TIMESTAMP_FULL, //!< `YYYY-MM-DDThh:mm:ss` TIMESTAMP_TIME, //!< `hh:mm:ss` TIMESTAMP_DATE //!< `YYYY-MM-DD` }; String timestamp(timestampOpt opt = TIMESTAMP_FULL); - DateTime operator+(const TimeSpan& span); - DateTime operator-(const TimeSpan& span); - TimeSpan operator-(const DateTime& right); - bool operator<(const DateTime& right) const; + DateTime operator+(const TimeSpan &span); + DateTime operator-(const TimeSpan &span); + TimeSpan operator-(const DateTime &right); + bool operator<(const DateTime &right) const; /*! @brief Test if one DateTime is greater (later) than another. @param right DateTime object to compare @return True if the left DateTime is later than the right one, false otherwise */ - bool operator>(const DateTime& right) const { return right < *this; } + bool operator>(const DateTime &right) const { return right < *this; } /*! @brief Test if one DateTime is less (earlier) than or equal to another @param right DateTime object to compare @return True if the left DateTime is earlier than or equal to the right one, false otherwise */ - bool operator<=(const DateTime& right) const { return !(*this > right); } + bool operator<=(const DateTime &right) const { return !(*this > right); } /*! @brief Test if one DateTime is greater (later) than or equal to another @param right DateTime object to compare @return True if the left DateTime is later than or equal to the right one, false otherwise */ - bool operator>=(const DateTime& right) const { return !(*this < right); } - bool operator==(const DateTime& right) const; + bool operator>=(const DateTime &right) const { return !(*this < right); } + bool operator==(const DateTime &right) const; /*! @brief Test if two DateTime objects are not equal. @param right DateTime object to compare @return True if the two objects are not equal, false if they are */ - bool operator!=(const DateTime& right) const { return !(*this == right); } + bool operator!=(const DateTime &right) const { return !(*this == right); } protected: - uint8_t yOff; ///< Year offset from 2000 - uint8_t m; ///< Month 1-12 - uint8_t d; ///< Day 1-31 - uint8_t hh; ///< Hours 0-23 - uint8_t mm; ///< Minutes 0-59 - uint8_t ss; ///< Seconds 0-59 + uint8_t yOff; ///< Year offset from 2000 + uint8_t m; ///< Month 1-12 + uint8_t d; ///< Day 1-31 + uint8_t hh; ///< Hours 0-23 + uint8_t mm; ///< Minutes 0-59 + uint8_t ss; ///< Seconds 0-59 }; - /**************************************************************************/ /*! @brief Timespan which can represent changes in time with seconds accuracy. @@ -181,60 +182,58 @@ protected: /**************************************************************************/ class TimeSpan { public: - TimeSpan (int32_t seconds = 0); - TimeSpan (int16_t days, int8_t hours, int8_t minutes, int8_t seconds); - TimeSpan (const TimeSpan& copy); + TimeSpan(int32_t seconds = 0); + TimeSpan(int16_t days, int8_t hours, int8_t minutes, int8_t seconds); + TimeSpan(const TimeSpan ©); /*! @brief Number of days in the TimeSpan e.g. 4 @return int16_t days */ - int16_t days() const { return _seconds / 86400L; } + int16_t days() const { return _seconds / 86400L; } /*! @brief Number of hours in the TimeSpan This is not the total hours, it includes the days e.g. 4 days, 3 hours - NOT 99 hours @return int8_t hours */ - int8_t hours() const { return _seconds / 3600 % 24; } + int8_t hours() const { return _seconds / 3600 % 24; } /*! @brief Number of minutes in the TimeSpan This is not the total minutes, it includes days/hours e.g. 4 days, 3 hours, 27 minutes @return int8_t minutes */ - int8_t minutes() const { return _seconds / 60 % 60; } + int8_t minutes() const { return _seconds / 60 % 60; } /*! @brief Number of seconds in the TimeSpan This is not the total seconds, it includes the days/hours/minutes e.g. 4 days, 3 hours, 27 minutes, 7 seconds @return int8_t seconds */ - int8_t seconds() const { return _seconds % 60; } + int8_t seconds() const { return _seconds % 60; } /*! @brief Total number of seconds in the TimeSpan, e.g. 358027 @return int32_t seconds */ int32_t totalseconds() const { return _seconds; } - TimeSpan operator+(const TimeSpan& right); - TimeSpan operator-(const TimeSpan& right); + TimeSpan operator+(const TimeSpan &right); + TimeSpan operator-(const TimeSpan &right); protected: - int32_t _seconds; ///< Actual TimeSpan value is stored as seconds + int32_t _seconds; ///< Actual TimeSpan value is stored as seconds }; - - /** DS1307 SQW pin mode settings */ enum Ds1307SqwPinMode { - DS1307_OFF = 0x00, // Low - DS1307_ON = 0x80, // High - DS1307_SquareWave1HZ = 0x10, // 1Hz square wave - DS1307_SquareWave4kHz = 0x11, // 4kHz square wave - DS1307_SquareWave8kHz = 0x12, // 8kHz square wave - DS1307_SquareWave32kHz = 0x13 // 32kHz square wave + DS1307_OFF = 0x00, // Low + DS1307_ON = 0x80, // High + DS1307_SquareWave1HZ = 0x10, // 1Hz square wave + DS1307_SquareWave4kHz = 0x11, // 4kHz square wave + DS1307_SquareWave8kHz = 0x12, // 8kHz square wave + DS1307_SquareWave32kHz = 0x13 // 32kHz square wave }; /**************************************************************************/ @@ -245,23 +244,21 @@ enum Ds1307SqwPinMode { class RTC_DS1307 { public: boolean begin(void); - static void adjust(const DateTime& dt); + static void adjust(const DateTime &dt); uint8_t isrunning(void); static DateTime now(); static Ds1307SqwPinMode readSqwPinMode(); static void writeSqwPinMode(Ds1307SqwPinMode mode); uint8_t readnvram(uint8_t address); - void readnvram(uint8_t* buf, uint8_t size, uint8_t address); + void readnvram(uint8_t *buf, uint8_t size, uint8_t address); void writenvram(uint8_t address, uint8_t data); - void writenvram(uint8_t address, uint8_t* buf, uint8_t size); + void writenvram(uint8_t address, uint8_t *buf, uint8_t size); }; - - /** DS3231 SQW pin mode settings */ enum Ds3231SqwPinMode { - DS3231_OFF = 0x01, // Off - DS3231_SquareWave1Hz = 0x00, // 1Hz square wave + DS3231_OFF = 0x01, // Off + DS3231_SquareWave1Hz = 0x00, // 1Hz square wave DS3231_SquareWave1kHz = 0x08, // 1kHz square wave DS3231_SquareWave4kHz = 0x10, // 4kHz square wave DS3231_SquareWave8kHz = 0x18 // 8kHz square wave @@ -293,37 +290,35 @@ enum Ds3231Alarm2Mode { class RTC_DS3231 { public: boolean begin(void); - static void adjust(const DateTime& dt); + static void adjust(const DateTime &dt); bool lostPower(void); static DateTime now(); static Ds3231SqwPinMode readSqwPinMode(); static void writeSqwPinMode(Ds3231SqwPinMode mode); - bool setAlarm1(const DateTime& dt, Ds3231Alarm1Mode alarm_mode); - bool setAlarm2(const DateTime& dt, Ds3231Alarm2Mode alarm_mode); + bool setAlarm1(const DateTime &dt, Ds3231Alarm1Mode alarm_mode); + bool setAlarm2(const DateTime &dt, Ds3231Alarm2Mode alarm_mode); void disableAlarm(uint8_t alarm_num); void clearAlarm(uint8_t alarm_num); bool alarmFired(uint8_t alarm_num); - static float getTemperature(); // in Celcius degree + static float getTemperature(); // in Celcius degree }; - - /** PCF8523 SQW pin mode settings */ enum Pcf8523SqwPinMode { - PCF8523_OFF = 7, // Off - PCF8523_SquareWave1HZ = 6, // 1Hz square wave - PCF8523_SquareWave32HZ = 5, // 32Hz square wave - PCF8523_SquareWave1kHz = 4, // 1kHz square wave - PCF8523_SquareWave4kHz = 3, // 4kHz square wave - PCF8523_SquareWave8kHz = 2, // 8kHz square wave + PCF8523_OFF = 7, // Off + PCF8523_SquareWave1HZ = 6, // 1Hz square wave + PCF8523_SquareWave32HZ = 5, // 32Hz square wave + PCF8523_SquareWave1kHz = 4, // 1kHz square wave + PCF8523_SquareWave4kHz = 3, // 4kHz square wave + PCF8523_SquareWave8kHz = 2, // 8kHz square wave PCF8523_SquareWave16kHz = 1, // 16kHz square wave PCF8523_SquareWave32kHz = 0 // 32kHz square wave }; /** PCF8523 Offset modes for making temperature/aging/accuracy adjustments */ enum Pcf8523OffsetMode { - PCF8523_TwoHours = 0x00, // Offset made every two hours - PCF8523_OneMinute = 0x80 // Offset made every minute + PCF8523_TwoHours = 0x00, // Offset made every two hours + PCF8523_OneMinute = 0x80 // Offset made every minute }; /**************************************************************************/ @@ -334,7 +329,7 @@ enum Pcf8523OffsetMode { class RTC_PCF8523 { public: boolean begin(void); - void adjust(const DateTime& dt); + void adjust(const DateTime &dt); boolean initialized(void); static DateTime now(); @@ -343,11 +338,10 @@ public: void calibrate(Pcf8523OffsetMode mode, int8_t offset); }; - /**************************************************************************/ /*! - @brief RTC using the internal millis() clock, has to be initialized before use. - NOTE: this is immune to millis() rollover events. + @brief RTC using the internal millis() clock, has to be initialized before + use. NOTE: this is immune to millis() rollover events. */ /**************************************************************************/ class RTC_Millis { @@ -356,17 +350,17 @@ public: @brief Start the RTC @param dt DateTime object with the date/time to set */ - static void begin(const DateTime& dt) { adjust(dt); } - static void adjust(const DateTime& dt); + static void begin(const DateTime &dt) { adjust(dt); } + static void adjust(const DateTime &dt); static DateTime now(); protected: - static uint32_t lastUnix; ///< Unix time from the previous call to now() - prevents rollover issues - static uint32_t lastMillis; ///< the millis() value corresponding to the last **full second** of Unix time + static uint32_t lastUnix; ///< Unix time from the previous call to now() - + ///< prevents rollover issues + static uint32_t lastMillis; ///< the millis() value corresponding to the last + ///< **full second** of Unix time }; - - /**************************************************************************/ /*! @brief RTC using the internal micros() clock, has to be initialized before @@ -382,15 +376,18 @@ public: @brief Start the RTC @param dt DateTime object with the date/time to set */ - static void begin(const DateTime& dt) { adjust(dt); } - static void adjust(const DateTime& dt); + static void begin(const DateTime &dt) { adjust(dt); } + static void adjust(const DateTime &dt); static void adjustDrift(int ppm); static DateTime now(); protected: - static uint32_t microsPerSecond; ///< Number of microseconds reported by micros() per "true" (calibrated) second - static uint32_t lastUnix; ///< Unix time from the previous call to now() - prevents rollover issues - static uint32_t lastMicros; ///< micros() value corresponding to the last full second of Unix time + static uint32_t microsPerSecond; ///< Number of microseconds reported by + ///< micros() per "true" (calibrated) second + static uint32_t lastUnix; ///< Unix time from the previous call to now() - + ///< prevents rollover issues + static uint32_t lastMicros; ///< micros() value corresponding to the last full + ///< second of Unix time }; #endif // _RTCLIB_H_ diff --git a/library.properties b/library.properties index a43dd6d..44a50c4 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=RTClib -version=1.4.1 +version=1.4.2 author=Adafruit maintainer=Adafruit sentence=A fork of Jeelab's fantastic RTC library @@ -7,3 +7,4 @@ paragraph=A fork of Jeelab's fantastic RTC library category=Timing url=https://github.com/adafruit/RTClib architectures=* +depends=TinyWireM, OneWire diff --git a/LICENSE b/license.txt similarity index 100% rename from LICENSE rename to license.txt