Skip to content

Commit

Permalink
Instantiate RTC object for AVR architecture only.
Browse files Browse the repository at this point in the history
Add parameter to constructor to indicate whether to
initialize the I2C bus (default is true). Closes #37.
  • Loading branch information
JChristensen committed Mar 3, 2018
1 parent 7ee483b commit 5be912a
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 33 deletions.
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=DS3232RTC
version=1.1.1
version=1.2.0
author=Jack Christensen <[email protected]>
maintainer=Jack Christensen <[email protected]>
sentence=Arduino Library for Maxim Integrated DS3232 and DS3231 Real-Time Clocks.
Expand Down
52 changes: 22 additions & 30 deletions src/DS3232RTC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@
// https://playground.arduino.cc/Code/Time
// https://github.com/PaulStoffregen/Time
//
// For AVR architecture, a DS3232RTC object named RTC is instantiated
// by the library and I2C initialization occurs in the constructor;
// this is for backwards compatibility.
// For other architectures, the user needs to instantiate a DS3232RTC
// object and optionally initialize the I2C bus by calling
// DS3232RTC::begin(). The constructor has an optional bool parameter
// to indicate whether I2C initialization should occur in the
// constructor; this parameter defaults to true if not given.
//
// Jack Christensen 06Mar2013
// https://github.com/JChristensen/DS3232RTC
//
Expand All @@ -14,39 +23,22 @@

#include <DS3232RTC.h>

//define release-independent I2C functions
#if defined(__AVR_ATtiny44__) || defined(__AVR_ATtiny84__) || defined(__AVR_ATtiny45__) || defined(__AVR_ATtiny85__)
#include <TinyWireM.h>
#define i2cBegin TinyWireM.begin
#define i2cBeginTransmission TinyWireM.beginTransmission
#define i2cEndTransmission TinyWireM.endTransmission
#define i2cRequestFrom TinyWireM.requestFrom
#define i2cRead TinyWireM.receive
#define i2cWrite TinyWireM.send
#elif ARDUINO >= 100
#include <Wire.h>
#define i2cBegin Wire.begin
#define i2cBeginTransmission Wire.beginTransmission
#define i2cEndTransmission Wire.endTransmission
#define i2cRequestFrom Wire.requestFrom
#define i2cRead Wire.read
#define i2cWrite Wire.write
#else
#include <Wire.h>
#define i2cBegin Wire.begin
#define i2cBeginTransmission Wire.beginTransmission
#define i2cEndTransmission Wire.endTransmission
#define i2cRequestFrom Wire.requestFrom
#define i2cRead Wire.receive
#define i2cWrite Wire.send
#endif

byte DS3232RTC::errCode; //for debug

/*----------------------------------------------------------------------*
* Constructor. *
* Constructor. Initializes the I2C bus by default, but better *
* practice is to pass false in the constructor and call *
* the begin() function in the setup code. *
*----------------------------------------------------------------------*/
DS3232RTC::DS3232RTC(bool initI2C)
{
if (initI2C) i2cBegin();
}

/*----------------------------------------------------------------------*
* Initialize the I2C bus. *
*----------------------------------------------------------------------*/
DS3232RTC::DS3232RTC()
void DS3232RTC::begin()
{
i2cBegin();
}
Expand Down Expand Up @@ -323,6 +315,6 @@ uint8_t __attribute__ ((noinline)) DS3232RTC::bcd2dec(uint8_t n)
return n - 6 * (n >> 4);
}

#if defined ARDUINO_ARCH_AVR
#ifdef ARDUINO_ARCH_AVR
DS3232RTC RTC; //instantiate an RTC object
#endif
41 changes: 39 additions & 2 deletions src/DS3232RTC.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@
// https://playground.arduino.cc/Code/Time
// https://github.com/PaulStoffregen/Time
//
// For AVR architecture, a DS3232RTC object named RTC is instantiated
// by the library and I2C initialization occurs in the constructor;
// this is for backwards compatibility.
// For other architectures, the user needs to instantiate a DS3232RTC
// object and optionally initialize the I2C bus by calling
// DS3232RTC::begin(). The constructor has an optional bool parameter
// to indicate whether I2C initialization should occur in the
// constructor; this parameter defaults to true if not given.
//
// Jack Christensen 06Mar2013
// https://github.com/JChristensen/DS3232RTC
//
Expand All @@ -22,6 +31,33 @@
#include <WProgram.h>
#endif

//define release-independent I2C functions
#if defined(__AVR_ATtiny44__) || defined(__AVR_ATtiny84__) || defined(__AVR_ATtiny45__) || defined(__AVR_ATtiny85__)
#include <TinyWireM.h>
#define i2cBegin TinyWireM.begin
#define i2cBeginTransmission TinyWireM.beginTransmission
#define i2cEndTransmission TinyWireM.endTransmission
#define i2cRequestFrom TinyWireM.requestFrom
#define i2cRead TinyWireM.receive
#define i2cWrite TinyWireM.send
#elif ARDUINO >= 100
#include <Wire.h>
#define i2cBegin Wire.begin
#define i2cBeginTransmission Wire.beginTransmission
#define i2cEndTransmission Wire.endTransmission
#define i2cRequestFrom Wire.requestFrom
#define i2cRead Wire.read
#define i2cWrite Wire.write
#else
#include <Wire.h>
#define i2cBegin Wire.begin
#define i2cBeginTransmission Wire.beginTransmission
#define i2cEndTransmission Wire.endTransmission
#define i2cRequestFrom Wire.requestFrom
#define i2cRead Wire.receive
#define i2cWrite Wire.send
#endif

//DS3232 I2C Address
#define RTC_ADDR 0x68

Expand Down Expand Up @@ -107,7 +143,8 @@ enum ALARM_TYPES_t {
class DS3232RTC
{
public:
DS3232RTC();
DS3232RTC(bool initI2C = true);
void begin();
static time_t get(); //must be static to work with setSyncProvider() in the Time library
byte set(time_t t);
static byte read(tmElements_t &tm);
Expand All @@ -130,7 +167,7 @@ class DS3232RTC
static uint8_t bcd2dec(uint8_t n);
};

#if defined ARDUINO_ARCH_AVR
#ifdef ARDUINO_ARCH_AVR
extern DS3232RTC RTC;
#endif

Expand Down

0 comments on commit 5be912a

Please sign in to comment.