From 04dc463153e3c92ab9bc6583c09f52d0e983caca Mon Sep 17 00:00:00 2001 From: Develo Date: Mon, 4 Feb 2019 14:47:29 -0300 Subject: [PATCH] Wire Examples based on AVR ones of the same name (#5713) * Wire Examples based on AVR ones of the same name * Overload for begin(), change in callback arg from int to size_t * Update master_reader.ino Astyle * Update master_writer.ino Astyle * Update slave_receiver.ino Remove warning * Update slave_receiver.ino Astyle --- libraries/Wire/Wire.cpp | 14 ++++++- libraries/Wire/Wire.h | 5 ++- .../examples/master_reader/master_reader.ino | 37 +++++++++++++++++ .../examples/master_writer/master_writer.ino | 38 +++++++++++++++++ .../slave_receiver/slave_receiver.ino | 41 +++++++++++++++++++ .../examples/slave_sender/slave_sender.ino | 32 +++++++++++++++ 6 files changed, 163 insertions(+), 4 deletions(-) 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/Wire.cpp b/libraries/Wire/Wire.cpp index 7e8689499..e3ce0d7b6 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 4687a8599..79d47ec14 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); } 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 000000000..fb1316c1d --- /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 000000000..d2a6da842 --- /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 000000000..270cc4312 --- /dev/null +++ b/libraries/Wire/examples/slave_receiver/slave_receiver.ino @@ -0,0 +1,41 @@ +// 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) { + + (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 + } + 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 000000000..e177be853 --- /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 +}