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