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