Skip to content

Commit c4bc309

Browse files
authored
Add files via upload
1 parent 16e19f9 commit c4bc309

1 file changed

Lines changed: 124 additions & 0 deletions

File tree

  • Crickits/CircuitPython_BLE_Rover
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
# SPDX-FileCopyrightText: 2019 John Edgar Park for Adafruit Industries
2+
#
3+
# SPDX-License-Identifier: MIT
4+
5+
# CircuitPython BLE Rover
6+
# Use with the Adafruit BlueFruit LE Connect app
7+
# Works with CircuitPython 4.0.0-beta.1 and later
8+
# running on an nRF52840 Feather board and Crickit FeatherWing
9+
10+
import time
11+
12+
import board
13+
import digitalio
14+
15+
from adafruit_crickit import crickit
16+
17+
from adafruit_ble import BLERadio
18+
from adafruit_ble.advertising.standard import ProvideServicesAdvertisement
19+
from adafruit_ble.services.nordic import UARTService
20+
21+
from adafruit_bluefruit_connect.packet import Packet
22+
# Only the packet classes that are imported will be known to Packet.
23+
from adafruit_bluefruit_connect.button_packet import ButtonPacket
24+
from adafruit_bluefruit_connect.color_packet import ColorPacket
25+
26+
# Prep the status LEDs on the Feather
27+
blue_led = digitalio.DigitalInOut(board.BLUE_LED)
28+
red_led = digitalio.DigitalInOut(board.RED_LED)
29+
blue_led.direction = digitalio.Direction.OUTPUT
30+
red_led.direction = digitalio.Direction.OUTPUT
31+
32+
ble = BLERadio()
33+
uart_service = UARTService()
34+
advertisement = ProvideServicesAdvertisement(uart_service)
35+
36+
# motor setup
37+
motor_1 = crickit.dc_motor_1
38+
motor_2 = crickit.dc_motor_2
39+
40+
FWD = -1.0
41+
REV = 0.7
42+
43+
crickit.init_neopixel(24, brightness = 0.2) # create Crickit neopixel object
44+
RED = (200, 0, 0)
45+
GREEN = (0, 200, 0)
46+
BLUE = (0, 0, 200)
47+
PURPLE = (120, 0, 160)
48+
YELLOW = (100, 100, 0)
49+
AQUA = (0, 100, 100)
50+
color = PURPLE # current NeoPixel color
51+
prior_color = PURPLE # to store state of previous color when changing them
52+
crickit.neopixel.fill(color)
53+
54+
print("BLE Rover")
55+
print("Use Adafruit Bluefruit app to connect")
56+
while True:
57+
blue_led.value = False
58+
ble.start_advertising(advertisement)
59+
while not ble.connected:
60+
# Wait for a connection.
61+
pass
62+
blue_led.value = True # turn on blue LED when connected
63+
while ble.connected:
64+
if uart_service.in_waiting:
65+
# Packet is arriving.
66+
red_led.value = False # turn off red LED
67+
packet = Packet.from_stream(uart_service)
68+
if isinstance(packet, ColorPacket):
69+
# Change the color.
70+
color = packet.color
71+
crickit.neopixel.fill(color)
72+
73+
# do this when buttons are pressed
74+
if isinstance(packet, ButtonPacket) and packet.pressed:
75+
red_led.value = True # blink to show a packet has been received
76+
if packet.button == ButtonPacket.UP: # UP button pressed
77+
crickit.neopixel.fill(color)
78+
motor_1.throttle = FWD
79+
motor_2.throttle = FWD
80+
elif packet.button == ButtonPacket.DOWN: # DOWN button
81+
crickit.neopixel.fill(color)
82+
motor_1.throttle = REV
83+
motor_2.throttle = REV
84+
elif packet.button == ButtonPacket.RIGHT:
85+
prior_color = color
86+
color = YELLOW
87+
crickit.neopixel.fill(color)
88+
motor_1.throttle = FWD
89+
motor_2.throttle = FWD * 0.5
90+
elif packet.button == ButtonPacket.LEFT:
91+
prior_color = color
92+
color = YELLOW
93+
crickit.neopixel.fill(color)
94+
motor_1.throttle = FWD * 0.5
95+
motor_2.throttle = FWD
96+
elif packet.button == ButtonPacket.BUTTON_1:
97+
crickit.neopixel.fill(RED)
98+
motor_1.throttle = 0.0
99+
motor_2.throttle = 0.0
100+
time.sleep(0.5)
101+
crickit.neopixel.fill(color)
102+
elif packet.button == ButtonPacket.BUTTON_2:
103+
color = GREEN
104+
crickit.neopixel.fill(color)
105+
elif packet.button == ButtonPacket.BUTTON_3:
106+
color = BLUE
107+
crickit.neopixel.fill(color)
108+
elif packet.button == ButtonPacket.BUTTON_4:
109+
color = PURPLE
110+
crickit.neopixel.fill(color)
111+
# do this when some buttons are released
112+
elif isinstance(packet, ButtonPacket) and not packet.pressed:
113+
if packet.button == ButtonPacket.RIGHT:
114+
print("released right")
115+
color = prior_color
116+
crickit.neopixel.fill(color)
117+
motor_1.throttle = FWD
118+
motor_2.throttle = FWD
119+
if packet.button == ButtonPacket.LEFT:
120+
print("released left")
121+
color = prior_color
122+
crickit.neopixel.fill(color)
123+
motor_1.throttle = FWD
124+
motor_2.throttle = FWD

0 commit comments

Comments
 (0)