Skip to content

Commit 6d19471

Browse files
authored
Merge pull request #1823 from adafruit/updated_ble_gesture_mouse
Updating code for BLE gesture mouse
2 parents 737b8fb + 580363b commit 6d19471

1 file changed

Lines changed: 21 additions & 52 deletions

File tree

BLE_Gesture_Mouse/code.py

Lines changed: 21 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import time
22
import board
33
import digitalio
4+
import simpleio
45
import adafruit_lsm6ds.lsm6ds33
56
import adafruit_apds9960.apds9960
67
from adafruit_hid.mouse import Mouse
@@ -22,10 +23,6 @@
2223
# enable proximity sensor
2324
apds9960.enable_proximity = True
2425

25-
# x and y axis setup
26-
x_axis = 0
27-
y_axis = 0
28-
2926
# setup for onboard button
3027
click = digitalio.DigitalInOut(board.SWITCH)
3128
click.direction = digitalio.Direction.INPUT
@@ -73,63 +70,35 @@ def steps(axis):
7370
pass
7471
while ble.connected:
7572
# sets x and y values for accelerometer x and y values
76-
x = lsm6ds33.acceleration[1]
77-
y = lsm6ds33.acceleration[0]
73+
# x and y are swapped for orientation of feather
74+
y, x, z = lsm6ds33.acceleration
75+
76+
# map range of horizontal movement to mouse x movement
77+
horizontal_mov = simpleio.map_range(steps(x), 1.0, 20.0, -15.0, 15.0)
78+
# map range of vertical movement to mouse y movement
79+
vertical_mov = simpleio.map_range(steps(y), 20.0, 1.0, -15.0, 15.0)
80+
# map range of mouse y movement to scrolling
81+
scroll_dir = simpleio.map_range(vertical_mov, -15.0, 15.0, 3.0, -3.0)
7882

7983
# if onboard button is pressed, sends left mouse click
80-
if click.value is False:
84+
if not click.value:
8185
mouse.click(Mouse.LEFT_BUTTON)
8286
time.sleep(0.2)
87+
# if the proximity sensor is covered
88+
# scroll the mouse
89+
if apds9960.proximity > distance:
90+
mouse.move(wheel=int(scroll_dir))
91+
# otherwise move mouse cursor in x and y directions
92+
else:
93+
mouse.move(x=int(horizontal_mov))
94+
mouse.move(y=int(vertical_mov))
95+
8396
# debugging print for x and y values
8497
# time.monotonic() is used so that the
8598
# code is not delayed with time.sleep
8699
if (clock + 2) < time.monotonic():
87100
print("x", steps(x))
88101
print("y", steps(y))
89102
clock = time.monotonic()
90-
# mouse movement left and right
91-
if steps(x) > 11.0:
92-
mouse.move(x=1)
93-
if steps(x) < 9.0:
94-
mouse.move(x=-1)
95-
96-
if steps(x) > 19.0:
97-
mouse.move(x=8)
98-
if steps(x) < 1.0:
99-
mouse.move(x=-8)
100-
# mouse movement up and down
101-
# and mouse scrolling using
102-
# proximity sensor
103-
if steps(y) > 11.0:
104-
if apds9960.proximity > distance:
105-
# scroll down
106-
mouse.move(wheel=1)
107-
time.sleep(0.1)
108-
else:
109-
# move down
110-
mouse.move(y=-1)
111-
if steps(y) < 9.0:
112-
if apds9960.proximity > distance:
113-
# scroll up
114-
mouse.move(wheel=-1)
115-
time.sleep(0.1)
116-
else:
117-
# move up
118-
mouse.move(y=1)
119-
120-
if steps(y) > 15.0:
121-
if apds9960.proximity > distance:
122-
# scroll down
123-
mouse.move(wheel=3)
124-
else:
125-
# move down
126-
mouse.move(y=-8)
127-
if steps(y) < 1.0:
128-
if apds9960.proximity > distance:
129-
# scroll up
130-
mouse.move(wheel=-3)
131-
else:
132-
# move up
133-
mouse.move(y=8)
134-
#print(apds9960.proximity)
103+
135104
ble.start_advertising(advertisement)

0 commit comments

Comments
 (0)