Skip to content

Commit 5df8f0f

Browse files
committed
Linting
1 parent c6066b9 commit 5df8f0f

File tree

1 file changed

+36
-25
lines changed

1 file changed

+36
-25
lines changed

CPB_ANCS/code.py

Lines changed: 36 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,6 @@
4848
file = open("/triode_rise.wav", "rb")
4949
wave = WaveFile(file)
5050

51-
update_time = time.monotonic()
52-
5351
def play_sound():
5452
audio.play(wave)
5553
time.sleep(1)
@@ -63,17 +61,26 @@ def find_connection():
6361
return connection, connection[AppleNotificationService]
6462
return None, None
6563

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()
7672

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()
7784

7885
# Start advertising before messing with the display so that we can connect immediately.
7986
radio = adafruit_ble.BLERadio()
@@ -103,32 +110,34 @@ def wrap_in_tilegrid(open_file):
103110

104111
while not active_connection:
105112
active_connection, notification_service = find_connection()
106-
check_dim_timeout()
113+
dimmer.check_timeout()
107114

108115
# Connected
109-
update_time = time.monotonic()
116+
dimmer.update()
110117
play_sound()
111118

112119
with open("/ancs_none.bmp", "rb") as no_notifications:
113120
group.append(wrap_in_tilegrid(no_notifications))
114121
while active_connection.connected:
115122
all_ids.clear()
116123
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]
119126
if notification.app_id not in APP_ICONS or notification.app_id in BLACKLIST:
120127
continue
121-
all_ids.append(id)
128+
all_ids.append(notif_id)
122129

130+
# pylint: disable=protected-access
123131
all_ids.sort(key=lambda x: current_notifications[x]._raw_date)
132+
# pylint: enable=protected-access
124133

125134
if current_notification and current_notification.removed:
126135
# Stop showing the latest and show that there are no new notifications.
127136
current_notification = None
128137

129138
if not current_notification and not all_ids and not cleared:
130139
cleared = True
131-
update_time = time.monotonic()
140+
dimmer.update()
132141
group[1] = wrap_in_tilegrid(no_notifications)
133142
elif all_ids:
134143
cleared = False
@@ -145,18 +154,20 @@ def wrap_in_tilegrid(open_file):
145154
if a.value and index < len(all_ids) - 1:
146155
last_press = now
147156
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
152162
print(current_notification._raw_date, current_notification)
153-
163+
# pylint: enable=protected-access
154164
app_icon_file = open(APP_ICONS[current_notification.app_id], "rb")
155165
group[1] = wrap_in_tilegrid(app_icon_file)
156166

157-
check_dim_timeout()
167+
dimmer.check_timeout()
158168

169+
# Bluetooth Disconnected
159170
group.pop()
160-
update_time = time.monotonic()
171+
dimmer.update()
161172
active_connection = None
162173
notification_service = None

0 commit comments

Comments
 (0)