Hello everybody!
In order to avoid any unnecessary detour on the topic, I'm going straight to the point: the idea is to create a program that converts any of the most popular AVR Arduinos into a simple I/O expander (and keeping fuctions like analogRead and PWM output aka analogWrite) by using I2C as the controller interface.
I know (more or less) how I2C works and the way the master reads and writes to a slave; but I don't have clear how the Wire library handles the slave's events:
If the master, in order to request (read) data from a slave, has to specify the amount of bytes it expects to receive; then how the slave knows that if the onRequest event doesn't even have any parameter that tells it? What happens if the slave doesn't transmit the exact amount of bytes the master requests?
I make this question because I want to implement a muti-byte feature that sends the state of consecutive pins if the master requests multiple bytes on a digital/analog read. How the slave can know when to stop sending bytes? The event triggers once per request or once per byte?
Why onReceive does have a parameter and onRequest does not? It would be more useful if the slave acknowledges how many bytes the master requested, instead of how many the slave received.
I don't know what's really the deal, but I think it is redundant considering that available() does the same thing. Then... can I use the parameter's value instead of available() to determine how many bytes the slave received?
I'll appreciate any answer, since this doubt has me stuck