Skip to content

Commit 0d074a4

Browse files
committed
DSI2C is now a standalone controller, moved most files to utility folder to avoid polluting Arduino's global preprocessor namespace.
1 parent ffa9833 commit 0d074a4

File tree

5 files changed

+29
-20
lines changed

5 files changed

+29
-20
lines changed

DS3231.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
#ifndef __DS3231_H
22
#define __DS3231_H
33

4-
#include "DSTemp.h"
4+
#include "utility/DSTemp.h"
55

6-
class DS3231 : public DSTemp { };
6+
class DS3231 : public DSTemp {
7+
public:
8+
DS3231() : rtc(DS()) { };
9+
private:
10+
DS& rtc;
711

812
DS3231 RTC = DS3231();
913

DS.cpp renamed to utility/DS.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
#include "DS.h"
22

3+
uint8_t DSI2C::slaveAddress = DSRTC_SLAVE_ADDRESS;
4+
35
/**
46
* Read the value at the given register address.
57
* @param addr the register address to read
68
* @return The register value.
79
*/
8-
uint8_t DS::readRegister(uint8_t addr) {
10+
uint8_t DSI2C::readRegister(uint8_t addr) {
911
Wire.beginTransmission(slaveAddress);
1012
Wire.send(addr);
1113
Wire.endTransmission();
@@ -19,7 +21,7 @@ uint8_t DS::readRegister(uint8_t addr) {
1921
* @param len the number of bytes to read
2022
* @param buffer a buffer to read the register values into
2123
*/
22-
void DS::readRegister(uint8_t addr, uint8_t len, uint8_t * buffer) {
24+
void DSI2C::readRegister(uint8_t addr, uint8_t len, uint8_t * buffer) {
2325
Wire.beginTransmission(slaveAddress);
2426
Wire.send(addr);
2527
Wire.endTransmission();
@@ -32,7 +34,7 @@ void DS::readRegister(uint8_t addr, uint8_t len, uint8_t * buffer) {
3234
* @param addr the register address to write to
3335
* @param val the value to write to the given register
3436
*/
35-
void DS::writeRegister(uint8_t addr, uint8_t val) {
37+
void DSI2C::writeRegister(uint8_t addr, uint8_t val) {
3638
Wire.beginTransmission(slaveAddress);
3739
Wire.send(addr);
3840
Wire.send(val);
@@ -45,9 +47,11 @@ void DS::writeRegister(uint8_t addr, uint8_t val) {
4547
* @param len the number of values to write
4648
* @param buffer a buffer containing the values to write
4749
*/
48-
void DS::writeRegister(uint8_t addr, uint8_t len, uint8_t * buffer) {
50+
void DSI2C::writeRegister(uint8_t addr, uint8_t len, uint8_t * buffer) {
4951
Wire.beginTransmission(slaveAddress);
5052
Wire.send(addr);
5153
while (len-- > 0) Wire.send((*buffer++));
5254
Wire.endTransmission();
5355
}
56+
57+
DSI2C RTCI2C = DSI2C();

DS.h renamed to utility/DS.h

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,20 @@
1111
#define bcd2dec(bcd) (((((bcd)>>4) & 0x0F) * 10) + ((bcd) & 0x0F))
1212
#define dec2bcd(dec) ((((dec)/10)<<4)|((dec)%10))
1313

14-
class DS {
14+
class DSI2C {
1515
public:
16-
DS() : slaveAddress(DSRTC_SLAVE_ADDRESS) { Wire.begin(); };
16+
static void begin() { Wire.begin(); };
1717

1818
// Raw register read/write methods
19-
uint8_t readRegister(uint8_t addr);
20-
void readRegister(uint8_t addr, uint8_t len, uint8_t * buffer);
21-
22-
void writeRegister(uint8_t addr, uint8_t val);
23-
void writeRegister(uint8_t addr, uint8_t len, uint8_t * buffer);
19+
static uint8_t readRegister(uint8_t addr);
20+
static void readRegister(uint8_t addr, uint8_t len, uint8_t * buffer);
21+
static void writeRegister(uint8_t addr, uint8_t val);
22+
static void writeRegister(uint8_t addr, uint8_t len, uint8_t * buffer);
2423

2524
private:
26-
uint8_t slaveAddress;
25+
static uint8_t slaveAddress;
2726
};
2827

29-
#endif // __DSRTC_H
28+
extern DSI2C RTCI2C;
29+
30+
#endif // __DS_H

DSTemp.cpp renamed to utility/DSTemp.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* @return The current temperature in Celcius.
66
*/
77
float DSTemp::getTemperature() {
8-
int8_t fint = readRegister(tempReg);
9-
uint8_t ffrac = readRegister(tempReg + 1) >> 6;
8+
int8_t fint = RTCI2C.readRegister(tempReg);
9+
uint8_t ffrac = RTCI2C.readRegister(tempReg + 1) >> 6;
1010
return (float)fint + ((float)ffrac * 0.25);
1111
}

DSTemp.h renamed to utility/DSTemp.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77
// this provides convenient conversion.
88
#define celciusToFahrenheit(c) (9.0/5.0*(c)+32.0)
99

10-
class DSTemp : public DS {
10+
class DSTemp {
1111
public:
12-
DSTemp(uint8_t tempReg=0x11) : tempReg(tempReg) { DS(); };
12+
DSTemp(uint8_t tempReg=0x11) : tempReg(tempReg) { };
1313
float getTemperature();
1414
private:
15-
uint8_t tempReg; // Clock and Calendar register location
15+
uint8_t tempReg; // Clock and Calendar register location
1616
};
1717

1818
#endif // __DSTEMP_H

0 commit comments

Comments
 (0)