|
| 1 | +""" |
| 2 | +This example solicits that apple devices that provide notifications connect to it, initiates |
| 3 | +pairing, then allows the user to use a CLUE board as a media remote through both the buttons |
| 4 | +and capacitive touch pads. |
| 5 | +
|
| 6 | +This example requires the following additional libraries: |
| 7 | +adafruit_ble |
| 8 | +adafruit_ble_apple_media |
| 9 | +""" |
| 10 | + |
| 11 | +import time |
| 12 | +import adafruit_ble |
| 13 | +from adafruit_ble.advertising.standard import SolicitServicesAdvertisement |
| 14 | +from adafruit_ble_apple_media import AppleMediaService |
| 15 | +from adafruit_clue import clue |
| 16 | + |
| 17 | +# PyLint can't find BLERadio for some reason so special case it here. |
| 18 | +radio = adafruit_ble.BLERadio() # pylint: disable=no-member |
| 19 | +a = SolicitServicesAdvertisement() |
| 20 | +a.solicited_services.append(AppleMediaService) |
| 21 | +radio.start_advertising(a) |
| 22 | + |
| 23 | +while not radio.connected: |
| 24 | + pass |
| 25 | + |
| 26 | +print("connected") |
| 27 | + |
| 28 | +known_notifications = set() |
| 29 | + |
| 30 | +i = 0 |
| 31 | +if radio.connected: |
| 32 | + for connection in radio.connections: |
| 33 | + if not connection.paired: |
| 34 | + connection.pair() |
| 35 | + print("paired") |
| 36 | + |
| 37 | + ams = connection[AppleMediaService] |
| 38 | + |
| 39 | +while radio.connected: |
| 40 | + if ams.playing: |
| 41 | + play_str = "Playing" |
| 42 | + else: |
| 43 | + play_str = "Paused" |
| 44 | + print("{} - {}, {}".format(ams.title, ams.artist, play_str)) |
| 45 | + |
| 46 | + # Capacitive touch pad marked 0 goes to the previous track |
| 47 | + if clue.touch_0: |
| 48 | + ams.previous_track() |
| 49 | + time.sleep(0.25) |
| 50 | + |
| 51 | + # Capacitive touch pad marked 1 toggles pause/play |
| 52 | + if clue.touch_1: |
| 53 | + ams.toggle_play_pause() |
| 54 | + time.sleep(0.25) |
| 55 | + |
| 56 | + # Capacitive touch pad marked 2 advances to the next track |
| 57 | + if clue.touch_2: |
| 58 | + ams.next_track() |
| 59 | + time.sleep(0.25) |
| 60 | + |
| 61 | + # If button B (on the right) is pressed, it increases the volume |
| 62 | + if 'B' in clue.were_pressed: |
| 63 | + ams.volume_up() |
| 64 | + time.sleep(0.30) |
| 65 | + while 'B' in clue.were_pressed: |
| 66 | + ams.volume_up() |
| 67 | + time.sleep(0.07) |
| 68 | + |
| 69 | + # If button A (on the left) is pressed, the volume decreases |
| 70 | + if 'A' in clue.were_pressed: |
| 71 | + ams.volume_down() |
| 72 | + time.sleep(0.30) |
| 73 | + while 'A' in clue.were_pressed: |
| 74 | + ams.volume_down() |
| 75 | + time.sleep(0.07) |
| 76 | + |
| 77 | +print("disconnected") |
0 commit comments