Skip to content

Commit eccf0da

Browse files
committed
adding magtag menorah code and bitmap
1 parent 61a70cf commit eccf0da

File tree

2 files changed

+122
-0
lines changed

2 files changed

+122
-0
lines changed

MagTag/MagTag_IoT_Menorah/code.py

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
# SPDX-FileCopyrightText: 2025 Liz Clark for Adafruit Industries
2+
#
3+
# SPDX-License-Identifier: MIT
4+
5+
"""MagTag IoT Menorah"""
6+
import time
7+
import displayio
8+
from adafruit_magtag.magtag import MagTag
9+
from adafruit_display_shapes.circle import Circle
10+
11+
# latitude
12+
lat = 42.36
13+
# longitude
14+
long = -71.06
15+
# timezone offset from GMT
16+
tz_offset = -5
17+
hanukkah_date = [12, 14] # month, date
18+
# open meteo API for sunset, today and tomorrow
19+
sunset_fetch = (f"https://api.open-meteo.com/v1/forecast?"
20+
f"latitude={lat}&longitude={long}&daily=sunset"
21+
f"&timezone=auto&forecast_days=2&timeformat=unixtime")
22+
today_sunset = ["daily", "sunset", 0]
23+
tomorrow_sunset = ["daily", "sunset", 1]
24+
# create MagTag and connect to network
25+
try:
26+
magtag = MagTag(
27+
url=sunset_fetch,
28+
json_path=(today_sunset, tomorrow_sunset),
29+
default_bg=0x000000,
30+
)
31+
magtag.network.connect()
32+
except (ConnectionError, ValueError, RuntimeError) as e:
33+
print("*** MagTag(), Some error occured, retrying! -", e)
34+
# Exit program and restart in 1 seconds.
35+
magtag.exit_and_deep_sleep(1)
36+
37+
# displayio groups
38+
group = displayio.Group()
39+
menorah_group = displayio.Group()
40+
circle_group = displayio.Group()
41+
42+
# import menorah bitmap
43+
filename = "/magtag_menorah.bmp"
44+
menorah = displayio.OnDiskBitmap(filename)
45+
menorah_grid = displayio.TileGrid(menorah, pixel_shader=menorah.pixel_shader)
46+
47+
# add bitmap to its group
48+
menorah_group.append(menorah_grid)
49+
# add menorah group to the main group
50+
group.append(menorah_group)
51+
52+
# list of circle positions
53+
spots = (
54+
(148, 16), # shamash
55+
(272, 31), # 1st
56+
(242, 31), # 2nd
57+
(212, 31), # 3rd
58+
(182, 31), # 4th
59+
(114, 31), # 5th
60+
(84, 31), # 6th
61+
(54, 31), # 7th
62+
(24, 31), # 8th
63+
)
64+
65+
# creating the circles & pulling in positions from spots
66+
for spot in spots:
67+
circle = Circle(x0=spot[0], y0=spot[1], r=13, fill=0xFFFFFF)
68+
# adding circles to their display group
69+
circle_group.append(circle)
70+
71+
# adding circles group to main display group
72+
group.append(circle_group)
73+
74+
# grabs time from network
75+
magtag.get_local_time()
76+
# parses time into month, date, etc
77+
now = time.localtime()
78+
print(f"now is {now}")
79+
month = now[1]
80+
day = now[2]
81+
day_count = 0
82+
seconds_to_sleep = 3600
83+
# check if its hanukkah
84+
if month == hanukkah_date[0]:
85+
# get the night count for hanukkah
86+
if hanukkah_date[1] <= day <= hanukkah_date[1] + 8:
87+
day_count = (day - hanukkah_date[1]) + 1
88+
print(f"it's the {day_count} night of hanukkah!")
89+
elif day > hanukkah_date[1] + 8:
90+
day_count = 8
91+
unix_now = time.mktime(now)
92+
# adjust unixtime to your timezone (otherwise in GMT-0)
93+
unix_now = unix_now + -(tz_offset*3600)
94+
print(unix_now)
95+
sunsets = magtag.fetch()
96+
if unix_now < sunsets[0]:
97+
seconds_to_sleep = sunsets[0] - unix_now
98+
# don't light the next candle until sunset
99+
if 0 < day_count < 8:
100+
day_count -= 1
101+
print("the sun is still up")
102+
else:
103+
seconds_to_sleep = sunsets[1] - unix_now
104+
if day_count > 0:
105+
# sets colors of circles to transparent to reveal flames
106+
for i in range(day_count + 1):
107+
circle_group[i].fill = None
108+
time.sleep(0.1)
109+
110+
# updates display with bitmap and current candles
111+
magtag.display.root_group = group
112+
time.sleep(5)
113+
magtag.display.refresh()
114+
time.sleep(5)
115+
116+
# goes into deep sleep till next sunset
117+
print("entering deep sleep")
118+
print(f"sleeping for {seconds_to_sleep} seconds")
119+
magtag.exit_and_deep_sleep(seconds_to_sleep)
120+
121+
# entire code will run again after deep sleep cycle
122+
# similar to hitting the reset button
37.1 KB
Binary file not shown.

0 commit comments

Comments
 (0)