Strings (aka byte arrays of arbitrary length) are perfect for asynchronous and full duplex protocols like UART, essentially because those are no master-slave protocols. Here, both master and slave can transmit a byte stream independent of each other at any time and with any length.
But using strings on I2C is quite uncommon. As I2C is a master-slave bus where any communication (read and write) is initiated by the master generating the bus clock, using strings has the big disadvantage, that the master has to know the number of bytes the slave has to transmit. Of course you could implement it, but as long as you don't need the data to be human-readable, I suggest the more efficient way of transmitting bytes.
Usually, an i2c read transmission is done by
- addressing the slave in write mode
- Writing the target value's register address
- Addressing the slave in read mode
- Reading an arbitrary number of bytes (e.g. four bytes for float values)
A write transmission is a bit simpler:
- addressing the slave in write mode
- Writing the target value's register address
- Writing an arbitrary number of bytes (e.g. four bytes for float values)
This is the standard protocol that e.g. I2C sensors use and it's convenient to implement the same behaviour in the ISR when using a microcontroller (you could think of "register addresses" as "variable IDs").
To prepare values of any datatype for transmission (e.g. float to byte[4]) you can use a union
.
String
does not make sense. I'd suggest using fixed length arrays of characters without null terminators vs. null-terminated C strings however. That might make your commands easier to read. You can also use integer enum values. That would likely make for shorter byte counts, but make the data a little harder to read if you log it to the console.