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