Can someone explain why the I2c.write takes a string for the data?
seems strange to take “string” values.
such as if I wanted to write 3 bytes:
_i2c.write(_addr,"\x02" + “\x00” + “\x00”);
Yet using spi.write I dont need to do that. These “writes” should follow the same format.
And since it does take string values then why doesn’t this then work;
local value = blob(3);
value.writen("\x02",‘b’); // also tried the ‘c’ parm and still no go.
value.writen("\x00",‘b’);
value.writen("\x00",‘b’);
_i2c.write(_addr,value);
I mean it does for the spi write, why not then for the I2c ?
Lastly, how does one take in a value from say a sensor, then pass this value to a I2C device?
The data is going to be raw, and then I need to convert it to a string so I can pass it along using i2c. write.
You’re right, i2c.write is a bit inconsistent. In most other places, blobs are coerced to strings when required.
As for your bit of code, you’ve found another bug really in that the first parameter of blob.writen should be an integer (or float), not a string, but no error gets reported. Here’s a version which should hopefully have those issues fixed:
`local value = blob(3);
value.writen(0x02, 'b'); // note no quotes around the 2
value.writen(0x00, 'b');
value.writen(0x00, 'b');
_i2c.write(_addr, value.tostring()); // explicit coercion to string
`