I have a string read from an EEPROM using the I2C bus. The data represent the values in a ‘C’ structure, e.g., the first 33 or so bytes represent in order:
int, int, string[16], byte, int, byte, byte, long, long
I would like to display them as decimal values, when the type is int, byte and long, and as an ASCII string for the string[16].
Can they be read into a structure and then indexed out for sending to server.log each followed by a new line?
Thanks for your responses which I will pursue to help learn Squirrel. However, is a parallel to the following approach possible
In ‘C’ when I read from this EEPROM I use a union of a string and a structure.
Union EE_Data { Unsigned Char raw_data[ 33]; Struct Work_Data { int var1; int var2; char char_str[16]; //includes \\0 at end of string unsigned char Byte1; int var3; unsigned char Byte2; unsigned char Byte3; long var4; long var5; } };
I2C reads into EE_Data.raw_data and I access what I want via the structure
int a = EE_Data.Work_Data.var2;
Peter’s suggestion of using a series of readn() with the right type indication is the most straithforward and works pretty well. Only thing to take care of is making sure you put them in the right order as they appear in the struct.
You only have to write a small piece of code to retrieve the string by reading 16 bytes, cast them to char and add them to a string
function readstr(len,blob) { local str = ""; for (local i = 0; i < len; i++) { local byte = blob.readn('b'); str += byte.tochar() ; } return str; }