@@ -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 ) ]
6569struct 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
234274impl 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