Skip to content

Commit c6f7578

Browse files
author
brentru
committed
dynamically generate btns based off keys in secrets, neaten up code a bit
1 parent 359bf75 commit c6f7578

1 file changed

Lines changed: 43 additions & 46 deletions

File tree

PyPortal_TOTP_Friend/code.py

Lines changed: 43 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,23 @@
2222
# Background/Images
2323
BACKGROUND = 0x059ACE
2424

25+
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!
27+
ON_SECONDS = 60 # how long to stay on if not in always_on mode
28+
29+
# Get wifi details and more from a secrets.py file
30+
try:
31+
from secrets import secrets
32+
except ImportError:
33+
print("WiFi secrets are kept in secrets.py, please add them there!")
34+
raise
35+
36+
2537
# Initialize PyPortal Display
2638
display = board.DISPLAY
2739

2840
WIDTH = board.DISPLAY.width
2941
HEIGHT = board.DISPLAY.height
30-
3142
ts = adafruit_touchscreen.Touchscreen(board.TOUCH_XL, board.TOUCH_XR,
3243
board.TOUCH_YD, board.TOUCH_YU,
3344
calibration=(
@@ -37,18 +48,6 @@
3748
size=(WIDTH, HEIGHT))
3849

3950

40-
41-
# Get wifi details and more from a secrets.py file
42-
try:
43-
from secrets import secrets
44-
except ImportError:
45-
print("WiFi secrets are kept in secrets.py, please add them there!")
46-
raise
47-
48-
TEST = True # if you want to print out the tests the hashers
49-
ALWAYS_ON = True # Set to true if you never want to go to sleep!
50-
ON_SECONDS = 60 # how long to stay on if not in always_on mode
51-
5251
# Create a SHA1 Object
5352
SHA1 = hashlib.sha1
5453

@@ -199,9 +198,8 @@ def generate_otp(int_input, secret_key, digits=6):
199198
label_status.text("Retrying...")
200199
continue
201200

202-
label_status.text = "Connected! Fetching NTP..."
203-
#label_status.text("Connected to {}, fetching NTP...".format(secrets['ssid']))
204201
print("Connected to SSID: ", secrets['ssid'])
202+
label_status.text = "Connected! Fetching NTP..."
205203

206204
# Initialize the NTP object
207205
ntp = NTP(esp)
@@ -213,6 +211,15 @@ def generate_otp(int_input, secret_key, digits=6):
213211
print("Failed to obtain time, retrying in 5 seconds...")
214212
time.sleep(5)
215213

214+
# Get the current time in seconds since Jan 1, 1970
215+
t = time.time()
216+
print("Seconds since Jan 1, 1970: {} seconds".format(t))
217+
218+
# Instead of using RTC which means converting back and forth
219+
# we'll just keep track of seconds-elapsed-since-NTP-call
220+
mono_time = int(time.monotonic())
221+
print("Monotonic time", mono_time)
222+
216223
# Clear the status label
217224
label_status.text = ""
218225

@@ -221,57 +228,47 @@ def generate_otp(int_input, secret_key, digits=6):
221228
# TODO: Make these dynamically based on what is store in secrets.py
222229
# TODO: Add icons to buttons instead of text
223230

224-
BUTTON_WIDTH = 60
225-
BUTTON_HEIGHT = 60
226-
BUTTON_MARGIN = 20
227-
buttons = []
231+
assert len(secrets['totp_keys']) < 8, "This code can only render 8 keys at a time"
228232

229-
button_0 = Button(name='Gmail',x=0, y=130,
230-
width=BUTTON_WIDTH, height=BUTTON_HEIGHT,
231-
label="Gmail", label_font=font, style=Button.ROUNDRECT, fill_color=0xFF0000)
232-
buttons.append(button_0)
233-
234-
235-
button_1 = Button(name='Discord',x=70, y=130,
236-
width=BUTTON_WIDTH, height=BUTTON_HEIGHT,
237-
label="Discord", label_font=font, style=Button.ROUNDRECT, fill_color=0x9900FF)
238-
buttons.append(button_1)
233+
buttons = []
239234

235+
# generate buttons
236+
btn_x = 20
237+
for i in secrets['totp_keys']:
238+
print(i)
239+
button = Button(name=i[0], x=btn_x, y=130,
240+
width=60, height=60,
241+
label=i[0], label_font=font, label_color=0x0,
242+
fill_color=None, outline_color=None)
243+
buttons.append(button)
244+
btn_x+=60
245+
246+
# append buttons to splash group
240247
for b in buttons:
241248
splash.append(b.group)
242249

243250

244-
# Get the current time in seconds since Jan 1, 1970
245-
t = time.time()
246-
print("Seconds since Jan 1, 1970: {} seconds".format(t))
247-
248-
# Instead of using RTC which means converting back and forth
249-
# we'll just keep track of seconds-elapsed-since-NTP-call
250-
mono_time = int(time.monotonic())
251-
print("Monotonic time", mono_time)
252251

253252
countdown = ON_SECONDS # how long to stay on if not in always_on mode
254253
while ALWAYS_ON or (countdown > 0):
255254
# Calculate current time based on NTP + monotonic
256255
unix_time = t - mono_time + int(time.monotonic())
257256
p = ts.touch_point
258257
if p:
259-
print(p)
260258
for i, b in enumerate(buttons):
261259
if b.contains(p):
262-
print("Button %d pressed" % i)
263260
b.selected = True
264261
for name, secret in secrets['totp_keys']:
265-
print(b.name)
266262
if b.name == name:
267-
print('button selected: ', name)
263+
# generate OTP
264+
#print('Background color: ', background_color)
268265
otp = generate_otp(unix_time // 30, secret)
269-
# TODO: This needs to get cleaned up into a one-liner
270-
otp = str(otp)
271-
formatted_otp = "{} {}".format(otp[0:3],otp[3:6])
272-
label_key.text = formatted_otp
266+
print('{} selected: '.format(name))
267+
print(name + " OTP output: ", otp)
268+
# display the key's name
273269
label_title.text = name
274-
print(name + " OTP output: ", otp) # serial debugging output
270+
# format and display the OTP
271+
label_key.text = "{} {}".format(str(otp)[0:3],str(otp)[3:6])
275272
else:
276273
b.selected = False
277274
# We'll update every 1/4 second, we can hash very fast so its no biggie!

0 commit comments

Comments
 (0)