Skip to content

Commit 59524df

Browse files
authored
Merge pull request #3163 from FoamyGuy/50cent_cpi_data
MagTag 50 Cent CPI project files
2 parents 7e3eaf1 + ec9d79b commit 59524df

File tree

2 files changed

+158
-0
lines changed

2 files changed

+158
-0
lines changed
12.8 KB
Binary file not shown.

MagTag/MagTag_50Cent_CPI/code.py

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
# SPDX-FileCopyrightText: 2025 Tim C, written for Adafruit Industries
2+
#
3+
# SPDX-License-Identifier: MIT
4+
"""
5+
50 Cent Consumer Price Index Tracker
6+
7+
This project illustrates how to fetch CPI data from the
8+
US Bureau of Labor Statistics API. CPI data from 1994,
9+
when 50 Cent adopted his moniker, is compared with the latest
10+
data to determine how much 50 Cent from 1994 is worth today.
11+
12+
This project was inspired by: https://50centadjustedforinflation.com/
13+
"""
14+
import json
15+
import os
16+
import time
17+
18+
import alarm
19+
import displayio
20+
from displayio import OnDiskBitmap, TileGrid, Group
21+
import supervisor
22+
from terminalio import FONT
23+
import wifi
24+
25+
import adafruit_connection_manager
26+
from adafruit_display_text.text_box import TextBox
27+
import adafruit_requests
28+
29+
30+
# CPI Data URL
31+
latest_cpi_url = "https://api.bls.gov/publicAPI/v2/timeseries/data/CUUR0000SA0"
32+
# CUUR0000SA0 the series ID for CPI-U (All items)
33+
# Note: bls.gov API is limited to 20 requests per day for unregistered users
34+
35+
# CPI value for June 1994
36+
june_94_cpi = 148.0
37+
38+
# Get WiFi details, ensure these are setup in settings.toml
39+
ssid = os.getenv("CIRCUITPY_WIFI_SSID")
40+
password = os.getenv("CIRCUITPY_WIFI_PASSWORD")
41+
42+
# Initalize Wifi, Socket Pool, Request Session
43+
pool = adafruit_connection_manager.get_radio_socketpool(wifi.radio)
44+
ssl_context = adafruit_connection_manager.get_radio_ssl_context(wifi.radio)
45+
requests = adafruit_requests.Session(pool, ssl_context)
46+
47+
try:
48+
# Connect to the WiFi network
49+
wifi.radio.connect(ssid, password)
50+
except OSError as ose:
51+
print(f"OSError: {ose}")
52+
print("Wifi connected!")
53+
54+
# Get display reference
55+
display = supervisor.runtime.display
56+
57+
# Set to portrait orientation
58+
display.rotation = 0
59+
60+
# Group to hold all visual elements
61+
main_group = Group()
62+
63+
# Full display sized white background
64+
white_bg_group = Group(scale=8)
65+
white_bg_bmp = displayio.Bitmap(display.width // 8, display.height // 8, 1)
66+
white_bg_palette = displayio.Palette(1)
67+
white_bg_palette[0] = 0xFFFFFF
68+
white_bg_tg = TileGrid(bitmap=white_bg_bmp, pixel_shader=white_bg_palette)
69+
white_bg_group.append(white_bg_tg)
70+
main_group.append(white_bg_group)
71+
72+
# 50 Cent photo
73+
photo_bmp = OnDiskBitmap("50_cent.bmp")
74+
photo_tg = TileGrid(bitmap=photo_bmp, pixel_shader=photo_bmp.pixel_shader)
75+
main_group.append(photo_tg)
76+
77+
78+
def get_latest_cpi_value():
79+
"""
80+
Fetch the latest CPI data from BLS API.
81+
82+
:return: tuple containing (latest CPI value, month, year)
83+
"""
84+
try:
85+
response = requests.get(latest_cpi_url)
86+
try:
87+
json_data = response.json()
88+
except json.decoder.JSONDecodeError as json_error:
89+
raise Exception(f"JSON Parse error: {response.text}") from json_error
90+
91+
latest_data = json_data["Results"]["series"][0]["data"][0]
92+
93+
return (
94+
float(latest_data["value"]),
95+
latest_data["periodName"],
96+
latest_data["year"],
97+
)
98+
99+
except Exception as e:
100+
raise Exception(f"Error fetching data from BLS API: {e}") from e
101+
102+
103+
# fetch the latest CPI data
104+
latest_cpi_value, month, year = get_latest_cpi_value()
105+
106+
# Label for the message below the photo
107+
message_lbl = TextBox(
108+
FONT,
109+
128,
110+
TextBox.DYNAMIC_HEIGHT,
111+
align=TextBox.ALIGN_CENTER,
112+
text=f"As of {month} {year}\n50 Cent is worth",
113+
color=0x000000,
114+
scale=1,
115+
)
116+
message_lbl.anchor_point = (0, 0)
117+
message_lbl.anchored_position = (0, photo_tg.tile_height + 6)
118+
main_group.append(message_lbl)
119+
120+
# calculate current value of 50 cents from 1994
121+
current_value = round(50.0 * (latest_cpi_value / june_94_cpi) + 0.5)
122+
123+
# Label for the calculated value
124+
amount_lbl = TextBox(
125+
FONT,
126+
128 // 3,
127+
TextBox.DYNAMIC_HEIGHT,
128+
align=TextBox.ALIGN_CENTER,
129+
text=f"{current_value}",
130+
color=0x000000,
131+
scale=3,
132+
)
133+
amount_lbl.anchor_point = (0, 0)
134+
amount_lbl.anchored_position = (0, photo_tg.tile_height + message_lbl.height + 12)
135+
main_group.append(amount_lbl)
136+
137+
# Cent label at bottom of display
138+
cent_lbl = TextBox(
139+
FONT,
140+
128,
141+
TextBox.DYNAMIC_HEIGHT,
142+
align=TextBox.ALIGN_CENTER,
143+
text="Cent",
144+
color=0x000000,
145+
scale=1,
146+
)
147+
cent_lbl.anchor_point = (0, 1)
148+
cent_lbl.anchored_position = (0, display.height - 2)
149+
main_group.append(cent_lbl)
150+
151+
# set main_group showing on the display
152+
display.root_group = main_group
153+
display.refresh()
154+
155+
# Sleep for 1 day
156+
al = alarm.time.TimeAlarm(monotonic_time=time.monotonic() + 86400)
157+
alarm.exit_and_deep_sleep_until_alarms(al)
158+
# Does not return. Exits, and restarts after 1 day

0 commit comments

Comments
 (0)