Skip to content

Update shake() method#82

Merged
FoamyGuy merged 1 commit intoadafruit:mainfrom
caternuson:iss78
Jan 6, 2026
Merged

Update shake() method#82
FoamyGuy merged 1 commit intoadafruit:mainfrom
caternuson:iss78

Conversation

@caternuson
Copy link
Contributor

@caternuson caternuson commented Jan 6, 2026

Possible fix for #78.

This fixes the math logic in shake() and replaces the uberpythonic tuple/map/sum/zip approach with more simple code.

A new example lis3dh_shake.py is also added.

The default threshold has been reduced to 20 since 30 required fairly aggressive shaking.

Is the simple code less performant? Simple performance check:

import time
import board
import busio
import adafruit_lis3dh

i2c = busio.I2C(board.ACCELEROMETER_SCL, board.ACCELEROMETER_SDA)

lis = adafruit_lis3dh.LIS3DH_I2C(i2c, address=25)

while True:
    t1 = time.monotonic()
    lis.shake()
    t2 = time.monotonic()
    print("total time = ", t2-t1)
    time.sleep(1)

With existing shake() method:

Adafruit CircuitPython 10.0.3 on 2025-10-17; Adafruit Circuit Playground Bluefruit with nRF52840
>>> import lis_test
total time =  0.12207
total time =  0.123047
total time =  0.123047
total time =  0.123047
total time =  0.123047

With PR shake() method:

Adafruit CircuitPython 10.0.3 on 2025-10-17; Adafruit Circuit Playground Bluefruit with nRF52840
>>> import lis_test
total time =  0.120117
total time =  0.121094
total time =  0.12207
total time =  0.12207
total time =  0.12207

Note that 0.1 secs worth of this delay is part of the averaging delay in the shake() method. But TLDR = no significant difference in performance.

@caternuson caternuson requested a review from a team January 6, 2026 17:07
Copy link
Contributor

@FoamyGuy FoamyGuy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good to me. I tested this successfully on a MagTag 10.1.0-beta.1

I had to use the alternate I2C address. I confirmed the shake example doesn't trigger as expected with current code and does with the updated code.

This library is frozen in to MagTag and perhaps others that have one built-in so ultimately it will need updated in the core before these devices see benefits without putting the library in alternate root location.

@FoamyGuy FoamyGuy merged commit 83333f8 into adafruit:main Jan 6, 2026
1 check passed
adafruit-adabot pushed a commit to adafruit/Adafruit_CircuitPython_Bundle that referenced this pull request Jan 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants