Skip to content

Commit

Permalink
Merge branch 'dannystaple-patch-1'
Browse files Browse the repository at this point in the history
  • Loading branch information
Gadgetoid committed Jul 15, 2020
2 parents 8c035ca + ba1e835 commit 5f7b324
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
17 changes: 17 additions & 0 deletions library/icm20948/__init__.py
Expand Up @@ -34,6 +34,14 @@
ICM20948_ACCEL_XOUT_H = 0x2D
ICM20948_GRYO_XOUT_H = 0x33

ICM20948_TEMP_OUT_H = 0x39
ICM20948_TEMP_OUT_L = 0x3A

# Offset and sensitivity - defined in electrical characteristics, and TEMP_OUT_H/L of datasheet
ICM20948_TEMPERATURE_DEGREES_OFFSET = 21
ICM20948_TEMPERATURE_SENSITIVITY = 333.87
ICM20948_ROOM_TEMP_OFFSET = 21

AK09916_I2C_ADDR = 0x0c
AK09916_CHIP_ID = 0x09
AK09916_WIA = 0x01
Expand Down Expand Up @@ -209,6 +217,15 @@ def set_gyro_low_pass(self, enabled=True, mode=5):
value |= (mode & 0x07) << 4
self.write(ICM20948_GYRO_CONFIG_1, value)

def read_temperature(self):
"""Property to read the current IMU temperature"""
# PWR_MGMT_1 defaults to leave temperature enabled
self.bank(0)
temp_raw_bytes = self.read_bytes(ICM20948_TEMP_OUT_H, 2)
temp_raw = struct.unpack('>h', bytearray(temp_raw_bytes))[0]
temperature_deg_c = ((temp_raw - ICM20948_ROOM_TEMP_OFFSET) / ICM20948_TEMPERATURE_SENSITIVITY) + ICM20948_TEMPERATURE_DEGREES_OFFSET
return temperature_deg_c

def __init__(self, i2c_addr=I2C_ADDR, i2c_bus=None):
self._bank = -1
self._addr = i2c_addr
Expand Down
10 changes: 10 additions & 0 deletions library/tests/test_read.py
Expand Up @@ -13,3 +13,13 @@ def test_accel_gyro():
ax, ay, az, gx, gy, gz = icm20948.read_accelerometer_gyro_data()
assert (round(ax, 2), round(ay, 2), round(az, 2), int(gx), int(gy), int(gz)) == (0.05, 0.11, 0.16, 3, 4, 5)
del icm20948

def test_temperature():
smbus = mock.Mock()
smbus.SMBus = MockSMBus
sys.modules['smbus'] = smbus

from icm20948 import ICM20948
icm20948 = ICM20948()
temp_degrees = icm20948.read_temperature()
assert (round(temp_degrees, 2) == 24)
3 changes: 3 additions & 0 deletions library/tests/tools.py
Expand Up @@ -34,3 +34,6 @@ def read_i2c_block_data(self, addr, reg, length):
555,
666
))
# temperature data
if length == 2:
return struct.pack(">h", 0x400)

0 comments on commit 5f7b324

Please sign in to comment.