Skip to content

Commit 359bf75

Browse files
author
brentru
committed
remove old code, add code with buttons, still needs to dyn. generate buttons
1 parent 75189bd commit 359bf75

2 files changed

Lines changed: 77 additions & 231 deletions

File tree

PyPortal_TOTP_Friend/code.py

Lines changed: 77 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,17 @@
1111
import terminalio
1212
from adafruit_binascii import hexlify, unhexlify
1313
from adafruit_bitmap_font import bitmap_font
14+
from adafruit_button import Button
1415
from adafruit_display_shapes.circle import Circle
1516
from adafruit_display_shapes.roundrect import RoundRect
1617
from adafruit_display_text.label import Label
1718
from adafruit_esp32spi import adafruit_esp32spi
1819
from adafruit_ntp import NTP
1920
from adafruit_pyportal import PyPortal
2021

22+
# Background/Images
23+
BACKGROUND = 0x059ACE
24+
2125
# Initialize PyPortal Display
2226
display = board.DISPLAY
2327

@@ -149,24 +153,41 @@ def generate_otp(int_input, secret_key, digits=6):
149153

150154
# TODO: Add press-able icon group
151155

156+
BACKGROUND = BACKGROUND if isinstance(BACKGROUND, int) else 0x000000
157+
bg_bitmap = displayio.Bitmap(display.width, display.height, 1)
158+
bg_palette = displayio.Palette(1)
159+
bg_palette[0] = BACKGROUND
160+
background = displayio.TileGrid(bg_bitmap, pixel_shader=bg_palette)
161+
152162
# Text DisplayIO
153-
text_group = displayio.Group(max_size=100)
163+
splash = displayio.Group(max_size=100)
164+
165+
splash.append(background)
166+
154167

155-
# TOTP Key Label, scaled 5x
156168
key_group = displayio.Group(scale=5)
157169
# We'll use a default text placeholder for this label
158170
label_key = Label(font, text="000 000")
159-
label_key.x = (display.width // 2) // 15
160-
label_key.y = 5
171+
label_key.x = (display.width // 2) // 13
172+
label_key.y = 15
161173
key_group.append(label_key)
162-
text_group.append(key_group)
163174

164-
# Create a label for the status
175+
label_title = Label(font, max_glyphs=14)
176+
label_title.x = (display.width // 2) // 10
177+
label_title.y = 5
178+
key_group.append(label_title)
179+
180+
splash.append(key_group)
181+
182+
# Create a label to monitor the status
165183
label_status = Label(font, max_glyphs=45)
166184
label_status.x = (display.width // 2) - 50
167-
label_status.y = 75
168-
text_group.append(label_status)
169-
display.show(text_group)
185+
label_status.y = 120
186+
splash.append(label_status)
187+
188+
# Show the group
189+
display.show(splash)
190+
170191

171192
print("Connecting to AP...")
172193
label_status.text = "Connecting to AP..."
@@ -189,11 +210,36 @@ def generate_otp(int_input, secret_key, digits=6):
189210
# keep retrying until a valid time is returned
190211
while not ntp.valid_time:
191212
ntp.set_time()
192-
label_status.text = "NTP Fetch Failed, retrying.."
193213
print("Failed to obtain time, retrying in 5 seconds...")
194214
time.sleep(5)
195215

196-
label_status.text = "NTP Set!"
216+
# Clear the status label
217+
label_status.text = ""
218+
219+
# Add buttons to the interface
220+
# TODO: Generate them like in https://learn.adafruit.com/pyportal-philips-hue-lighting-controller/code-walkthrough
221+
# TODO: Make these dynamically based on what is store in secrets.py
222+
# TODO: Add icons to buttons instead of text
223+
224+
BUTTON_WIDTH = 60
225+
BUTTON_HEIGHT = 60
226+
BUTTON_MARGIN = 20
227+
buttons = []
228+
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)
239+
240+
for b in buttons:
241+
splash.append(b.group)
242+
197243

198244
# Get the current time in seconds since Jan 1, 1970
199245
t = time.time()
@@ -208,15 +254,26 @@ def generate_otp(int_input, secret_key, digits=6):
208254
while ALWAYS_ON or (countdown > 0):
209255
# Calculate current time based on NTP + monotonic
210256
unix_time = t - mono_time + int(time.monotonic())
211-
# print("Unix time: ", unix_time)
212-
for name, secret in secrets['totp_keys']:
213-
otp = generate_otp(unix_time // 30, secret)
214-
# TODO: This needs to get cleaned up into a one-liner
215-
otp = str(otp)
216-
formatted_otp = "{} {}".format(otp[0:3],otp[3:6])
217-
label_key.text = formatted_otp
218-
label_status.text = name
219-
print(name + " OTP output: ", otp) # serial debugging output
257+
p = ts.touch_point
258+
if p:
259+
print(p)
260+
for i, b in enumerate(buttons):
261+
if b.contains(p):
262+
print("Button %d pressed" % i)
263+
b.selected = True
264+
for name, secret in secrets['totp_keys']:
265+
print(b.name)
266+
if b.name == name:
267+
print('button selected: ', name)
268+
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
273+
label_title.text = name
274+
print(name + " OTP output: ", otp) # serial debugging output
275+
else:
276+
b.selected = False
220277
# We'll update every 1/4 second, we can hash very fast so its no biggie!
221278
countdown -= 0.25
222279
time.sleep(0.25)

PyPortal_TOTP_Friend/limor_code.py

Lines changed: 0 additions & 211 deletions
This file was deleted.

0 commit comments

Comments
 (0)