|
| 1 | +import time |
| 2 | +import board |
| 3 | +import neopixel |
| 4 | +from adafruit_ble import BLERadio |
| 5 | +from adafruit_ble.advertising.standard import ProvideServicesAdvertisement |
| 6 | +from adafruit_ble.services.nordic import UARTService |
| 7 | +from adafruit_bluefruit_connect.packet import Packet |
| 8 | +from adafruit_bluefruit_connect.button_packet import ButtonPacket |
| 9 | +from adafruit_bluefruit_connect.color_packet import ColorPacket |
| 10 | + |
| 11 | +# NeoPixel strip data pin |
| 12 | +pixel_pin = board.D5 |
| 13 | + |
| 14 | +# The number of NeoPixels |
| 15 | +num_pixels = 46 |
| 16 | + |
| 17 | +# Increase or decrease between 0 and 1 to increase or decrease the brightness of the LEDs |
| 18 | +brightness = 0.1 |
| 19 | + |
| 20 | +# The order of the pixel colors - RGB or GRB. Some NeoPixels have red and green reversed! |
| 21 | +ORDER = neopixel.GRB |
| 22 | + |
| 23 | +pixels = neopixel.NeoPixel(pixel_pin, num_pixels, brightness=brightness, auto_write=False, |
| 24 | + pixel_order=ORDER) |
| 25 | +# BLE Setup |
| 26 | +ble = BLERadio() |
| 27 | +uart_service = UARTService() |
| 28 | +advertisement = ProvideServicesAdvertisement(uart_service) |
| 29 | + |
| 30 | +#Wheel function for rainbows |
| 31 | +def wheel(pos): |
| 32 | + # Input a value 0 to 255 to get a color value. |
| 33 | + # The colours are a transition r - g - b - back to r. |
| 34 | + if pos < 0 or pos > 255: |
| 35 | + r = g = b = 0 |
| 36 | + elif pos < 85: |
| 37 | + r = int(pos * 3) |
| 38 | + g = int(255 - pos * 3) |
| 39 | + b = 0 |
| 40 | + elif pos < 170: |
| 41 | + pos -= 85 |
| 42 | + r = int(255 - pos * 3) |
| 43 | + g = 0 |
| 44 | + b = int(pos * 3) |
| 45 | + else: |
| 46 | + pos -= 170 |
| 47 | + r = 0 |
| 48 | + g = int(pos * 3) |
| 49 | + b = int(255 - pos * 3) |
| 50 | + return (r, g, b) if ORDER in (neopixel.RGB, neopixel.GRB) else (r, g, b, 0) |
| 51 | + |
| 52 | +#Rainbow Swirl Animation |
| 53 | +def rainbow_swirl(wait): |
| 54 | + for j in range(255): |
| 55 | + for i in range(num_pixels): |
| 56 | + pixel_index = (i * 256 // num_pixels) + j |
| 57 | + pixels[i] = wheel(pixel_index & 255) |
| 58 | + pixels.show() |
| 59 | + time.sleep(wait) |
| 60 | + |
| 61 | +#Rainbow Fill Animation |
| 62 | +def rainbow_fill(wait): |
| 63 | + for j in range(255): |
| 64 | + for i in range(num_pixels): |
| 65 | + pixel_index = int(i + j) |
| 66 | + pixels[i] = wheel(pixel_index & 255) |
| 67 | + pixels.show() |
| 68 | + time.sleep(wait) |
| 69 | + |
| 70 | +# List of colors |
| 71 | +RED = (255, 0, 0) |
| 72 | +ORANGE = (255, 50, 0) |
| 73 | +BLACK = (0, 0, 0) |
| 74 | +GREEN = (0, 255, 0) |
| 75 | +PURPLE = (100, 0, 255) |
| 76 | +YELLOW = (255,230, 0) |
| 77 | +BLUE = (0, 0, 255) |
| 78 | + |
| 79 | +while True: |
| 80 | + while ble.connected: |
| 81 | + if uart_service.in_waiting: |
| 82 | + if uart_service.in_waiting: |
| 83 | + packet = Packet.from_stream(uart_service) |
| 84 | + if isinstance(packet, ColorPacket): |
| 85 | + # Set all the pixels to one color and stay there. |
| 86 | + pixels.fill(packet.color) |
| 87 | + pixels.show() |
| 88 | + elif isinstance(packet, ButtonPacket): |
| 89 | + if packet.pressed: |
| 90 | + if packet.button == ButtonPacket.BUTTON_1: |
| 91 | + pixels.fill(BLUE) |
| 92 | + pixels.show() |
| 93 | + elif packet.button == ButtonPacket.BUTTON_2: |
| 94 | + pixels.fill(RED) |
| 95 | + pixels.show() |
| 96 | + elif packet.button == ButtonPacket.BUTTON_3: |
| 97 | + rainbow_swirl(0.01) |
| 98 | + pixels.show() |
| 99 | + elif packet.button == ButtonPacket.BUTTON_4: |
| 100 | + rainbow_fill(0.001) |
| 101 | + elif packet.button == ButtonPacket.DOWN: |
| 102 | + pixels.fill(BLACK) |
| 103 | + pixels.show() |
0 commit comments