Attempt to perform a bitwise op on a string

I’m porting some i2c code from adafruit (see my previous post), and I ran into a funky line that doesn’t translate well to squirrel.

writeRegister(CAP1188_MAIN, readRegister(CAP1188_MAIN) & ~CAP1188_MAIN_INT);

Note that the writeRegister function has the i2c device address hard coded (specified elsewhere), and takes the register and value to be written to that register as arguments, respectively. I think this line clears some sort of interrupt, but that’s a separate problem of figuring out what exactly this does and why.

So, I need to write the CAP1188_MAIN register value, that’s no problem.
i2c.write(i2c_addr, CAP1188_MAIN);

But when I have to write the value to go in the register, I have an issue. Here’s what I tried
`local val = i2c.read(i2c_addr, CAP1188_MAIN, 1);
local val2 = val & ~CAP1188_MAIN_INT;

i2c.write(i2c_addr, CAP1188_MAIN);
i2c.write(i2c_addr, val2);`

I get an error with this line
local val2 = val & ~CAP1188_MAIN_INT;
because I can’t do bitwise ops on strings, and i2c.read() returns strings.

So I guess that was a long winded way of asking if its possible to convert strings to a format that I can perform bitwise ops on. Or perhaps there’s a better way to do this in general.

Thanks!

“val” is a 1 character string here; you’d need to do:

`local val2 = val[0] & ~CAP1188_MAIN_INT; // val2 is now an int

i2c.write(i2c_addr, format("%c", val2)); // convert it back to a string to send over i2c
`

…or, optimize it like:

i2c.write(i2c_addr, format("%c", val[0] & ~CAP1188_MAIN_INT));

Do bear in mind that each “i2c.write” does a separate transaction.

i2c.write(i2c_addr, CAP1188_MAIN); i2c.write(i2c_addr, val2);

is not at all the same thing as:

i2c.write(i2c_addr, CAP1188_MAIN + val2);

Peter

Oh yes, like Peter says. You absolutely want to write the value back in a single transaction as the first byte will be the subaddress:

i2c.write(i2c_addr, CAP1188_MAIN + format("%c", val[0] & ~CAP1188_MAIN_INT));