Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Matrix API example: Compass Screen #8

Open
wants to merge 16 commits into
base: matrix_api
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ readme = "README.md"
measurements = "0.10.2"
i2cdev = "0.4.0"
byteorder = "1.0"
sensehat-screen = "0.1.4"
sensehat-screen = "0.1.9"
# sensehat-screen = { path = "../sensehat-screen-rs" }
libc = { version = "0.2", optional = true }

Expand Down
111 changes: 111 additions & 0 deletions examples/compass_screen.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,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(),
}
}
}
}
36 changes: 19 additions & 17 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ pub use measurements::Pressure;
pub use measurements::Angle;
pub use rh::RelativeHumidity;

pub use sensehat_screen::{FrameLine, PixelColor, Screen};
pub use sensehat_screen::{FrameLine, PixelFrame, PixelColor, Rotate, Screen};

use i2cdev::linux::{LinuxI2CDevice, LinuxI2CError};

Expand Down Expand Up @@ -103,12 +103,12 @@ pub enum SenseHatError {
PositionOutOfBounds,
I2CError(LinuxI2CError),
LSM9DS1Error(lsm9ds1::Error),
FramebufferError(sensehat_screen::FramebufferError)
FramebufferError(sensehat_screen::framebuffer::FramebufferError)
}

/// An image on the LED matrix
#[derive(Clone)]
pub struct Image([PixelColor; LED_NUM_PIXELS]);
#[derive(Copy, Clone, Debug)]
pub struct Image(PixelFrame);

/// A shortcut for Results that can return `T` or `SenseHatError`
pub type SenseHatResult<T> = Result<T, SenseHatError>;
Expand All @@ -128,7 +128,6 @@ impl<'a> SenseHat<'a> {
/// Will open the relevant I2C devices and then attempt to initialise the
/// chips on the Sense HAT.
pub fn new() -> SenseHatResult<SenseHat<'a>> {
let blue_pixel = PixelColor::new(0, 0, 255);
Ok(SenseHat {
humidity_chip: hts221::Hts221::new(LinuxI2CDevice::new("/dev/i2c-1", 0x5f)?)?,
pressure_chip: lps25h::Lps25h::new(LinuxI2CDevice::new("/dev/i2c-1", 0x5c)?)?,
Expand All @@ -139,7 +138,7 @@ impl<'a> SenseHat<'a> {
yaw: Angle::from_degrees(0.0),
},
rotation: Rotation::Normal,
image: Image([blue_pixel; LED_NUM_PIXELS]),
image: Image(PixelFrame::BLUE),
screen: Screen::open("/dev/fb1")?
})
}
Expand Down Expand Up @@ -301,9 +300,7 @@ impl<'a> SenseHat<'a> {

/// Clear the display
pub fn clear(&mut self, color: PixelColor, redraw: DrawMode) -> SenseHatResult<()> {
for pixel in self.image.0.iter_mut() {
*pixel = color;
}
self.image = Image([color; LED_NUM_PIXELS].into());
match redraw {
DrawMode::OutputNow => self.redraw(),
_ => {}
Expand All @@ -313,7 +310,7 @@ impl<'a> SenseHat<'a> {

pub fn redraw(&mut self) {
let image = self.image.rotate_copy(self.rotation);
let frame = FrameLine::from_pixels(&image.0);
let frame = image.0.frame_line();
self.screen.write_frame(&frame);
}
}
Expand All @@ -333,19 +330,19 @@ impl Image {
match rotation {
Rotation::Normal => {},
Rotation::Clockwise90 => {
unimplemented!()
self.0.rotate(Rotate::Ccw270);
}
Rotation::Clockwise180 => {
unimplemented!()
self.0.rotate(Rotate::Ccw180);
}
Rotation::Clockwise270 => {
unimplemented!()
self.0.rotate(Rotate::Ccw90);
}
}
}

fn rotate_copy(&self, rotation: Rotation) -> Image {
let mut im = self.clone();
pub fn rotate_copy(&self, rotation: Rotation) -> Image {
let mut im = *self;
im.rotate_mut(rotation);
im
}
Expand All @@ -363,10 +360,15 @@ impl From<lsm9ds1::Error> for SenseHatError {
}
}

impl From<sensehat_screen::FramebufferError> for SenseHatError {
fn from(err: sensehat_screen::FramebufferError) -> SenseHatError {
impl From<sensehat_screen::framebuffer::FramebufferError> for SenseHatError {
fn from(err: sensehat_screen::framebuffer::FramebufferError) -> SenseHatError {
SenseHatError::FramebufferError(err)
}
}

impl From<[PixelColor; LED_NUM_PIXELS]> for Image {
fn from(array: [PixelColor; 64]) -> Self {
Image(array.into())
}
}
// End of file