OpenMV bitwise logical operations

Is there support for standard bitwise operators (|=, &=, etc)?

I have reviewed the source/forum examples I could fine, as well as documentation from Section 4 of the Micropython guide, but it appears to be targeted specifically at inline assembly functions for Thumb2.

4.2. Logical instructions

  • and_(Rd, Rn) Rd &= Rn
  • orr(Rd, Rn) Rd |= Rn
  • eor(Rd, Rn) Rd ^= Rn
  • mvn(Rd, Rn) Rd = Rn ^ 0xffffffff i.e. Rd = 1’s complement of Rn
  • bic(Rd, Rn) Rd &= ~Rn bit clear Rd using mask in Rn

Note the use of “and_” instead of “and”, because “and” is a reserved keyword in Python.

I have values that I read from a off-module SPI GPIO expander, and I need to mask some of the values before updating them which I would typically use a bitwise operation for.

Typically I would expect to do something like: value |= 0x1C which looks to be a valid operator in Python, but is there an equivalent implementation that I am missing for MicroPython/OpenMV?

Yes, this is standard python. So, it’s supported.

value |= 0x1C

Is standard python. Micropython pretty much covers all Python syntax. You have to use some really obscure thing to find a place where it doesn’t have support.

Thank you for confirming. I took the following error as an indication that it wasn’t supported:

I am new to Python and now understand that it appears that it has to do with the types of data I am attempting to perform the operation on being unsupported, not the actual operation itself.

Yes, it’s because that’s a bytes() object. E.g. the container of the bytes.

You need to do something to get the value of the byte you want.

newValue = tmpByte0[0] | 0x1C.

The API for sending/receiving works on byte arrays. So, there’s a little bit of work you need to do on data serialization and deserialization.

I recommend using the struct module. Ask chatgpt about how to use the struct module and it will give you want you want.