Skip to content

Commit a575610

Browse files
author
brentru
committed
timer bar fills the display, add configurable colors, timer bar glows red when its close to expiring
1 parent 3e9c981 commit a575610

1 file changed

Lines changed: 31 additions & 18 deletions

File tree

PyPortal_TOTP_Friend/code.py

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,17 @@
1717
# Background Color
1818
BACKGROUND = 0x059ACE
1919

20-
TEST = False # If you want to print out the tests the hashers
21-
ALWAYS_ON = True # Set to true if you never want to go to sleep!
22-
ON_SECONDS = 60 # how long to stay on if not in always_on mode
20+
# Button color
21+
BTN_COLOR = 0xFFFFFF
22+
23+
# Button text color
24+
BTN_TEXT_COLOR = 0x0
25+
26+
# Set to true if you never want to go to sleep!
27+
ALWAYS_ON = True
28+
29+
# How long to stay on if not in always_on mode
30+
ON_SECONDS = 60
2331

2432
# Get wifi details and more from a secrets.py file
2533
try:
@@ -151,8 +159,7 @@ def generate_otp(int_input, secret_key, digits=6):
151159
root_group = displayio.Group(max_size=100)
152160
display.show(root_group)
153161

154-
155-
BACKGROUND = BACKGROUND if isinstance(BACKGROUND, int) else 0x000000
162+
BACKGROUND = BACKGROUND if isinstance(BACKGROUND, int) else 0x0
156163
bg_bitmap = displayio.Bitmap(display.width, display.height, 1)
157164
bg_palette = displayio.Palette(1)
158165
bg_palette[0] = BACKGROUND
@@ -167,7 +174,7 @@ def generate_otp(int_input, secret_key, digits=6):
167174
# We'll use a default text placeholder for this label
168175
label_secret = Label(font, text="000 000")
169176
label_secret.x = (display.width // 2) // 13
170-
label_secret.y = 20
177+
label_secret.y = 17
171178
key_group.append(label_secret)
172179

173180
label_title = Label(font, max_glyphs=14)
@@ -219,12 +226,13 @@ def generate_otp(int_input, secret_key, digits=6):
219226

220227
btn_x = 5
221228
for i in secrets['totp_keys']:
222-
button = Button(name=i[0], x=btn_x, y=175,
223-
width=60, height=60,
224-
label=i[0], label_font=font, label_color=0xFFFFFF,
225-
fill_color=0x00FF00, style=Button.ROUNDRECT)
229+
button = Button(name=i[0], x=btn_x,
230+
y=175, width=60,
231+
height=60, label=i[0],
232+
label_font=font, label_color=BTN_TEXT_COLOR,
233+
fill_color=BTN_COLOR, style=Button.ROUNDRECT)
226234
buttons.append(button)
227-
# add some padding btween buttons
235+
# add padding btween buttons
228236
btn_x += 63
229237

230238
# append buttons to splash group
@@ -238,7 +246,7 @@ def generate_otp(int_input, secret_key, digits=6):
238246
splash.append(label_timer)
239247

240248
# timer bar
241-
rect = Rect(0, 150, 100, 20, fill=0xFF0000)
249+
rect = Rect(0, 150, 0, 20, fill=0xFF0000)
242250
splash.append(rect)
243251

244252
# how long to stay on if not in always_on mode
@@ -256,26 +264,31 @@ def generate_otp(int_input, secret_key, digits=6):
256264
# Update the key refresh timer
257265
timer = time.localtime(time.time()).tm_sec
258266
print('Timer:', timer)
267+
# timer resets on :00/:30
259268
if timer > 30:
260269
countdown = 60 - timer
261270
else:
262271
countdown = 30 - timer
263272
print('countdown:', countdown)
264273

274+
# change the timer bar's color if text is about to refresh
275+
fill_color = 0xFFFFFF
276+
if countdown < 5:
277+
fill_color = 0xFF0000
265278
# "animate" the timer-bar
266-
# TODO: make bar reset at :00/:30
267-
# TODO: decrease the timer
268-
# TODO: make bar red when it gets close to :00/:30
269279
splash.remove(rect)
270-
rect = Rect(0, 140, countdown, 30, fill=0xFF0000)
280+
rect = Rect(0, 140, countdown*12, 30, fill=fill_color)
271281
splash.append(rect)
272282

283+
# poll the touchscreen
273284
p = ts.touch_point
285+
# if the touchscreen was pressed
274286
if p:
275287
for i, b in enumerate(buttons):
276288
if b.contains(p):
277289
b.selected = True
278290
for name, secret in secrets['totp_keys']:
291+
# check if button name is the same as a key name
279292
if b.name == name:
280293
current_button = name
281294
# Generate OTP
@@ -297,5 +310,5 @@ def generate_otp(int_input, secret_key, digits=6):
297310
# format and display the OTP
298311
label_secret.text = "{} {}".format(str(otp)[0:3], str(otp)[3:6])
299312
# We'll update every 1/4 second, we can hash very fast so its no biggie!
300-
countdown -= 0.25
301-
time.sleep(0.25)
313+
countdown -= 0.1
314+
time.sleep(0.1)

0 commit comments

Comments
 (0)