Object with buffer protocol for I2C ?? is there such an operation for i2c in micropython ??

The official pyboard running MicroPython.
This is the reference design and main target board for MicroPython.
You can buy one at the store.
Target audience: Users with a pyboard.
Post Reply
yoLo_
Posts: 9
Joined: Fri Mar 24, 2017 2:34 am

Object with buffer protocol for I2C ?? is there such an operation for i2c in micropython ??

Post by yoLo_ » Fri Dec 01, 2017 10:30 pm

Hello, uPythonians..
I'm writing a code that involves an i2c
this is the function for writing to the i2c

Code: Select all

        
   # write to device
   def _write(self, data, memaddr, addr):
        
       # perform a memory write. caller should trap OSError.
        
   self._ccs811_i2c.writeto_mem(addr, memaddr, self.buf1)
I'm required to perform a 4bytes write to the device example: data = b'\x00\x00\x00\x00'
the operation has to be done in one single write sequence
doing so results in error: cannot convert bytearray to int.
I then convert data to int using struct.unpack which results in a signed int value
when i try to perform the write operation, i get TypeError: object with buffer protocol required

How is a buffer protocol done for i2c ??

thanks

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: Object with buffer protocol for I2C ?? is there such an operation for i2c in micropython ??

Post by dhylands » Fri Dec 01, 2017 10:44 pm

Try using:

Code: Select all

self._ccs811_i2c.writeto_mem(addr, memaddr, bytearray(self.buf1))
assuming self.buf1 is equal to b'\x00\x00\x00\x00'

yoLo_
Posts: 9
Joined: Fri Mar 24, 2017 2:34 am

Re: Object with buffer protocol for I2C ?? is there such an operation for i2c in micropython ??

Post by yoLo_ » Thu Dec 14, 2017 1:59 am

Thanks a lot.
this solved my problem

Post Reply