Skip to content

Commit 4d73087

Browse files
author
Alvaro Figueroa
committed
First version of PyPortal_Electioncal_US
1 parent cd37f84 commit 4d73087

File tree

7 files changed

+13575
-0
lines changed

7 files changed

+13575
-0
lines changed

PyPortal_Electioncal_US/code.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import sys
2+
import time
3+
import board
4+
from adafruit_pyportal import PyPortal
5+
cwd = ("/"+__file__).rsplit('/', 1)[0] # the current working directory (where this file is)
6+
sys.path.append(cwd)
7+
import electioncal_graphics # pylint: disable=wrong-import-position
8+
9+
# Optional, to take a screenshot to SD card
10+
#from adafruit_bitmapsaver import save_pixels
11+
#import storage
12+
#import busio
13+
14+
# Get wifi details and more from a secrets.py file
15+
try:
16+
from secrets import secrets
17+
except ImportError:
18+
print("WiFi secrets are kept in secrets.py, please add them there!")
19+
raise
20+
21+
STATE="hawaii"
22+
COUNTY="hawaii"
23+
24+
DATA_SOURCE = "https://electioncal.us/en/" + STATE +"/" + COUNTY + "/voter.json"
25+
DATA_LOCATION = []
26+
27+
# Initialize the pyportal object and let us know what data to fetch and where
28+
# to display it
29+
pyportal = PyPortal(url=DATA_SOURCE,
30+
json_path=DATA_LOCATION,
31+
status_neopixel=board.NEOPIXEL,
32+
default_bg=0x000000)
33+
34+
gfx = electioncal_graphics.Electioncal_Graphics(pyportal.splash, am_pm=True)
35+
36+
display_refresh = None
37+
while True:
38+
# only query the online time once per hour (and on first run)
39+
if (not display_refresh) or (time.monotonic() - display_refresh) > 3600:
40+
try:
41+
print("Getting time from internet!")
42+
pyportal.get_local_time()
43+
display_refresh = time.monotonic()
44+
except RuntimeError as e:
45+
print("Some error occured, retrying! -", e)
46+
continue
47+
48+
try:
49+
value = pyportal.fetch()
50+
print("Response is", value)
51+
gfx.display_elections(value, STATE, COUNTY)
52+
except RuntimeError as e:
53+
print("Some error occured, retrying! -", e)
54+
continue
55+
56+
gfx.update_time()
57+
# Optional: to take screenshot to SD card
58+
#storage.remount("/", False)
59+
#print('Taking Screenshot...')
60+
#save_pixels('/screenshot.bmp')
61+
#print('Screenshot taken')
62+
63+
time.sleep(60) # wait 60 seconds before updating anything again
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
import time
2+
import json
3+
import displayio
4+
from adafruit_display_text.label import Label
5+
from adafruit_bitmap_font import bitmap_font
6+
7+
cwd = ("/"+__file__).rsplit('/', 1)[0] # the current working directory (where this file is)
8+
9+
small_font = cwd+"/fonts/Arial-12.bdf"
10+
medium_font = cwd+"/fonts/Arial-16.bdf"
11+
12+
class Electioncal_Graphics(displayio.Group):
13+
def __init__(self, root_group, *, am_pm=True):
14+
super().__init__(max_size=2)
15+
self.am_pm = am_pm
16+
root_group.append(self)
17+
self._icon_group = displayio.Group(max_size=1)
18+
self.append(self._icon_group)
19+
self._text_group = displayio.Group(max_size=9)
20+
self.append(self._text_group)
21+
22+
self._icon_sprite = None
23+
self._icon_file = None
24+
self.set_icon(cwd+"/icons/us-sat.bmp")
25+
26+
self.small_font = bitmap_font.load_font(small_font)
27+
self.medium_font = bitmap_font.load_font(medium_font)
28+
glyphs = b'0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-,.: '
29+
self.small_font.load_glyphs(glyphs)
30+
self.medium_font.load_glyphs(glyphs)
31+
32+
self.date_text = Label(self.small_font, max_glyphs=21)
33+
self.date_text.x = 10
34+
self.date_text.y = 220
35+
self.date_text.color = 0xFFFFFF
36+
self._text_group.append(self.date_text)
37+
38+
self.state_text = Label(self.small_font, max_glyphs=60)
39+
self.state_text.x = 10
40+
self.state_text.y = 10
41+
self.state_text.color = 0xFFFFFF
42+
self._text_group.append(self.state_text)
43+
44+
self.county_text = Label(self.small_font, max_glyphs=60)
45+
self.county_text.x = 10
46+
self.county_text.y = 35
47+
self.county_text.color = 0xFFFFFF
48+
self._text_group.append(self.county_text)
49+
50+
self.date0_name_text = Label(self.small_font, max_glyphs=60)
51+
self.date0_name_text.x = 10
52+
self.date0_name_text.y = 80
53+
self.date0_name_text.color = 0xFFFFFF
54+
self._text_group.append(self.date0_name_text)
55+
56+
self.date0_date_text = Label(self.medium_font, max_glyphs=11)
57+
self.date0_date_text.x = 10
58+
self.date0_date_text.y = 105
59+
self.date0_date_text.color = 0xFFFFFF
60+
self._text_group.append(self.date0_date_text)
61+
62+
self.date1_name_text = Label(self.small_font, max_glyphs=60)
63+
self.date1_name_text.x = 10
64+
self.date1_name_text.y = 140
65+
self.date1_name_text.color = 0xFFFFFF
66+
self._text_group.append(self.date1_name_text)
67+
68+
self.date1_date_text = Label(self.medium_font, max_glyphs=11)
69+
self.date1_date_text.x = 10
70+
self.date1_date_text.y = 165
71+
self.date1_date_text.color = 0xFFFFFF
72+
self._text_group.append(self.date1_date_text)
73+
74+
def display_elections(self, electioncal_data, STATE, COUNTY):
75+
electioncal = json.loads(electioncal_data)
76+
77+
self.update_time()
78+
self.state_text.text = "State: " + STATE
79+
self.county_text.text = "County: " + COUNTY
80+
self.date0_name_text.text = electioncal["dates"][0]["name"]
81+
self.date0_date_text.text = electioncal["dates"][0]["date"]
82+
self.date1_name_text.text = electioncal["dates"][1]["name"]
83+
self.date1_date_text.text = electioncal["dates"][1]["date"]
84+
85+
def update_time(self):
86+
"""Fetch the time.localtime(), parse it out and update the display text"""
87+
now = time.localtime()
88+
hour = now[3]
89+
minute = now[4]
90+
year = now[0]
91+
month = now[1]
92+
day = now[2]
93+
time_format_str = "%d:%02d"
94+
date_format_str = "%d-%02d-%02d"
95+
if self.am_pm:
96+
if hour >= 12:
97+
hour -= 12
98+
time_format_str = time_format_str+" PM"
99+
else:
100+
time_format_str = time_format_str+" AM"
101+
if hour == 0:
102+
hour = 12
103+
time_str = time_format_str % (hour, minute)
104+
date_str = date_format_str % (year, month, day)
105+
self.date_text.text = "Today is: " + date_str
106+
107+
def set_icon(self, filename):
108+
"""The background image to a bitmap file.
109+
110+
:param filename: The filename of the chosen icon
111+
112+
"""
113+
print("Set icon to ", filename)
114+
if self._icon_group:
115+
self._icon_group.pop()
116+
117+
if not filename:
118+
return # we're done, no icon desired
119+
if self._icon_file:
120+
self._icon_file.close()
121+
self._icon_file = open(filename, "rb")
122+
icon = displayio.OnDiskBitmap(self._icon_file)
123+
try:
124+
self._icon_sprite = displayio.TileGrid(icon,
125+
pixel_shader=displayio.ColorConverter())
126+
except TypeError:
127+
self._icon_sprite = displayio.TileGrid(icon,
128+
pixel_shader=displayio.ColorConverter(),
129+
position=(0,0))
130+
self._icon_group.append(self._icon_sprite)

0 commit comments

Comments
 (0)