File tree Expand file tree Collapse file tree 6 files changed +163
-4
lines changed Expand file tree Collapse file tree 6 files changed +163
-4
lines changed Original file line number Diff line number Diff line change @@ -51,7 +51,7 @@ uint8_t TwoWire::txBufferLength = 0;
5151
5252uint8_t TwoWire::transmitting = 0 ;
5353void (*TwoWire::user_onRequest)(void );
54- void (*TwoWire::user_onReceive)(int );
54+ void (*TwoWire::user_onReceive)(size_t );
5555
5656static int default_sda_pin = SDA;
5757static int default_scl_pin = SCL;
@@ -69,6 +69,16 @@ void TwoWire::begin(int sda, int scl){
6969 flush ();
7070}
7171
72+ void TwoWire::begin (int sda, int scl, uint8_t address){
73+ default_sda_pin = sda;
74+ default_scl_pin = scl;
75+ twi_setAddress (address);
76+ twi_init (sda, scl);
77+ twi_attachSlaveTxEvent (onRequestService);
78+ twi_attachSlaveRxEvent (onReceiveService);
79+ flush ();
80+ }
81+
7282void TwoWire::pins (int sda, int scl){
7383 default_sda_pin = sda;
7484 default_scl_pin = scl;
@@ -255,7 +265,7 @@ void TwoWire::onRequestService(void)
255265 user_onRequest ();
256266}
257267
258- void TwoWire::onReceive ( void (*function)(int ) ) {
268+ void TwoWire::onReceive ( void (*function)(size_t ) ) {
259269 user_onReceive = function;
260270}
261271
Original file line number Diff line number Diff line change @@ -45,12 +45,13 @@ class TwoWire : public Stream
4545
4646 static uint8_t transmitting;
4747 static void (*user_onRequest)(void );
48- static void (*user_onReceive)(int );
48+ static void (*user_onReceive)(size_t );
4949 static void onRequestService (void );
5050 static void onReceiveService (uint8_t *, size_t );
5151 public:
5252 TwoWire ();
5353 void begin (int sda, int scl);
54+ void begin (int sda, int scl, uint8_t address);
5455 void pins (int sda, int scl) __attribute__((deprecated)); // use begin(sda, scl) in new code
5556 void begin ();
5657 void begin (uint8_t );
@@ -75,7 +76,7 @@ class TwoWire : public Stream
7576 virtual int read (void );
7677 virtual int peek (void );
7778 virtual void flush (void );
78- void onReceive ( void (*)(int ) );
79+ void onReceive ( void (*)(size_t ) );
7980 void onRequest ( void (*)(void ) );
8081
8182 inline size_t write (unsigned long n) { return write ((uint8_t )n); }
Original file line number Diff line number Diff line change 1+ // Wire Master Reader
2+ // by devyte
3+ // based on the example of the same name by Nicholas Zambetti <http://www.zambetti.com>
4+
5+ // Demonstrates use of the Wire library
6+ // Reads data from an I2C/TWI slave device
7+ // Refer to the "Wire Slave Sender" example for use with this
8+
9+ // This example code is in the public domain.
10+
11+
12+ #include < Wire.h>
13+ #include < PolledTimeout.h>
14+
15+ #define SDA_PIN 4
16+ #define SCL_PIN 5
17+ const int16_t I2C_MASTER = 0x42 ;
18+ const int16_t I2C_SLAVE = 0x08 ;
19+
20+ void setup () {
21+ Serial.begin (115200 ); // start serial for output
22+ Wire.begin (SDA_PIN, SCL_PIN, I2C_MASTER); // join i2c bus (address optional for master)
23+ }
24+
25+ void loop () {
26+ using periodic = esp8266::polledTimeout::periodic;
27+ static periodic nextPing (1000 );
28+
29+ if (nextPing) {
30+ Wire.requestFrom (I2C_SLAVE, 6 ); // request 6 bytes from slave device #8
31+
32+ while (Wire.available ()) { // slave may send less than requested
33+ char c = Wire.read (); // receive a byte as character
34+ Serial.print (c); // print the character
35+ }
36+ }
37+ }
Original file line number Diff line number Diff line change 1+ // Wire Master Writer
2+ // by devyte
3+ // based on the example of the same name by Nicholas Zambetti <http://www.zambetti.com>
4+
5+ // Demonstrates use of the Wire library
6+ // Writes data to an I2C/TWI slave device
7+ // Refer to the "Wire Slave Receiver" example for use with this
8+
9+ // This example code is in the public domain.
10+
11+
12+ #include < Wire.h>
13+ #include < PolledTimeout.h>
14+
15+ #define SDA_PIN 4
16+ #define SCL_PIN 5
17+ const int16_t I2C_MASTER = 0x42 ;
18+ const int16_t I2C_SLAVE = 0x08 ;
19+
20+ void setup () {
21+ Wire.begin (SDA_PIN, SCL_PIN, I2C_MASTER); // join i2c bus (address optional for master)
22+ }
23+
24+ byte x = 0 ;
25+
26+ void loop () {
27+ using periodic = esp8266::polledTimeout::periodic;
28+ static periodic nextPing (1000 );
29+
30+ if (nextPing) {
31+ Wire.beginTransmission (I2C_SLAVE); // transmit to device #8
32+ Wire.write (" x is " ); // sends five bytes
33+ Wire.write (x); // sends one byte
34+ Wire.endTransmission (); // stop transmitting
35+
36+ x++;
37+ }
38+ }
Original file line number Diff line number Diff line change 1+ // Wire Slave Receiver
2+ // by devyte
3+ // based on the example by Nicholas Zambetti <http://www.zambetti.com>
4+
5+ // Demonstrates use of the Wire library
6+ // Receives data as an I2C/TWI slave device
7+ // Refer to the "Wire Master Writer" example for use with this
8+
9+ // This example code is in the public domain.
10+
11+
12+ #include < Wire.h>
13+
14+ #define SDA_PIN 4
15+ #define SCL_PIN 5
16+
17+ const int16_t I2C_MASTER = 0x42 ;
18+ const int16_t I2C_SLAVE = 0x08 ;
19+
20+ void setup () {
21+ Serial.begin (115200 ); // start serial for output
22+
23+ Wire.begin (SDA_PIN, SCL_PIN, I2C_SLAVE); // new syntax: join i2c bus (address required for slave)
24+ Wire.onReceive (receiveEvent); // register event
25+ }
26+
27+ void loop () {
28+ }
29+
30+ // function that executes whenever data is received from master
31+ // this function is registered as an event, see setup()
32+ void receiveEvent (size_t howMany) {
33+
34+ (void ) howMany;
35+ while (1 < Wire.available ()) { // loop through all but the last
36+ char c = Wire.read (); // receive byte as a character
37+ Serial.print (c); // print the character
38+ }
39+ int x = Wire.read (); // receive byte as an integer
40+ Serial.println (x); // print the integer
41+ }
Original file line number Diff line number Diff line change 1+ // Wire Slave Sender
2+ // by devyte
3+ // based on the example of the same name by Nicholas Zambetti <http://www.zambetti.com>
4+
5+ // Demonstrates use of the Wire library
6+ // Sends data as an I2C/TWI slave device
7+ // Refer to the "Wire Master Reader" example for use with this
8+
9+ // This example code is in the public domain.
10+
11+
12+ #include < Wire.h>
13+
14+ #define SDA_PIN 4
15+ #define SCL_PIN 5
16+ const int16_t I2C_MASTER = 0x42 ;
17+ const int16_t I2C_SLAVE = 0x08 ;
18+
19+ void setup () {
20+ Wire.begin (SDA_PIN, SCL_PIN, I2C_SLAVE); // join i2c bus with address #8
21+ Wire.onRequest (requestEvent); // register event
22+ }
23+
24+ void loop () {
25+ }
26+
27+ // function that executes whenever data is requested by master
28+ // this function is registered as an event, see setup()
29+ void requestEvent () {
30+ Wire.write (" hello\n " ); // respond with message of 6 bytes
31+ // as expected by master
32+ }
You can’t perform that action at this time.
0 commit comments