Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch events to properties #42

Open
tannewt opened this issue Sep 13, 2024 · 2 comments
Open

Switch events to properties #42

tannewt opened this issue Sep 13, 2024 · 2 comments

Comments

@tannewt
Copy link
Member

tannewt commented Sep 13, 2024

Right now there is an events property that returns a dictionary of what events are enabled. There are separate enable_ and disable_ functions for each event type. Instead, we should remove events in favor of a property for each event. To know if it is enable, then you can read it. To change it, the value can be set.

The 37x library should be updated to match. https://github.com/adafruit/Adafruit_CircuitPython_ADXL37x

@FoamyGuy
Copy link
Contributor

FoamyGuy commented Nov 5, 2024

I tinkered with this a little bit on this branch: https://github.com/FoamyGuy/Adafruit_CircuitPython_ADXL34x/tree/seperate_events_props, adding these properties:

@property
def tap_event(self) -> bool:
if "tap" in self._enabled_interrupts:
    interrupt_source_register = self._read_clear_interrupt_source()
    if self._enabled_interrupts["tap"] == 1:
        return interrupt_source_register & _INT_SINGLE_TAP > 0
    else:
        return interrupt_source_register & _INT_DOUBLE_TAP > 0
return False

@property
def motion_event(self) -> bool:
if "motion" in self._enabled_interrupts:
    interrupt_source_register = self._read_clear_interrupt_source()
    return interrupt_source_register & _INT_ACT > 0
return False

With a basic code.py that checks both in it's main loop:

print("Motion: %s" % accelerometer.motion_event)
print("Tapped: %s" % accelerometer.tap_event)

I found that having the individual properties like this seems to clear out the event trigger data when you read any property. So reading the motion_event property first leads to "clearing out" the tap_event event. With both in the main loop motion seemed to always be True and tap always False when I tapped the device.

If the user code is only intending to check for a single kind of event then this is fine, but if the user code is interested in multiple event types using the individual properties would cause it to miss events of some types depending on which order they check them in.

With the events dictionary it reads both at once so user code is able to get accurate event trigger on both.

print(accerometer.events)

Shows True for motion and tap events at the appropriate times.

I'm unsure if that behavior changes the desire to have them broken out into separate properties or not. If we do still want that I'd be happy to work on it. I'll set this down for now though and await more direction.

@FoamyGuy
Copy link
Contributor

FoamyGuy commented Nov 7, 2024

spoke with @ladyada about this one a bit during the meeting tonight. She suggested keeping them together since they clear each other, but maybe returning an int instead of a dictionary and user code would need to use bitwise to check which events have happened.

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

No branches or pull requests

2 participants