Skip to content
151 changes: 151 additions & 0 deletions examples/advanced_examples/clue_ams_remote_advanced.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
""" This example solicits that apple devices that provide notifications connect to it, initiates
pairing, then allows the user to use a CLUE board as a media remote through both the buttons
and capacitive touch pads.

This example requires the following additional libraries:
adafruit_ble
adafruit_ble_apple_media
adafruit_bitmap_font
adafruit_display_shapes
adafruit_display_text
"""

import time
import board
import adafruit_ble
from adafruit_ble.advertising.standard import SolicitServicesAdvertisement
from adafruit_ble_apple_media import AppleMediaService
from adafruit_ble_apple_media import UnsupportedCommand
from adafruit_clue import clue
import displayio
from adafruit_bitmap_font import bitmap_font
from adafruit_display_shapes.rect import Rect
from adafruit_display_text import label


# PyLint can't find BLERadio for some reason so special case it here.
radio = adafruit_ble.BLERadio() # pylint: disable=no-member
a = SolicitServicesAdvertisement()
a.solicited_services.append(AppleMediaService)
radio.start_advertising(a)

while not radio.connected:
pass

print("connected")

known_notifications = set()

i = 0
if radio.connected:
for connection in radio.connections:
if not connection.paired:
connection.pair()
print("paired")

ams = connection[AppleMediaService]


#arial12 = bitmap_font.load_font("/fonts/Arial-12.bdf")
arial16 = bitmap_font.load_font("/fonts/Arial-16.bdf")
#arial24 = bitmap_font.load_font("/fonts/Arial-Bold-24.bdf")

display = board.DISPLAY

group = displayio.Group(max_size=25)

title = label.Label(font=arial16, x=15, y=25, text='_', color=0xFFFFFF, max_glyphs=30)
group.append(title)

artist = label.Label(font=arial16, x=15, y=50, text='_', color=0xFFFFFF, max_glyphs=30)
group.append(artist)

album = label.Label(font=arial16, x=15, y=75, text='_', color=0xFFFFFF, max_glyphs=30)
group.append(album)

player = label.Label(font=arial16, x=15, y=100, text='_', color=0xFFFFFF, max_glyphs=30)
group.append(player)

volume = Rect(15, 170, 210, 20, fill=0x0, outline=0xFFFFFF)
group.append(volume)

track_time = Rect(15, 210, 210, 20, fill=0x0, outline=0xFFFFFF)
#time = label.Label(font=arial16, x=15, y=215, text='Time', color=0xFFFFFF)
group.append(track_time)

time_inner = Rect(15, 210, 1, 20, fill=0xFFFFFF, outline=0xFFFFFF)
group.append(time_inner)

volume_inner = Rect(15, 170, 1, 20, fill=0xFFFFFF, outline=0xFFFFFF)
group.append(volume_inner)

display.show(group)
time.sleep(0.01)

width1 = 1

ref_time = time.time()
ela_time = ams.elapsed_time
while radio.connected:
try:
if ams.elapsed_time != ela_time:
ela_time = ams.elapsed_time
ref_time = time.time()
title.text = ams.title
artist.text = ams.artist
album.text = ams.album
player.text = ams.player_name
if ams.volume is not None:
width = int(16*13.125*float(ams.volume))
if not width:
width = 1
if ams.duration and ams.playing:
width1 = int(210*((time.time() - ref_time + ela_time)/float(ams.duration)))
if not width1:
width1 = 1
elif not ams.duration:
width1 = 1

time_inner = Rect(15, 210, width1, 20, fill=0xFFFFFF)#, outline=0xFFFFFF)
group[-2] = time_inner
volume_inner = Rect(15, 170, width, 20, fill=0xFFFFFF)#, outline=0xFFFFFF)
group[-1] = volume_inner

# Capacitive touch pad marked 0 goes to the previous track
if clue.touch_0:
ams.previous_track()
time.sleep(0.25)

# Capacitive touch pad marked 1 toggles pause/play
if clue.touch_1:
ams.toggle_play_pause()
time.sleep(0.25)

# Capacitive touch pad marked 2 advances to the next track
if clue.touch_2:
ams.next_track()
time.sleep(0.25)

# If button B (on the right) is pressed, it increases the volume
if 'B' in clue.were_pressed:
ams.volume_up()
a = clue.were_pressed
time.sleep(0.35)
while 'B' in clue.were_pressed:
ams.volume_up()
time.sleep(0.07)

# If button A (on the left) is pressed, the volume decreases
if 'A' in clue.were_pressed:
ams.volume_down()
a = clue.were_pressed
time.sleep(0.35)
while 'A' in clue.were_pressed:
ams.volume_down()
time.sleep(0.07)
time.sleep(0.01)
except (RuntimeError, UnsupportedCommand, AttributeError):
time.sleep(0.01)
continue

print("disconnected")
77 changes: 77 additions & 0 deletions examples/clue_ams_remote.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
"""
This example solicits that apple devices that provide notifications connect to it, initiates
pairing, then allows the user to use a CLUE board as a media remote through both the buttons
and capacitive touch pads.

This example requires the following additional libraries:
adafruit_ble
adafruit_ble_apple_media
"""
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer if you could include a note in the doc string about the separate libraries needed to run this example. Whenever I include an example in a library-specific repo that requires an atypical library (e.g. adafruit_ble, and adafruit_ble_apple_media), I include a note in the docstring to alleviate any potential confusion.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will do


import time
import adafruit_ble
from adafruit_ble.advertising.standard import SolicitServicesAdvertisement
from adafruit_ble_apple_media import AppleMediaService
from adafruit_clue import clue

# PyLint can't find BLERadio for some reason so special case it here.
radio = adafruit_ble.BLERadio() # pylint: disable=no-member
a = SolicitServicesAdvertisement()
a.solicited_services.append(AppleMediaService)
radio.start_advertising(a)

while not radio.connected:
pass

print("connected")

known_notifications = set()

i = 0
if radio.connected:
for connection in radio.connections:
if not connection.paired:
connection.pair()
print("paired")

ams = connection[AppleMediaService]

while radio.connected:
if ams.playing:
play_str = "Playing"
else:
play_str = "Paused"
print("{} - {}, {}".format(ams.title, ams.artist, play_str))

# Capacitive touch pad marked 0 goes to the previous track
if clue.touch_0:
ams.previous_track()
time.sleep(0.25)

# Capacitive touch pad marked 1 toggles pause/play
if clue.touch_1:
ams.toggle_play_pause()
time.sleep(0.25)

# Capacitive touch pad marked 2 advances to the next track
if clue.touch_2:
ams.next_track()
time.sleep(0.25)

# If button B (on the right) is pressed, it increases the volume
if 'B' in clue.were_pressed:
ams.volume_up()
time.sleep(0.30)
while 'B' in clue.were_pressed:
ams.volume_up()
time.sleep(0.07)

# If button A (on the left) is pressed, the volume decreases
if 'A' in clue.were_pressed:
ams.volume_down()
time.sleep(0.30)
while 'A' in clue.were_pressed:
ams.volume_down()
time.sleep(0.07)

print("disconnected")