|
| 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