Skip to content

adding rotation modifier for gestures #26

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

Merged
merged 4 commits into from
Jun 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 31 additions & 2 deletions adafruit_apds9960/apds9960.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,14 @@ class APDS9960:
_proximity_persistance = RWBits(4, APDS9960_PERS, 4)

def __init__(
self, i2c, *, interrupt_pin=None, address=0x39, integration_time=0x01, gain=0x01
self,
i2c,
*,
interrupt_pin=None,
address=0x39,
integration_time=0x01,
gain=0x01,
rotation=0
):

self.buf129 = None
Expand All @@ -125,6 +132,7 @@ def __init__(
self.enable_gesture = False
self.enable_proximity = False
self.enable_color = False
self._rotation = rotation
self.enable_proximity_interrupt = False
self.clear_interrupt()

Expand Down Expand Up @@ -169,6 +177,19 @@ def _reset_counts(self):
"""Proximity interrupt enable flag. True if enabled,
False to disable"""

## GESTURE ROTATION
@property
def rotation(self):
"""Gesture rotation offset. Acceptable values are 0, 90, 180, 270."""
return self._rotation

Choose a reason for hiding this comment

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

Indicate that only [0,90,180,270] are acceptable


@rotation.setter
def rotation(self, new_rotation):
if new_rotation in [0, 90, 180, 270]:
self._rotation = new_rotation
else:
raise ValueError("Rotation value must be one of: 0, 90, 180, 270")

Choose a reason for hiding this comment

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

I think this should throw a ValueError if the input is not in [0,90,180,270] - it will just ignore it silently as written

Choose a reason for hiding this comment

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

Thanks

## GESTURE DETECTION
@property
def enable_gesture(self):
Expand All @@ -182,6 +203,12 @@ def enable_gesture(self, enable_flag):
self._gesture_mode = False
self._gesture_enable = enable_flag

def rotated_gesture(self, original_gesture):
"""Applies rotation offset to the given gesture direction and returns the result"""
directions = [1, 4, 2, 3]
new_index = (directions.index(original_gesture) + self._rotation // 90) % 4
return directions[new_index]

def gesture(self): # pylint: disable-msg=too-many-branches
"""Returns gesture code if detected. =0 if no gesture detected
=1 if an UP, =2 if a DOWN, =3 if an LEFT, =4 if a RIGHT
Expand Down Expand Up @@ -263,7 +290,9 @@ def gesture(self): # pylint: disable-msg=too-many-branches
if gesture_received or time.monotonic() - time_mark > 0.300:
self._reset_counts()
break

if gesture_received != 0:
if self._rotation != 0:
return self.rotated_gesture(gesture_received)
return gesture_received

@property
Expand Down
3 changes: 3 additions & 0 deletions examples/apds9960_gesture_simpletest.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
apds.enable_proximity = True
apds.enable_gesture = True

# Uncomment and set the rotation if depending on how your sensor is mounted.
# apds.rotation = 270 # 270 for CLUE

while True:
gesture = apds.gesture()

Expand Down