-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathcompass_screen.rs
111 lines (100 loc) · 4.05 KB
/
compass_screen.rs
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
extern crate sensehat;
extern crate sensehat_screen;
use sensehat::{Image, PixelColor, Rotation, SenseHat};
const DARK: PixelColor = PixelColor::BLACK;
const BLUE: PixelColor = PixelColor::BLUE;
const RED: PixelColor = PixelColor::RED;
const BACKGROUND: [PixelColor; 64] = [
BLUE, BLUE, DARK, DARK, DARK, DARK, BLUE, BLUE, // 0-7
BLUE, DARK, BLUE, BLUE, BLUE, BLUE, DARK, BLUE, // 8-15
DARK, BLUE, BLUE, BLUE, BLUE, BLUE, BLUE, DARK, // 16-23
DARK, BLUE, BLUE, BLUE, BLUE, BLUE, BLUE, DARK, // 24-31
DARK, BLUE, BLUE, BLUE, BLUE, BLUE, BLUE, DARK, // 32-39
DARK, BLUE, BLUE, BLUE, BLUE, BLUE, BLUE, DARK, // 40-47
BLUE, DARK, BLUE, BLUE, BLUE, BLUE, DARK, BLUE, // 48-55
BLUE, BLUE, DARK, DARK, DARK, DARK, BLUE, BLUE, // 56-63
];
fn main() {
let mut sense_hat = SenseHat::new().expect("Couldn't create Sense HAT object");
let background: Image = BACKGROUND.into();
let right: Image = {
let mut pxs = BACKGROUND;
pxs[23] = RED.dim(0.5);
pxs[47] = RED;
pxs[31] = RED;
pxs[39] = RED.dim(0.5);
pxs.into()
};
let right_up: Image = {
let mut pxs = BACKGROUND;
pxs[5] = RED.dim(0.5);
pxs[14] = RED;
pxs[23] = RED;
pxs[31] = RED.dim(0.5);
pxs.into()
};
let right_down: Image = {
let mut pxs = BACKGROUND;
pxs[39] = RED.dim(0.5);
pxs[47] = RED;
pxs[54] = RED;
pxs[61] = RED.dim(0.5);
pxs.into()
};
let up = right.rotate_copy(Rotation::Clockwise270);
let up_left = right_up.rotate_copy(Rotation::Clockwise270);
let up_right = right_down.rotate_copy(Rotation::Clockwise270);
let left = right.rotate_copy(Rotation::Clockwise180);
let left_up = right_down.rotate_copy(Rotation::Clockwise180);
let left_down = right_up.rotate_copy(Rotation::Clockwise180);
let down = up.rotate_copy(Rotation::Clockwise180);
let down_left = up_right.rotate_copy(Rotation::Clockwise180);
let down_right = up_left.rotate_copy(Rotation::Clockwise180);
sense_hat.set_pixels(background).unwrap();
loop {
if let Ok(needle) = sense_hat.get_compass() {
// println!("Compass needle @{}", needle.as_degrees());
match needle.as_degrees() {
angle if angle > -15.0 && angle <= 15.0 => {
sense_hat.set_pixels(right).unwrap();
}
angle if angle > 15.0 && angle <= 45.0 => {
sense_hat.set_pixels(right_up).unwrap();
}
angle if angle > 45.0 && angle <= 75.0 => {
sense_hat.set_pixels(up_right).unwrap();
}
angle if angle > 75.0 && angle <= 105.0 => {
sense_hat.set_pixels(up).unwrap();
}
angle if angle > 105.0 && angle <= 135.0 => {
sense_hat.set_pixels(up_left).unwrap();
}
angle if angle > 135.0 && angle <= 165.0 => {
sense_hat.set_pixels(left_up).unwrap();
}
angle
if (angle > 165.0 && angle <= 180.0) || (angle < -165.0 && angle >= -180.0) =>
{
sense_hat.set_pixels(left).unwrap();
}
angle if angle < -15.0 && angle >= -45.0 => {
sense_hat.set_pixels(right_down).unwrap();
}
angle if angle < -45.0 && angle >= -75.0 => {
sense_hat.set_pixels(down_right).unwrap();
}
angle if angle < -75.0 && angle >= -105.0 => {
sense_hat.set_pixels(down).unwrap();
}
angle if angle < -105.0 && angle >= -135.0 => {
sense_hat.set_pixels(down_left).unwrap();
}
angle if angle < -135.0 && angle >= -165.0 => {
sense_hat.set_pixels(left_down).unwrap();
}
_ => sense_hat.set_pixels(background).unwrap(),
}
}
}
}