@@ -215,7 +215,7 @@ def acceleration(self) -> AccelerationTuple:
215215 return AccelerationTuple (x , y , z )
216216
217217 def shake (
218- self , shake_threshold : int = 30 , avg_count : int = 10 , total_delay : float = 0.1
218+ self , shake_threshold : int = 20 , avg_count : int = 10 , total_delay : float = 0.1
219219 ) -> bool :
220220 """Detect when the accelerometer is shaken. Optional parameters:
221221
@@ -224,7 +224,7 @@ def shake(
224224 10 is the total acceleration if the board is not
225225 moving, therefore anything less than
226226 10 will erroneously report a constant shake detected.
227- Defaults to :const:`30 `
227+ Defaults to :const:`20 `
228228
229229 :param int avg_count: The number of readings taken and used for the average
230230 acceleration. Default to :const:`10`
@@ -234,19 +234,15 @@ def shake(
234234
235235 """
236236
237- shake_accel = (0 , 0 , 0 )
237+ xs = ys = zs = 0
238+ delay = total_delay / avg_count
238239 for _ in range (avg_count ):
239- # shake_accel creates a list of tuples from acceleration data.
240- # zip takes multiple tuples and zips them together, as in:
241- # In : zip([-0.2, 0.0, 9.5], [37.9, 13.5, -72.8])
242- # Out: [(-0.2, 37.9), (0.0, 13.5), (9.5, -72.8)]
243- # map applies sum to each member of this tuple, resulting in a
244- # 3-member list. tuple converts this list into a tuple which is
245- # used as shake_accel.
246- shake_accel = tuple (map (sum , zip (shake_accel , self .acceleration )))
247- time .sleep (total_delay / avg_count )
248- avg = tuple (value / avg_count for value in shake_accel )
249- total_accel = math .sqrt (sum (map (lambda x : x * x , avg )))
240+ x , y , z = self .acceleration
241+ xs += x * x
242+ ys += y * y
243+ zs += z * z
244+ time .sleep (delay )
245+ total_accel = math .sqrt ((xs + ys + zs ) / avg_count )
250246 return total_accel > shake_threshold
251247
252248 def read_adc_raw (self , adc : Literal [1 , 2 , 3 ]) -> int :
0 commit comments