-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
177 lines (146 loc) · 5.39 KB
/
main.py
File metadata and controls
177 lines (146 loc) · 5.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import adafruit_display_text.label
import board
import displayio
import framebufferio
import rgbmatrix
import terminalio
import time
import datetime
import random
displayio.release_displays()
matrix = rgbmatrix.RGBMatrix(
width=64, height=32, bit_depth=1,
rgb_pins=[board.D6, board.D5, board.D9, board.D11, board.D10, board.D12],
addr_pins=[board.A5, board.A4, board.A3, board.A2],
clock_pin=board.D13, latch_pin=board.D0, output_enable_pin=board.D1)
display = framebufferio.FramebufferDisplay(matrix, auto_refresh=False)
g = displayio.Group()
display.root_group = g
clouds = [{"x": random.uniform(0, 64), "y": random.randint(0, 8)} for _ in range(3)]
cloud_speed = 1
def get_text_color():
hour = datetime.datetime.now().hour
if 6 <= hour < 12:
return 0xFFA500
elif hour < 18:
return 0x00FF00
else:
return 0xFF1493
def get_time_rn():
hr = datetime.datetime.now().hour
min = str(datetime.datetime.now().minute)
if int(min) < 10:
min = '0' + min
return str(hr) + ':' + min
def update_clouds():
for cloud in clouds:
cloud["x"] -= cloud_speed # Move clouds left
if cloud["x"] < -5: # Reset if off screen
cloud["x"] = 48
cloud["y"] = random.randint(0, 8)
def fill_display():
curr_time = get_time_rn()
hour, minute = curr_time.split(":")
hour = int(hour)
bitmap = displayio.Bitmap(64, 32, 6)
if 6 <= hour < 18:
for y in range(32):
for x in range(64):
bitmap[x, y] = 2
# CLOUDS
for cloud in clouds:
x, y = int(cloud["x"]), cloud["y"] # Round x for pixel grid
for dx in range(-2, 3): # Cloud width (~5 pixels)
for dy in range(-1, 2): # Cloud height (~3 pixels)
if (0 <= x+dx < 64 and 0 <= y+dy < 32 and
not (54 <= x+dx <= 63 and 0 <= y+dy <= 9)):
bitmap[x+dx, y+dy] = 5 # Light Gray for clouds
else:
for y in range(32):
for x in range(64):
bitmap[x, y] = 3
# STARS
if random.randint(0, 30) == 1: # 1 in 30 chance of a star
bitmap[x, y] = 4
bg_palette = displayio.Palette(6)
bg_palette[0] = 0x000000
bg_palette[1] = 0x3b3b3b
bg_palette[2] = 0x00afda
bg_palette[3] = 0x1B003B
bg_palette[4] = 0xADD8E6
bg_palette[5] = 0xF0F8FF
tile_grid = displayio.TileGrid(bitmap, pixel_shader=bg_palette)
g.append(tile_grid)
def get_icon():
hour = datetime.datetime.now().hour
shape_bitmap = displayio.Bitmap(9, 9, 3) # 9x9 grid, 3 colors (0 = background, 1 = shape)
# Define the yellow parts (value = 1)
sun_pixels = [
(4, 0), (4, 8), (0, 4), (8, 4), # Cross center
(2, 2), (2, 6), (6, 2), (6, 6), # Outer corners
(4, 2), (4, 6), (2, 4), (6, 4), # Mid arms
(3, 3), (3, 5), (5, 3), (5, 5), # Inner corners
(3, 4), (4, 3), (4, 4), (4, 5), (5, 4) # Filling the middle
]
moon_pixels = [
(3, 1), (4, 1), (5, 1),
(2, 2), (3, 2), (4, 2), (5, 2), (6, 2),
(2, 3), (3, 3), (4, 3), (5, 3), (6, 3),
(1, 4), (2, 4), (3, 4), (4, 4), (5, 4), (6, 4), (7, 4),
(2, 5), (3, 5), (4, 5), (5, 5), (6, 5),
(2, 6), (3, 6), (4, 6), (5, 6), (6, 6),
(3, 7), (4, 7), (5, 7)
]
shape_p = displayio.Palette(4)
shape_p[0] = 0x00afda #day sky
shape_p[1] = 0xffb400 #sun
shape_p[2] = 0xC0C0C0 #moon
shape_p[3] = 0x1B003B #night sky
#SUN -------------------------------------------------------------------------------------------------------------------------------------------------
if 6 <= hour < 18:
for x in range(9):
for y in range(9):
if (x, y) in sun_pixels:
shape_bitmap[x, y] = 1
else:
shape_bitmap[x, y] = 0
# MOON -------------------------------------------------------------------------------------------------------------------------------------------------
else:
for x in range(9):
for y in range(9):
if (x, y) in moon_pixels:
shape_bitmap[x,y] = 2
else:
shape_bitmap[x,y] = 3
g.append(displayio.TileGrid(bitmap=shape_bitmap, pixel_shader=shape_p,
width=1,height=1,
tile_width=9,tile_height=9,
default_tile=0,
x= display.width - 10,
y=1))
def clock():
current_time = get_time_rn()
line1 = adafruit_display_text.label.Label(
font=terminalio.FONT,
color= get_text_color(),
label_direction="LTR",
scale=2,
anchor_point=(0.5,0.5),
anchored_position=(32,22),
text=current_time,
)
g.append(line1)
while True:
if 6 <= datetime.datetime.now().hour < 18: # Daytime
update_clouds() # Move clouds smoothly across the screen
fill_display() # Draw sky + clouds
else: # Nighttime
fill_display() # Just draw sky + stars (no clouds)
clock() # Always update the time
get_icon() # Sun (day) or Moon (night)
display.refresh(minimum_frames_per_second=0)
# Adjust speed: Daytime updates less frequently
if 6 <= datetime.datetime.now().hour < 18:
time.sleep(0.5) # Slower updates in the day
else:
time.sleep(0.1) # Faster updates at night