|
| 1 | +extern crate sensehat; |
| 2 | +#[cfg(feature = "led-matrix")] |
| 3 | +extern crate sensehat_screen; |
| 4 | + |
| 5 | +use sensehat::SenseHat; |
| 6 | +#[cfg(feature = "led-matrix")] |
| 7 | +use sensehat_screen::{FrameLine, PixelColor, Screen}; |
| 8 | + |
| 9 | +#[cfg(feature = "led-matrix")] |
| 10 | +fn main() { |
| 11 | + let mut sense_hat = SenseHat::new().expect("Couldn't create Sense HAT object"); |
| 12 | + let mut screen = Screen::open("/dev/fb1").expect("Couldn't find Sense HAT LED matrix"); |
| 13 | + |
| 14 | + //let dark_px = PixelColor::new(0, 0, 0); |
| 15 | + let blue_px = PixelColor::new(0, 0, 255); |
| 16 | + let grey_px = PixelColor::new(0, 0, 0); |
| 17 | + let red_px = PixelColor::new(255, 0, 0); |
| 18 | + let background_pixels = vec![ |
| 19 | + &blue_px, &blue_px, &grey_px, &grey_px, &grey_px, &grey_px, &blue_px, &blue_px, |
| 20 | + &blue_px, &grey_px, &blue_px, &blue_px, &blue_px, &blue_px, &grey_px, &blue_px, |
| 21 | + &grey_px, &blue_px, &blue_px, &blue_px, &blue_px, &blue_px, &blue_px, &grey_px, |
| 22 | + &grey_px, &blue_px, &blue_px, &blue_px, &blue_px, &blue_px, &blue_px, &grey_px, |
| 23 | + &grey_px, &blue_px, &blue_px, &blue_px, &blue_px, &blue_px, &blue_px, &grey_px, |
| 24 | + &grey_px, &blue_px, &blue_px, &blue_px, &blue_px, &blue_px, &blue_px, &grey_px, |
| 25 | + &blue_px, &grey_px, &blue_px, &blue_px, &blue_px, &blue_px, &grey_px, &blue_px, |
| 26 | + &blue_px, &blue_px, &grey_px, &grey_px, &grey_px, &grey_px, &blue_px, &blue_px, |
| 27 | + ]; |
| 28 | + |
| 29 | + let right = { |
| 30 | + let mut pxs = background_pixels.clone(); |
| 31 | + pxs[31] = &red_px; |
| 32 | + pxs[39] = &red_px; |
| 33 | + pxs |
| 34 | + }; |
| 35 | + let right_up = { |
| 36 | + let mut pxs = background_pixels.clone(); |
| 37 | + pxs[14] = &red_px; |
| 38 | + pxs[23] = &red_px; |
| 39 | + pxs |
| 40 | + }; |
| 41 | + let right_down = { |
| 42 | + let mut pxs = background_pixels.clone(); |
| 43 | + pxs[47] = &red_px; |
| 44 | + pxs[54] = &red_px; |
| 45 | + pxs |
| 46 | + }; |
| 47 | + |
| 48 | + let up = { |
| 49 | + let mut pxs = background_pixels.clone(); |
| 50 | + pxs[3] = &red_px; |
| 51 | + pxs[4] = &red_px; |
| 52 | + pxs |
| 53 | + }; |
| 54 | + |
| 55 | + let up_left = { |
| 56 | + let mut pxs = background_pixels.clone(); |
| 57 | + pxs[2] = &red_px; |
| 58 | + pxs[9] = &red_px; |
| 59 | + pxs |
| 60 | + }; |
| 61 | + let up_right = { |
| 62 | + let mut pxs = background_pixels.clone(); |
| 63 | + pxs[5] = &red_px; |
| 64 | + pxs[14] = &red_px; |
| 65 | + pxs |
| 66 | + }; |
| 67 | + |
| 68 | + let left = { |
| 69 | + let mut pxs = background_pixels.clone(); |
| 70 | + pxs[24] = &red_px; |
| 71 | + pxs[32] = &red_px; |
| 72 | + pxs |
| 73 | + }; |
| 74 | + let left_up = { |
| 75 | + let mut pxs = background_pixels.clone(); |
| 76 | + pxs[9] = &red_px; |
| 77 | + pxs[16] = &red_px; |
| 78 | + pxs |
| 79 | + }; |
| 80 | + let left_down = { |
| 81 | + let mut pxs = background_pixels.clone(); |
| 82 | + pxs[40] = &red_px; |
| 83 | + pxs[49] = &red_px; |
| 84 | + pxs |
| 85 | + }; |
| 86 | + |
| 87 | + let down = { |
| 88 | + let mut pxs = background_pixels.clone(); |
| 89 | + pxs[59] = &red_px; |
| 90 | + pxs[60] = &red_px; |
| 91 | + pxs |
| 92 | + }; |
| 93 | + let down_left = { |
| 94 | + let mut pxs = background_pixels.clone(); |
| 95 | + pxs[49] = &red_px; |
| 96 | + pxs[58] = &red_px; |
| 97 | + pxs |
| 98 | + }; |
| 99 | + let down_right = { |
| 100 | + let mut pxs = background_pixels.clone(); |
| 101 | + pxs[54] = &red_px; |
| 102 | + pxs[61] = &red_px; |
| 103 | + pxs |
| 104 | + }; |
| 105 | + let bg_frame = FrameLine::from_pixels(&background_pixels); |
| 106 | + let left_frame = FrameLine::from_pixels(&left); |
| 107 | + let left_up_frame = FrameLine::from_pixels(&left_up); |
| 108 | + let left_down_frame = FrameLine::from_pixels(&left_down); |
| 109 | + |
| 110 | + let right_frame = FrameLine::from_pixels(&right); |
| 111 | + let right_up_frame = FrameLine::from_pixels(&right_up); |
| 112 | + let right_down_frame = FrameLine::from_pixels(&right_down); |
| 113 | + |
| 114 | + let up_frame = FrameLine::from_pixels(&up); |
| 115 | + let up_left_frame = FrameLine::from_pixels(&up_left); |
| 116 | + let up_right_frame = FrameLine::from_pixels(&up_right); |
| 117 | + |
| 118 | + let down_frame = FrameLine::from_pixels(&down); |
| 119 | + let down_left_frame = FrameLine::from_pixels(&down_left); |
| 120 | + let down_right_frame = FrameLine::from_pixels(&down_right); |
| 121 | + |
| 122 | + screen.write_frame(&bg_frame); |
| 123 | + |
| 124 | + loop { |
| 125 | + if let Ok(needle) = sense_hat.get_compass() { |
| 126 | + // println!("Compass needle @{}", needle.as_degrees()); |
| 127 | + match needle.as_degrees() { |
| 128 | + angle if angle > -15.0 && angle <= 15.0 => { |
| 129 | + screen.write_frame(&right_frame); |
| 130 | + }, |
| 131 | + angle if angle > 15.0 && angle <= 45.0 => { |
| 132 | + screen.write_frame(&right_up_frame); |
| 133 | + }, |
| 134 | + angle if angle > 45.0 && angle <= 75.0 => { |
| 135 | + screen.write_frame(&up_right_frame); |
| 136 | + }, |
| 137 | + angle if angle > 75.0 && angle <= 105.0 => { |
| 138 | + screen.write_frame(&up_frame); |
| 139 | + }, |
| 140 | + angle if angle > 105.0 && angle <= 135.0 => { |
| 141 | + screen.write_frame(&up_left_frame); |
| 142 | + }, |
| 143 | + angle if angle > 135.0 && angle <= 165.0 => { |
| 144 | + screen.write_frame(&left_up_frame); |
| 145 | + }, |
| 146 | + angle if (angle > 165.0 && angle <= 180.0) || (angle < -165.0 && angle >= -180.0) => { |
| 147 | + screen.write_frame(&left_frame); |
| 148 | + }, |
| 149 | + angle if angle < -15.0 && angle >= -45.0 => { |
| 150 | + screen.write_frame(&right_down_frame); |
| 151 | + }, |
| 152 | + angle if angle < -45.0 && angle >= -75.0 => { |
| 153 | + screen.write_frame(&down_right_frame); |
| 154 | + }, |
| 155 | + angle if angle < -75.0 && angle >= -105.0 => { |
| 156 | + screen.write_frame(&down_frame); |
| 157 | + }, |
| 158 | + angle if angle < -105.0 && angle >= -135.0 => { |
| 159 | + screen.write_frame(&down_left_frame); |
| 160 | + }, |
| 161 | + angle if angle < -135.0 && angle >= -165.0 => { |
| 162 | + screen.write_frame(&left_down_frame); |
| 163 | + }, |
| 164 | + _ => screen.write_frame(&bg_frame), |
| 165 | + } |
| 166 | + } |
| 167 | + } |
| 168 | +} |
| 169 | + |
| 170 | +#[cfg(not(feature = "led-matrix"))] |
| 171 | +fn main() { |
| 172 | + println!("the 'led-matrix' feature is required to run this example."); |
| 173 | + println!("To run with the 'led-matrix' feature, try:"); |
| 174 | + println!(); |
| 175 | + println!(" cargo run --features=\"led-matrix\" --example compass_screen") |
| 176 | +} |
0 commit comments