mirror of
https://github.com/esp8266/Arduino.git
synced 2025-07-27 18:02:17 +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:
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
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user