Skip to content

Instantly share code, notes, and snippets.

@dglaude
Created Dec 1, 2021
Embed
What would you like to do?
Belgian vaccination status for MagTag
from adafruit_magtag.magtag import MagTag
from adafruit_progressbar.progressbar import ProgressBar
# BELGIUM
POPULATION = 11589616
DATA_SOURCE = "https://raw.githubusercontent.com/owid/covid-19-data/master/public/data/vaccinations/country_data/Belgium.csv"
magtag = MagTag(url=DATA_SOURCE)
magtag.network.connect()
magtag.add_text(
text_font="/fonts/ncenR14.pcf",
text_position=(
(magtag.graphics.display.width // 2) - 1,
8,
),
text_anchor_point=(0.5, 0.5),
is_data=False,
) # Location Date
magtag.add_text(
text_font="/fonts/ncenR14.pcf",
text_position=(
(magtag.graphics.display.width // 2) - 1,
25,
),
text_anchor_point=(0.5, 0.5),
is_data=False,
) # Vaccinated text
magtag.add_text(
text_font="/fonts/ncenR14.pcf",
text_position=(
(magtag.graphics.display.width // 2) - 1,
60,
),
text_anchor_point=(0.5, 0.5),
is_data=False,
) # Fully vaccinated text
magtag.add_text(
text_font="/fonts/ncenR14.pcf",
text_position=(
(magtag.graphics.display.width // 2) - 1,
95,
),
text_anchor_point=(0.5, 0.5),
is_data=False,
) # Booster text
BAR_WIDTH = magtag.graphics.display.width - 80
BAR_HEIGHT = 15
BAR_X = magtag.graphics.display.width // 2 - BAR_WIDTH // 2
progress_bar_1 = ProgressBar(
BAR_X, 35, BAR_WIDTH, BAR_HEIGHT, 1.0, bar_color=0x999999, outline_color=0x000000
)
progress_bar_2 = ProgressBar(
BAR_X, 70, BAR_WIDTH, BAR_HEIGHT, 1.0, bar_color=0x999999, outline_color=0x000000
)
progress_bar_3 = ProgressBar(
BAR_X, 105, BAR_WIDTH, BAR_HEIGHT, 1.0, bar_color=0x999999, outline_color=0x000000
)
magtag.graphics.splash.append(progress_bar_1)
magtag.graphics.splash.append(progress_bar_2)
magtag.graphics.splash.append(progress_bar_3)
#magtag.graphics.set_background("/bmps/background.bmp")
def l_split(line):
line_list = []
print(line)
while "," in line:
if line[0] == '"':
temp = line.split('"', 2)[1]
line_list.append(temp)
line = line.split('"', 2)[2][1:]
else:
temp, line = line.split(",", 1)
line_list.append(temp)
line_list.append(line)
return line_list
try:
table = magtag.fetch().split("\n")
columns = l_split(table[0])
latest = l_split(table[-2])
print(columns)
print(latest)
value = dict(zip(columns, latest))
print("Response is", value)
print(value)
vaccinated = int(value["people_vaccinated"]) / POPULATION
fully_vaccinated = int(value["people_fully_vaccinated"]) / POPULATION
total_boosters = int(value["total_boosters"]) / POPULATION
magtag.set_text(f"{value['location']} - {value['date']}", 0, False)
magtag.set_text("Vaccinated: {:.2f}%".format(vaccinated * 100), 1, False)
magtag.set_text("Fully Vaccinated: {:.2f}%".format(fully_vaccinated * 100), 2, False)
magtag.set_text("Booster: {:.2f}%".format(total_boosters * 100), 3, False)
progress_bar_1.progress = vaccinated
progress_bar_2.progress = fully_vaccinated
progress_bar_3.progress = total_boosters
magtag.refresh()
SECONDS_TO_SLEEP = 24 * 60 * 60 # Sleep for one day
except (ValueError, RuntimeError) as e:
print("Some error occured, retrying in one hour! -", e)
seconds_to_sleep = 60 * 60 # Sleep for one hour
print(f"Sleeping for {SECONDS_TO_SLEEP} seconds")
magtag.exit_and_deep_sleep(SECONDS_TO_SLEEP)
@dglaude
Copy link
Author

dglaude commented Dec 1, 2021

Based on https://learn.adafruit.com/adafruit-magtag-covid-vaccination-percent-tracker/featured_products
But I made change for Belgium, added the booster percentage.
In the battle, I lost the background image (might be due to the upgrade I did, and API change).
Would require more effort to reconcile my version and Adafruit version, but that is for another day.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment