@@ -14,7 +14,9 @@ def __init__(self, i2c, address=ISM330DL_I2C_DEFAULT_ADDR):
1414 self ._buffer_1 = bytearray (1 )
1515
1616 self ._accel_scale = ACCEL_FS_2G
17+ self ._accel_odr = ACCEL_ODR_104HZ
1718 self ._gyro_scale = GYRO_FS_250DPS
19+ self ._gyro_odr = GYRO_ODR_104HZ
1820
1921 self ._temp_gain = 1.0
2022 self ._temp_offset = 0.0
@@ -101,6 +103,8 @@ def configure_accel(self, odr, scale):
101103 if scale not in ACCEL_FS_BITS :
102104 raise ISM330DLConfigError ("Invalid accel scale" )
103105 self ._accel_scale = scale
106+ if odr != ACCEL_ODR_POWER_DOWN :
107+ self ._accel_odr = odr
104108 value = (odr << 4 ) | (ACCEL_FS_BITS [scale ] << 2 )
105109 self ._write_u8 (REG_CTRL1_XL , value )
106110
@@ -114,19 +118,41 @@ def configure_gyro(self, odr, scale):
114118 raise ISM330DLConfigError ("Invalid gyro scale" )
115119 value = (odr << 4 ) | (GYRO_FS_BITS [scale ] << 2 )
116120 self ._gyro_scale = scale
121+ if odr != GYRO_ODR_POWER_DOWN :
122+ self ._gyro_odr = odr
117123 self ._write_u8 (REG_CTRL2_G , value )
118124
125+ # --------------------------------------------------
126+ # Auto-trigger
127+ # --------------------------------------------------
128+
129+ def _is_power_down (self ):
130+ """Return True if both accel and gyro ODR are 0 (power-down)."""
131+ ctrl1 = self ._read_u8 (REG_CTRL1_XL )
132+ ctrl2 = self ._read_u8 (REG_CTRL2_G )
133+ return (ctrl1 & 0xF0 ) == 0 and (ctrl2 & 0xF0 ) == 0
134+
135+ def _ensure_data (self ):
136+ """Restore previous ODR and wait for data if in power-down mode."""
137+ if self ._is_power_down ():
138+ self .configure_accel (self ._accel_odr , self ._accel_scale )
139+ self .configure_gyro (self ._gyro_odr , self ._gyro_scale )
140+ sleep_ms (100 )
141+
119142 # --------------------------------------------------
120143 # Raw readings
121144 # --------------------------------------------------
122145
123146 def acceleration_raw (self ):
147+ self ._ensure_data ()
124148 return self ._read_vector (REG_OUTX_L_XL )
125149
126150 def gyroscope_raw (self ):
151+ self ._ensure_data ()
127152 return self ._read_vector (REG_OUTX_L_G )
128153
129154 def temperature_raw (self ):
155+ self ._ensure_data ()
130156 return self ._read_i16 (REG_OUT_TEMP_L )
131157
132158 # --------------------------------------------------
0 commit comments