@@ -108,7 +108,14 @@ class APDS9960:
108
108
_proximity_persistance = RWBits (4 , APDS9960_PERS , 4 )
109
109
110
110
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
112
119
):
113
120
114
121
self .buf129 = None
@@ -125,6 +132,7 @@ def __init__(
125
132
self .enable_gesture = False
126
133
self .enable_proximity = False
127
134
self .enable_color = False
135
+ self ._rotation = rotation
128
136
self .enable_proximity_interrupt = False
129
137
self .clear_interrupt ()
130
138
@@ -169,6 +177,19 @@ def _reset_counts(self):
169
177
"""Proximity interrupt enable flag. True if enabled,
170
178
False to disable"""
171
179
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
+
172
193
## GESTURE DETECTION
173
194
@property
174
195
def enable_gesture (self ):
@@ -182,6 +203,12 @@ def enable_gesture(self, enable_flag):
182
203
self ._gesture_mode = False
183
204
self ._gesture_enable = enable_flag
184
205
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
+
185
212
def gesture (self ): # pylint: disable-msg=too-many-branches
186
213
"""Returns gesture code if detected. =0 if no gesture detected
187
214
=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
263
290
if gesture_received or time .monotonic () - time_mark > 0.300 :
264
291
self ._reset_counts ()
265
292
break
266
-
293
+ if gesture_received != 0 :
294
+ if self ._rotation != 0 :
295
+ return self .rotated_gesture (gesture_received )
267
296
return gesture_received
268
297
269
298
@property
0 commit comments