Ok, I'll admit to being in a bit over my head with the i2c stuff, and the wiring (and arduino) didn't give me much insight into my problem.
The problem is that I wanted to facilitate bi-directional communication between two arduinos using i2c (Wire.h).
That is, arduino 1 (a1, master) sends a command to arduino 2 (a2, slave @ address 1) and a2 responds with the results of that command. In my test case, command '1' should result in '5' and command '2' should result in '6'.
It took some serious experimenting, as I found that (undocumented, afaict) if I call Wire.requestFrom( 1, 1 ) - it doesn't trigger the callback for onRequest in the slave unless I first Wire.beginTransmission(1). (As an aside, what's the point of requesting a number of bytes if this isn't communicated to the slave? The callback for onReceive() gets a byte count, but the callback for onRequest() gets no argument?)
So, here's what I found that I could do:
Master:
1: Wire.beginTransmission(slave)
2: Wire.send(byte)
3: Wire.send(byte) -- I send two bytes
4: Wire.requestFrom(slave, 1) -- want one byte back
5: while( Wire.available ) { ... = Wire.receive() }
6: Wire.endTransmission()
Slave:
onReceive handler:
1: while( Wire.available() ) { ... = Wire.receive() }
2: set 'command' value
onRequest handler:
1: check command value
2: Wire.send() appropriate response
The real kicker here, is that onRequest is always triggered on the slave before onReceive(), and the response is received on the master after send()! That means my answer is always to the previous question, I have to throw away the first couple of answers, AND I never get a response to my last question.
How do I make it so that the onRequest() handler isn't called before the onReceive() handler?
Now, obviously, I've tried to Wire.send() from the slave w/o sending a Wire.requestFrom() from the master, but that just results in the master reading back the last value it sent to the slave. (Remove step 4 from the master steps above, that is)
Here's the serial output from the master (code in response below):
Ready...
Sending Values 1, 1
Sent Command:
1:1:1
Got Response:
0
Sending Values 2, 2
Sent Command:
1:2:2
Got Response:
5
Sending Values 1, 1
Sent Command:
1:1:1
Got Response:
6
Sending Values 2, 2
Sent Command:
1:2:2
Got Response:
5
And the slave's output:
Ready...
Entering receive_request
Got a request without a known command.
Entering receive_data
Got count of bytes to receive:
2
Got a command byte:
1
Got value:
1
Entering receive_request
Responding to command 1: 5
Entering receive_data
Got count of bytes to receive:
2
Got a command byte:
2
Got value:
2
Entering receive_request
Responding to command 2: 6
Entering receive_data
Got count of bytes to receive:
2
Got a command byte:
1
Got value:
1
Entering receive_request
Responding to command 1: 5
Entering receive_data
Got count of bytes to receive:
2
Got a command byte:
2
Got value:
2
Note in the slave's output, there isn't a receive_request following the final receive_data.
TIA,
!c