display_notification_simpletest.py
1 """This demo shows the latest notification from a connected Apple device on a TFT Gizmo screen. 2 3 The A and B buttons on the CircuitPlayground Bluefruit can be used to scroll through all active 4 notifications. 5 """ 6 7 import time 8 import board 9 import digitalio 10 import displayio 11 import adafruit_ble 12 from adafruit_ble.advertising.standard import SolicitServicesAdvertisement 13 14 from adafruit_ble_apple_notification_center import AppleNotificationCenterService 15 from adafruit_display_notification import apple 16 from adafruit_display_notification import NotificationFree 17 from adafruit_display_ble_status.advertising import AdvertisingWidget 18 19 # from adafruit_circuitplayground import cp 20 from adafruit_gizmo import tft_gizmo 21 22 # This is a whitelist of apps to show notifications from. 23 # APPS = ["com.tinyspeck.chatlyio", "com.atebits.Tweetie2"] 24 APPS = [] 25 26 DELAY_AFTER_PRESS = 15 27 DEBOUNCE = 0.1 28 29 a = digitalio.DigitalInOut(board.BUTTON_A) 30 a.switch_to_input(pull=digitalio.Pull.DOWN) 31 b = digitalio.DigitalInOut(board.BUTTON_B) 32 b.switch_to_input(pull=digitalio.Pull.DOWN) 33 34 35 def find_connection(): 36 for connection in radio.connections: 37 if AppleNotificationCenterService not in connection: 38 continue 39 if not connection.paired: 40 connection.pair() 41 return connection, connection[AppleNotificationCenterService] 42 return None, None 43 44 45 # Start advertising before messing with the display so that we can connect immediately. 46 radio = adafruit_ble.BLERadio() 47 advertisement = SolicitServicesAdvertisement() 48 advertisement.solicited_services.append(AppleNotificationCenterService) 49 50 SCALE = 2 51 52 display = tft_gizmo.TFT_Gizmo() 53 group = displayio.Group(scale=SCALE) 54 display.show(group) 55 56 width = display.width // SCALE 57 height = display.height // SCALE 58 59 radio_widget = AdvertisingWidget("CIRCUITPY", width, height) 60 group.append(radio_widget) 61 62 current_notification = None 63 all_ids = [] 64 last_press = time.monotonic() 65 active_connection, notification_service = find_connection() 66 while True: 67 if not active_connection: 68 radio.start_advertising(advertisement) 69 70 while not active_connection: 71 active_connection, notification_service = find_connection() 72 73 while active_connection.connected: 74 all_ids.clear() 75 current_notifications = notification_service.active_notifications 76 for notification_id in current_notifications: 77 notification = current_notifications[notification_id] 78 if APPS and notification.app_id not in APPS: 79 continue 80 all_ids.append(notification_id) 81 82 # For now, use _raw_date even though we should use a parsed version of the date. 83 # pylint: disable=protected-access 84 all_ids.sort(key=lambda x: current_notifications[x]._raw_date) 85 86 if current_notification and current_notification.removed: 87 # Stop showing the latest and show that there are no new notifications. 88 current_notification = None 89 90 if not current_notification and not all_ids: 91 group[0] = NotificationFree(width, height) 92 elif all_ids: 93 now = time.monotonic() 94 if ( 95 current_notification 96 and current_notification.id in all_ids 97 and now - last_press < DELAY_AFTER_PRESS 98 ): 99 index = all_ids.index(current_notification.id) 100 else: 101 index = len(all_ids) - 1 102 if now - last_press >= DEBOUNCE: 103 if b.value and index > 0: 104 last_press = now 105 index += -1 106 if a.value and index < len(all_ids) - 1: 107 last_press = now 108 index += 1 109 110 notification_id = all_ids[index] 111 if not current_notification or current_notification.id != notification_id: 112 current_notification = current_notifications[notification_id] 113 print(current_notification._raw_date, current_notification) 114 group[0] = apple.create_notification_widget( 115 current_notification, width, height 116 ) 117 118 active_connection = None 119 notification_service = None