Skip to content

Commit 84615a2

Browse files
Merge pull request #1 from collab-project/gardjuino-v4
Updates for Garduino v4
2 parents 2b39774 + be74ffb commit 84615a2

File tree

14 files changed

+99
-87
lines changed

14 files changed

+99
-87
lines changed

AT24C32_EEPROM/AT24C32_EEPROM.cpp

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,56 @@
1-
/* Copyright (c) 2021, Collab
1+
/* Copyright (c) 2021-2023, Collab
22
* All rights reserved
33
*/
44
#include "AT24C32_EEPROM.h"
55

6-
AT24C32_EEPROM::AT24C32_EEPROM(uint8_t eeprom_address) {
6+
AT24C32_EEPROM::AT24C32_EEPROM(uint8_t eeprom_address, TwoWire* wire) {
77
_eepromAddress = eeprom_address;
8+
_wire = wire;
89
}
910

1011
void AT24C32_EEPROM::write_byte(unsigned int eeaddress, byte data) {
1112
int rdata = data;
12-
Wire.beginTransmission(_eepromAddress);
13-
Wire.write((int)(eeaddress >> 8)); // MSB
14-
Wire.write((int)(eeaddress & 0xFF)); // LSB
15-
Wire.write(rdata);
16-
Wire.endTransmission();
13+
_wire->beginTransmission(_eepromAddress);
14+
_wire->write((int)(eeaddress >> 8)); // MSB
15+
_wire->write((int)(eeaddress & 0xFF)); // LSB
16+
_wire->write(rdata);
17+
_wire->endTransmission();
1718
}
1819

1920
void AT24C32_EEPROM::write_page(unsigned int eeaddresspage, byte* data, byte length) {
20-
Wire.beginTransmission(_eepromAddress);
21-
Wire.write((int)(eeaddresspage >> 8)); // MSB
22-
Wire.write((int)(eeaddresspage & 0xFF)); // LSB
21+
_wire->beginTransmission(_eepromAddress);
22+
_wire->write((int)(eeaddresspage >> 8)); // MSB
23+
_wire->write((int)(eeaddresspage & 0xFF)); // LSB
2324
byte c;
2425
for (c = 0; c < length; c++) {
25-
Wire.write(data[c]);
26+
_wire->write(data[c]);
2627
}
27-
Wire.endTransmission();
28+
_wire->endTransmission();
2829
}
2930

3031
byte AT24C32_EEPROM::read_byte(unsigned int eeaddress) {
3132
byte rdata = 0xFF;
32-
Wire.beginTransmission(_eepromAddress);
33-
Wire.write((int)(eeaddress >> 8)); // MSB
34-
Wire.write((int)(eeaddress & 0xFF)); // LSB
35-
Wire.endTransmission();
36-
Wire.requestFrom(_eepromAddress, 1);
37-
if (Wire.available()) {
38-
rdata = Wire.read();
33+
_wire->beginTransmission(_eepromAddress);
34+
_wire->write((int)(eeaddress >> 8)); // MSB
35+
_wire->write((int)(eeaddress & 0xFF)); // LSB
36+
_wire->endTransmission();
37+
_wire->requestFrom(_eepromAddress, 1);
38+
if (_wire->available()) {
39+
rdata = _wire->read();
3940
}
4041
return rdata;
4142
}
4243

4344
void AT24C32_EEPROM::read_buffer(unsigned int eeaddress, byte *buffer, int length) {
44-
Wire.beginTransmission(_eepromAddress);
45-
Wire.write((int)(eeaddress >> 8)); // MSB
46-
Wire.write((int)(eeaddress & 0xFF)); // LSB
47-
Wire.endTransmission();
48-
Wire.requestFrom(_eepromAddress, length);
45+
_wire->beginTransmission(_eepromAddress);
46+
_wire->write((int)(eeaddress >> 8)); // MSB
47+
_wire->write((int)(eeaddress & 0xFF)); // LSB
48+
_wire->endTransmission();
49+
_wire->requestFrom(_eepromAddress, length);
4950
int c = 0;
5051
for (c = 0; c < length; c++) {
51-
if (Wire.available()) buffer[c] = Wire.read();
52+
if (_wire->available()) {
53+
buffer[c] = _wire->read();
54+
}
5255
}
5356
}

AT24C32_EEPROM/AT24C32_EEPROM.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright (c) 2021, Collab
1+
/* Copyright (c) 2021-2023, Collab
22
* All rights reserved
33
*/
44
#ifndef AT24C32_EEPROM_h
@@ -10,14 +10,15 @@
1010
class AT24C32_EEPROM
1111
{
1212
public:
13-
AT24C32_EEPROM(uint8_t eeprom_address);
13+
AT24C32_EEPROM(uint8_t eeprom_address, TwoWire* wire);
1414
byte read_byte(unsigned int eeaddress);
1515
void write_byte(unsigned int eeaddress, byte data);
1616
void write_page(unsigned int eeaddresspage, byte* data, byte length);
1717
void read_buffer(unsigned int eeaddress, byte *buffer, int length);
1818

1919
private:
2020
uint8_t _eepromAddress;
21+
TwoWire* _wire;
2122
};
2223

2324
#endif

DS3231_RealtimeClock/DS3231_RealtimeClock.cpp

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,24 @@
1-
/* Copyright (c) 2020-2021, Collab
1+
/* Copyright (c) 2020-2023, Collab
22
* All rights reserved
33
*/
44
/*
55
DS3231_RealtimeClock.cpp - Read and write to a DS3231 realtime clock.
66
*/
77
#include "DS3231_RealtimeClock.h"
88

9-
DS3231_RealtimeClock::DS3231_RealtimeClock(int scl_pin, int sda_pin, uint8_t eeprom_address) {
10-
_sclPin = scl_pin;
11-
_sdaPin = sda_pin;
9+
DS3231_RealtimeClock::DS3231_RealtimeClock(TwoWire* wire, uint8_t eeprom_address) {
10+
_wire = wire;
1211

1312
// storage
14-
_storage = new AT24C32_EEPROM(eeprom_address);
13+
_storage = new AT24C32_EEPROM(eeprom_address, wire);
1514

1615
// rtc
1716
_rtc = new RTC_DS3231();
1817
}
1918

20-
void DS3231_RealtimeClock::begin(bool callWireBegin) {
21-
// rtc
22-
if (callWireBegin) {
23-
Wire.begin(_sdaPin, _sclPin);
24-
}
25-
Wire.beginTransmission(DS3231_ADDRESS);
26-
if (Wire.endTransmission() != 0) {
19+
void DS3231_RealtimeClock::begin() {
20+
// initializing the rtc
21+
if (!_rtc->begin(_wire)) {
2722
Log.warning(F("Initializing DS3231... Error!" CR));
2823
}
2924

DS3231_RealtimeClock/DS3231_RealtimeClock.h

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright (c) 2020-2021, Collab
1+
/* Copyright (c) 2020-2023, Collab
22
* All rights reserved
33
*/
44
/*
@@ -18,8 +18,8 @@
1818
class DS3231_RealtimeClock
1919
{
2020
public:
21-
DS3231_RealtimeClock(int scl_pin = -1, int sda_pin = -1, uint8_t eeprom_address = 0x57);
22-
void begin(bool callWireBegin = true);
21+
DS3231_RealtimeClock(TwoWire* wire, uint8_t eeprom_address = 0x57);
22+
void begin();
2323
DateTime now();
2424
DateTime load(int address = 0);
2525
void save(DateTime timestamp, int address = 0);
@@ -34,10 +34,9 @@ class DS3231_RealtimeClock
3434
float startupTemperature;
3535

3636
private:
37+
TwoWire *_wire;
3738
RTC_DS3231 *_rtc;
3839
AT24C32_EEPROM *_storage;
39-
int _sclPin;
40-
int _sdaPin;
4140
};
4241

4342
#endif

LED_PCF8574/LED_PCF8574.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
LED_PCF8574.h - Control LED light using PCF8574 multiplexer.
66
*/
77

8-
#include "LED_PCF8574.h"
8+
#include <LED_PCF8574.h>
99

1010
LED_PCF8574::LED_PCF8574(
1111
int pin,

LED_PCF8574/LED_PCF8574.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright (c) 2020, Collab
1+
/* Copyright (c) 2020-2023, Collab
22
* All rights reserved
33
*/
44
/*
@@ -7,7 +7,7 @@
77
#ifndef LED_PCF8574_h
88
#define LED_PCF8574_h
99

10-
#include "Arduino.h"
10+
#include <Arduino.h>
1111
#include <MultiPlexer_PCF8574.h>
1212

1313
class LED_PCF8574

MultiPlexer_PCF8574/MultiPlexer_PCF8574.cpp

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,22 @@
1-
/* Copyright (c) 2020-2021, Collab
1+
/* Copyright (c) 2020-2023, Collab
22
* All rights reserved
33
*/
44

55
#include <MultiPlexer_PCF8574.h>
66

7-
MultiPlexer_PCF8574::MultiPlexer_PCF8574(uint8_t address, uint8_t sda, uint8_t scl) {
8-
_sdaPin = sda;
9-
_sclPin = scl;
10-
_expander = new PCF8574(address);
7+
MultiPlexer_PCF8574::MultiPlexer_PCF8574(uint8_t address, TwoWire* wire) {
8+
_expander = new PCF8574(address, wire);
119
}
1210

1311
bool MultiPlexer_PCF8574::begin() {
14-
return _expander->begin(_sdaPin, _sclPin);
12+
bool expanderReady = _expander->begin();
13+
if (!expanderReady) {
14+
Log.warning(F("PCF8574 could not initialize!" CR));
15+
}
16+
if (!isConnected()) {
17+
Log.warning(F("PCF8574 not connected!" CR));
18+
}
19+
return expanderReady;
1520
}
1621

1722
bool MultiPlexer_PCF8574::isConnected() {
Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright (c) 2020-2021, Collab
1+
/* Copyright (c) 2020-2023, Collab
22
* All rights reserved
33
*/
44
/*
@@ -8,22 +8,20 @@
88
#define MultiPlexer_PCF8574_h
99

1010
#include <Arduino.h>
11-
#include <Method.h>
1211
#include <PCF8574.h>
12+
#include <ArduinoLog.h>
1313

1414
class MultiPlexer_PCF8574
1515
{
1616
public:
17-
MultiPlexer_PCF8574(uint8_t address, uint8_t sda, uint8_t scl);
17+
MultiPlexer_PCF8574(uint8_t address, TwoWire* wire = &Wire);
1818
bool begin();
1919
bool isConnected();
2020
void digitalWrite(uint8_t pin, uint8_t value);
2121
uint8_t digitalRead(uint8_t pin);
2222

2323
private:
2424
PCF8574* _expander;
25-
uint8_t _sdaPin;
26-
uint8_t _sclPin;
2725
};
2826

2927
#endif

MultiPlexer_TCA9548A/MultiPlexer_TCA9548A.cpp

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright (c) 2021, Collab
1+
/* Copyright (c) 2021-2023, Collab
22
* All rights reserved
33
*/
44
/*
@@ -8,10 +8,12 @@
88
#include <MultiPlexer_TCA9548A.h>
99

1010
MultiPlexer_TCA9548A::MultiPlexer_TCA9548A(int i2c_addr) {
11+
_address = i2c_addr;
1112
_mux = new TCA9548A(i2c_addr);
1213
}
1314

1415
void MultiPlexer_TCA9548A::begin() {
16+
// XXX: don't hardcode Wire instance
1517
_mux->begin(Wire1);
1618

1719
// set a base state which we know (also the default state on power on)
@@ -35,32 +37,40 @@ void MultiPlexer_TCA9548A::closeAll() {
3537
_mux->closeAll();
3638
}
3739

38-
void MultiPlexer_TCA9548A::scan() {
40+
void MultiPlexer_TCA9548A::scan(bool ignoreMultiplexer) {
3941
byte error, address;
40-
int nDevices;
41-
int delayTime = 5000;
42-
nDevices = 0;
42+
int nDevices = 0;
43+
4344
for (address = 1; address < 127; address++) {
4445
Wire1.beginTransmission(address);
4546
error = Wire1.endTransmission();
4647
if (error == 0) {
47-
Serial.print(F("I2C device found at address 0x"));
48-
if (address < 16) {
49-
Serial.print(F("0"));
48+
if (!(ignoreMultiplexer && address == _address)) {
49+
if (address < 16) {
50+
Log.info(F("TCA9548A - I2C device found at address 0x0%x" CR), address);
51+
} else {
52+
Log.info(F("TCA9548A - I2C device found at address 0x%x" CR), address);
53+
}
54+
nDevices++;
5055
}
51-
Serial.println(address, HEX);
52-
nDevices++;
53-
}
54-
else if (error == 4) {
55-
Serial.print(F("Unknow error at address 0x"));
56+
} else if (error == 4) {
5657
if (address < 16) {
57-
Serial.print(F("0"));
58+
Log.info(F("TCA9548A - Unknown error at address 0x0%x" CR), address);
59+
} else {
60+
Log.info(F("TCA9548A - Unknown error at address 0x%x" CR), address);
5861
}
59-
Serial.println(address, HEX);
6062
}
6163
}
64+
6265
if (nDevices == 0) {
63-
Serial.println(F("No I2C devices found\n"));
66+
Log.warning(F("TCA9548A - No I2C devices found" CR));
67+
}
68+
}
69+
70+
void MultiPlexer_TCA9548A::scanAll() {
71+
for (int channel = 0; channel < 8; channel++) {
72+
Log.info(F("TCA9548A - Scanning channel %d..." CR), channel);
73+
openChannel(channel);
74+
scan();
6475
}
65-
delay(delayTime);
6676
}

MultiPlexer_TCA9548A/MultiPlexer_TCA9548A.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright (c) 2021, Collab
1+
/* Copyright (c) 2021-2023, Collab
22
* All rights reserved
33
*/
44
/*
@@ -10,20 +10,23 @@
1010
#include <Arduino.h>
1111
#include <Wire.h>
1212
#include <TCA9548A.h>
13+
#include <ArduinoLog.h>
1314

1415
class MultiPlexer_TCA9548A
1516
{
1617
public:
1718
MultiPlexer_TCA9548A(int i2c_addr = 0x70);
1819
void begin();
19-
void scan();
20+
void scan(bool ignoreMultiplexer = true);
21+
void scanAll();
2022
void closeAll();
2123
void openChannel(uint8_t channel_nr);
2224
void closeChannel(uint8_t channel_nr);
2325
void switchChannel(uint8_t channel_nr);
2426

2527
private:
2628
TCA9548A* _mux;
29+
int _address;
2730
};
2831

2932
#endif

0 commit comments

Comments
 (0)