2626from adafruit_bitmap_font import bitmap_font
2727from adafruit_qualia .graphics import Graphics , Displays
2828
29- key = keypad .Keys ((board .A0 ,), value_when_pressed = False , pull = True )
30-
29+ # timezone offset for calculating mars time
3130timezone = - 5
3231
32+ key = keypad .Keys ((board .A0 ,), value_when_pressed = False , pull = True )
33+
3334wifi .radio .connect (os .getenv ("CIRCUITPY_WIFI_SSID" ), os .getenv ("CIRCUITPY_WIFI_PASSWORD" ))
3435print (f"Connected to { os .getenv ('CIRCUITPY_WIFI_SSID' )} " )
3536
3637aio_username = os .getenv ('ADAFRUIT_AIO_USERNAME' )
3738aio_key = os .getenv ('ADAFRUIT_AIO_KEY' )
3839
3940context = ssl .create_default_context ()
40- print ("socketpool" )
4141pool = socketpool .SocketPool (wifi .radio )
42- print ("requests" )
4342requests = adafruit_requests .Session (pool , context )
44- print ("io" )
4543io = IO_HTTP (aio_username , aio_key , requests )
4644
4745earth_bitmap = displayio .OnDiskBitmap ("/earth.bmp" )
4846mars_bitmap = displayio .OnDiskBitmap ("/mars.bmp" )
4947
5048graphics = Graphics (Displays .ROUND40 , default_bg = None , auto_refresh = True )
5149
52- # Create a TileGrid to hold the bitmap
5350earth_grid = displayio .TileGrid (earth_bitmap , pixel_shader = earth_bitmap .pixel_shader )
54-
55- # Create a Group to hold the TileGrid
5651earth_group = displayio .Group ()
57-
58- # Add the TileGrid to the Group
5952earth_group .append (earth_grid )
6053
61- # Create a TileGrid to hold the bitmap
6254mars_grid = displayio .TileGrid (mars_bitmap , pixel_shader = mars_bitmap .pixel_shader )
55+ mars_group = displayio .Group ()
56+ mars_group .append (mars_grid )
6357
6458def center (grid , bitmap ):
65- # Center the image
59+ # center the image
6660 grid .x -= (bitmap .width - graphics .display .width ) // 2
6761 grid .y -= (bitmap .height - graphics .display .height ) // 2
6862
6963center (mars_grid , mars_bitmap )
7064center (earth_grid , earth_bitmap )
71- # Create a Group to hold the TileGrid
72- mars_group = displayio .Group ()
73-
74- # Add the TileGrid to the Group
75- mars_group .append (mars_grid )
7665
7766graphics .display .root_group = mars_group
7867
79- # Pointer using vectorio, first the hub
68+ # pointer using vectorio, first the hub
8069pointer_pal = displayio .Palette (4 )
8170pointer_pal [0 ] = 0xff0000
8271pointer_pal [1 ] = 0x000000
8372pointer_pal [2 ] = 0x0000ff
8473pointer_pal [3 ] = 0xffffff
85- pointer_hub = vectorio .Circle (pixel_shader = pointer_pal , radius = 16 , x = 0 , y = 0 )
74+ pointer_hub = vectorio .Circle (pixel_shader = pointer_pal , radius = 26 , x = 0 , y = 0 )
8675pointer_hub .x = graphics .display .width // 2
8776pointer_hub .y = graphics .display .height // 2
8877
89- # Pointer itself
90- pw = 15 # pointer width
91- ph = 200 # pointer height
92- pointer_points = [(pw ,0 ), (pw ,- ph ), (- pw ,- ph ), (- pw ,0 )]
93- pointer = vectorio .Polygon (pixel_shader = pointer_pal , points = pointer_points , x = 0 ,y = 0 )
94- pointer .x = graphics .display .width // 2
95- pointer .y = graphics .display .height // 2
96- mars_group .append (pointer )
97- earth_group .append (pointer )
98- hw = 13 # pointer width
99- hh = 150 # pointer height
78+ # minute hand
79+ mw = 23
80+ mh = 225
81+ min_points = [(mw ,0 ), (mw ,- mh ), (- mw ,- mh ), (- mw ,0 )]
82+ min_hand = vectorio .Polygon (pixel_shader = pointer_pal , points = min_points , x = 0 ,y = 0 )
83+ min_hand .x = graphics .display .width // 2
84+ min_hand .y = graphics .display .height // 2
85+ mars_group .append (min_hand )
86+ earth_group .append (min_hand )
87+ # hour hand
88+ hw = 25
89+ hh = 175
10090hour_points = [(hw ,0 ), (hw ,- hh ), (- hw ,- hh ), (- hw ,0 )]
10191hour_hand = vectorio .Polygon (pixel_shader = pointer_pal , points = hour_points ,
10292 x = 0 , y = 0 , color_index = 1 )
@@ -105,6 +95,7 @@ def center(grid, bitmap):
10595mars_group .append (hour_hand )
10696earth_group .append (hour_hand )
10797
98+ # add numbers to the clock face
10899def calculate_number_position (number , center_x , center_y , radius ):
109100 angle = (360 / 12 ) * (number - 3 ) # -3 adjusts the angle to start at 12 o'clock
110101 rad_angle = pi * angle / 180
@@ -144,6 +135,8 @@ def calculate_number_position(number, center_x, center_y, radius):
144135mars_group .append (pointer_hub )
145136earth_group .append (pointer_hub )
146137
138+ # get time from adafruit io
139+ # called once an hour in the loop
147140def update_time ():
148141 print ("time" )
149142 now = io .receive_time ()
@@ -160,10 +153,11 @@ def convert_time(the_time):
160153 h = 12
161154 return h , a
162155
156+ # get mars time
163157def mars_time ():
164158 dt = io .receive_time ()
165159 print (dt )
166- utc_offset = 3600 * abs ( timezone )
160+ utc_offset = 3600 * - timezone
167161 tai_offset = 37
168162 millis = time .mktime (dt )
169163 unix_timestamp = millis + utc_offset
@@ -181,20 +175,20 @@ def mars_time():
181175 return mtc_minutes , mtc_hours
182176
183177def time_angle (m , the_hour ):
184- if m == 15 :
185- m = 16
186- elif m == 45 :
187- m = 46
178+ m_offset = 25 if 12 <= m < 18 or 42 <= m < 48 else 5
179+ h_offset = 25 if 2 <= the_hour % 12 < 4 or 8 <= the_hour % 12 < 10 else 5
188180 # Adjusted angle calculation for minute hand
189181 theta_minute = 360 - (m / 60 ) * 360
190182 theta_hour = ((the_hour / 12 ) + (m / (12 * 60 ))) * 360
191183 # Calculate coordinates for minute hand (mirrored)
192- minute_x = - int (cos (pi * (theta_minute - 90 ) / 180 ) * ph )
193- minute_y = int (sin (pi * (theta_minute + 90 ) / 180 ) * ph )
184+ minute_x = - int (cos (pi * (theta_minute - 90 ) / 180 ) * mh )
185+ minute_y = int (sin (pi * (theta_minute + 90 ) / 180 ) * mh )
194186 hour_x = int (cos (pi * (theta_hour - 90 ) / 180 ) * hh )
195187 hour_y = int (sin (pi * (theta_hour + 90 ) / 180 ) * hh )
196- pointer .points = [(pw , 0 ), (minute_x + 2 , - minute_y ), (minute_x - 2 , - minute_y ), (- pw , 0 )]
197- hour_hand .points = [(hw , 0 ), (hour_x + 2 , - hour_y ), (hour_x - 2 , - hour_y ), (- hw , 0 )]
188+ min_hand .points = [(mw , 0 ), (minute_x + m_offset , - minute_y ),
189+ (minute_x - m_offset , - minute_y ), (- mw , 0 )]
190+ hour_hand .points = [(hw , 0 ), (hour_x + h_offset , - hour_y ),
191+ (hour_x - h_offset , - hour_y ), (- hw , 0 )]
198192
199193clock_timer = 1 * 1000
200194clock_clock = ticks_ms ()
@@ -213,22 +207,28 @@ def time_angle(m, the_hour):
213207
214208while True :
215209 event = key .events .get ()
210+ # swap between earth or mars time
216211 if event :
217212 if event .pressed :
218213 print ("updating display" )
219214 show_earth = not show_earth
215+ # update background image
216+ # change minute hand color depending on background
220217 if show_earth :
221- if pointer .color_index != 2 :
218+ if min_hand .color_index != 2 :
222219 time_angle (minute , hour )
223220 graphics .display .root_group = earth_group
224- pointer .color_index = 2
221+ min_hand .color_index = 2
225222 pointer_hub .color_index = 2
226223 else :
227- if pointer .color_index != 0 :
224+ if min_hand .color_index != 0 :
228225 time_angle (mars_min , mars_hour )
229226 graphics .display .root_group = mars_group
230- pointer .color_index = 0
227+ min_hand .color_index = 0
231228 pointer_hub .color_index = 0
229+ # use ticks for timekeeping
230+ # every minute update clock hands
231+ # recheck IO time every hour
232232 if ticks_diff (ticks_ms (), clock_clock ) >= clock_timer :
233233 tick += 1
234234 if tick > 59 :
0 commit comments