Envirobit support (python I/O)


Im trying to add I/O capabilities to my envirobit. The envirobit is connected to my raspberry pi 4 via a microbit, and I am building on https://github.com/whaleygeek/bitio.

I am also using pimoroni/bme280-python to better structure the code, and then I added my own changes to i2cdevice-python.

Now, in this code I changed the write_i2c_block_data and read_i2c_block_data functions to be getting I/O data from the microbit.

The problem is that the bytes are printed as strings, and I have to change them back to bytes - which I think I have succeeded in.
But the length of the byte output is longer than prescribed, which might be wrong. In particular, the first calibration should be length 26, but I get 38 bytes.

I don’t know how to move forward, or how to correctly limit the length of the received bytes (or send correct write to limit it).

My read_i2c_block_data looks like this

def read_i2c_block_data(self, i2c_address, register, length=1):
microbit.cmd(f"i2c.write({i2c_address}, {bytes([register])}, repeat=True)")
read = microbit.cmd(f"print(i2c.read({i2c_address}, {length}))")
read = re.sub(r’("|’)$’, ‘’, read)
read = re.sub(r’^b("|’)’, ‘’, read)
read = read.encode().decode(‘unicode-escape’).encode(‘utf-8’)
return read

Would anybody mind checking my setup and forking my code to how it could be done?

I have very limited knowledge on i2cbus communication and bytes.

The repo is here: https://github.com/Hojland/bitio

Just a guess but 26 in Hex is 38 in decimal