From 4b9ad573c38e3b27e1da821792f1f1fe16d237aa Mon Sep 17 00:00:00 2001 From: John Edgar Park Date: Fri, 8 Nov 2019 08:57:10 -0800 Subject: [PATCH] first commit Turtle BLE rover --- .../turtle_ble_cpb_crickit_rover.py | 118 ++++++++++++++++++ 1 file changed, 118 insertions(+) create mode 100644 Turtle_BLE_CPB_Crickit_Rover/turtle_ble_cpb_crickit_rover.py diff --git a/Turtle_BLE_CPB_Crickit_Rover/turtle_ble_cpb_crickit_rover.py b/Turtle_BLE_CPB_Crickit_Rover/turtle_ble_cpb_crickit_rover.py new file mode 100644 index 000000000..79e55e7e7 --- /dev/null +++ b/Turtle_BLE_CPB_Crickit_Rover/turtle_ble_cpb_crickit_rover.py @@ -0,0 +1,118 @@ +# Circuit Playground Bluefruit Rover +# Use with the Adafruit BlueFruit LE Connect app +# Works with CircuitPython 4.0.0-beta.1 and later +# running on an nRF52840 CPB board and Crickit + +import time +import board +import digitalio +import neopixel +from adafruit_crickit import crickit +from adafruit_ble.uart_server import UARTServer + +from adafruit_bluefruit_connect.packet import Packet +# Only the packet classes that are imported will be known to Packet. +from adafruit_bluefruit_connect.button_packet import ButtonPacket +from adafruit_bluefruit_connect.color_packet import ColorPacket + +# Prep the status LED on the CPB +red_led = digitalio.DigitalInOut(board.D13) +red_led.direction = digitalio.Direction.OUTPUT + +uart_server = UARTServer() + +# motor setup +motor_1 = crickit.dc_motor_1 +motor_2 = crickit.dc_motor_2 + +FWD = 0.25 +REV = -0.25 + +neopixels = neopixel.NeoPixel(board.NEOPIXEL, 10, brightness=0.1) +RED = (200, 0, 0) +GREEN = (0, 200, 0) +BLUE = (0, 0, 200) +PURPLE = (120, 0, 160) +YELLOW = (100, 100, 0) +AQUA = (0, 100, 100) +BLACK = (0, 0, 0) +color = PURPLE # current NeoPixel color +neopixels.fill(color) + +print("BLE Turtle Rover") +print("Use Adafruit Bluefruit app to connect") +while True: + # blue_led.value = False + neopixels[0] = BLACK + neopixels.show() + uart_server.start_advertising() + while not uart_server.connected: + # Wait for a connection. + pass + # set a pixel blue when connected + neopixels[0] = BLUE + neopixels.show() + while uart_server.connected: + if uart_server.in_waiting: + # Packet is arriving. + red_led.value = False # turn off red LED + packet = Packet.from_stream(uart_server) + if isinstance(packet, ColorPacket): + # Change the color. + color = packet.color + neopixels.fill(color) + + # do this when buttons are pressed + if isinstance(packet, ButtonPacket) and packet.pressed: + red_led.value = True # blink to show packet has been received + if packet.button == ButtonPacket.UP: + neopixels.fill(color) + motor_1.throttle = FWD + motor_2.throttle = FWD + elif packet.button == ButtonPacket.DOWN: + neopixels.fill(color) + motor_1.throttle = REV + motor_2.throttle = REV + elif packet.button == ButtonPacket.RIGHT: + color = YELLOW + neopixels.fill(color) + motor_2.throttle = 0 + motor_1.throttle = FWD + elif packet.button == ButtonPacket.LEFT: + color = YELLOW + neopixels.fill(color) + motor_2.throttle = FWD + motor_1.throttle = 0 + elif packet.button == ButtonPacket.BUTTON_1: + neopixels.fill(RED) + motor_1.throttle = 0.0 + motor_2.throttle = 0.0 + time.sleep(0.5) + neopixels.fill(color) + elif packet.button == ButtonPacket.BUTTON_2: + color = GREEN + neopixels.fill(color) + elif packet.button == ButtonPacket.BUTTON_3: + color = BLUE + neopixels.fill(color) + elif packet.button == ButtonPacket.BUTTON_4: + color = PURPLE + neopixels.fill(color) + # do this when some buttons are released + elif isinstance(packet, ButtonPacket) and not packet.pressed: + if packet.button == ButtonPacket.UP: + neopixels.fill(RED) + motor_1.throttle = 0 + motor_2.throttle = 0 + if packet.button == ButtonPacket.DOWN: + neopixels.fill(RED) + motor_1.throttle = 0 + motor_2.throttle = 0 + if packet.button == ButtonPacket.RIGHT: + neopixels.fill(RED) + motor_1.throttle = 0 + motor_2.throttle = 0 + if packet.button == ButtonPacket.LEFT: + neopixels.fill(RED) + motor_1.throttle = 0 + motor_2.throttle = 0