48
48
file = open ("/triode_rise.wav" , "rb" )
49
49
wave = WaveFile (file )
50
50
51
- update_time = time .monotonic ()
52
-
53
51
def play_sound ():
54
52
audio .play (wave )
55
53
time .sleep (1 )
@@ -63,17 +61,26 @@ def find_connection():
63
61
return connection , connection [AppleNotificationService ]
64
62
return None , None
65
63
66
- def check_dim_timeout ():
67
- global update_time
68
- if a .value or b .value :
69
- update_time = time .monotonic ()
70
- if time .monotonic () - update_time > DIM_TIMEOUT :
71
- if display .brightness > DIM_LEVEL :
72
- display .brightness = DIM_LEVEL
73
- else :
74
- if display .brightness == DIM_LEVEL :
75
- display .brightness = 1.0
64
+ class Dimmer :
65
+ def __init__ (self ):
66
+ self ._update_time = time .monotonic ()
67
+ self ._level = DIM_LEVEL
68
+ self ._timeout = DIM_TIMEOUT
69
+
70
+ def update (self ):
71
+ self ._update_time = time .monotonic ()
76
72
73
+ def check_timeout (self ):
74
+ if a .value or b .value :
75
+ self ._update_time = time .monotonic ()
76
+ if time .monotonic () - self ._update_time > self ._timeout :
77
+ if display .brightness > self ._level :
78
+ display .brightness = self ._level
79
+ else :
80
+ if display .brightness == self ._level :
81
+ display .brightness = 1.0
82
+
83
+ dimmer = Dimmer ()
77
84
78
85
# Start advertising before messing with the display so that we can connect immediately.
79
86
radio = adafruit_ble .BLERadio ()
@@ -103,32 +110,34 @@ def wrap_in_tilegrid(open_file):
103
110
104
111
while not active_connection :
105
112
active_connection , notification_service = find_connection ()
106
- check_dim_timeout ()
113
+ dimmer . check_timeout ()
107
114
108
115
# Connected
109
- update_time = time . monotonic ()
116
+ dimmer . update ()
110
117
play_sound ()
111
118
112
119
with open ("/ancs_none.bmp" , "rb" ) as no_notifications :
113
120
group .append (wrap_in_tilegrid (no_notifications ))
114
121
while active_connection .connected :
115
122
all_ids .clear ()
116
123
current_notifications = notification_service .active_notifications
117
- for id in current_notifications :
118
- notification = current_notifications [id ]
124
+ for notif_id in current_notifications :
125
+ notification = current_notifications [notif_id ]
119
126
if notification .app_id not in APP_ICONS or notification .app_id in BLACKLIST :
120
127
continue
121
- all_ids .append (id )
128
+ all_ids .append (notif_id )
122
129
130
+ # pylint: disable=protected-access
123
131
all_ids .sort (key = lambda x : current_notifications [x ]._raw_date )
132
+ # pylint: enable=protected-access
124
133
125
134
if current_notification and current_notification .removed :
126
135
# Stop showing the latest and show that there are no new notifications.
127
136
current_notification = None
128
137
129
138
if not current_notification and not all_ids and not cleared :
130
139
cleared = True
131
- update_time = time . monotonic ()
140
+ dimmer . update ()
132
141
group [1 ] = wrap_in_tilegrid (no_notifications )
133
142
elif all_ids :
134
143
cleared = False
@@ -145,18 +154,20 @@ def wrap_in_tilegrid(open_file):
145
154
if a .value and index < len (all_ids ) - 1 :
146
155
last_press = now
147
156
index += 1
148
- id = all_ids [index ]
149
- if not current_notification or current_notification .id != id :
150
- update_time = now
151
- current_notification = current_notifications [id ]
157
+ notif_id = all_ids [index ]
158
+ if not current_notification or current_notification .id != notif_id :
159
+ dimmer .update ()
160
+ current_notification = current_notifications [notif_id ]
161
+ # pylint: disable=protected-access
152
162
print (current_notification ._raw_date , current_notification )
153
-
163
+ # pylint: enable=protected-access
154
164
app_icon_file = open (APP_ICONS [current_notification .app_id ], "rb" )
155
165
group [1 ] = wrap_in_tilegrid (app_icon_file )
156
166
157
- check_dim_timeout ()
167
+ dimmer . check_timeout ()
158
168
169
+ # Bluetooth Disconnected
159
170
group .pop ()
160
- update_time = time . monotonic ()
171
+ dimmer . update ()
161
172
active_connection = None
162
173
notification_service = None
0 commit comments