Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 21 additions & 9 deletions src/I2C_Slave.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,35 @@
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() {}

// 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) {
_addr = 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; }

Expand All @@ -66,27 +78,27 @@ 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) {
if (size < 2) return; // let the request handler process this

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);
}

Expand Down
1 change: 1 addition & 0 deletions src/I2C_Slave.h
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down