Skip to content

Commit 7cb7f28

Browse files
authored
Merge pull request #2144 from adafruit/paper_airplane
Adding code for Glider
2 parents ce931a1 + 94efa06 commit 7cb7f28

File tree

1 file changed

+78
-0
lines changed
  • Glider_Paper_Airplane_Controller

1 file changed

+78
-0
lines changed
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
# SPDX-FileCopyrightText: 2022 Liz Clark for Adafruit Industries
2+
# SPDX-License-Identifier: MIT
3+
4+
import time
5+
import board
6+
import adafruit_lis3dh
7+
import simpleio
8+
import adafruit_ble
9+
from adafruit_ble.advertising import Advertisement
10+
from adafruit_ble.advertising.standard import ProvideServicesAdvertisement
11+
from adafruit_ble.services.standard.hid import HIDService
12+
from adafruit_hid.keyboard import Keyboard
13+
from adafruit_hid.keycode import Keycode
14+
15+
# I2C setup
16+
i2c = board.I2C()
17+
lis3dh = adafruit_lis3dh.LIS3DH_I2C(i2c)
18+
19+
# range of LIS3DH
20+
lis3dh.range = adafruit_lis3dh.RANGE_2_G
21+
22+
# BLE HID setup
23+
hid = HIDService()
24+
25+
advertisement = ProvideServicesAdvertisement(hid)
26+
advertisement.appearance = 961
27+
scan_response = Advertisement()
28+
scan_response.complete_name = "CircuitPython HID"
29+
30+
# BLE instance
31+
ble = adafruit_ble.BLERadio()
32+
33+
# keyboard HID setup
34+
keyboard = Keyboard(hid.devices)
35+
36+
# BLE advertisement
37+
if not ble.connected:
38+
print("advertising")
39+
ble.start_advertising(advertisement, scan_response)
40+
else:
41+
print("connected")
42+
print(ble.connections)
43+
44+
while True:
45+
while not ble.connected:
46+
pass
47+
# while BLE connected
48+
while ble.connected:
49+
# read LIS3DH
50+
x, y, z = [
51+
value / adafruit_lis3dh.STANDARD_GRAVITY for value in lis3dh.acceleration
52+
]
53+
# map Y coordinate of LIS3DH
54+
mapped_y = simpleio.map_range(y, -1.1, 1.1, 0, 3)
55+
# convert mapped value to an integer
56+
plane = int(mapped_y)
57+
58+
# if you're tilting down...
59+
if plane == 0:
60+
# send R, glider moves right
61+
keyboard.press(Keycode.R)
62+
# debug
63+
# print("right")
64+
# if there's no tilt...
65+
if plane == 1:
66+
# release all keys, send nothing to glider
67+
keyboard.release_all()
68+
# debug
69+
# print("none")
70+
# if you're tilting up...
71+
if plane == 2:
72+
# send L, glider moves left
73+
keyboard.press(Keycode.L)
74+
# debug
75+
# print("left")
76+
time.sleep(0.01)
77+
# if BLE disconnects, begin advertising again
78+
ble.start_advertising(advertisement)

0 commit comments

Comments
 (0)