-
Notifications
You must be signed in to change notification settings - Fork 20
Added clue_ams_remote.py #14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
7cd481c
Added clue_ams_remote.py
evaherrada 892d7d7
Fixed trailing whitespace
evaherrada 689da9b
Made docstring describe example
evaherrada 7a6f276
Added required libraries in docstring
evaherrada 7ef68b1
Added displayio to clue_ams_remote
evaherrada d7a250c
Moved clue_ams_examples to advanced_examples
evaherrada 59ff443
Renamed clue_ams_remote to clue_ams_remote_advanced
evaherrada e6f7d5e
Replaced advanced version with regular version
evaherrada f6b04d7
Merge pull request #1 from dherrada/ams_with_displayio
evaherrada dd5a26b
Linting
evaherrada a85bc5d
Removed unneccessary library from docstring
evaherrada File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
""" | ||
|
||
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") |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will do