From 7b7b32e5588e175085b42ae621f94e35cc10fee9 Mon Sep 17 00:00:00 2001 From: Martin Hanes Date: Sun, 27 Jan 2019 13:35:27 +0100 Subject: [PATCH 1/3] using custom wire pins, useful for esp-based boads Signed-off-by: Martin Hanes --- NDIR_I2C/NDIR_I2C.cpp | 15 ++++++++++++++- NDIR_I2C/NDIR_I2C.h | 1 + .../ReadConcentration/ReadConcentration.ino | 3 +++ 3 files changed, 18 insertions(+), 1 deletion(-) mode change 100644 => 100755 NDIR_I2C/NDIR_I2C.cpp mode change 100644 => 100755 NDIR_I2C/NDIR_I2C.h diff --git a/NDIR_I2C/NDIR_I2C.cpp b/NDIR_I2C/NDIR_I2C.cpp old mode 100644 new mode 100755 index a159e3e..e1a2b72 --- a/NDIR_I2C/NDIR_I2C.cpp +++ b/NDIR_I2C/NDIR_I2C.cpp @@ -71,6 +71,10 @@ uint8_t NDIR_I2C::cmd_calibrateZero[9] = {0xFF,0x01,0x87,0x00,0x00,0x00 uint8_t NDIR_I2C::cmd_enableAutoCalibration[9] = {0xFF,0x01,0x79,0xA0,0x00,0x00,0x00,0x00,0xE6}; uint8_t NDIR_I2C::cmd_disableAutoCalibration[9] = {0xFF,0x01,0x79,0x00,0x00,0x00,0x00,0x00,0x86}; +#define UNSET 254 +uint8_t customSda = UNSET; +uint8_t customScl = UNSET; + NDIR_I2C::NDIR_I2C(uint8_t i2c_addr) { if (i2c_addr >= 8 && i2c_addr < 120) { @@ -80,11 +84,20 @@ NDIR_I2C::NDIR_I2C(uint8_t i2c_addr) } } +uint8_t NDIR_I2C::setCustomWirePorts(uint8_t sda, uint8_t scl) { + customSda = sda; + customScl = scl; +} uint8_t NDIR_I2C::begin() { if (i2c_addr) { - WIRE.begin(); + if(customSda == UNSET || customScl == UNSET) { + WIRE.begin(); + } else { + WIRE.begin(customSda, customScl); + } + write_register(IOCONTROL, 0x08); if (write_register(FCR, 0x07)) { diff --git a/NDIR_I2C/NDIR_I2C.h b/NDIR_I2C/NDIR_I2C.h old mode 100644 new mode 100755 index fd1b73f..33d2c39 --- a/NDIR_I2C/NDIR_I2C.h +++ b/NDIR_I2C/NDIR_I2C.h @@ -30,6 +30,7 @@ class NDIR_I2C { uint32_t ppm; uint8_t begin(); + uint8_t setCustomWirePorts(uint8_t sda, uint8_t scl); uint8_t measure(); uint8_t reset(); void calibrateZero(); diff --git a/NDIR_I2C/examples/ReadConcentration/ReadConcentration.ino b/NDIR_I2C/examples/ReadConcentration/ReadConcentration.ino index a06f963..c4960fd 100644 --- a/NDIR_I2C/examples/ReadConcentration/ReadConcentration.ino +++ b/NDIR_I2C/examples/ReadConcentration/ReadConcentration.ino @@ -7,6 +7,9 @@ void setup() { Serial.begin(9600); + //if working with esp-based boards, set which pins you're using (in this case SDA is D2, SCL is D3) + //mySensor.setCustomWirePins(D2, D3); + if (mySensor.begin()) { Serial.println("Wait 10 seconds for sensor initialization..."); delay(10000); From e815c57ada1e45c18380853747789cadc8aeb293 Mon Sep 17 00:00:00 2001 From: Martin Hanes Date: Sun, 27 Jan 2019 19:57:53 +0100 Subject: [PATCH 2/3] fixed return type to void --- NDIR_I2C/NDIR_I2C.cpp | 2 +- NDIR_I2C/NDIR_I2C.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/NDIR_I2C/NDIR_I2C.cpp b/NDIR_I2C/NDIR_I2C.cpp index e1a2b72..df23e69 100755 --- a/NDIR_I2C/NDIR_I2C.cpp +++ b/NDIR_I2C/NDIR_I2C.cpp @@ -84,7 +84,7 @@ NDIR_I2C::NDIR_I2C(uint8_t i2c_addr) } } -uint8_t NDIR_I2C::setCustomWirePorts(uint8_t sda, uint8_t scl) { +void NDIR_I2C::setCustomWirePorts(uint8_t sda, uint8_t scl) { customSda = sda; customScl = scl; } diff --git a/NDIR_I2C/NDIR_I2C.h b/NDIR_I2C/NDIR_I2C.h index 33d2c39..ba7aca1 100755 --- a/NDIR_I2C/NDIR_I2C.h +++ b/NDIR_I2C/NDIR_I2C.h @@ -30,7 +30,7 @@ class NDIR_I2C { uint32_t ppm; uint8_t begin(); - uint8_t setCustomWirePorts(uint8_t sda, uint8_t scl); + void setCustomWirePorts(uint8_t sda, uint8_t scl); uint8_t measure(); uint8_t reset(); void calibrateZero(); From dddf85c2337917f79bb584fdca194dfffa932eca Mon Sep 17 00:00:00 2001 From: Martin Hanes Date: Sun, 27 Jan 2019 19:57:53 +0100 Subject: [PATCH 3/3] removed unused result variable to avoid compiler warnings --- NDIR_I2C/NDIR_I2C.cpp | 2 -- 1 file changed, 2 deletions(-) diff --git a/NDIR_I2C/NDIR_I2C.cpp b/NDIR_I2C/NDIR_I2C.cpp index a159e3e..e539208 100644 --- a/NDIR_I2C/NDIR_I2C.cpp +++ b/NDIR_I2C/NDIR_I2C.cpp @@ -258,7 +258,6 @@ uint8_t NDIR_I2C::receive(uint8_t *pbuf, uint8_t n) { uint8_t NDIR_I2C::read_register(uint8_t reg_addr, uint8_t *pval) { - uint8_t result; WIRE.beginTransmission(i2c_addr); WIRE.write(reg_addr << 3); @@ -278,7 +277,6 @@ uint8_t NDIR_I2C::read_register(uint8_t reg_addr, uint8_t *pval) uint8_t NDIR_I2C::write_register(uint8_t reg_addr, uint8_t *pdata, uint8_t n) { - uint8_t result; WIRE.beginTransmission(i2c_addr); WIRE.write(reg_addr << 3);