|
| 1 | +""" |
| 2 | +This example solicits that apple devices that provide notifications connect to it, initiates |
| 3 | +pairing, prints existing notifications and then prints any new ones as they arrive. |
| 4 | +""" |
| 5 | + |
| 6 | +import time |
| 7 | +import displayio |
| 8 | +import terminalio |
| 9 | +from adafruit_gizmo import tft_gizmo |
| 10 | +from adafruit_display_text.label import Label |
| 11 | +from adafruit_display_shapes.rect import Rect |
| 12 | +from adafruit_bitmap_font import bitmap_font |
| 13 | +import adafruit_ble |
| 14 | +from adafruit_ble.advertising.standard import SolicitServicesAdvertisement |
| 15 | +from adafruit_ble_apple_media import AppleMediaService |
| 16 | +from adafruit_ble_apple_media import UnsupportedCommand |
| 17 | +from adafruit_circuitplayground import cp |
| 18 | + |
| 19 | +BACKGROUND_COLOR = 0x49523b # Gray |
| 20 | +TEXT_COLOR = 0xFF0000 # Red |
| 21 | +BORDER_COLOR = 0xAAAAAA # Light Gray |
| 22 | +STATUS_COLOR = BORDER_COLOR |
| 23 | + |
| 24 | +# PyLint can't find BLERadio for some reason so special case it here. |
| 25 | +radio = adafruit_ble.BLERadio() # pylint: disable=no-member |
| 26 | +radio.name = "Now Playing Gizmo" |
| 27 | +a = SolicitServicesAdvertisement() |
| 28 | +a.solicited_services.append(AppleMediaService) |
| 29 | +radio.start_advertising(a) |
| 30 | + |
| 31 | +def wrap_in_tilegrid(open_file): |
| 32 | + odb = displayio.OnDiskBitmap(open_file) |
| 33 | + return displayio.TileGrid(odb, pixel_shader=displayio.ColorConverter()) |
| 34 | + |
| 35 | +def make_background(width, height, color): |
| 36 | + color_bitmap = displayio.Bitmap(width, height, 1) |
| 37 | + color_palette = displayio.Palette(1) |
| 38 | + color_palette[0] = color |
| 39 | + |
| 40 | + return displayio.TileGrid(color_bitmap, |
| 41 | + pixel_shader=color_palette, |
| 42 | + x=0, y=0) |
| 43 | + |
| 44 | +def load_font(fontname, text): |
| 45 | + font = bitmap_font.load_font(fontname) |
| 46 | + font.load_glyphs(text.encode('utf-8')) |
| 47 | + return font |
| 48 | + |
| 49 | +def make_label(text, x, y, color, max_glyphs=30, font=terminalio.FONT): |
| 50 | + if isinstance(font, str): |
| 51 | + font = load_font(font, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz.,?()") |
| 52 | + text_area = Label(font, text=text, color=color, max_glyphs=max_glyphs) |
| 53 | + text_area.x = x |
| 54 | + text_area.y = y |
| 55 | + return text_area |
| 56 | + |
| 57 | +def set_label(label, value, max_length): |
| 58 | + text = "{}".format(value) |
| 59 | + if len(text) > max_length: |
| 60 | + text = text[:max_length-3] + "..." |
| 61 | + label.text = text |
| 62 | + |
| 63 | +def set_status(label, action_text, player): |
| 64 | + label.text = "{} on {}".format(action_text, player) |
| 65 | + _, _, label_width, _ = label.bounding_box |
| 66 | + label.x = display.width - 10 - label_width |
| 67 | + |
| 68 | +display = tft_gizmo.TFT_Gizmo() |
| 69 | +group = displayio.Group(max_size=20) |
| 70 | +display.show(group) |
| 71 | + |
| 72 | +while True: |
| 73 | + if not radio.connected: |
| 74 | + group.append(wrap_in_tilegrid(open("/graphic_tfts_ams_connect.bmp", "rb"))) |
| 75 | + |
| 76 | + while not radio.connected: |
| 77 | + pass |
| 78 | + |
| 79 | + group.pop() |
| 80 | + print("connected") |
| 81 | + |
| 82 | + known_notifications = set() |
| 83 | + |
| 84 | + # Draw the text fields |
| 85 | + print("Loading Font Glyphs...") |
| 86 | + group.append(wrap_in_tilegrid(open("/graphic_tfts_ams_loading.bmp", "rb"))) |
| 87 | + title_label = make_label("None", 12, 30, TEXT_COLOR, font="/fonts/Arial-Bold-18.bdf") |
| 88 | + artist_label = make_label("None", 12, 70, TEXT_COLOR, font="/fonts/Arial-16.bdf") |
| 89 | + album_label = make_label("None", 12, 184, TEXT_COLOR, font="/fonts/Arial-16.bdf") |
| 90 | + status_label = make_label("None", 80, 220, STATUS_COLOR, font="/fonts/Arial-16.bdf") |
| 91 | + group.pop() |
| 92 | + group.append(make_background(240, 240, BACKGROUND_COLOR)) |
| 93 | + border = Rect(4, 4, 232, 200, outline=BORDER_COLOR, stroke=2) |
| 94 | + group.append(title_label) |
| 95 | + group.append(artist_label) |
| 96 | + group.append(album_label) |
| 97 | + group.append(status_label) |
| 98 | + group.append(border) |
| 99 | + |
| 100 | + while radio.connected: |
| 101 | + try: |
| 102 | + for connection in radio.connections: |
| 103 | + if not connection.paired: |
| 104 | + connection.pair() |
| 105 | + print("paired") |
| 106 | + |
| 107 | + ams = connection[AppleMediaService] |
| 108 | + set_label(title_label, ams.title, 18) |
| 109 | + set_label(album_label, ams.album, 21) |
| 110 | + set_label(artist_label, ams.artist, 21) |
| 111 | + action = "?" |
| 112 | + if ams.playing: |
| 113 | + action = "Playing" |
| 114 | + elif ams.paused: |
| 115 | + action = "Paused" |
| 116 | + set_status(status_label, action, ams.player_name) |
| 117 | + if cp.button_a: |
| 118 | + ams.toggle_play_pause() |
| 119 | + time.sleep(0.1) |
| 120 | + |
| 121 | + if cp.button_b: |
| 122 | + if cp.switch: |
| 123 | + ams.previous_track() |
| 124 | + else: |
| 125 | + ams.next_track() |
| 126 | + time.sleep(0.1) |
| 127 | + except (RuntimeError, UnsupportedCommand): |
| 128 | + # Skip Bad Packets, unknown commands, etc. |
| 129 | + pass |
| 130 | + |
| 131 | + print("disconnected") |
| 132 | + # Remove all layers |
| 133 | + while len(group): |
| 134 | + group.pop() |
0 commit comments