Skip to content

Commit 4242772

Browse files
authored
Merge pull request #26 from FoamyGuy/rotation_modifier
adding rotation modifier for gestures
2 parents ebe44a1 + b392639 commit 4242772

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

adafruit_apds9960/apds9960.py

+31-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,19 @@ 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. Acceptable values are 0, 90, 180, 270."""
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+
else:
191+
raise ValueError("Rotation value must be one of: 0, 90, 180, 270")
192+
172193
## GESTURE DETECTION
173194
@property
174195
def enable_gesture(self):
@@ -182,6 +203,12 @@ def enable_gesture(self, enable_flag):
182203
self._gesture_mode = False
183204
self._gesture_enable = enable_flag
184205

206+
def rotated_gesture(self, original_gesture):
207+
"""Applies rotation offset to the given gesture direction and returns the result"""
208+
directions = [1, 4, 2, 3]
209+
new_index = (directions.index(original_gesture) + self._rotation // 90) % 4
210+
return directions[new_index]
211+
185212
def gesture(self): # pylint: disable-msg=too-many-branches
186213
"""Returns gesture code if detected. =0 if no gesture detected
187214
=1 if an UP, =2 if a DOWN, =3 if an LEFT, =4 if a RIGHT
@@ -263,7 +290,9 @@ def gesture(self): # pylint: disable-msg=too-many-branches
263290
if gesture_received or time.monotonic() - time_mark > 0.300:
264291
self._reset_counts()
265292
break
266-
293+
if gesture_received != 0:
294+
if self._rotation != 0:
295+
return self.rotated_gesture(gesture_received)
267296
return gesture_received
268297

269298
@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)