|
| 1 | +# SPDX-FileCopyrightText: 2019 John Park for Adafruit Industries |
| 2 | +# |
| 3 | +# SPDX-License-Identifier: MIT |
| 4 | + |
| 5 | +# Circuit Playground Bluefruit Rover |
| 6 | +# Use with the Adafruit BlueFruit LE Connect app |
| 7 | +# Works with CircuitPython 5.0.0-beta.0 and later |
| 8 | +# running on an nRF52840 CPB board and Crickit |
| 9 | + |
| 10 | +import time |
| 11 | +import board |
| 12 | +import digitalio |
| 13 | +import neopixel |
| 14 | +from adafruit_crickit import crickit |
| 15 | + |
| 16 | +from adafruit_ble import BLERadio |
| 17 | +from adafruit_ble.advertising.standard import ProvideServicesAdvertisement |
| 18 | +from adafruit_ble.services.nordic import UARTService |
| 19 | + |
| 20 | +from adafruit_bluefruit_connect.packet import Packet |
| 21 | +# Only the packet classes that are imported will be known to Packet. |
| 22 | +from adafruit_bluefruit_connect.button_packet import ButtonPacket |
| 23 | +from adafruit_bluefruit_connect.color_packet import ColorPacket |
| 24 | + |
| 25 | +# Prep the status LED on the CPB |
| 26 | +red_led = digitalio.DigitalInOut(board.D13) |
| 27 | +red_led.direction = digitalio.Direction.OUTPUT |
| 28 | + |
| 29 | +ble = BLERadio() |
| 30 | +uart_service = UARTService() |
| 31 | +advertisement = ProvideServicesAdvertisement(uart_service) |
| 32 | + |
| 33 | +# motor setup |
| 34 | +motor_1 = crickit.dc_motor_1 |
| 35 | +motor_2 = crickit.dc_motor_2 |
| 36 | + |
| 37 | +FWD = 0.25 |
| 38 | +REV = -0.25 |
| 39 | + |
| 40 | +neopixels = neopixel.NeoPixel(board.NEOPIXEL, 10, brightness=0.1) |
| 41 | +RED = (200, 0, 0) |
| 42 | +GREEN = (0, 200, 0) |
| 43 | +BLUE = (0, 0, 200) |
| 44 | +PURPLE = (120, 0, 160) |
| 45 | +YELLOW = (100, 100, 0) |
| 46 | +AQUA = (0, 100, 100) |
| 47 | +BLACK = (0, 0, 0) |
| 48 | +color = PURPLE # current NeoPixel color |
| 49 | +neopixels.fill(color) |
| 50 | + |
| 51 | +print("BLE Turtle Rover") |
| 52 | +print("Use Adafruit Bluefruit app to connect") |
| 53 | +while True: |
| 54 | + neopixels[0] = BLACK |
| 55 | + neopixels.show() |
| 56 | + ble.start_advertising(advertisement) |
| 57 | + while not ble.connected: |
| 58 | + # Wait for a connection. |
| 59 | + pass |
| 60 | + # set a pixel blue when connected |
| 61 | + neopixels[0] = BLUE |
| 62 | + neopixels.show() |
| 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 | + neopixels.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 packet has been received |
| 76 | + if packet.button == ButtonPacket.UP: |
| 77 | + neopixels.fill(color) |
| 78 | + motor_1.throttle = FWD |
| 79 | + motor_2.throttle = FWD |
| 80 | + elif packet.button == ButtonPacket.DOWN: |
| 81 | + neopixels.fill(color) |
| 82 | + motor_1.throttle = REV |
| 83 | + motor_2.throttle = REV |
| 84 | + elif packet.button == ButtonPacket.RIGHT: |
| 85 | + color = YELLOW |
| 86 | + neopixels.fill(color) |
| 87 | + motor_2.throttle = 0 |
| 88 | + motor_1.throttle = FWD |
| 89 | + elif packet.button == ButtonPacket.LEFT: |
| 90 | + color = YELLOW |
| 91 | + neopixels.fill(color) |
| 92 | + motor_2.throttle = FWD |
| 93 | + motor_1.throttle = 0 |
| 94 | + elif packet.button == ButtonPacket.BUTTON_1: |
| 95 | + neopixels.fill(RED) |
| 96 | + motor_1.throttle = 0.0 |
| 97 | + motor_2.throttle = 0.0 |
| 98 | + time.sleep(0.5) |
| 99 | + neopixels.fill(color) |
| 100 | + elif packet.button == ButtonPacket.BUTTON_2: |
| 101 | + color = GREEN |
| 102 | + neopixels.fill(color) |
| 103 | + elif packet.button == ButtonPacket.BUTTON_3: |
| 104 | + color = BLUE |
| 105 | + neopixels.fill(color) |
| 106 | + elif packet.button == ButtonPacket.BUTTON_4: |
| 107 | + color = PURPLE |
| 108 | + neopixels.fill(color) |
| 109 | + # do this when some buttons are released |
| 110 | + elif isinstance(packet, ButtonPacket) and not packet.pressed: |
| 111 | + if packet.button == ButtonPacket.UP: |
| 112 | + neopixels.fill(RED) |
| 113 | + motor_1.throttle = 0 |
| 114 | + motor_2.throttle = 0 |
| 115 | + if packet.button == ButtonPacket.DOWN: |
| 116 | + neopixels.fill(RED) |
| 117 | + motor_1.throttle = 0 |
| 118 | + motor_2.throttle = 0 |
| 119 | + if packet.button == ButtonPacket.RIGHT: |
| 120 | + neopixels.fill(RED) |
| 121 | + motor_1.throttle = 0 |
| 122 | + motor_2.throttle = 0 |
| 123 | + if packet.button == ButtonPacket.LEFT: |
| 124 | + neopixels.fill(RED) |
| 125 | + motor_1.throttle = 0 |
| 126 | + motor_2.throttle = 0 |
0 commit comments