mirror of
https://github.com/esp8266/Arduino.git
synced 2025-04-19 23:22:16 +03:00
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
This commit is contained in:
parent
8412d11b02
commit
04dc463153
@ -51,7 +51,7 @@ uint8_t TwoWire::txBufferLength = 0;
|
|||||||
|
|
||||||
uint8_t TwoWire::transmitting = 0;
|
uint8_t TwoWire::transmitting = 0;
|
||||||
void (*TwoWire::user_onRequest)(void);
|
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_sda_pin = SDA;
|
||||||
static int default_scl_pin = SCL;
|
static int default_scl_pin = SCL;
|
||||||
@ -69,6 +69,16 @@ void TwoWire::begin(int sda, int scl){
|
|||||||
flush();
|
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){
|
void TwoWire::pins(int sda, int scl){
|
||||||
default_sda_pin = sda;
|
default_sda_pin = sda;
|
||||||
default_scl_pin = scl;
|
default_scl_pin = scl;
|
||||||
@ -255,7 +265,7 @@ void TwoWire::onRequestService(void)
|
|||||||
user_onRequest();
|
user_onRequest();
|
||||||
}
|
}
|
||||||
|
|
||||||
void TwoWire::onReceive( void (*function)(int) ) {
|
void TwoWire::onReceive( void (*function)(size_t) ) {
|
||||||
user_onReceive = function;
|
user_onReceive = function;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -45,12 +45,13 @@ class TwoWire : public Stream
|
|||||||
|
|
||||||
static uint8_t transmitting;
|
static uint8_t transmitting;
|
||||||
static void (*user_onRequest)(void);
|
static void (*user_onRequest)(void);
|
||||||
static void (*user_onReceive)(int);
|
static void (*user_onReceive)(size_t);
|
||||||
static void onRequestService(void);
|
static void onRequestService(void);
|
||||||
static void onReceiveService(uint8_t*, size_t);
|
static void onReceiveService(uint8_t*, size_t);
|
||||||
public:
|
public:
|
||||||
TwoWire();
|
TwoWire();
|
||||||
void begin(int sda, int scl);
|
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 pins(int sda, int scl) __attribute__((deprecated)); // use begin(sda, scl) in new code
|
||||||
void begin();
|
void begin();
|
||||||
void begin(uint8_t);
|
void begin(uint8_t);
|
||||||
@ -75,7 +76,7 @@ class TwoWire : public Stream
|
|||||||
virtual int read(void);
|
virtual int read(void);
|
||||||
virtual int peek(void);
|
virtual int peek(void);
|
||||||
virtual void flush(void);
|
virtual void flush(void);
|
||||||
void onReceive( void (*)(int) );
|
void onReceive( void (*)(size_t) );
|
||||||
void onRequest( void (*)(void) );
|
void onRequest( void (*)(void) );
|
||||||
|
|
||||||
inline size_t write(unsigned long n) { return write((uint8_t)n); }
|
inline size_t write(unsigned long n) { return write((uint8_t)n); }
|
||||||
|
37
libraries/Wire/examples/master_reader/master_reader.ino
Normal file
37
libraries/Wire/examples/master_reader/master_reader.ino
Normal file
@ -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
libraries/Wire/examples/master_writer/master_writer.ino
Normal file
38
libraries/Wire/examples/master_writer/master_writer.ino
Normal file
@ -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
libraries/Wire/examples/slave_receiver/slave_receiver.ino
Normal file
41
libraries/Wire/examples/slave_receiver/slave_receiver.ino
Normal file
@ -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
libraries/Wire/examples/slave_sender/slave_sender.ino
Normal file
32
libraries/Wire/examples/slave_sender/slave_sender.ino
Normal file
@ -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
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user