Skip to content

Wire Examples based on AVR ones of the same name #5713

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Feb 4, 2019
Merged
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
14 changes: 12 additions & 2 deletions libraries/Wire/Wire.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
}

Expand Down
5 changes: 3 additions & 2 deletions libraries/Wire/Wire.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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); }
Expand Down
37 changes: 37 additions & 0 deletions libraries/Wire/examples/master_reader/master_reader.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Wire Master Reader
// by devyte
// based on the example of the same name by Nicholas Zambetti <http://www.zambetti.com>

// 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 <Wire.h>
#include <PolledTimeout.h>

#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
}
}
}
38 changes: 38 additions & 0 deletions libraries/Wire/examples/master_writer/master_writer.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Wire Master Writer
// by devyte
// based on the example of the same name by Nicholas Zambetti <http://www.zambetti.com>

// 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 <Wire.h>
#include <PolledTimeout.h>

#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++;
}
}
41 changes: 41 additions & 0 deletions libraries/Wire/examples/slave_receiver/slave_receiver.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Wire Slave Receiver
// by devyte
// based on the example by Nicholas Zambetti <http://www.zambetti.com>

// 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 <Wire.h>

#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
}
32 changes: 32 additions & 0 deletions libraries/Wire/examples/slave_sender/slave_sender.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Wire Slave Sender
// by devyte
// based on the example of the same name by Nicholas Zambetti <http://www.zambetti.com>

// 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 <Wire.h>

#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
}