Simple test¶
Ensure your device works with this simple test.
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
11import board
12import digitalio
13import displayio
14import adafruit_ble
15from adafruit_ble.advertising.standard import SolicitServicesAdvertisement
16
17from adafruit_ble_apple_notification_center import AppleNotificationCenterService
18from adafruit_display_ble_status.advertising import AdvertisingWidget
19from adafruit_gizmo import tft_gizmo
20from adafruit_display_notification import apple
21from adafruit_display_notification import NotificationFree
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 # pylint: disable=protected-access
87 all_ids.sort(key=lambda x: current_notifications[x]._raw_date)
88
89 if current_notification and current_notification.removed:
90 # Stop showing the latest and show that there are no new notifications.
91 current_notification = None
92
93 if not current_notification and not all_ids:
94 group[0] = NotificationFree(width, height)
95 elif all_ids:
96 now = time.monotonic()
97 if (
98 current_notification
99 and current_notification.id in all_ids
100 and now - last_press < DELAY_AFTER_PRESS
101 ):
102 index = all_ids.index(current_notification.id)
103 else:
104 index = len(all_ids) - 1
105 if now - last_press >= DEBOUNCE:
106 if b.value and index > 0:
107 last_press = now
108 index += -1
109 if a.value and index < len(all_ids) - 1:
110 last_press = now
111 index += 1
112
113 notification_id = all_ids[index]
114 if not current_notification or current_notification.id != notification_id:
115 current_notification = current_notifications[notification_id]
116 print(current_notification._raw_date, current_notification)
117 group[0] = apple.create_notification_widget(
118 current_notification, width, height
119 )
120
121 active_connection = None
122 notification_service = None