Skip to content

Commit 8c9a8fa

Browse files
committed
Small fixes.
1 parent 94e7ce6 commit 8c9a8fa

File tree

3 files changed

+42
-40
lines changed

3 files changed

+42
-40
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
__pycache__
22
_build
33
*.pyc
4+
*.mpy

adafruit_lsm9ds1.py

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -149,24 +149,24 @@ def accel_range(self):
149149
- ACCELRANGE_8G
150150
- ACCELRANGE_16G
151151
"""
152-
reg = self._read_u8(_XGTYPE, LSM9DS1_REGISTER_CTRL_REG6_XL)
152+
reg = self._read_u8(_XGTYPE, _LSM9DS1_REGISTER_CTRL_REG6_XL)
153153
return (reg & 0b00011000) & 0xFF
154154

155155
@accel_range.setter
156156
def accel_range(self, val):
157157
assert val in (ACCELRANGE_2G, ACCELRANGE_4G, ACCELRANGE_8G,
158158
ACCELRANGE_16G)
159-
reg = self._read_u8(_XGTYPE, LSM9DS1_REGISTER_CTRL_REG6_XL)
159+
reg = self._read_u8(_XGTYPE, _LSM9DS1_REGISTER_CTRL_REG6_XL)
160160
reg = (reg & ~(0b00011000)) & 0xFF
161161
reg |= val
162162
self._write_u8(_XGTYPE, _LSM9DS1_REGISTER_CTRL_REG6_XL, reg)
163-
if val == _LSM9DS1_ACCELRANGE_2G:
163+
if val == ACCELRANGE_2G:
164164
self._accel_mg_lsb = _LSM9DS1_ACCEL_MG_LSB_2G
165-
elif val == _LSM9DS1_ACCELRANGE_4G:
165+
elif val == ACCELRANGE_4G:
166166
self._accel_mg_lsb = _LSM9DS1_ACCEL_MG_LSB_4G
167-
elif val == _LSM9DS1_ACCELRANGE_8G:
167+
elif val == ACCELRANGE_8G:
168168
self._accel_mg_lsb = _LSM9DS1_ACCEL_MG_LSB_8G
169-
elif val == _LSM9DS1_ACCELRANGE_16G:
169+
elif val == ACCELRANGE_16G:
170170
self._accel_mg_lsb = _LSM9DS1_ACCEL_MG_LSB_16G
171171

172172
@property
@@ -188,13 +188,13 @@ def mag_gain(self, val):
188188
reg = (reg & ~(0b01100000)) & 0xFF
189189
reg |= val
190190
self._write_u8(_MAGTYPE, _LSM9DS1_REGISTER_CTRL_REG2_M, reg)
191-
if val == _LSM9DS1_MAGGAIN_4GAUSS:
191+
if val == MAGGAIN_4GAUSS:
192192
self._mag_mgauss_lsb = _LSM9DS1_MAG_MGAUSS_4GAUSS
193-
elif val == _LSM9DS1_MAGGAIN_8GAUSS:
193+
elif val == MAGGAIN_8GAUSS:
194194
self._mag_mgauss_lsb = _LSM9DS1_MAG_MGAUSS_8GAUSS
195-
elif val == _LSM9DS1_MAGGAIN_12GAUSS:
195+
elif val == MAGGAIN_12GAUSS:
196196
self._mag_mgauss_lsb = _LSM9DS1_MAG_MGAUSS_12GAUSS
197-
elif val == _LSM9DS1_MAGGAIN_16GAUSS:
197+
elif val == MAGGAIN_16GAUSS:
198198
self._mag_mgauss_lsb = _LSM9DS1_MAG_MGAUSS_16GAUSS
199199

200200
@property
@@ -214,11 +214,11 @@ def gyro_scale(self, val):
214214
reg = (reg & ~(0b00110000)) & 0xFF
215215
reg |= val
216216
self._write_u8(_XGTYPE, _LSM9DS1_REGISTER_CTRL_REG1_G, reg)
217-
if val == _LSM9DS1_GYROSCALE_245DPS:
217+
if val == GYROSCALE_245DPS:
218218
self._gyro_dps_digit = _LSM9DS1_GYRO_DPS_DIGIT_245DPS
219-
elif val == _LSM9DS1_GYROSCALE_500DPS:
219+
elif val == GYROSCALE_500DPS:
220220
self._gyro_dps_digit = _LSM9DS1_GYRO_DPS_DIGIT_500DPS
221-
elif val == _LSM9DS1_GYROSCALE_2000DPS:
221+
elif val == GYROSCALE_2000DPS:
222222
self._gyro_dps_digit = _LSM9DS1_GYRO_DPS_DIGIT_2000DPS
223223

224224
def read_accel_raw(self):
@@ -228,7 +228,7 @@ def read_accel_raw(self):
228228
accelerometer property!
229229
"""
230230
# Read the accelerometer
231-
self._read_bytes(_XGTYPE, 0x80 | LSM9DS1_REGISTER_OUT_X_L_XL, 6,
231+
self._read_bytes(_XGTYPE, 0x80 | _LSM9DS1_REGISTER_OUT_X_L_XL, 6,
232232
self._BUFFER)
233233
xlo = self._BUFFER[0];
234234
xhi = self._BUFFER[1];
@@ -315,7 +315,7 @@ def read_temp_raw(self):
315315
want to use the temperature property!
316316
"""
317317
# Read temp sensor
318-
self._read_bytes(_XGTYPE, 0x80 | LSM9DS1_REGISTER_TEMP_OUT_L, 2,
318+
self._read_bytes(_XGTYPE, 0x80 | _LSM9DS1_REGISTER_TEMP_OUT_L, 2,
319319
self._BUFFER)
320320
temp = (self._BUFFER[1] << 8) | self._BUFFER[0]
321321
return temp
@@ -362,31 +362,31 @@ def _read_u8(self, sensor_type, address):
362362
device = self._mag_device
363363
else:
364364
device = self._xg_device
365-
with device:
365+
with device as i2c:
366366
self._BUFFER[0] = address & 0xFF
367-
device.write(self._BUFFER, end=1)
368-
device.readinto(self._BUFFER, end=1)
367+
i2c.write(self._BUFFER, end=1, stop=False)
368+
i2c.readinto(self._BUFFER, end=1)
369369
return self._BUFFER[0]
370370

371371
def _read_bytes(self, sensor_type, address, count, buffer):
372372
if sensor_type == _MAGTYPE:
373373
device = self._mag_device
374374
else:
375375
device = self._xg_device
376-
with device:
376+
with device as i2c:
377377
self._BUFFER[0] = address & 0xFF
378-
device.write(self._BUFFER, end=1)
379-
device.readinto(self._BUFFER, end=count)
378+
i2c.write(self._BUFFER, end=1, stop=False)
379+
i2c.readinto(self._BUFFER, end=count)
380380

381381
def _write_u8(self, sensor_type, address, val):
382382
if sensor_type == _MAGTYPE:
383383
device = self._mag_device
384384
else:
385385
device = self._xg_device
386-
with device:
386+
with device as i2c:
387387
self._BUFFER[0] = address & 0xFF
388388
self._BUFFER[1] = val & 0xFF
389-
device.write(self._BUFFER, end=2)
389+
i2c.write(self._BUFFER, end=2)
390390

391391

392392
class LSM9DS1_SPI(LSM9DS1):
@@ -401,31 +401,31 @@ def _read_u8(self, sensor_type, address):
401401
device = self._mag_device
402402
else:
403403
device = self._xg_device
404-
with device:
405-
device.configure(baudrate=200000, phase=0, polarity=0)
404+
with device as spi:
405+
spi.configure(baudrate=200000, phase=0, polarity=0)
406406
self._BUFFER[0] = (address | 0x80) & 0xFF
407-
device.write(self._BUFFER, end=1)
408-
device.readinto(self._BUFFER, end=1)
407+
spi.write(self._BUFFER, end=1)
408+
spi.readinto(self._BUFFER, end=1)
409409
return self._BUFFER[0]
410410

411411
def _read_bytes(self, sensor_type, address, count, buffer):
412412
if sensor_type == _MAGTYPE:
413413
device = self._mag_device
414414
else:
415415
device = self._xg_device
416-
with device:
417-
device.configure(baudrate=200000, phase=0, polarity=0)
416+
with device as spi:
417+
spi.configure(baudrate=200000, phase=0, polarity=0)
418418
self._BUFFER[0] = (address | 0x80) & 0xFF
419-
device.write(self._BUFFER, end=1)
420-
device.readinto(self._BUFFER, end=count)
419+
spi.write(self._BUFFER, end=1)
420+
spi.readinto(self._BUFFER, end=count)
421421

422422
def _write_u8(self, sensor_type, address, val):
423423
if sensor_type == _MAGTYPE:
424424
device = self._mag_device
425425
else:
426426
device = self._xg_device
427-
with device:
428-
device.configure(baudrate=200000, phase=0, polarity=0)
427+
with device as spi:
428+
spi.configure(baudrate=200000, phase=0, polarity=0)
429429
self._BUFFER[0] = (address & 0x7F) & 0xFF
430430
self._BUFFER[1] = val & 0xFF
431-
device.write(self._BUFFER, end=2)
431+
spi.write(self._BUFFER, end=2)

examples/simpletest.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
# Will print the acceleration, magnetometer, and gyroscope values every second.
33
import board
44
import busio
5+
import digitalio
56
import time
67

78
import adafruit_lsm9ds1
@@ -16,8 +17,8 @@
1617

1718
# SPI connection:
1819
#spi = busio.SPI(board.SCK, MOSI=board.MOSI, MISO=board.MISO)
19-
#xgcs = board.D5 # Pin connected to XGCS (accel/gyro chip select).
20-
#mcs = board.D6 # Pin connected to MCS (magnetometer chip select).
20+
#xgcs = digitalio.DigitalInOut(board.D5) # Pin connected to XGCS (accel/gyro chip select).
21+
#mcs = digitalio.DigitalInOut(board.D6) # Pin connected to MCS (magnetometer chip select).
2122
#sensor = adafruit_lsm9ds1.LSM9DS1_SPI(spi, xgcs, mcs)
2223

2324
# Main loop will read the acceleration, magnetometer, gyroscope, Temperature
@@ -29,12 +30,12 @@
2930
gyro_x, gyro_y, gyro_z = sensor.gyroscope
3031
temp = sensor.temperature
3132
# Print values.
32-
print('Acceleration (m/s^2): ({0:0.3f},{0:0.3f},{0:0.3f})'.format(
33+
print('Acceleration (m/s^2): ({0:0.3f},{1:0.3f},{2:0.3f})'.format(
3334
accel_x, accel_y, accel_z))
34-
print('Magnetometer (gauss): ({0:0.3f},{0:0.3f},{0:0.3f})'.format(
35+
print('Magnetometer (gauss): ({0:0.3f},{1:0.3f},{2:0.3f})'.format(
3536
mag_x, mag_y, mag_z))
36-
print('Gyroscope (radians/sec): ({0:0.3f},{0:0.3f},{0:0.3f})'.format(
37+
print('Gyroscope (radians/sec): ({0:0.3f},{1:0.3f},{2:0.3f})'.format(
3738
gyro_x, gyro_y, gyro_z))
38-
print('Temperature: {0.3f}C'.format(temp))
39+
print('Temperature: {0:0.3f}C'.format(temp))
3940
# Delay for a second.
4041
time.sleep(1.0)

0 commit comments

Comments
 (0)