bluefruitconnect_controlpad.py
1 # Basic structure example for using the BLE Connect Control Pad 2 # To use, start this program, and start the Adafruit Bluefruit LE Connect app. 3 # Connect, and then select Controller-> Control Pad. 4 5 from adafruit_ble import BLERadio 6 from adafruit_ble.advertising.standard import ProvideServicesAdvertisement 7 from adafruit_ble.services.nordic import UARTService 8 from adafruit_bluefruit_connect.packet import Packet 9 10 # Only the packet classes that are imported will be known to Packet. 11 from adafruit_bluefruit_connect.button_packet import ButtonPacket 12 13 ble = BLERadio() 14 uart_server = UARTService() 15 advertisement = ProvideServicesAdvertisement(uart_server) 16 17 while True: 18 print("WAITING...") 19 # Advertise when not connected. 20 ble.start_advertising(advertisement) 21 while not ble.connected: 22 pass 23 24 # Connected 25 ble.stop_advertising() 26 print("CONNECTED") 27 28 # Loop and read packets 29 while ble.connected: 30 31 # Keeping trying until a good packet is received 32 try: 33 packet = Packet.from_stream(uart_server) 34 except ValueError: 35 continue 36 37 # Only handle button packets 38 if isinstance(packet, ButtonPacket) and packet.pressed: 39 if packet.button == ButtonPacket.UP: 40 print("Button UP") 41 if packet.button == ButtonPacket.DOWN: 42 print("Button DOWN") 43 if packet.button == ButtonPacket.LEFT: 44 print("Button LEFT") 45 if packet.button == ButtonPacket.RIGHT: 46 print("Button RIGHT") 47 if packet.button == ButtonPacket.BUTTON_1: 48 print("Button 1") 49 if packet.button == ButtonPacket.BUTTON_2: 50 print("Button 2") 51 if packet.button == ButtonPacket.BUTTON_3: 52 print("Button 3") 53 if packet.button == ButtonPacket.BUTTON_4: 54 print("Button 4") 55 56 # Disconnected 57 print("DISCONNECTED")