File tree 6 files changed +163
-4
lines changed
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;
51
51
52
52
uint8_t TwoWire::transmitting = 0 ;
53
53
void (*TwoWire::user_onRequest)(void );
54
- void (*TwoWire::user_onReceive)(int );
54
+ void (*TwoWire::user_onReceive)(size_t );
55
55
56
56
static int default_sda_pin = SDA;
57
57
static int default_scl_pin = SCL;
@@ -69,6 +69,16 @@ void TwoWire::begin(int sda, int scl){
69
69
flush ();
70
70
}
71
71
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
+
72
82
void TwoWire::pins (int sda, int scl){
73
83
default_sda_pin = sda;
74
84
default_scl_pin = scl;
@@ -255,7 +265,7 @@ void TwoWire::onRequestService(void)
255
265
user_onRequest ();
256
266
}
257
267
258
- void TwoWire::onReceive ( void (*function)(int ) ) {
268
+ void TwoWire::onReceive ( void (*function)(size_t ) ) {
259
269
user_onReceive = function;
260
270
}
261
271
Original file line number Diff line number Diff line change @@ -45,12 +45,13 @@ class TwoWire : public Stream
45
45
46
46
static uint8_t transmitting;
47
47
static void (*user_onRequest)(void );
48
- static void (*user_onReceive)(int );
48
+ static void (*user_onReceive)(size_t );
49
49
static void onRequestService (void );
50
50
static void onReceiveService (uint8_t *, size_t );
51
51
public:
52
52
TwoWire ();
53
53
void begin (int sda, int scl);
54
+ void begin (int sda, int scl, uint8_t address);
54
55
void pins (int sda, int scl) __attribute__((deprecated)); // use begin(sda, scl) in new code
55
56
void begin ();
56
57
void begin (uint8_t );
@@ -75,7 +76,7 @@ class TwoWire : public Stream
75
76
virtual int read (void );
76
77
virtual int peek (void );
77
78
virtual void flush (void );
78
- void onReceive ( void (*)(int ) );
79
+ void onReceive ( void (*)(size_t ) );
79
80
void onRequest ( void (*)(void ) );
80
81
81
82
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