def read_from_rpi_to_esp32(): try: # change 1 of SMBus(1) to bus number on your RPI smbus = SMBus(1) # prepare the data packed = None with Packer() as packer: packer.write(register) packer.end() packed = packer.read() raw_list = None smbus.write_bytes(register, bytearray(packed)) time.sleep(0.3) # wait i2c process the request raw_list = list( smbus.read_bytes(register, 5) ) # the read_bytes contains the data format: first, length, data, crc8, end bytes print(raw_list) # let's clean received data unpacked = None with Unpacker as unpacker: unpacker.write(raw_list) unpacked = unpacker.read() return unpacked except Exception as e: print("ERROR: {}".format(e))
def read_from_rpi_to_esp32(): try: # prepare the data packed = None with Packer() as packer: packer.write(value) packer.end() packed = packer.read() print("in packer") # change 1 of SMBus(1) to bus number on your RPI raw_list = None with SMBus(1) as smbus: smbus.write_bytes(slave_address, bytearray(packed)) #how would the slave know whether to write or receive? time.sleep(0.3) # wait i2c process the request raw_list = list(smbus.read_bytes(slave_address, 5)) # the read_bytes contains the data format: first, length, data, crc8, end bytes rawList = smbus.read_byte(slave_address) #print(rawList) print(raw_list) print("in smbus") # let's clean received data unpacked = None with Unpacker() as unpacker: print("unpacker 1") unpacker.write(raw_list) print("unpacker 2") unpacked = unpacker.read() print("in unpacker") return unpacked except Exception as e: print("ERROR: {}".format(e))
def test_packer(self): packed = None with Packer() as packer: packer.write(127) packer.end() packed = [i for i in packer.read() if i != 0] expected = [2, 5, 127, 185, 4] self.assertIsNotNone(packed) self.assertTrue(type(packed).__name__ == "list") self.assertEqual(expected, packed)
def write_from_rpi_to_esp32(): try: # change 1 of SMBus(1) to bus number on your RPI smbus = SMBus(1) # prepare the data packed = None with Packer() as packer: packer.write(register) packer.write(value) packer.end() packed = packer.read() smbus.write_bytes(register, bytearray(packed)) except Exception as e: print("ERROR: {}".format(e))