Skip to content

Commit a6293f9

Browse files
committed
ism330dl: Address Copilot review on PR #50.
1 parent 9e3c185 commit a6293f9

3 files changed

Lines changed: 51 additions & 11 deletions

File tree

lib/ism330dl/ism330dl/const.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@
6565
ACCEL_ODR_833HZ = const(0x07)
6666
ACCEL_ODR_1660HZ = const(0x08)
6767

68+
ACCEL_ODR_VALUES = (0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08)
69+
6870

6971
# ---------------------------------------------------------------------
7072
# Accelerometer full scale
@@ -102,6 +104,8 @@
102104
GYRO_ODR_833HZ = const(0x07)
103105
GYRO_ODR_1660HZ = const(0x08)
104106

107+
GYRO_ODR_VALUES = (0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08)
108+
105109

106110
# ---------------------------------------------------------------------
107111
# Gyroscope full scale

lib/ism330dl/ism330dl/device.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,13 +93,17 @@ def reset(self):
9393
# --------------------------------------------------
9494

9595
def configure_accel(self, odr, scale):
96+
if odr not in ACCEL_ODR_VALUES:
97+
raise ISM330DLConfigError("Invalid accel ODR")
9698
if scale not in ACCEL_FS_BITS:
9799
raise ISM330DLConfigError("Invalid accel scale")
98100
self._accel_scale = scale
99101
value = (odr << 4) | (ACCEL_FS_BITS[scale] << 2)
100102
self._write_u8(REG_CTRL1_XL, value)
101103

102104
def configure_gyro(self, odr, scale):
105+
if odr not in GYRO_ODR_VALUES:
106+
raise ISM330DLConfigError("Invalid gyro ODR")
103107
if scale == GYRO_FS_125DPS:
104108
value = (odr << 4) | 0x02
105109
else:

tests/scenarios/ism330dl.yaml

Lines changed: 43 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -171,21 +171,27 @@ tests:
171171

172172
# ----- Configuration -----
173173

174-
- name: "Configure accel changes scale"
174+
- name: "Configure accel writes correct register value"
175175
action: script
176176
script: |
177177
from ism330dl.const import ACCEL_ODR_104HZ, ACCEL_FS_4G
178+
i2c.clear_write_log()
178179
dev.configure_accel(ACCEL_ODR_104HZ, ACCEL_FS_4G)
179-
result = dev._accel_scale == ACCEL_FS_4G
180+
log = i2c.get_write_log()
181+
reg, data = log[-1]
182+
result = reg == 0x10 and data[0] == (0x04 << 4) | (0x02 << 2)
180183
expect_true: true
181184
mode: [mock]
182185

183-
- name: "Configure gyro changes scale"
186+
- name: "Configure gyro writes correct register value"
184187
action: script
185188
script: |
186189
from ism330dl.const import GYRO_ODR_104HZ, GYRO_FS_500DPS
190+
i2c.clear_write_log()
187191
dev.configure_gyro(GYRO_ODR_104HZ, GYRO_FS_500DPS)
188-
result = dev._gyro_scale == GYRO_FS_500DPS
192+
log = i2c.get_write_log()
193+
reg, data = log[-1]
194+
result = reg == 0x11 and data[0] == (0x04 << 4) | (0x01 << 2)
189195
expect_true: true
190196
mode: [mock]
191197

@@ -201,6 +207,18 @@ tests:
201207
expect_true: true
202208
mode: [mock]
203209

210+
- name: "Configure accel rejects invalid ODR"
211+
action: script
212+
script: |
213+
from ism330dl.const import ACCEL_FS_2G
214+
try:
215+
dev.configure_accel(99, ACCEL_FS_2G)
216+
result = False
217+
except Exception:
218+
result = True
219+
expect_true: true
220+
mode: [mock]
221+
204222
- name: "Configure gyro rejects invalid scale"
205223
action: script
206224
script: |
@@ -213,15 +231,28 @@ tests:
213231
expect_true: true
214232
mode: [mock]
215233

234+
- name: "Configure gyro rejects invalid ODR"
235+
action: script
236+
script: |
237+
from ism330dl.const import GYRO_FS_250DPS
238+
try:
239+
dev.configure_gyro(99, GYRO_FS_250DPS)
240+
result = False
241+
except Exception:
242+
result = True
243+
expect_true: true
244+
mode: [mock]
245+
216246
# ----- Power -----
217247

218248
- name: "Power down writes zero to CTRL registers"
219249
action: script
220250
script: |
251+
i2c.clear_write_log()
221252
dev.power_down()
222-
ctrl1 = dev._read_u8(0x10)
223-
ctrl2 = dev._read_u8(0x11)
224-
result = ctrl1 == 0 and ctrl2 == 0
253+
log = i2c.get_write_log()
254+
writes = {reg: data[0] for reg, data in log}
255+
result = writes.get(0x10) == 0 and writes.get(0x11) == 0
225256
expect_true: true
226257
mode: [mock]
227258

@@ -230,11 +261,12 @@ tests:
230261
- name: "Reset sets BDU and IF_INC in CTRL3_C"
231262
action: script
232263
script: |
264+
i2c.clear_write_log()
233265
dev.reset()
234-
ctrl3 = dev._read_u8(0x12)
235-
bdu = bool(ctrl3 & (1 << 6))
236-
if_inc = bool(ctrl3 & (1 << 2))
237-
result = bdu and if_inc
266+
log = i2c.get_write_log()
267+
ctrl3_writes = [data[0] for reg, data in log if reg == 0x12]
268+
bdu_if_inc = (1 << 6) | (1 << 2)
269+
result = len(ctrl3_writes) == 2 and ctrl3_writes[1] == bdu_if_inc
238270
expect_true: true
239271
mode: [mock]
240272

0 commit comments

Comments
 (0)