-
Notifications
You must be signed in to change notification settings - Fork 0
/
code.py
executable file
·127 lines (119 loc) · 4.38 KB
/
code.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
"""
Main module for the Gen Con 2019 PyPortal badge.
Setup the PyPortal with a connection to a time api for the countdown clock functionality
and then monitor for button events and route to the menu.py module.
For more information about the PyPortal see https://learn.adafruit.com/adafruit-pyportal
"""
import board
from random import randint
from time import sleep
from analogio import AnalogIn
from adafruit_pyportal import PyPortal
from utils import countdown_formatter
from menu import new_screen, init_home_screen, RETURN_MENU
try:
from secrets import secrets # noqa
except ImportError:
print("WiFi secrets are kept in secrets.py, please add them there!")
raise
# Setup light sensor.
light_sensor = AnalogIn(board.LIGHT)
mode = 0
mode_change = None
initial_light_value = light_sensor.value
# Setup portal with time feed.
pyportal = PyPortal(
url="http://worldtimeapi.org/api/timezone/America/Indiana/Indianapolis",
json_path=["day_of_year"],
default_bg=0xFFFFFF,
)
pyportal.preload_font()
# Render default home menu
buttons = init_home_screen(pyportal)
# Setup our super loop to manage screen brightness and monitor our home menu for
# button events.
while True:
# Calibrate light sensor on start to deal with different lighting situations
# If the mode change isn't responding properly, reset your PyPortal to recalibrate
if light_sensor.value < (initial_light_value * 0.3) and mode_change is None:
mode_change = "mode_change"
if (
light_sensor.value > (initial_light_value * 0.5)
and mode_change == "mode_change"
):
mode += 1
mode_change = None
if mode > 2:
mode = 0
touch = pyportal.touchscreen.touch_point
# In the case of a button event figure out which button was pressed and render
# that buttons screen.
if touch:
for button in buttons:
if button.contains(touch):
if button.name == "exhibit_hall_map":
buttons = new_screen(
pyportal,
new_background="/images/2019-exhibit-map.bmp",
menu=RETURN_MENU,
)
break
elif button.name == "con_map":
buttons = new_screen(
pyportal,
new_background="/images/2019-con-map.bmp",
menu=RETURN_MENU,
)
break
elif button.name == "schedule":
buttons = new_screen(
pyportal,
new_background="/images/2019-schedule.bmp",
menu=RETURN_MENU,
)
break
elif button.name == "d20":
buttons = new_screen(
pyportal,
new_background="/images/d20.bmp",
menu=[
(
str(randint(1, 20)),
"return",
(138, 100),
(45, 45),
(255, 255, 255),
0xFFFFFF,
0x0,
)
],
)
break
elif button.name == "countdown":
day = pyportal.fetch()
buttons = new_screen(
pyportal,
new_background="/images/gen-con-logo.bmp",
menu=[
(
countdown_formatter(day),
"return",
(40, 200),
(240, 40),
(0, 0, 0),
0x0,
0xFFFFFF,
)
],
)
break
elif button.name == "badge":
buttons = new_screen(
pyportal,
new_background="/images/2019-con-badge.bmp",
menu=RETURN_MENU,
)
break
else:
break
sleep(0.5)