Skip to content

Commit a007c82

Browse files
committed
adding rotation modifier for gestures
1 parent ebe44a1 commit a007c82

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

adafruit_apds9960/apds9960.py

+28-2
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,14 @@ class APDS9960:
108108
_proximity_persistance = RWBits(4, APDS9960_PERS, 4)
109109

110110
def __init__(
111-
self, i2c, *, interrupt_pin=None, address=0x39, integration_time=0x01, gain=0x01
111+
self,
112+
i2c,
113+
*,
114+
interrupt_pin=None,
115+
address=0x39,
116+
integration_time=0x01,
117+
gain=0x01,
118+
rotation=0
112119
):
113120

114121
self.buf129 = None
@@ -125,6 +132,7 @@ def __init__(
125132
self.enable_gesture = False
126133
self.enable_proximity = False
127134
self.enable_color = False
135+
self._rotation = rotation
128136
self.enable_proximity_interrupt = False
129137
self.clear_interrupt()
130138

@@ -169,6 +177,17 @@ def _reset_counts(self):
169177
"""Proximity interrupt enable flag. True if enabled,
170178
False to disable"""
171179

180+
## GESTURE ROTATION
181+
@property
182+
def rotation(self):
183+
"""Gesture rotation offset"""
184+
return self._rotation
185+
186+
@rotation.setter
187+
def rotation(self, new_rotation):
188+
if new_rotation in [0, 90, 180, 270]:
189+
self._rotation = new_rotation
190+
172191
## GESTURE DETECTION
173192
@property
174193
def enable_gesture(self):
@@ -263,7 +282,14 @@ def gesture(self): # pylint: disable-msg=too-many-branches
263282
if gesture_received or time.monotonic() - time_mark > 0.300:
264283
self._reset_counts()
265284
break
266-
285+
if gesture_received != 0:
286+
if self._rotation != 0:
287+
directions = [1, 4, 2, 3]
288+
new_index = (
289+
directions.index(gesture_received) + self._rotation // 90
290+
) % 4
291+
modified_gesture = directions[new_index]
292+
return modified_gesture
267293
return gesture_received
268294

269295
@property

examples/apds9960_gesture_simpletest.py

+3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
apds.enable_proximity = True
99
apds.enable_gesture = True
1010

11+
# Uncomment and set the rotation if depending on how your sensor is mounted.
12+
# apds.rotation = 270 # 270 for CLUE
13+
1114
while True:
1215
gesture = apds.gesture()
1316

0 commit comments

Comments
 (0)