Skip to content

Commit 284d41a

Browse files
author
Alvaro Figueroa
committed
Paging, line splitting, date validation, new background. Ready for testing.
1 parent 4d73087 commit 284d41a

File tree

5 files changed

+6138
-47
lines changed

5 files changed

+6138
-47
lines changed

PyPortal_Electioncal_US/code.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@
1818
print("WiFi secrets are kept in secrets.py, please add them there!")
1919
raise
2020

21-
STATE="hawaii"
22-
COUNTY="hawaii"
21+
#STATE="puerto_rico"
22+
#COUNTY="aguada"
23+
STATE="texas"
24+
COUNTY="andrews"
2325

2426
DATA_SOURCE = "https://electioncal.us/en/" + STATE +"/" + COUNTY + "/voter.json"
2527
DATA_LOCATION = []
@@ -31,8 +33,8 @@
3133
status_neopixel=board.NEOPIXEL,
3234
default_bg=0x000000)
3335

34-
gfx = electioncal_graphics.Electioncal_Graphics(pyportal.splash, am_pm=True)
3536

37+
gfx = electioncal_graphics.Electioncal_Graphics(pyportal.splash, am_pm=True)
3638
display_refresh = None
3739
while True:
3840
# only query the online time once per hour (and on first run)
@@ -47,17 +49,21 @@
4749

4850
try:
4951
value = pyportal.fetch()
50-
print("Response is", value)
51-
gfx.display_elections(value, STATE, COUNTY)
52+
#print("Response is", value)
53+
gfx.load_data(value)
5254
except RuntimeError as e:
5355
print("Some error occured, retrying! -", e)
5456
continue
57+
try:
58+
gfx.elections_cycle()
59+
except RuntimeError as e:
60+
print("Some error ocurred, retrying! -", e)
61+
continue
5562

56-
gfx.update_time()
5763
# Optional: to take screenshot to SD card
5864
#storage.remount("/", False)
5965
#print('Taking Screenshot...')
6066
#save_pixels('/screenshot.bmp')
6167
#print('Screenshot taken')
6268

63-
time.sleep(60) # wait 60 seconds before updating anything again
69+
#time.sleep(60) # wait 60 seconds before updating anything again

PyPortal_Electioncal_US/electioncal_graphics.py

Lines changed: 57 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,21 @@ def __init__(self, root_group, *, am_pm=True):
2121

2222
self._icon_sprite = None
2323
self._icon_file = None
24-
self.set_icon(cwd+"/icons/us-sat.bmp")
24+
self.set_icon(cwd+"/icons/electioncal.bmp")
2525

2626
self.small_font = bitmap_font.load_font(small_font)
2727
self.medium_font = bitmap_font.load_font(medium_font)
2828
glyphs = b'0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-,.: '
2929
self.small_font.load_glyphs(glyphs)
3030
self.medium_font.load_glyphs(glyphs)
3131

32+
self.url_text = Label(self.small_font, max_glyphs=35)
33+
self.url_text.x = 10
34+
self.url_text.y = 195
35+
self.url_text.color = 0xFFFFFF
36+
self._text_group.append(self.url_text)
37+
self.url_text.text = "Visit us at https://electioncal.us"
38+
3239
self.date_text = Label(self.small_font, max_glyphs=21)
3340
self.date_text.x = 10
3441
self.date_text.y = 220
@@ -41,46 +48,39 @@ def __init__(self, root_group, *, am_pm=True):
4148
self.state_text.color = 0xFFFFFF
4249
self._text_group.append(self.state_text)
4350

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)
51+
self.election_date_text = Label(self.medium_font, max_glyphs=11)
52+
self.election_date_text.x = 10
53+
self.election_date_text.y = 60
54+
self.election_date_text.color = 0xFFFFFF
55+
self._text_group.append(self.election_date_text)
56+
57+
self.election_name_text = Label(self.small_font, max_glyphs=60)
58+
self.election_name_text.x = 10
59+
self.election_name_text.y = 95
60+
self.election_name_text.color = 0xFFFFFF
61+
self._text_group.append(self.election_name_text)
62+
63+
self.election_name_text_line2 = Label(self.small_font, max_glyphs=60)
64+
self.election_name_text_line2.x = 10
65+
self.election_name_text_line2.y = 120
66+
self.election_name_text_line2.color = 0xFFFFFF
67+
self._text_group.append(self.election_name_text_line2)
68+
7669

70+
def load_data(self, election_data):
71+
self.electioncal = json.loads(election_data)
72+
self.state_text.text = self.electioncal["dates"][0]["state"] + " State, " + self.electioncal["dates"][1]["county"]
73+
74+
def elections_cycle(self):
7775
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"]
76+
num_elections = len(self.electioncal["dates"])
77+
78+
for i in range(0,num_elections):
79+
if self.date_text.text[10:] < self.electioncal["dates"][i]["date"]:
80+
self.election_date_text.text = self.electioncal["dates"][i]["date"]
81+
# splitting the line at around 40 chars seems ok for regular PyPortal
82+
self.election_name_text_line2.text, self.election_name_text.text = self.paragrapher(self.electioncal["dates"][i]["name"], 40)
83+
time.sleep(4)
8484

8585
def update_time(self):
8686
"""Fetch the time.localtime(), parse it out and update the display text"""
@@ -104,6 +104,23 @@ def update_time(self):
104104
date_str = date_format_str % (year, month, day)
105105
self.date_text.text = "Today is: " + date_str
106106

107+
def paragrapher(self, text, cut):
108+
""" Cuts a long line into two, having spaces in mind.
109+
Note we return line2 first as it looks better to clear the line2
110+
before printing a line1 with empty line2
111+
We run from cut, backwards till we find a space.
112+
"""
113+
if len(text) > cut:
114+
for i in range(cut,0,-1):
115+
if text[i] == " ":
116+
break
117+
line1 = text[0:i]
118+
line2 = text[i+1:80]
119+
else:
120+
line1 = text
121+
line2 = ""
122+
return line2, line1
123+
107124
def set_icon(self, filename):
108125
"""The background image to a bitmap file.
109126
@@ -127,4 +144,4 @@ def set_icon(self, filename):
127144
self._icon_sprite = displayio.TileGrid(icon,
128145
pixel_shader=displayio.ColorConverter(),
129146
position=(0,0))
130-
self._icon_group.append(self._icon_sprite)
147+
self._icon_group.append(self._icon_sprite)

0 commit comments

Comments
 (0)