|
| 1 | +""" This example solicits that apple devices that provide notifications connect to it, initiates |
| 2 | +pairing, then allows the user to use a CLUE board as a media remote through both the buttons |
| 3 | +and capacitive touch pads. |
| 4 | +
|
| 5 | +This example requires the following additional libraries: |
| 6 | +adafruit_ble |
| 7 | +adafruit_ble_apple_media |
| 8 | +displayio |
| 9 | +adafruit_bitmap_font |
| 10 | +adafruit_display_shapes |
| 11 | +adafruit_display_text |
| 12 | +""" |
| 13 | + |
| 14 | +import time |
| 15 | +import adafruit_ble |
| 16 | +from adafruit_ble.advertising.standard import SolicitServicesAdvertisement |
| 17 | +from adafruit_ble_apple_media import AppleMediaService |
| 18 | +from adafruit_ble_apple_media import UnsupportedCommand |
| 19 | +from adafruit_clue import clue |
| 20 | +import displayio |
| 21 | +from adafruit_bitmap_font import bitmap_font |
| 22 | +from adafruit_display_shapes.rect import Rect |
| 23 | +from adafruit_display_text import label |
| 24 | +import board |
| 25 | + |
| 26 | +# PyLint can't find BLERadio for some reason so special case it here. |
| 27 | +radio = adafruit_ble.BLERadio() # pylint: disable=no-member |
| 28 | +a = SolicitServicesAdvertisement() |
| 29 | +a.solicited_services.append(AppleMediaService) |
| 30 | +radio.start_advertising(a) |
| 31 | + |
| 32 | +while not radio.connected: |
| 33 | + pass |
| 34 | + |
| 35 | +print("connected") |
| 36 | + |
| 37 | +known_notifications = set() |
| 38 | + |
| 39 | +i = 0 |
| 40 | +if radio.connected: |
| 41 | + for connection in radio.connections: |
| 42 | + if not connection.paired: |
| 43 | + connection.pair() |
| 44 | + print("paired") |
| 45 | + |
| 46 | + ams = connection[AppleMediaService] |
| 47 | + |
| 48 | + |
| 49 | +#arial12 = bitmap_font.load_font("/fonts/Arial-12.bdf") |
| 50 | +arial16 = bitmap_font.load_font("/fonts/Arial-16.bdf") |
| 51 | +#arial24 = bitmap_font.load_font("/fonts/Arial-Bold-24.bdf") |
| 52 | + |
| 53 | +display = board.DISPLAY |
| 54 | + |
| 55 | +group = displayio.Group(max_size=25) |
| 56 | + |
| 57 | +title = label.Label(font=arial16, x=15, y=25, text='_', color=0xFFFFFF, max_glyphs=30) |
| 58 | +group.append(title) |
| 59 | + |
| 60 | +artist = label.Label(font=arial16, x=15, y=50, text='_', color=0xFFFFFF, max_glyphs=30) |
| 61 | +group.append(artist) |
| 62 | + |
| 63 | +album = label.Label(font=arial16, x=15, y=75, text='_', color=0xFFFFFF, max_glyphs=30) |
| 64 | +group.append(album) |
| 65 | + |
| 66 | +player = label.Label(font=arial16, x=15, y=100, text='_', color=0xFFFFFF, max_glyphs=30) |
| 67 | +group.append(player) |
| 68 | + |
| 69 | +volume = Rect(15, 170, 210, 20, fill=0x0, outline=0xFFFFFF) |
| 70 | +group.append(volume) |
| 71 | + |
| 72 | +track_time = Rect(15, 210, 210, 20, fill=0x0, outline=0xFFFFFF) |
| 73 | +#time = label.Label(font=arial16, x=15, y=215, text='Time', color=0xFFFFFF) |
| 74 | +group.append(track_time) |
| 75 | + |
| 76 | +time_inner = Rect(15, 210, 1, 20, fill=0xFFFFFF, outline=0xFFFFFF) |
| 77 | +group.append(time_inner) |
| 78 | + |
| 79 | +volume_inner = Rect(15, 170, 1, 20, fill=0xFFFFFF, outline=0xFFFFFF) |
| 80 | +group.append(volume_inner) |
| 81 | + |
| 82 | +display.show(group) |
| 83 | +time.sleep(0.01) |
| 84 | + |
| 85 | +width1 = 1 |
| 86 | + |
| 87 | +ref_time = time.time() |
| 88 | +ela_time = ams.elapsed_time |
| 89 | +while radio.connected: |
| 90 | + try: |
| 91 | + if ams.elapsed_time != ela_time: |
| 92 | + ela_time = ams.elapsed_time |
| 93 | + ref_time = time.time() |
| 94 | + title.text = ams.title |
| 95 | + artist.text = ams.artist |
| 96 | + album.text = ams.album |
| 97 | + player.text = ams.player_name |
| 98 | + if ams.volume is not None: |
| 99 | + width = int(16*13.125*float(ams.volume)) |
| 100 | + if not width: |
| 101 | + width = 1 |
| 102 | + if ams.duration and ams.playing: |
| 103 | + width1 = int(210*((time.time() - ref_time + ela_time)/float(ams.duration))) |
| 104 | + if not width1: |
| 105 | + width1 = 1 |
| 106 | + elif not ams.duration: |
| 107 | + width1 = 1 |
| 108 | + |
| 109 | + time_inner = Rect(15, 210, width1, 20, fill=0xFFFFFF)#, outline=0xFFFFFF) |
| 110 | + group[-2] = time_inner |
| 111 | + volume_inner = Rect(15, 170, width, 20, fill=0xFFFFFF)#, outline=0xFFFFFF) |
| 112 | + group[-1] = volume_inner |
| 113 | + |
| 114 | + # Capacitive touch pad marked 0 goes to the previous track |
| 115 | + if clue.touch_0: |
| 116 | + ams.previous_track() |
| 117 | + time.sleep(0.25) |
| 118 | + |
| 119 | + # Capacitive touch pad marked 1 toggles pause/play |
| 120 | + if clue.touch_1: |
| 121 | + ams.toggle_play_pause() |
| 122 | + time.sleep(0.25) |
| 123 | + |
| 124 | + # Capacitive touch pad marked 2 advances to the next track |
| 125 | + if clue.touch_2: |
| 126 | + ams.next_track() |
| 127 | + time.sleep(0.25) |
| 128 | + |
| 129 | + # If button B (on the right) is pressed, it increases the volume |
| 130 | + if 'B' in clue.were_pressed: |
| 131 | + ams.volume_up() |
| 132 | + a = clue.were_pressed |
| 133 | + time.sleep(0.35) |
| 134 | + while 'B' in clue.were_pressed: |
| 135 | + ams.volume_up() |
| 136 | + time.sleep(0.07) |
| 137 | + |
| 138 | + # If button A (on the left) is pressed, the volume decreases |
| 139 | + if 'A' in clue.were_pressed: |
| 140 | + ams.volume_down() |
| 141 | + a = clue.were_pressed |
| 142 | + time.sleep(0.35) |
| 143 | + while 'A' in clue.were_pressed: |
| 144 | + ams.volume_down() |
| 145 | + time.sleep(0.07) |
| 146 | + time.sleep(0.01) |
| 147 | + except (RuntimeError, UnsupportedCommand, AttributeError): |
| 148 | + time.sleep(0.01) |
| 149 | + continue |
| 150 | + |
| 151 | +print("disconnected") |
0 commit comments