|
| 1 | +# SPDX-FileCopyrightText: 2019 Dan Halbert for Adafruit Industries |
| 2 | +# |
| 3 | +# SPDX-License-Identifier: MIT |
| 4 | + |
| 5 | +# CircusPython! |
| 6 | +# For use with the Adafruit BlueFruit LE Connect app. |
| 7 | +# Works with CircuitPython 5.0.0-beta.0 and later running on an nRF52840 board. |
| 8 | + |
| 9 | +import random |
| 10 | +import time |
| 11 | + |
| 12 | +from adafruit_crickit import crickit |
| 13 | +from adafruit_ble import BLERadio |
| 14 | +from adafruit_ble.advertising.standard import ProvideServicesAdvertisement |
| 15 | +from adafruit_ble.services.nordic import UARTService |
| 16 | + |
| 17 | +from adafruit_bluefruit_connect.packet import Packet |
| 18 | +# Only the packet classes that are imported will be known to Packet. |
| 19 | +from adafruit_bluefruit_connect.color_packet import ColorPacket |
| 20 | +from adafruit_bluefruit_connect.button_packet import ButtonPacket |
| 21 | + |
| 22 | +# Initialize the NeoPixel ring to a fire color, not too bright. |
| 23 | +crickit.init_neopixel(24, brightness=0.1) |
| 24 | +color = (25, 12, 0) |
| 25 | + |
| 26 | +# Creates a sparkly "fire"-like effect. |
| 27 | +def sparkle(): |
| 28 | + crickit.neopixel[random.randrange(24)] = (0, 0, 0) |
| 29 | + crickit.neopixel[random.randrange(24)] = color |
| 30 | + crickit.neopixel[random.randrange(24)] = color |
| 31 | + crickit.neopixel[random.randrange(24)] = color |
| 32 | + |
| 33 | +ble = BLERadio() |
| 34 | +uart_service = UARTService() |
| 35 | +advertisement = ProvideServicesAdvertisement(uart_service) |
| 36 | + |
| 37 | +# Increase this to slow down movement of the servo arm. |
| 38 | +DELAY = 0.0 |
| 39 | + |
| 40 | +# Angle for Blinka before jumping through the ring. |
| 41 | +UP_ANGLE = 50 |
| 42 | + |
| 43 | +# Go to this angle when jumping through the ring. Adjust |
| 44 | +# slightly as necessary so you don't bump into the ring. |
| 45 | +DOWN_ANGLE = 2 |
| 46 | + |
| 47 | +crickit.servo_1.angle = UP_ANGLE |
| 48 | +angle = UP_ANGLE |
| 49 | + |
| 50 | +while True: |
| 51 | + ble.start_advertising(advertisement) |
| 52 | + while not ble.connected: |
| 53 | + sparkle() |
| 54 | + while ble.connected: |
| 55 | + sparkle() |
| 56 | + if uart_service.in_waiting: |
| 57 | + packet = Packet.from_stream(uart_service) |
| 58 | + if isinstance(packet, ColorPacket): |
| 59 | + # Change the fire color. |
| 60 | + color = packet.color |
| 61 | + elif isinstance(packet, ButtonPacket): |
| 62 | + if packet.pressed: |
| 63 | + if packet.button == '5' and angle != UP_ANGLE: |
| 64 | + # The Up button was pressed. |
| 65 | + for a in range(angle, UP_ANGLE+1, 1): |
| 66 | + crickit.servo_1.angle = a |
| 67 | + # Sparkle while moving. |
| 68 | + sparkle() |
| 69 | + time.sleep(DELAY) |
| 70 | + angle = UP_ANGLE |
| 71 | + elif packet.button == '6' and angle != DOWN_ANGLE: |
| 72 | + # The Down button was pressed. |
| 73 | + for a in range(angle, DOWN_ANGLE-1, -1): |
| 74 | + crickit.servo_1.angle = a |
| 75 | + # Sparkle while moving. |
| 76 | + sparkle() |
| 77 | + time.sleep(DELAY) |
| 78 | + angle = DOWN_ANGLE |
0 commit comments