From c07687e47b37c90b0ed65bc1cbd5537498ac3748 Mon Sep 17 00:00:00 2001 From: devyte Date: Mon, 4 Feb 2019 02:14:55 -0300 Subject: [PATCH 1/6] Wire Examples based on AVR ones of the same name --- .../examples/master_reader/master_reader.ino | 37 ++++++++++++++++++ .../examples/master_writer/master_writer.ino | 38 ++++++++++++++++++ .../slave_receiver/slave_receiver.ino | 39 +++++++++++++++++++ .../examples/slave_sender/slave_sender.ino | 32 +++++++++++++++ 4 files changed, 146 insertions(+) create mode 100644 libraries/Wire/examples/master_reader/master_reader.ino create mode 100644 libraries/Wire/examples/master_writer/master_writer.ino create mode 100644 libraries/Wire/examples/slave_receiver/slave_receiver.ino create mode 100644 libraries/Wire/examples/slave_sender/slave_sender.ino diff --git a/libraries/Wire/examples/master_reader/master_reader.ino b/libraries/Wire/examples/master_reader/master_reader.ino new file mode 100644 index 0000000000..1a52d6959d --- /dev/null +++ b/libraries/Wire/examples/master_reader/master_reader.ino @@ -0,0 +1,37 @@ +// Wire Master Reader +// by devyte +// based on the example of the same name by Nicholas Zambetti + +// Demonstrates use of the Wire library +// Reads data from an I2C/TWI slave device +// Refer to the "Wire Slave Sender" example for use with this + +// This example code is in the public domain. + + +#include +#include + +#define SDA_PIN 4 +#define SCL_PIN 5 +const int16_t I2C_MASTER = 0x42; +const int16_t I2C_SLAVE = 0x08; + +void setup() { + Serial.begin(115200); // start serial for output + Wire.begin(SDA_PIN, SCL_PIN, I2C_MASTER); // join i2c bus (address optional for master) +} + +void loop() { + using periodic = esp8266::polledTimeout::periodic; + static periodic nextPing(1000); + + if(nextPing) { + Wire.requestFrom(I2C_SLAVE, 6); // request 6 bytes from slave device #8 + + while (Wire.available()) { // slave may send less than requested + char c = Wire.read(); // receive a byte as character + Serial.print(c); // print the character + } + } +} diff --git a/libraries/Wire/examples/master_writer/master_writer.ino b/libraries/Wire/examples/master_writer/master_writer.ino new file mode 100644 index 0000000000..8498737b3f --- /dev/null +++ b/libraries/Wire/examples/master_writer/master_writer.ino @@ -0,0 +1,38 @@ +// Wire Master Writer +// by devyte +// based on the example of the same name by Nicholas Zambetti + +// Demonstrates use of the Wire library +// Writes data to an I2C/TWI slave device +// Refer to the "Wire Slave Receiver" example for use with this + +// This example code is in the public domain. + + +#include +#include + +#define SDA_PIN 4 +#define SCL_PIN 5 +const int16_t I2C_MASTER = 0x42; +const int16_t I2C_SLAVE = 0x08; + +void setup() { + Wire.begin(SDA_PIN, SCL_PIN, I2C_MASTER); // join i2c bus (address optional for master) +} + +byte x = 0; + +void loop() { + using periodic = esp8266::polledTimeout::periodic; + static periodic nextPing(1000); + + if(nextPing) { + Wire.beginTransmission(I2C_SLAVE); // transmit to device #8 + Wire.write("x is "); // sends five bytes + Wire.write(x); // sends one byte + Wire.endTransmission(); // stop transmitting + + x++; + } +} diff --git a/libraries/Wire/examples/slave_receiver/slave_receiver.ino b/libraries/Wire/examples/slave_receiver/slave_receiver.ino new file mode 100644 index 0000000000..627532f713 --- /dev/null +++ b/libraries/Wire/examples/slave_receiver/slave_receiver.ino @@ -0,0 +1,39 @@ +// Wire Slave Receiver +// by devyte +// based on the example by Nicholas Zambetti + +// Demonstrates use of the Wire library +// Receives data as an I2C/TWI slave device +// Refer to the "Wire Master Writer" example for use with this + +// This example code is in the public domain. + + +#include + +#define SDA_PIN 4 +#define SCL_PIN 5 + +const int16_t I2C_MASTER = 0x42; +const int16_t I2C_SLAVE = 0x08; + +void setup() { + Serial.begin(115200); // start serial for output + + Wire.begin(SDA_PIN, SCL_PIN, I2C_SLAVE); // new syntax: join i2c bus (address required for slave) + Wire.onReceive(receiveEvent); // register event +} + +void loop() { +} + +// function that executes whenever data is received from master +// this function is registered as an event, see setup() +void receiveEvent(size_t howMany) { + while (1 < Wire.available()) { // loop through all but the last + char c = Wire.read(); // receive byte as a character + Serial.print(c); // print the character + } + int x = Wire.read(); // receive byte as an integer + Serial.println(x); // print the integer +} diff --git a/libraries/Wire/examples/slave_sender/slave_sender.ino b/libraries/Wire/examples/slave_sender/slave_sender.ino new file mode 100644 index 0000000000..e177be8538 --- /dev/null +++ b/libraries/Wire/examples/slave_sender/slave_sender.ino @@ -0,0 +1,32 @@ +// Wire Slave Sender +// by devyte +// based on the example of the same name by Nicholas Zambetti + +// Demonstrates use of the Wire library +// Sends data as an I2C/TWI slave device +// Refer to the "Wire Master Reader" example for use with this + +// This example code is in the public domain. + + +#include + +#define SDA_PIN 4 +#define SCL_PIN 5 +const int16_t I2C_MASTER = 0x42; +const int16_t I2C_SLAVE = 0x08; + +void setup() { + Wire.begin(SDA_PIN, SCL_PIN, I2C_SLAVE); // join i2c bus with address #8 + Wire.onRequest(requestEvent); // register event +} + +void loop() { +} + +// function that executes whenever data is requested by master +// this function is registered as an event, see setup() +void requestEvent() { + Wire.write("hello\n"); // respond with message of 6 bytes + // as expected by master +} From 0ef5e9717f9392555a4b76a973edf3e19c14176a Mon Sep 17 00:00:00 2001 From: devyte Date: Mon, 4 Feb 2019 02:53:14 -0300 Subject: [PATCH 2/6] Overload for begin(), change in callback arg from int to size_t --- libraries/Wire/Wire.cpp | 14 ++++++++++++-- libraries/Wire/Wire.h | 5 +++-- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/libraries/Wire/Wire.cpp b/libraries/Wire/Wire.cpp index 7e86894991..e3ce0d7b60 100644 --- a/libraries/Wire/Wire.cpp +++ b/libraries/Wire/Wire.cpp @@ -51,7 +51,7 @@ uint8_t TwoWire::txBufferLength = 0; uint8_t TwoWire::transmitting = 0; void (*TwoWire::user_onRequest)(void); -void (*TwoWire::user_onReceive)(int); +void (*TwoWire::user_onReceive)(size_t); static int default_sda_pin = SDA; static int default_scl_pin = SCL; @@ -69,6 +69,16 @@ void TwoWire::begin(int sda, int scl){ flush(); } +void TwoWire::begin(int sda, int scl, uint8_t address){ + default_sda_pin = sda; + default_scl_pin = scl; + twi_setAddress(address); + twi_init(sda, scl); + twi_attachSlaveTxEvent(onRequestService); + twi_attachSlaveRxEvent(onReceiveService); + flush(); +} + void TwoWire::pins(int sda, int scl){ default_sda_pin = sda; default_scl_pin = scl; @@ -255,7 +265,7 @@ void TwoWire::onRequestService(void) user_onRequest(); } -void TwoWire::onReceive( void (*function)(int) ) { +void TwoWire::onReceive( void (*function)(size_t) ) { user_onReceive = function; } diff --git a/libraries/Wire/Wire.h b/libraries/Wire/Wire.h index 4687a8599f..79d47ec14b 100644 --- a/libraries/Wire/Wire.h +++ b/libraries/Wire/Wire.h @@ -45,12 +45,13 @@ class TwoWire : public Stream static uint8_t transmitting; static void (*user_onRequest)(void); - static void (*user_onReceive)(int); + static void (*user_onReceive)(size_t); static void onRequestService(void); static void onReceiveService(uint8_t*, size_t); public: TwoWire(); void begin(int sda, int scl); + void begin(int sda, int scl, uint8_t address); void pins(int sda, int scl) __attribute__((deprecated)); // use begin(sda, scl) in new code void begin(); void begin(uint8_t); @@ -75,7 +76,7 @@ class TwoWire : public Stream virtual int read(void); virtual int peek(void); virtual void flush(void); - void onReceive( void (*)(int) ); + void onReceive( void (*)(size_t) ); void onRequest( void (*)(void) ); inline size_t write(unsigned long n) { return write((uint8_t)n); } From 625f7d5e058ffbfded184a906dfe1e93aa5d2081 Mon Sep 17 00:00:00 2001 From: Develo Date: Mon, 4 Feb 2019 04:46:49 -0300 Subject: [PATCH 3/6] Update master_reader.ino Astyle --- libraries/Wire/examples/master_reader/master_reader.ino | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/Wire/examples/master_reader/master_reader.ino b/libraries/Wire/examples/master_reader/master_reader.ino index 1a52d6959d..fb1316c1d3 100644 --- a/libraries/Wire/examples/master_reader/master_reader.ino +++ b/libraries/Wire/examples/master_reader/master_reader.ino @@ -26,7 +26,7 @@ void loop() { using periodic = esp8266::polledTimeout::periodic; static periodic nextPing(1000); - if(nextPing) { + if (nextPing) { Wire.requestFrom(I2C_SLAVE, 6); // request 6 bytes from slave device #8 while (Wire.available()) { // slave may send less than requested From b6d73153747577a97540f1a7d09add8b73b3bec2 Mon Sep 17 00:00:00 2001 From: Develo Date: Mon, 4 Feb 2019 04:47:22 -0300 Subject: [PATCH 4/6] Update master_writer.ino Astyle --- libraries/Wire/examples/master_writer/master_writer.ino | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/Wire/examples/master_writer/master_writer.ino b/libraries/Wire/examples/master_writer/master_writer.ino index 8498737b3f..d2a6da8420 100644 --- a/libraries/Wire/examples/master_writer/master_writer.ino +++ b/libraries/Wire/examples/master_writer/master_writer.ino @@ -27,7 +27,7 @@ void loop() { using periodic = esp8266::polledTimeout::periodic; static periodic nextPing(1000); - if(nextPing) { + if (nextPing) { Wire.beginTransmission(I2C_SLAVE); // transmit to device #8 Wire.write("x is "); // sends five bytes Wire.write(x); // sends one byte From 5019ac5036a3f5af0036b929064a949017630da6 Mon Sep 17 00:00:00 2001 From: Develo Date: Mon, 4 Feb 2019 04:48:38 -0300 Subject: [PATCH 5/6] Update slave_receiver.ino Remove warning --- libraries/Wire/examples/slave_receiver/slave_receiver.ino | 2 ++ 1 file changed, 2 insertions(+) diff --git a/libraries/Wire/examples/slave_receiver/slave_receiver.ino b/libraries/Wire/examples/slave_receiver/slave_receiver.ino index 627532f713..270b5468ef 100644 --- a/libraries/Wire/examples/slave_receiver/slave_receiver.ino +++ b/libraries/Wire/examples/slave_receiver/slave_receiver.ino @@ -30,6 +30,8 @@ void loop() { // function that executes whenever data is received from master // this function is registered as an event, see setup() void receiveEvent(size_t howMany) { + (void) howMany; + while (1 < Wire.available()) { // loop through all but the last char c = Wire.read(); // receive byte as a character Serial.print(c); // print the character From 26e9cd67915891947cf40690a8817f2c7bed4f6a Mon Sep 17 00:00:00 2001 From: Develo Date: Mon, 4 Feb 2019 13:22:04 -0300 Subject: [PATCH 6/6] Update slave_receiver.ino Astyle --- libraries/Wire/examples/slave_receiver/slave_receiver.ino | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/Wire/examples/slave_receiver/slave_receiver.ino b/libraries/Wire/examples/slave_receiver/slave_receiver.ino index 270b5468ef..270cc43129 100644 --- a/libraries/Wire/examples/slave_receiver/slave_receiver.ino +++ b/libraries/Wire/examples/slave_receiver/slave_receiver.ino @@ -30,8 +30,8 @@ void loop() { // function that executes whenever data is received from master // this function is registered as an event, see setup() void receiveEvent(size_t howMany) { + (void) howMany; - while (1 < Wire.available()) { // loop through all but the last char c = Wire.read(); // receive byte as a character Serial.print(c); // print the character