From 7cd481c0d1a948ff1ace3e7066ee2ea4b869dea3 Mon Sep 17 00:00:00 2001 From: dherrada Date: Thu, 13 Feb 2020 16:26:40 -0500 Subject: [PATCH 01/10] Added clue_ams_remote.py --- examples/clue_ams_remote.py | 72 +++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 examples/clue_ams_remote.py diff --git a/examples/clue_ams_remote.py b/examples/clue_ams_remote.py new file mode 100644 index 0000000..b3b878a --- /dev/null +++ b/examples/clue_ams_remote.py @@ -0,0 +1,72 @@ +""" +This example solicits that apple devices that provide notifications connect to it, initiates +pairing, prints existing notifications and then prints any new ones as they arrive. +""" + +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 = "Playing" + else: + play = "Paused" + print("{} - {}, {}".format(ams.title, ams.artist, play)) + + # 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") From 892d7d73cadadf7d868f6e62f852c55efd01572e Mon Sep 17 00:00:00 2001 From: dherrada Date: Thu, 13 Feb 2020 16:40:27 -0500 Subject: [PATCH 02/10] Fixed trailing whitespace --- examples/clue_ams_remote.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/clue_ams_remote.py b/examples/clue_ams_remote.py index b3b878a..23a07ef 100644 --- a/examples/clue_ams_remote.py +++ b/examples/clue_ams_remote.py @@ -30,7 +30,7 @@ print("paired") ams = connection[AppleMediaService] - + while radio.connected: if ams.playing: play = "Playing" From 689da9b8fa78c5a3852df3d022468df927078ccf Mon Sep 17 00:00:00 2001 From: dherrada Date: Thu, 13 Feb 2020 16:51:09 -0500 Subject: [PATCH 03/10] Made docstring describe example --- examples/clue_ams_remote.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/examples/clue_ams_remote.py b/examples/clue_ams_remote.py index 23a07ef..ac696e0 100644 --- a/examples/clue_ams_remote.py +++ b/examples/clue_ams_remote.py @@ -1,6 +1,7 @@ """ This example solicits that apple devices that provide notifications connect to it, initiates -pairing, prints existing notifications and then prints any new ones as they arrive. +pairing, then allows the user to use a CLUE board as a media remote through both the buttons +and capacitive touch pads. """ import time @@ -33,10 +34,10 @@ while radio.connected: if ams.playing: - play = "Playing" + play_str = "Playing" else: - play = "Paused" - print("{} - {}, {}".format(ams.title, ams.artist, play)) + 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: From 7a6f27644610054521c4998e3f9e95164a4ac51f Mon Sep 17 00:00:00 2001 From: dherrada Date: Sun, 16 Feb 2020 09:31:43 -0500 Subject: [PATCH 04/10] Added required libraries in docstring --- examples/clue_ams_remote.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/examples/clue_ams_remote.py b/examples/clue_ams_remote.py index ac696e0..45bae99 100644 --- a/examples/clue_ams_remote.py +++ b/examples/clue_ams_remote.py @@ -2,6 +2,10 @@ 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 From 7ef68b1df6bb2035343050ee9770ba452f27a660 Mon Sep 17 00:00:00 2001 From: dherrada Date: Sun, 16 Feb 2020 11:17:14 -0500 Subject: [PATCH 05/10] Added displayio to clue_ams_remote --- examples/clue_ams_remote.py | 140 +++++++++++++++++++++++++++--------- 1 file changed, 105 insertions(+), 35 deletions(-) diff --git a/examples/clue_ams_remote.py b/examples/clue_ams_remote.py index 45bae99..978c027 100644 --- a/examples/clue_ams_remote.py +++ b/examples/clue_ams_remote.py @@ -1,5 +1,4 @@ -""" -This example solicits that apple devices that provide notifications connect to it, initiates +""" 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. @@ -12,7 +11,13 @@ 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 +import board # PyLint can't find BLERadio for some reason so special case it here. radio = adafruit_ble.BLERadio() # pylint: disable=no-member @@ -36,42 +41,107 @@ 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: - 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: + 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() - time.sleep(0.07) + 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() - time.sleep(0.30) - while 'A' in clue.were_pressed: + # If button A (on the left) is pressed, the volume decreases + if 'A' in clue.were_pressed: ams.volume_down() - time.sleep(0.07) + 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") From d7a250c129cb1a3cf0ca628ab5e09ba157691059 Mon Sep 17 00:00:00 2001 From: dherrada Date: Mon, 17 Feb 2020 14:26:34 -0500 Subject: [PATCH 06/10] Moved clue_ams_examples to advanced_examples --- examples/{ => advanced_examples}/clue_ams_remote.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename examples/{ => advanced_examples}/clue_ams_remote.py (100%) diff --git a/examples/clue_ams_remote.py b/examples/advanced_examples/clue_ams_remote.py similarity index 100% rename from examples/clue_ams_remote.py rename to examples/advanced_examples/clue_ams_remote.py From 59ff443c515ac017096f3bee6a93ac47311338a3 Mon Sep 17 00:00:00 2001 From: dherrada Date: Mon, 17 Feb 2020 14:29:15 -0500 Subject: [PATCH 07/10] Renamed clue_ams_remote to clue_ams_remote_advanced --- .../{clue_ams_remote.py => clue_ams_remote_advanced.py} | 4 ++++ 1 file changed, 4 insertions(+) rename examples/advanced_examples/{clue_ams_remote.py => clue_ams_remote_advanced.py} (98%) diff --git a/examples/advanced_examples/clue_ams_remote.py b/examples/advanced_examples/clue_ams_remote_advanced.py similarity index 98% rename from examples/advanced_examples/clue_ams_remote.py rename to examples/advanced_examples/clue_ams_remote_advanced.py index 978c027..29fe7cd 100644 --- a/examples/advanced_examples/clue_ams_remote.py +++ b/examples/advanced_examples/clue_ams_remote_advanced.py @@ -5,6 +5,10 @@ This example requires the following additional libraries: adafruit_ble adafruit_ble_apple_media +displayio +adafruit_bitmap_font +adafruit_display_shapes +adafruit_display_text """ import time From e6f7d5e75a2cf3328c4229d668aa1b5f08a73a73 Mon Sep 17 00:00:00 2001 From: dherrada Date: Mon, 17 Feb 2020 14:29:34 -0500 Subject: [PATCH 08/10] Replaced advanced version with regular version --- examples/clue_ams_remote.py | 77 +++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 examples/clue_ams_remote.py diff --git a/examples/clue_ams_remote.py b/examples/clue_ams_remote.py new file mode 100644 index 0000000..45bae99 --- /dev/null +++ b/examples/clue_ams_remote.py @@ -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") From dd5a26b09aa95da8c68313356e8908b3141edc64 Mon Sep 17 00:00:00 2001 From: dherrada Date: Mon, 17 Feb 2020 14:35:58 -0500 Subject: [PATCH 09/10] Linting --- examples/advanced_examples/clue_ams_remote_advanced.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/examples/advanced_examples/clue_ams_remote_advanced.py b/examples/advanced_examples/clue_ams_remote_advanced.py index 29fe7cd..e6d0998 100644 --- a/examples/advanced_examples/clue_ams_remote_advanced.py +++ b/examples/advanced_examples/clue_ams_remote_advanced.py @@ -12,6 +12,7 @@ """ import time +import board import adafruit_ble from adafruit_ble.advertising.standard import SolicitServicesAdvertisement from adafruit_ble_apple_media import AppleMediaService @@ -21,7 +22,7 @@ from adafruit_bitmap_font import bitmap_font from adafruit_display_shapes.rect import Rect from adafruit_display_text import label -import board + # PyLint can't find BLERadio for some reason so special case it here. radio = adafruit_ble.BLERadio() # pylint: disable=no-member @@ -73,10 +74,10 @@ #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) +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) +volume_inner = Rect(15, 170, 1, 20, fill=0xFFFFFF, outline=0xFFFFFF) group.append(volume_inner) display.show(group) From a85bc5d8ad564b7b038dede0441b9459d9f53c0e Mon Sep 17 00:00:00 2001 From: dherrada Date: Mon, 17 Feb 2020 15:19:40 -0500 Subject: [PATCH 10/10] Removed unneccessary library from docstring --- examples/advanced_examples/clue_ams_remote_advanced.py | 1 - 1 file changed, 1 deletion(-) diff --git a/examples/advanced_examples/clue_ams_remote_advanced.py b/examples/advanced_examples/clue_ams_remote_advanced.py index e6d0998..31019f3 100644 --- a/examples/advanced_examples/clue_ams_remote_advanced.py +++ b/examples/advanced_examples/clue_ams_remote_advanced.py @@ -5,7 +5,6 @@ This example requires the following additional libraries: adafruit_ble adafruit_ble_apple_media -displayio adafruit_bitmap_font adafruit_display_shapes adafruit_display_text