def _eeprom_i2c_wait(i2c: busio.I2C, i2c_addr: int, mem_addr: int, timeout: float = 1.0) -> bool: # Try to access the I2C EEPROM (it becomes unresonsive during a write) timestamp = time.monotonic() while time.monotonic() < timestamp + timeout: try: i2c.writeto(i2c_addr, bytearray([mem_addr]), end=1) return True except OSError: pass return False
def _eeprom_i2c_write_byte(i2c: busio.I2C, i2c_addr: int, mem_addr: int, mem_data: int) -> bool: # Make sure address is only one byte: if mem_addr > 255: return False # Make sure data is only one byte: if mem_data > 255: return False # Write data to memory at given address try: i2c.writeto(i2c_addr, bytearray([mem_addr, mem_data])) except OSError: return False return True
def init(i2c: busio.I2C): ctrl_reg1_a_value = ((9 << 4) | (1 << 0) | (1 << 1) | (1 << 2)) i2c.writeto(ACC_DEVICE_ADDRESS, bytes([CTRL_REG1_A, ctrl_reg1_a_value])) ctrl_reg4_a_value = (1 << 3) i2c.writeto(ACC_DEVICE_ADDRESS, bytes([CTRL_REG4_A, ctrl_reg4_a_value])) cra_reg_m = (1 << 7) | (0x4 << 2) i2c.writeto(MAG_DEVICE_ADDRESS, bytes([CRA_REG_M, cra_reg_m])) crb_read_m_data = bytearray(1) i2c.writeto_then_readfrom(MAG_DEVICE_ADDRESS, bytes( [CRB_REG_M]), crb_read_m_data, stop=True) print(f"CRB_REG_M: {hex(crb_read_m_data[0])}") mr_reg_m = (0) i2c.writeto(MAG_DEVICE_ADDRESS, bytes([MR_REG_M, mr_reg_m]))