From c28fd391a4631ea8d63b6e1134f9156672af9f6e Mon Sep 17 00:00:00 2001 From: eye <34344989+naveen-nayan@users.noreply.github.com> Date: Thu, 6 Feb 2025 15:11:27 +0530 Subject: [PATCH 1/2] Update I2C_Slave.cpp - Added pin assignment for I2C if alternate pins are used. - Added new TwoWire object to overcome conflict with default object. --- src/I2C_Slave.cpp | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/src/I2C_Slave.cpp b/src/I2C_Slave.cpp index f7c79f5..5e3d66d 100644 --- a/src/I2C_Slave.cpp +++ b/src/I2C_Slave.cpp @@ -23,6 +23,10 @@ volatile uint32_t _numErrors = 0; void (*I2C_Slave::_user_onCommand)(uint8_t, uint8_t){}; +// create a new wire object +TwoWire new_Wire = TwoWire(); + + // Constructors //////////////////////////////////////////////////////////////// I2C_Slave::I2C_Slave() {} @@ -30,9 +34,9 @@ I2C_Slave::I2C_Slave() {} // Public Methods ////////////////////////////////////////////////////////////// void I2C_Slave::begin() { - Wire.begin(_addr); - Wire.onRequest(_onRequest); - Wire.onReceive(_onReceive); + new_Wire.begin(_addr); + new_Wire.onRequest(_onRequest); + new_Wire.onReceive(_onReceive); } void I2C_Slave::begin(uint8_t addr) { @@ -40,6 +44,14 @@ void I2C_Slave::begin(uint8_t addr) { this->begin(); } +// assign the pin if alternate I2C is used esle assign default pin +void I2C_Slave::begin(uint8_t addr, uint32_t sda = SDA, uint32_t scl = SCL) { + new_Wire.setSDA(sda); + new_Wire.setSCL(scl); + _addr = addr; + this->begin(); +} + // return the number of I2C comm errors encountered uint32_t I2C_Slave::numErrors() { return _numErrors; } @@ -66,11 +78,11 @@ size_t I2C_Slave::writeRegisters(char* buf) { void I2C_Slave::_onRequest() { uint8_t idx = 0; - if (Wire.available()) { - idx = Wire.read(); + if (new_Wire.available()) { + idx = new_Wire.read(); if (idx > BUFFER_LENGTH - 1) idx = 0; } - Wire.write(_registers[idx]); + new_Wire.write(_registers[idx]); } void I2C_Slave::_onReceive(int size) { @@ -78,15 +90,15 @@ void I2C_Slave::_onReceive(int size) { if (size > 2) { // assume an error on more than 2 bytes - flush read buffer - while (Wire.available()) Wire.read(); + while (new_Wire.available()) new_Wire.read(); _numErrors++; return; } // otherwise, two bytes indicate a command, pass to user function uint8_t b0, b1; - b0 = Wire.read(); - b1 = Wire.read(); + b0 = new_Wire.read(); + b1 = new_Wire.read(); _user_onCommand(b0, b1); } From 9f8701d3b73b984051fa92894caa886675a0e4db Mon Sep 17 00:00:00 2001 From: eye <34344989+naveen-nayan@users.noreply.github.com> Date: Thu, 6 Feb 2025 15:14:33 +0530 Subject: [PATCH 2/2] Update I2C_Slave.h - added function declarations. --- src/I2C_Slave.h | 1 + 1 file changed, 1 insertion(+) diff --git a/src/I2C_Slave.h b/src/I2C_Slave.h index 4b6f902..0688f30 100644 --- a/src/I2C_Slave.h +++ b/src/I2C_Slave.h @@ -45,6 +45,7 @@ class I2C_Slave { I2C_Slave(); void begin(); void begin(uint8_t); + void begin(uint8_t addr, uint32_t sda, uint32_t scl); uint32_t numErrors(); size_t numRegisters(); void onCommand(void (*)(uint8_t, uint8_t));