-
Notifications
You must be signed in to change notification settings - Fork 0
/
compass.py
114 lines (85 loc) · 2.78 KB
/
compass.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
import serial
from time import sleep
from json import loads
import pygame
from pygame.locals import *
# initialize serial
BAUDRATE = 115200
serial_data = serial.Serial("com10", BAUDRATE)
sleep(1)
# when no data is passed
while serial_data.in_waiting == 0:
pass
# waiting for setup to finish
while (data_packet := str(serial_data.readline(), "utf-8")) != "all set up, starting to send data...\r\n":
print(data_packet)
# prepare pygame canvas
pygame.init()
# screen res
HEIGHT = 700
WIDTH = 1000
print(f"width: {WIDTH}, height: {HEIGHT}")
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("seismometer")
FONT_SIZE = 60
FONT = pygame.font.SysFont("cascadiamonoregular", FONT_SIZE)
compass_bg = pygame.image.load("assets/compass.png")
compass_center = compass_bg.get_rect()
compass_center.center = screen.get_rect().center
compass_center.move_ip(0, FONT_SIZE) # translate compass down to make space for text
compass_needle = pygame.image.load("assets/needle.png")
compass_needle = pygame.transform.scale(
compass_needle,
(
compass_needle.get_width() * 0.6,
compass_needle.get_height() * 0.6
)
)
def degrees_to_heading(degrees):
heading = ""
if (degrees > 337) or (degrees >= 0 and degrees <= 22):
heading = "N " # leave spaces for better formatting when printing font
if degrees >22 and degrees <= 67:
heading = "NE"
if degrees >67 and degrees <= 112:
heading = "E "
if degrees >112 and degrees <= 157:
heading = "SE"
if degrees > 157 and degrees <= 202:
heading = "S "
if degrees > 202 and degrees <= 247:
heading = "SW"
if degrees > 247 and degrees <= 292:
heading = "W "
if degrees > 292 and degrees <= 337:
heading = "NW"
return heading
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
while serial_data.in_waiting == 0:
pass
# read data
data_packet = serial_data.readline()
data_packet = loads(str(data_packet, "utf-8"))
screen.fill((0, 0, 0))
screen.blit(compass_bg, compass_center)
rotated_needle = pygame.transform.rotate(compass_needle, 360 - data_packet["heading"])
screen.blit(
rotated_needle,
(
compass_center.center[0] - int(rotated_needle.get_width() / 2),
compass_center.center[1] - int(rotated_needle.get_height() / 2)
)
)
# print font
screen.blit(
FONT.render(
f"{degrees_to_heading(data_packet['heading'])}: {data_packet['heading']}°", True, (255, 255, 255)
),
(FONT_SIZE, FONT_SIZE)
)
print(data_packet)
pygame.display.flip()