Skip to content

Commit acebfbb

Browse files
committed
Add API to scroll text and clear display.
1 parent 71d54e6 commit acebfbb

File tree

2 files changed

+52
-4
lines changed

2 files changed

+52
-4
lines changed

Cargo.toml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "sensehat"
3-
version = "0.4.0"
3+
version = "0.5.0"
44
authors = ["Jonathan Pallant <[email protected]>"]
55
license-file = "LICENSE.md"
66
documentation = "https://docs.rs/crate/sensehat"
@@ -22,11 +22,12 @@ gcc = "0.3"
2222
[features]
2323
# The default set of optional packages. Most people will want to use these
2424
# packages, but they are strictly optional.
25-
default = ["rtimu"]
26-
rtimu = [ "libc" ]
25+
default = ["rtimu", "led-matrix"]
26+
# Extra packages required by these features.
27+
rtimu = ["libc"]
2728
led-matrix = ["sensehat-screen"]
2829

2930
[package.metadata.docs.rs]
30-
features = [ ]
31+
features = ["led-matrix"]
3132
all-features = false
3233
no-default-features = true

src/lib.rs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ pub struct Vector3D {
6060
pub z: f64,
6161
}
6262

63+
/// Represents an RGB colour
64+
#[cfg(feature = "led-matrix")]
65+
pub use sensehat_screen::color::PixelColor as Colour;
66+
6367
/// A collection of all the data from the IMU
6468
#[derive(Debug, Default)]
6569
struct ImuData {
@@ -92,6 +96,8 @@ pub enum SenseHatError {
9296
GenericError,
9397
I2CError(LinuxI2CError),
9498
LSM9DS1Error(lsm9ds1::Error),
99+
ScreenError,
100+
CharacterError(std::string::FromUtf16Error)
95101
}
96102

97103
/// A shortcut for Results that can return `T` or `SenseHatError`
@@ -229,6 +235,40 @@ impl<'a> SenseHat<'a> {
229235
None => Err(SenseHatError::NotReady)
230236
}
231237
}
238+
239+
/// Displays a scrolling message on the LED matrix.
240+
///
241+
/// Blocks until the entire message has scrolled past.
242+
#[cfg(feature = "led-matrix")]
243+
pub fn text(&mut self, message: &str, fg: Colour, bg: Colour) -> SenseHatResult<()> {
244+
// Connect to our LED Matrix screen.
245+
let mut screen = sensehat_screen::Screen::open("/dev/fb1").map_err(|_| SenseHatError::ScreenError)?;
246+
// Get the default `FontCollection`.
247+
let fonts = sensehat_screen::FontCollection::new();
248+
// Create a sanitized `FontString`.
249+
let sanitized = fonts.sanitize_str(message)?;
250+
// Render the `FontString` as a vector of pixel frames.
251+
let pixel_frames = sanitized.pixel_frames(fg, bg);
252+
// Create a `Scroll` from the pixel frame vector.
253+
let scroll = sensehat_screen::Scroll::new(&pixel_frames);
254+
// Consume the `FrameSequence` returned by the `left_to_right` method.
255+
scroll.right_to_left().for_each(|frame| {
256+
screen.write_frame(&frame.frame_line());
257+
::std::thread::sleep(::std::time::Duration::from_millis(100));
258+
});
259+
Ok(())
260+
}
261+
262+
/// Clears the LED matrix
263+
#[cfg(feature = "led-matrix")]
264+
pub fn clear(&mut self) -> SenseHatResult<()> {
265+
// Connect to our LED Matrix screen.
266+
let mut screen = sensehat_screen::Screen::open("/dev/fb1").map_err(|_| SenseHatError::ScreenError)?;
267+
// Send a blank image to clear the screen
268+
const OFF: [u8; 128] = [0x00; 128];
269+
screen.write_frame(&sensehat_screen::FrameLine::from_slice(&OFF));
270+
Ok(())
271+
}
232272
}
233273

234274
impl From<LinuxI2CError> for SenseHatError {
@@ -243,4 +283,11 @@ impl From<lsm9ds1::Error> for SenseHatError {
243283
}
244284
}
245285

286+
impl From<std::string::FromUtf16Error> for SenseHatError {
287+
fn from(err: std::string::FromUtf16Error) -> SenseHatError {
288+
SenseHatError::CharacterError(err)
289+
}
290+
}
291+
292+
246293
// End of file

0 commit comments

Comments
 (0)