-
Notifications
You must be signed in to change notification settings - Fork 0
/
GPS-CLOCK5.py
269 lines (232 loc) · 11.4 KB
/
GPS-CLOCK5.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
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
import tkinter as tk
import serial
import pynmea2
import serial.tools.list_ports
from datetime import datetime
import pytz
import configparser
import math
class LEDClockApplication(tk.Frame):
def __init__(self, master=None, port=None, baudrate=9600):
super().__init__(master)
self.master = master
self.port = port
self.baudrate = baudrate
self.fullscreen = True
self.sats_info = ""
self.load_config()
self.configure_gui()
self.create_widgets()
self.create_menu()
self.ser = serial.Serial(port, baudrate)
self.clock_mode = 'analog' # Start in analog mode by default
self.set_clock_mode(self.clock_mode)
self.master.bind("<Control-q>", self.exit_fullscreen)
self.master.bind("<Control-f>", self.toggle_fullscreen)
self.master.attributes("-fullscreen", self.fullscreen) # Start in fullscreen mode
self.update_time()
def load_config(self):
self.config = configparser.ConfigParser()
self.config.read('config.ini')
self.selected_time_zone = self.config.get('Settings', 'TimeZone', fallback='UTC')
def save_config(self):
if not self.config.has_section('Settings'):
self.config.add_section('Settings')
self.config.set('Settings', 'TimeZone', self.selected_time_zone)
with open('config.ini', 'w') as configfile:
self.config.write(configfile)
def configure_gui(self):
self.master.title("GPS LED Clock")
self.master.configure(background='black')
self.pack(fill=tk.BOTH, expand=1)
def create_widgets(self):
self.time_label = tk.Label(self, font=("Courier", 48, "bold"), fg="#00FF00", bg="black")
self.time_label.pack(expand=1, fill=tk.BOTH)
self.sats_label = tk.Label(self, font=("Courier", 16, "bold"), fg="#00FF00", bg="black")
self.sats_label.pack(anchor="se", padx=10, pady=10)
self.canvas = tk.Canvas(self, bg='black')
self.canvas.pack(fill=tk.BOTH, expand=1)
self.canvas.pack_forget()
def create_menu(self):
self.menu_bar = tk.Menu(self.master)
self.master.config(menu=self.menu_bar)
self.time_zone_menu = tk.Menu(self.menu_bar, tearoff=0)
self.menu_bar.add_cascade(label="Time Zone", menu=self.time_zone_menu)
self.clock_mode_menu = tk.Menu(self.menu_bar, tearoff=0)
self.menu_bar.add_cascade(label="Clock Mode", menu=self.clock_mode_menu)
self.clock_mode_menu.add_command(label="Digital", command=lambda: self.set_clock_mode('digital'))
self.clock_mode_menu.add_command(label="Analog", command=lambda: self.set_clock_mode('analog'))
self.menu_bar.add_command(label="About", command=self.show_about)
self.menu_bar.add_command(label="Exit", command=self.close_program)
# US time zones
us_time_zones = [
'US/Eastern', 'US/Central', 'US/Mountain', 'US/Pacific',
'US/Alaska', 'US/Hawaii', 'US/Aleutian', 'US/Arizona',
'US/East-Indiana', 'US/Indiana-Starke', 'US/Michigan',
'US/Samoa', 'US/Guam'
]
# Top ten global time zones
global_time_zones = [
'UTC', 'Europe/London', 'Europe/Paris', 'Asia/Tokyo', 'Asia/Hong_Kong',
'Australia/Sydney', 'Europe/Moscow', 'Asia/Dubai', 'Asia/Singapore',
'Europe/Berlin', 'Europe/Rome'
]
time_zones = us_time_zones + global_time_zones
for tz in time_zones:
self.time_zone_menu.add_command(label=tz, command=lambda tz=tz: self.set_time_zone(tz))
def set_clock_mode(self, mode):
self.clock_mode = mode
if mode == 'digital':
self.time_label.pack(expand=1, fill=tk.BOTH)
self.canvas.pack_forget()
else:
self.time_label.pack_forget()
self.canvas.pack(fill=tk.BOTH, expand=1)
self.update_time()
def set_time_zone(self, tz):
self.selected_time_zone = tz
self.save_config()
def update_time(self):
while self.ser.in_waiting:
try:
line = self.ser.readline().decode('ascii', 'ignore').rstrip()
msg = pynmea2.parse(line)
if isinstance(msg, pynmea2.types.talker.RMC) and msg.timestamp and msg.datestamp:
datetime_obj = datetime.combine(msg.datestamp, msg.timestamp)
timezone = pytz.timezone(self.selected_time_zone)
datetime_obj = datetime_obj.replace(tzinfo=pytz.UTC).astimezone(timezone)
if self.clock_mode == 'digital':
self.time_label["text"] = datetime_obj.strftime('%Y-%m-%d\n%H:%M:%S')
else:
self.draw_analog_clock(datetime_obj)
break
elif isinstance(msg, pynmea2.types.talker.GGA):
num_sats = msg.num_sats
fix_quality = int(msg.gps_qual)
if fix_quality == 0:
tracking = "None"
elif fix_quality == 1:
tracking = "2D"
elif fix_quality == 2:
tracking = "3D"
else:
tracking = "Unknown"
self.sats_info = f"Sats = {num_sats} Tracking = {tracking}"
except pynmea2.ParseError:
print(f"Parse error with line: {line}")
except UnicodeDecodeError:
print("Failed to decode line, ignoring.")
self.sats_label["text"] = self.sats_info
self.master.after(1000, self.update_time)
def draw_analog_clock(self, datetime_obj):
self.canvas.delete("all")
width = self.canvas.winfo_width()
height = self.canvas.winfo_height()
center_x = width // 2
center_y = height // 2
radius = min(center_x, center_y) - 10
# Draw clock face
self.canvas.create_oval(center_x - radius, center_y - radius, center_x + radius, center_y + radius, outline="#00FF00", width=2)
# Draw clock numbers and tick marks
for i in range(1, 61):
angle = math.radians((i * 6) - 90)
x_start = center_x + radius * math.cos(angle)
y_start = center_y + radius * math.sin(angle)
if i % 5 == 0:
x_end = center_x + (radius * 0.85) * math.cos(angle)
y_end = center_y + (radius * 0.85) * math.sin(angle)
self.canvas.create_text(center_x + (radius * 0.75) * math.cos(angle),
center_y + (radius * 0.75) * math.sin(angle),
text=str(i // 5 if i // 5 != 0 else 12), fill="#00FF00", font=("Courier", 14))
else:
x_end = center_x + (radius * 0.95) * math.cos(angle)
y_end = center_y + (radius * 0.95) * math.sin(angle)
self.canvas.create_line(x_start, y_start, x_end, y_end, fill="#00FF00", width=2 if i % 5 == 0 else 1)
# Draw hour, minute, and second hands
hours = datetime_obj.hour % 12
minutes = datetime_obj.minute
seconds = datetime_obj.second
hour_angle = math.radians((hours + minutes / 60) * 30 - 90)
minute_angle = math.radians(minutes * 6 - 90)
second_angle = math.radians(seconds * 6 - 90)
hour_hand_length = radius * 0.5
minute_hand_length = radius * 0.75
second_hand_length = radius * 0.9
self.canvas.create_line(center_x, center_y, center_x + hour_hand_length * math.cos(hour_angle),
center_y + hour_hand_length * math.sin(hour_angle), fill="#00FF00", width=4)
self.canvas.create_line(center_x, center_y, center_x + minute_hand_length * math.cos(minute_angle),
center_y + minute_hand_length * math.sin(minute_angle), fill="#00FF00", width=2)
self.canvas.create_line(center_x, center_y, center_x + second_hand_length * math.cos(second_angle),
center_y + second_hand_length * math.sin(second_angle), fill="#FF0000", width=1)
def toggle_fullscreen(self, event=None):
self.fullscreen = not self.fullscreen
self.master.attributes("-fullscreen", self.fullscreen)
self.center_clock()
def exit_fullscreen(self, event=None):
self.fullscreen = False
self.master.attributes("-fullscreen", False)
self.center_clock()
def close_program(self, event=None):
self.master.quit()
def center_clock(self):
self.master.update_idletasks()
if self.clock_mode == 'analog':
self.canvas.config(width=self.master.winfo_width(), height=self.master.winfo_height())
self.draw_analog_clock(datetime.now(pytz.timezone(self.selected_time_zone)))
elif self.clock_mode == 'digital':
self.time_label.config(width=self.master.winfo_width(), height=self.master.winfo_height())
def show_about(self):
about_window = tk.Toplevel(self.master)
about_window.title("About")
about_window.configure(background='black')
about_window.attributes("-fullscreen", True)
about_text = (
"GPS LED Clock\n\n"
"This program displays the current time using GPS data.\n\n"
"Keyboard shortcuts:\n"
"Ctrl-F: Toggle fullscreen\n"
"Ctrl-Q: Exit fullscreen\n"
"Ctrl-X: Close the program\n"
"Esc: Close this About window\n\n"
"You can switch between digital and analog clock modes, and\n"
"select different time zones from the menu.\n\n"
"How it works:\n"
"The program reads GPS data from a connected GPS module.\n"
"It extracts the current time, date, and number of satellites\n"
"in view and uses this information to display the current\n"
"time. The program can show the time in digital or analog\n"
"formats, and it allows you to select different time zones."
)
label = tk.Label(about_window, text="", font=("Courier", 18), fg="#00FF00", bg="black", justify=tk.LEFT, padx=10, pady=10)
label.pack(expand=True, fill=tk.BOTH)
def type_text(label, text, index=0):
if index < len(text):
label.config(text=label.cget("text") + text[index])
about_window.after(50, type_text, label, text, index + 1)
else:
label.config(text=label.cget("text") + "\n_")
self.blink_cursor(label)
def blink_cursor(label):
current_text = label.cget("text")
if current_text.endswith("_"):
label.config(text=current_text[:-1])
else:
label.config(text=current_text + "_")
about_window.after(500, blink_cursor, label)
def close_about(event=None):
about_window.destroy()
about_window.bind("<Escape>", close_about)
type_text(label, about_text)
def main():
print("Available ports:")
ports = serial.tools.list_ports.comports()
for i, port in enumerate(ports, start=1):
print(f"{i}: {port.device}")
port_index = int(input("Select the port number: ")) - 1
port = ports[port_index].device
baudrate = int(input("Enter the baudrate: "))
root = tk.Tk()
app = LEDClockApplication(master=root, port=port, baudrate=baudrate)
app.mainloop()
if __name__ == "__main__":
main()