Skip to content

Commit ce9ff87

Browse files
author
brentru
committed
add a default button state, if/else for button selection
1 parent 5d21652 commit ce9ff87

1 file changed

Lines changed: 47 additions & 40 deletions

File tree

PyPortal_TOTP_Friend/code.py

Lines changed: 47 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@
1919
from adafruit_ntp import NTP
2020
from adafruit_pyportal import PyPortal
2121

22-
# Background/Images
22+
# Background Color
2323
BACKGROUND = 0x059ACE
2424

2525
TEST = True # if you want to print out the tests the hashers
26-
ALWAYS_ON = True # Set to true if you never want to go to sleep!
26+
ALWAYS_ON = False # Set to true if you never want to go to sleep!
2727
ON_SECONDS = 60 # how long to stay on if not in always_on mode
2828

2929
# Get wifi details and more from a secrets.py file
@@ -150,7 +150,6 @@ def generate_otp(int_input, secret_key, digits=6):
150150
root_group = displayio.Group(max_size=200)
151151
display.show(root_group)
152152

153-
# TODO: Add press-able icon group
154153

155154
BACKGROUND = BACKGROUND if isinstance(BACKGROUND, int) else 0x000000
156155
bg_bitmap = displayio.Bitmap(display.width, display.height, 1)
@@ -165,48 +164,23 @@ def generate_otp(int_input, secret_key, digits=6):
165164

166165
key_group = displayio.Group(scale=5)
167166
# We'll use a default text placeholder for this label
168-
label_key = Label(font, text="000 000")
169-
label_key.x = (display.width // 2) // 13
170-
label_key.y = 20
171-
key_group.append(label_key)
167+
label_secret = Label(font, text="000 000")
168+
label_secret.x = (display.width // 2) // 13
169+
label_secret.y = 20
170+
key_group.append(label_secret)
172171

173172
label_title = Label(font, max_glyphs=14)
174173
label_title.text = "loading..."
175-
label_title.x = (display.width // 2) // 10
174+
label_title.x = (display.width // 2) // 13
176175
label_title.y = 5
177176
key_group.append(label_title)
178177

179178
# append key_group to splash
180179
splash.append(key_group)
180+
181181
# Show the group
182182
display.show(splash)
183183

184-
# Add buttons to the interface
185-
assert len(secrets['totp_keys']) < 6, "This code can only render 5 keys at a time"
186-
187-
# generate buttons
188-
buttons = []
189-
190-
btn_x = 5
191-
for i in secrets['totp_keys']:
192-
print(i)
193-
try:
194-
if i[2]:
195-
color = i[2]
196-
except IndexError:
197-
color = 0x00FF00
198-
button = Button(name=i[0], x=btn_x, y=175,
199-
width=60, height=60,
200-
label=i[0], label_font=font, label_color=0xFFFFFF,
201-
fill_color=color, style=Button.ROUNDRECT)
202-
buttons.append(button)
203-
btn_x+=63
204-
205-
# append buttons to splash group
206-
for b in buttons:
207-
splash.append(b.group)
208-
209-
210184
print("Connecting to AP...")
211185
while not esp.is_connected:
212186
try:
@@ -236,29 +210,62 @@ def generate_otp(int_input, secret_key, digits=6):
236210
mono_time = int(time.monotonic())
237211
print("Monotonic time", mono_time)
238212

213+
# Add buttons to the interface
214+
assert len(secrets['totp_keys']) < 6, "This code can only render 5 keys at a time"
215+
216+
# generate buttons
217+
buttons = []
218+
219+
btn_x = 5
220+
for i in secrets['totp_keys']:
221+
button = Button(name=i[0], x=btn_x, y=175,
222+
width=60, height=60,
223+
label=i[0], label_font=font, label_color=0xFFFFFF,
224+
fill_color=0x00FF00, style=Button.ROUNDRECT)
225+
buttons.append(button)
226+
# add some padding btween buttons
227+
btn_x+=63
228+
229+
# append buttons to splash group
230+
for b in buttons:
231+
splash.append(b.group)
232+
233+
# how long to stay on if not in always_on mode
234+
countdown = ON_SECONDS
235+
236+
# current button state, defaults to first item in totp_keys
237+
current_button = secrets['totp_keys'][0][0]
239238

240-
countdown = ON_SECONDS # how long to stay on if not in always_on mode
241239
while ALWAYS_ON or (countdown > 0):
242240
# Calculate current time based on NTP + monotonic
243241
unix_time = t - mono_time + int(time.monotonic())
242+
244243
p = ts.touch_point
245244
if p:
246245
for i, b in enumerate(buttons):
247246
if b.contains(p):
248247
b.selected = True
249248
for name, secret in secrets['totp_keys']:
250249
if b.name == name:
251-
# generate OTP
252-
#print('Background color: ', background_color)
250+
current_button = name
251+
# Generate OTP
253252
otp = generate_otp(unix_time // 30, secret)
254-
print('{} selected: '.format(name))
255-
print(name + " OTP output: ", otp)
256253
# display the key's name
257254
label_title.text = name
258255
# format and display the OTP
259-
label_key.text = "{} {}".format(str(otp)[0:3],str(otp)[3:6])
256+
label_secret.text = "{} {}".format(str(otp)[0:3],str(otp)[3:6])
260257
else:
261258
b.selected = False
259+
else:
260+
for name, secret in secrets['totp_keys']:
261+
if current_button == name:
262+
# Generate OTP
263+
otp = generate_otp(unix_time // 30, secret)
264+
# display the key's name
265+
label_title.text = name
266+
# format and display the OTP
267+
label_secret.text = "{} {}".format(str(otp)[0:3],str(otp)[3:6])
268+
262269
# We'll update every 1/4 second, we can hash very fast so its no biggie!
263270
countdown -= 0.25
264271
time.sleep(0.25)

0 commit comments

Comments
 (0)