|
| 1 | +'''BLE Synth |
| 2 | +File for the Feather nFR52840 |
| 3 | +Keyboard Portion''' |
| 4 | +import time |
| 5 | +import board |
| 6 | +import digitalio |
| 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.color_packet import ColorPacket |
| 12 | +from adafruit_bluefruit_connect.button_packet import ButtonPacket |
| 13 | + |
| 14 | +# setup for LED to indicate BLE connection |
| 15 | +blue_led = digitalio.DigitalInOut(board.BLUE_LED) |
| 16 | +blue_led.direction = digitalio.Direction.OUTPUT |
| 17 | + |
| 18 | +# setting up the buttons |
| 19 | +switch_pins = [board.D5, board.D6, board.D9, board.D10, |
| 20 | + board.D11, board.D12, board.D13, board.A0, board.A1, board.A2, |
| 21 | + board.A3, board.A4] |
| 22 | +switch_array = [] |
| 23 | + |
| 24 | +# creating the button array |
| 25 | +for pin in switch_pins: |
| 26 | + switch_pin = digitalio.DigitalInOut(pin) |
| 27 | + switch_pin.direction = digitalio.Direction.INPUT |
| 28 | + switch_pin.pull = digitalio.Pull.UP |
| 29 | + switch_array.append(switch_pin) |
| 30 | + |
| 31 | +# states for button debouncing |
| 32 | +switch1_pressed = False |
| 33 | +switch2_pressed = False |
| 34 | +switch3_pressed = False |
| 35 | +switch4_pressed = False |
| 36 | +switch5_pressed = False |
| 37 | +switch6_pressed = False |
| 38 | +switch7_pressed = False |
| 39 | +switch8_pressed = False |
| 40 | +switch9_pressed = False |
| 41 | +switch10_pressed = False |
| 42 | +switch11_pressed = False |
| 43 | +switch12_pressed = False |
| 44 | +switches_pressed = [switch1_pressed, switch2_pressed, switch3_pressed, switch4_pressed, |
| 45 | + switch5_pressed, switch6_pressed, switch7_pressed, switch8_pressed, |
| 46 | + switch9_pressed, switch10_pressed, switch11_pressed, switch12_pressed] |
| 47 | + |
| 48 | +# colors from Animation library to send as color packets |
| 49 | +# named for notes |
| 50 | +color_C = color.RED |
| 51 | +color_Csharp = color.ORANGE |
| 52 | +color_D = color.YELLOW |
| 53 | +color_Dsharp = color.GREEN |
| 54 | +color_E = color.TEAL |
| 55 | +color_F = color.CYAN |
| 56 | +color_Fsharp = color.BLUE |
| 57 | +color_G = color.PURPLE |
| 58 | +color_Gsharp = color.MAGENTA |
| 59 | +color_A = color.GOLD |
| 60 | +color_Asharp = color.PINK |
| 61 | +color_B = color.WHITE |
| 62 | + |
| 63 | +# array for colors |
| 64 | +color = [color_C, color_Csharp, color_D, color_Dsharp, color_E, |
| 65 | + color_F, color_Fsharp, color_G, color_Gsharp, color_A, |
| 66 | + color_Asharp, color_B] |
| 67 | + |
| 68 | +# BLE send_packet function |
| 69 | +def send_packet(uart_connection_name, packet): |
| 70 | + """Returns False if no longer connected.""" |
| 71 | + try: |
| 72 | + uart_connection_name[UARTService].write(packet.to_bytes()) |
| 73 | + except: # pylint: disable=bare-except |
| 74 | + try: |
| 75 | + uart_connection_name.disconnect() |
| 76 | + except: # pylint: disable=bare-except |
| 77 | + pass |
| 78 | + return False |
| 79 | + return True |
| 80 | + |
| 81 | +ble = BLERadio() |
| 82 | + |
| 83 | +uart_connection = None |
| 84 | + |
| 85 | +if ble.connected: |
| 86 | + for connection in ble.connections: |
| 87 | + if UARTService in connection: |
| 88 | + uart_connection = connection |
| 89 | + break |
| 90 | + |
| 91 | +while True: |
| 92 | + blue_led.value = False |
| 93 | + # BLE connection |
| 94 | + if not uart_connection or not uart_connection.connected: # If not connected... |
| 95 | + print("Scanning...") |
| 96 | + for adv in ble.start_scan(ProvideServicesAdvertisement, timeout=5): # Scan... |
| 97 | + if UARTService in adv.services: # If UARTService found... |
| 98 | + print("Found a UARTService advertisement.") |
| 99 | + blue_led.value = True # LED turns on when connected |
| 100 | + uart_connection = ble.connect(adv) # Create a UART connection... |
| 101 | + break |
| 102 | + ble.stop_scan() # And stop scanning. |
| 103 | + # while connected.. |
| 104 | + while uart_connection and uart_connection.connected: |
| 105 | + # iterate through buttons and colors |
| 106 | + for switch_pin in switch_array: |
| 107 | + i = switch_array.index(switch_pin) |
| 108 | + switches_pressed_state = switches_pressed[i] |
| 109 | + colors = color[i] |
| 110 | + # if the button is released |
| 111 | + # worked best if placed before the button press portion |
| 112 | + if switch_pin.value and switches_pressed_state: |
| 113 | + print("button off") |
| 114 | + # send button packet to stop tone & color (happens on CPB) |
| 115 | + if not send_packet(uart_connection, |
| 116 | + ButtonPacket(ButtonPacket.RIGHT, pressed=True)): |
| 117 | + uart_connection = None |
| 118 | + continue |
| 119 | + switches_pressed[i] = False # Set to False. |
| 120 | + # time delay for BLE, otherwise issues can arrise |
| 121 | + time.sleep(0.05) |
| 122 | + # if button is pressed: |
| 123 | + if not switch_pin.value and not switches_pressed_state: # If button A pressed... |
| 124 | + # send color packet |
| 125 | + if not send_packet(uart_connection, ColorPacket(colors)): |
| 126 | + uart_connection = None |
| 127 | + continue |
| 128 | + switches_pressed[i] = True # Set to True. |
| 129 | + time.sleep(0.05) # Debounce. |
0 commit comments