Skip to content

Commit 3e9c981

Browse files
author
brentru
committed
add animated timer for countdown
1 parent 00ad8c2 commit 3e9c981

1 file changed

Lines changed: 23 additions & 8 deletions

File tree

PyPortal_TOTP_Friend/code.py

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
import time
22

3-
import board
4-
import busio
5-
from digitalio import DigitalInOut
6-
73
import adafruit_hashlib as hashlib
84
import adafruit_touchscreen
5+
import board
6+
import busio
97
import displayio
108
import terminalio
119
from adafruit_button import Button
10+
from adafruit_display_shapes.rect import Rect
1211
from adafruit_display_text.label import Label
1312
from adafruit_esp32spi import adafruit_esp32spi
1413
from adafruit_ntp import NTP
1514
from adafruit_pyportal import PyPortal
15+
from digitalio import DigitalInOut
1616

1717
# Background Color
1818
BACKGROUND = 0x059ACE
@@ -234,9 +234,13 @@ def generate_otp(int_input, secret_key, digits=6):
234234
# refrsh timer label
235235
label_timer = Label(font, max_glyphs=2)
236236
label_timer.x = (display.width // 2) // 13
237-
label_timer.y = 150
237+
label_timer.y = 15
238238
splash.append(label_timer)
239239

240+
# timer bar
241+
rect = Rect(0, 150, 100, 20, fill=0xFF0000)
242+
splash.append(rect)
243+
240244
# how long to stay on if not in always_on mode
241245
countdown = ON_SECONDS
242246

@@ -249,11 +253,22 @@ def generate_otp(int_input, secret_key, digits=6):
249253
# Calculate current time based on NTP + monotonic
250254
unix_time = t - mono_time + int(time.monotonic())
251255

256+
# Update the key refresh timer
252257
timer = time.localtime(time.time()).tm_sec
253258
print('Timer:', timer)
254-
timer = 30 - timer
255-
print('Timer 2:', timer)
256-
label_timer.text = str(timer)
259+
if timer > 30:
260+
countdown = 60 - timer
261+
else:
262+
countdown = 30 - timer
263+
print('countdown:', countdown)
264+
265+
# "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
269+
splash.remove(rect)
270+
rect = Rect(0, 140, countdown, 30, fill=0xFF0000)
271+
splash.append(rect)
257272

258273
p = ts.touch_point
259274
if p:

0 commit comments

Comments
 (0)