|
| 1 | +'''BLE Synth |
| 2 | +File for the Circuit Playground Bluefruit |
| 3 | +Amp Portion''' |
| 4 | +from adafruit_circuitplayground.bluefruit import cpb |
| 5 | +from adafruit_led_animation.animation import Comet, AnimationGroup,\ |
| 6 | + AnimationSequence |
| 7 | +import adafruit_led_animation.color as color |
| 8 | +from adafruit_ble import BLERadio |
| 9 | +from adafruit_ble.advertising.standard import ProvideServicesAdvertisement |
| 10 | +from adafruit_ble.services.nordic import UARTService |
| 11 | +from adafruit_bluefruit_connect.packet import Packet |
| 12 | +from adafruit_bluefruit_connect.color_packet import ColorPacket |
| 13 | +from adafruit_bluefruit_connect.button_packet import ButtonPacket |
| 14 | + |
| 15 | +# easily call for NeoPixels to be off |
| 16 | +off = (0, 0, 0) |
| 17 | +# state to debounce on CPB end |
| 18 | +tone = False |
| 19 | + |
| 20 | +# Setup for comet animation |
| 21 | +COMET_SPEED = 0.04 # Lower numbers increase the animation speed |
| 22 | +CPB_COMET_TAIL_LENGTH = 5 # The length of the comet on the Circuit Playground Bluefruit |
| 23 | +CPB_COMET_BOUNCE = False # Set to True to make the comet "bounce" the opposite direction on CPB |
| 24 | + |
| 25 | +animations = AnimationSequence( |
| 26 | + AnimationGroup( |
| 27 | + Comet(cpb.pixels, COMET_SPEED, off, tail_length=CPB_COMET_TAIL_LENGTH, |
| 28 | + bounce=CPB_COMET_BOUNCE))) |
| 29 | + |
| 30 | +# note frequencies |
| 31 | +C4 = 261.63 |
| 32 | +Csharp4 = 277.18 |
| 33 | +D4 = 293.66 |
| 34 | +Dsharp4 = 311.13 |
| 35 | +E4 = 329.63 |
| 36 | +F4 = 349.23 |
| 37 | +Fsharp4 = 369.99 |
| 38 | +G4 = 392 |
| 39 | +Gsharp4 = 415.3 |
| 40 | +A4 = 440 |
| 41 | +Asharp4 = 466.16 |
| 42 | +B4 = 493.88 |
| 43 | + |
| 44 | +# note array |
| 45 | +note = [C4, Csharp4, D4, Dsharp4, E4, F4, |
| 46 | + Fsharp4, G4, Gsharp4, A4, Asharp4, B4] |
| 47 | + |
| 48 | +# colors to recieve from color packet & for neopixels |
| 49 | +color_C = color.RED |
| 50 | +color_Csharp = color.ORANGE |
| 51 | +color_D = color.YELLOW |
| 52 | +color_Dsharp = color.GREEN |
| 53 | +color_E = color.TEAL |
| 54 | +color_F = color.CYAN |
| 55 | +color_Fsharp = color.BLUE |
| 56 | +color_G = color.PURPLE |
| 57 | +color_Gsharp = color.MAGENTA |
| 58 | +color_A = color.GOLD |
| 59 | +color_Asharp = color.PINK |
| 60 | +color_B = color.WHITE |
| 61 | + |
| 62 | +# color array |
| 63 | +color = [color_C, color_Csharp, color_D, color_Dsharp, color_E, |
| 64 | + color_F, color_Fsharp, color_G, color_Gsharp, color_A, |
| 65 | + color_Asharp, color_B] |
| 66 | + |
| 67 | +# Setup BLE connection |
| 68 | +ble = BLERadio() |
| 69 | +uart = UARTService() |
| 70 | +advertisement = ProvideServicesAdvertisement(uart) |
| 71 | + |
| 72 | +while True: |
| 73 | + # connect via BLE |
| 74 | + ble.start_advertising(advertisement) # Start advertising. |
| 75 | + was_connected = False |
| 76 | + while not was_connected or ble.connected: |
| 77 | + if ble.connected: # If BLE is connected... |
| 78 | + was_connected = True |
| 79 | + # start animations |
| 80 | + animations.animate() |
| 81 | + # look for packets |
| 82 | + if uart.in_waiting: |
| 83 | + try: |
| 84 | + packet = Packet.from_stream(uart) # Create the packet object. |
| 85 | + except ValueError: |
| 86 | + continue |
| 87 | + # if it's a color packet: |
| 88 | + if isinstance(packet, ColorPacket): |
| 89 | + for i in range(12): |
| 90 | + colors = color[i] |
| 91 | + notes = note[i] |
| 92 | + # if the packet matches one of our colors: |
| 93 | + if packet.color == colors and not tone: |
| 94 | + # animate with that color |
| 95 | + animations.color = colors |
| 96 | + # play matching note |
| 97 | + cpb.start_tone(notes) |
| 98 | + tone = True |
| 99 | + # if it's a button packet aka feather's button has been released: |
| 100 | + elif isinstance(packet, ButtonPacket) and packet.pressed: |
| 101 | + if packet.button == ButtonPacket.RIGHT and tone: |
| 102 | + tone = False |
| 103 | + # stop playing the note |
| 104 | + cpb.stop_tone() |
| 105 | + # turn off the neopixels but keep animation active |
| 106 | + animations.color = off |
0 commit comments