-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcompass.py
executable file
·37 lines (27 loc) · 959 Bytes
/
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
#!/usr/bin/python3
import sys
from sense_hat import SenseHat
from time import sleep
# To get good results with the magnetometer you must first calibrate it using
# the program in RTIMULib/Linux/RTIMULibCal
# The calibration program will produce the file RTIMULib.ini
# Copy it into the same folder as your Python code
led_loop = [4, 5, 6, 7, 15, 23, 31, 39, 47, 55, 63, 62, 61, 60, 59, 58, 57, 56, 48, 40, 32, 24, 16, 8, 0, 1, 2, 3]
sense = SenseHat()
sense.set_rotation(0)
sense.clear()
prev_x = 0
prev_y = 0
led_degree_ratio = len(led_loop) / 360.0
while True:
dir = sense.get_compass()
dir_inverted = 360 - dir # So LED appears to follow North
led_index = int(led_degree_ratio * dir_inverted)
offset = led_loop[led_index]
y = offset // 8 # row
x = offset % 8 # column
if x != prev_x or y != prev_y:
sense.set_pixel(prev_x, prev_y, 0, 0, 0)
sense.set_pixel(x, y, 0, 0, 255)
prev_x = x
prev_y = y