def get_temp(i2c: busio.I2C) -> int: """ MSB 0x31 LSB 0x31 8 LSB / deg - 12 bit resolution """ read_temp = bytearray(2) i2c.writeto_then_readfrom(MAG_DEVICE_ADDRESS, bytes( [TEMP_OUT_H_M]), read_temp, stop=True) # print(f"Raw Temp: {hex(read_temp[0])} : {hex(read_temp[1])}") p = (read_temp[0] << 8) | (read_temp[1] >> 3) return (p >> 4)
def get_magnetometer(i2c: busio.I2C): read_mag = bytearray(6) i2c.writeto_then_readfrom(MAG_DEVICE_ADDRESS, bytes( [OUT_X_H_M]), read_mag, stop=True) # print(f"Raw Magnetometer: {hex(read_mag[1])} : {hex(read_mag[0])}") # print(f"Raw Magnetometer: {hex(read_mag[3])} : {hex(read_mag[2])}") # print(f"Raw Magnetometer: {hex(read_mag[5])} : {hex(read_mag[4])}") mag_x = read_mag[1] << 8 | read_mag[0] mag_z = read_mag[3] << 8 | read_mag[2] mag_y = read_mag[5] << 8 | read_mag[4] return (mag_x, mag_y, mag_z)
def get_accelerometer(i2c: busio.I2C): read_acc = bytearray(6) i2c.writeto_then_readfrom(ACC_DEVICE_ADDRESS, bytes( [OUT_X_L_A]), read_acc, stop=True) # 0x1000 0000 # print(f"Raw Accelerometer: {hex(read_acc[1])} : {hex(read_acc[0])}") # print(f"Raw Accelerometer: {hex(read_acc[3])} : {hex(read_acc[2])}") # print(f"Raw Accelerometer: {hex(read_acc[5])} : {hex(read_acc[4])}") acc_x = read_acc[1] << 8 | read_acc[0] acc_y = read_acc[3] << 8 | read_acc[2] acc_z = read_acc[5] << 8 | read_acc[4] return (acc_x, acc_y, acc_z)
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]))
def _eeprom_i2c_read_byte(i2c: busio.I2C, i2c_addr: int, mem_addr: int, timeout: float = 1.0) -> Tuple[bool, bytearray]: # Make sure address is only one byte: if mem_addr > 255: return False, bytearray() # Try writing to address (EEPROM is unresponsive while writing) if not _eeprom_i2c_wait(i2c, i2c_addr, mem_addr, timeout): return False, bytearray() # Finish the read buf = bytearray(1) i2c.writeto_then_readfrom(i2c_addr, bytearray([mem_addr]), buf) return True, buf