/ examples / advanced_examples / clue_ams_remote_advanced.py
clue_ams_remote_advanced.py
  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  adafruit_bitmap_font
  9  adafruit_display_shapes
 10  adafruit_display_text
 11  """
 12  
 13  import time
 14  import board
 15  import displayio
 16  from adafruit_display_text import label
 17  import adafruit_ble
 18  from adafruit_ble.advertising.standard import SolicitServicesAdvertisement
 19  from adafruit_ble_apple_media import AppleMediaService
 20  from adafruit_ble_apple_media import UnsupportedCommand
 21  from adafruit_bitmap_font import bitmap_font
 22  from adafruit_display_shapes.rect import Rect
 23  from adafruit_clue import clue
 24  
 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(
104                      210 * ((time.time() - ref_time + ela_time) / float(ams.duration))
105                  )
106                  if not width1:
107                      width1 = 1
108              elif not ams.duration:
109                  width1 = 1
110  
111              time_inner = Rect(15, 210, width1, 20, fill=0xFFFFFF)  # , outline=0xFFFFFF)
112              group[-2] = time_inner
113              volume_inner = Rect(
114                  15, 170, width, 20, fill=0xFFFFFF
115              )  # , outline=0xFFFFFF)
116              group[-1] = volume_inner
117  
118          # Capacitive touch pad marked 0 goes to the previous track
119          if clue.touch_0:
120              ams.previous_track()
121              time.sleep(0.25)
122  
123          # Capacitive touch pad marked 1 toggles pause/play
124          if clue.touch_1:
125              ams.toggle_play_pause()
126              time.sleep(0.25)
127  
128          # Capacitive touch pad marked 2 advances to the next track
129          if clue.touch_2:
130              ams.next_track()
131              time.sleep(0.25)
132  
133          # If button B (on the right) is pressed, it increases the volume
134          if "B" in clue.were_pressed:
135              ams.volume_up()
136              a = clue.were_pressed
137              time.sleep(0.35)
138              while "B" in clue.were_pressed:
139                  ams.volume_up()
140                  time.sleep(0.07)
141  
142          # If button A (on the left) is pressed, the volume decreases
143          if "A" in clue.were_pressed:
144              ams.volume_down()
145              a = clue.were_pressed
146              time.sleep(0.35)
147              while "A" in clue.were_pressed:
148                  ams.volume_down()
149                  time.sleep(0.07)
150          time.sleep(0.01)
151      except (RuntimeError, UnsupportedCommand, AttributeError):
152          time.sleep(0.01)
153          continue
154  
155  print("disconnected")