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