-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
120 lines (96 loc) · 3.89 KB
/
main.py
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
# Created by Sam Green
# Date: 17-05-2024
# This code uses the users current time/latitude/longitude and calculates the moons altitude and
# azimuth (moon.py). These 2 values are then used to plot the position of the moon on a Galactic Unicorn using
# the inbuilt Raspberry Pi Pico W.
# The graphics part of this code was built for Pimoroni's pirate-brand MicroPython.
# The ntptime library is used to set the Pico at the correct time. For this to work we need to connect to the internet
# You need to create a secrets.py script and place your SSID and its password in there. I've aslo added my
# latitude and longitude in there too.
import machine, time, network, ntptime, utime
from galactic import GalacticUnicorn
from picographics import PicoGraphics, DISPLAY_GALACTIC_UNICORN as DISPLAY
from moon import MoonPosition
import secrets
# overclock to 200Mhz
machine.freq(200000000)
# The calculations in map_moon_position() are assuming the user is in the southern hemisphere, you'll need
# to modify the azimuth extents for your region.
# azimuth extents for my location in southern hemisphere:
az_east = 125.0
az_west = 235.0
# Initialize the Galactic Unicorn and graphics surface
galactic = GalacticUnicorn()
graphics = PicoGraphics(DISPLAY)
def connect():
#Connect to WLAN
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(secrets.ssid, secrets.password)
while wlan.isconnected() == False:
print('Waiting for connection...')
time.sleep(1)
ip = wlan.ifconfig()[0]
print(f'Connected on {ip}')
return ip
try:
ip = connect()
except KeyboardInterrupt:
machine.reset()
# Set the pico to the right time using an ntp server.
ntptime.settime()
# Function to map altitude and azimuth to display coordinates
def map_moon_position(altitude, azimuth):
# Ensure altitude is within the valid range
if 0 < altitude < 90:
# Normalize altitude (0° to 90°) to y-axis (10 to 0 pixels)
y = int(10 - (altitude / 90.0 * 10))
else:
# Invalid altitude value
return None, None
# Normalize azimuth
if 0 <= azimuth <= az_east:
# Map az_east (max East) to 0 (leftmost), 0° (North) to 26 (center)
x = int((az_east - azimuth) / az_east * 26)
elif az_west <= azimuth <= 359:
# Map 359° (slightly west) to 27, az_west (max West) to 52 (rightmost)
x = int((azimuth - az_west) / (359 - az_west) * 26) + 26
else:
# Invalid azimuth value
return None, None
return x, y
# Function to draw the moon on the display
def draw_moon(altitude, azimuth):
# Get the moon's position in display coordinates
x, y = map_moon_position(altitude, azimuth)
# Check if the position is valid (i.e., not None)
if x is not None and y is not None:
# Set the pen color to white
moon_pen = graphics.create_pen(255, 255, 255)
graphics.set_pen(moon_pen)
# Draw the moon as 4 adjacent pixels
graphics.pixel(x, y)
graphics.pixel(x+1, y)
graphics.pixel(x, y-1)
graphics.pixel(x+1, y-1)
# Update the display
galactic.update(graphics)
# Loop to update the moon's position every 10 minutes
while True:
# Clear the display
graphics.set_pen(graphics.create_pen(0, 0, 0))
graphics.clear()
# Add a pixel for the observer at North
graphics.set_pen(graphics.create_pen(0, 255, 0))
graphics.pixel(26, 10)
galactic.update(graphics)
# get the current time
current_time = utime.localtime()
date = utime.mktime(current_time)
# Calculate the moon's position
azimuth, altitude, distance = MoonPosition().moon_position(date, secrets.latitude, secrets.longitude)
#print(f"Moon Altitude: {alt:.2f} degrees, Azimuth: {az:.2f} degrees")
# Draw the moon
draw_moon(altitude, azimuth)
# Wait for 10 minutes before updating again (600 seconds)
utime.sleep(600)