8000 Merge pull request #699 from toxuin/PCF8574-i2c · schoosch/esp32-snippets@f8d6a65 · GitHub
[go: up one dir, main page]

Skip to content

Commit f8d6a65

Browse files
authored
Merge pull request nkolban#699 from toxuin/PCF8574-i2c
PCF8574 & PCF8575: i2c on heap
2 parents e4ce7da + e215f9d commit f8d6a65

File tree

4 files changed

+25
-21
lines changed

4 files changed

+25
-21
lines changed

cpp_utils/PCF8574.cpp

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,16 @@
1818
* @param [in] address The %I2C address of the device on the %I2C bus.
1919
*/
2020
PCF8574::PCF8574(uint8_t address) {
21-
i2c.setAddress(address);
21+
i2c = new I2C();
22+
i2c->setAddress(address);
2223
lastWrite = 0;
2324
}
2425

2526
/**
2627
* @brief Class instance destructor.
2728
*/
2829
PCF8574::~PCF8574() {
30+
delete i2c;
2931
}
3032

3133

@@ -35,9 +37,9 @@ PCF8574::~PCF8574() {
3537
*/
3638
uint8_t PCF8574::read() {
3739
uint8_t value;
38-
i2c.beginTransaction();
39-
i2c.read(&value,true);
40-
i2c.endTransaction();
40+
i2c->beginTransaction();
41+
i2c->read(&value,true);
42+
i2c->endTransaction();
4143
return value;
4244
} // read
4345

@@ -66,9 +68,9 @@ void PCF8574::write(uint8_t value) {
6668
if (invert) {
6769
value = ~value;
6870
}
69-
i2c.beginTransaction();
70-
i2c.write(value, true);
71-
i2c.endTransaction();
71+
i2c->beginTransaction();
72+
i2c->write(value, true);
73+
i2c->endTransaction();
7274
lastWrite = value;
7375
} // write
7476

@@ -117,5 +119,5 @@ void PCF8574::setInvert(bool value) {
117119
* @param [in] clkPin The pin to use for the %I2C CLK functions.
118120
*/
119121
void PCF8574::init(gpio_num_t sdaPin, gpio_num_t clkPin) {
120-
i2c.init(0, sdaPin, clkPin);
122+
i2c->init(0, sdaPin, clkPin);
121123
} // init

cpp_utils/PCF8574.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class PCF8574 {
2929
void writeBit(uint8_t bit, bool value);
3030

3131
private:
32-
I2C i2c = I2C();
32+
I2C* i2c;
3333
uint8_t lastWrite;
3434
bool invert = false;
3535
};

cpp_utils/PCF8575.cpp

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,16 @@
1818
* @param [in] address The %I2C address of the device on the %I2C bus.
1919
*/
2020
PCF8575::PCF8575(uint8_t address) {
21-
i2c.setAddress(address);
21+
i2c = new I2C();
22+
i2c->setAddress(address);
2223
m_lastWrite = 0;
2324
}
2425

2526
/**
2627
* @brief Class instance destructor.
2728
*/
2829
PCF8575::~PCF8575() {
30+
delete i2c;
2931
}
3032

3133

@@ -35,10 +37,10 @@ PCF8575::~PCF8575() {
3537
*/
3638
uint16_t PCF8575::read() {
3739
uint16_t value;
38-
i2c.beginTransaction();
39-
i2c.read((uint8_t*)&value,true);
40-
i2c.read(((uint8_t*)&value) + 1,true);
41-
i2c.endTransaction();
40+
i2c->beginTransaction();
41+
i2c->read((uint8_t*)&value,true);
42+
i2c->read(((uint8_t*)&value) + 1,true);
43+
i2c->endTransaction();
4244
return value;
4345
} // read
4446

@@ -67,10 +69,10 @@ void PCF8575::write(uint16_t value) {
6769
if (invert) {
6870
value = ~value;
6971
}
70-
i2c.beginTransaction();
71-
i2c.write(value & 0xff, true);
72-
i2c.write((value >> 8) & 0xff, true);
73-
i2c.endTransaction();
72+
i2c->beginTransaction();
73+
i2c->write(value & 0xff, true);
74+
i2c->write((value >> 8) & 0xff, true);
75+
i2c->endTransaction();
7476
m_lastWrite = value;
7577
} // write
7678

@@ -119,5 +121,5 @@ void PCF8575::setInvert(bool value) {
119121
* @param [in] clkPin The pin to use for the %I2C CLK functions.
120122
*/
121123
void PCF8575::init(gpio_num_t sdaPin, gpio_num_t clkPin) {
122-
i2c.init(0, sdaPin, clkPin);
124+
i2c->init(0, sdaPin, clkPin);
123125
} // init

cpp_utils/PCF8575.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ class PCF8575 {
2929
void writeBit(uint16_t bit, bool value);
3030

3131
private:
32-
I2C i2c = I2C();
33-
uint8_t m_lastWrite;
32+
I2C* i2c;
33+
uint16_t m_lastWrite;
3434
bool invert = false;
3535
};
3636

0 commit comments

Comments
 (0)
0