From 82d4eeb0f35ba207c47eecfdf161f7c1baba355a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20Vis=C3=A9e?= Date: Sun, 31 Dec 2017 00:05:37 +0100 Subject: [PATCH] Move pix modules to sub module --- src/image_manager.rs | 23 +++++++++++++++++++++-- src/main.rs | 7 +++---- src/painter.rs | 6 +++--- src/{pix_canvas.rs => pix/canvas.rs} | 12 ++++++------ src/{pix_client.rs => pix/client.rs} | 8 ++++---- src/pix/mod.rs | 2 ++ 6 files changed, 39 insertions(+), 19 deletions(-) rename src/{pix_canvas.rs => pix/canvas.rs} (95%) rename src/{pix_client.rs => pix/client.rs} (94%) create mode 100644 src/pix/mod.rs diff --git a/src/image_manager.rs b/src/image_manager.rs index c4e9457..83c21d8 100644 --- a/src/image_manager.rs +++ b/src/image_manager.rs @@ -1,9 +1,11 @@ use std::path::Path; +use std::thread::sleep; +use std::time::Duration; use image; use image::{DynamicImage, FilterType}; -use pix_canvas::PixCanvas; +use pix::canvas::Canvas; @@ -41,7 +43,7 @@ impl ImageManager { } /// Tick the image - pub fn tick(&mut self, canvas: &mut PixCanvas) { + pub fn tick(&mut self, canvas: &mut Canvas) { // Get the image index bound let bound = self.images.len(); @@ -56,6 +58,23 @@ impl ImageManager { // Increase the index self.index += 1; } + + /// Start working in the image manager. + /// + /// This will start walking through all image frames, + /// and pushes each frame to all painters, + /// with the specified frames per second. + pub fn work(&mut self, canvas: &mut Canvas, fps: u32) { + loop { + // Tick to use the next image + self.tick(canvas); + + // Sleep until we need to show the next image + sleep(Duration::from_millis( + (1000f32 / (fps as f32)) as u64 + )); + } + } } diff --git a/src/main.rs b/src/main.rs index 0536091..0b619c7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,13 +7,12 @@ mod color; mod image_manager; mod painter; mod painter_handle; -mod pix_canvas; -mod pix_client; +mod pix; mod rect; use arg_handler::ArgHandler; use image_manager::ImageManager; -use pix_canvas::PixCanvas; +use pix::canvas::Canvas; @@ -32,7 +31,7 @@ fn start<'a>(arg_handler: &ArgHandler<'a>) { println!("Starting..."); // Create a new pixelflut canvas - let mut canvas = PixCanvas::new( + let mut canvas = Canvas::new( arg_handler.host(), arg_handler.count(), arg_handler.size(), diff --git a/src/painter.rs b/src/painter.rs index 4af57a0..3a77b4a 100644 --- a/src/painter.rs +++ b/src/painter.rs @@ -6,14 +6,14 @@ use image::{DynamicImage, Pixel}; use app::PAINTER_IMAGE_WAIT_DELAY_MILLIS; use color::Color; -use pix_client::PixClient; +use pix::client::Client; use rect::Rect; /// A painter that paints on a pixelflut panel. pub struct Painter { - client: PixClient, + client: Client, area: Rect, offset: (u32, u32), image: Option, @@ -21,7 +21,7 @@ pub struct Painter { impl Painter { /// Create a new painter. - pub fn new(client: PixClient, area: Rect, offset: (u32, u32), image: Option) -> Painter { + pub fn new(client: Client, area: Rect, offset: (u32, u32), image: Option) -> Painter { Painter { client, area, diff --git a/src/pix_canvas.rs b/src/pix/canvas.rs similarity index 95% rename from src/pix_canvas.rs rename to src/pix/canvas.rs index 189cdff..8565523 100644 --- a/src/pix_canvas.rs +++ b/src/pix/canvas.rs @@ -8,13 +8,13 @@ use image::DynamicImage; use painter::Painter; use painter_handle::PainterHandle; -use pix_client::PixClient; +use pix::client::Client; use rect::Rect; /// A pixflut instance -pub struct PixCanvas { +pub struct Canvas { host: String, painter_count: usize, painter_handles: Vec, @@ -22,16 +22,16 @@ pub struct PixCanvas { offset: (u32, u32), } -impl PixCanvas { +impl Canvas { /// Create a new pixelflut canvas. pub fn new( host: &str, painter_count: usize, size: (u32, u32), offset: (u32, u32), - ) -> PixCanvas { + ) -> Canvas { // Initialize the object - let mut canvas = PixCanvas { + let mut canvas = Canvas { host: host.to_string(), painter_count, painter_handles: Vec::with_capacity(painter_count), @@ -88,7 +88,7 @@ impl PixCanvas { .expect("failed to open stream to pixelflut"); // Create a new client - let client = PixClient::new(stream); + let client = Client::new(stream); // Create a painter let mut painter = Painter::new( diff --git a/src/pix_client.rs b/src/pix/client.rs similarity index 94% rename from src/pix_client.rs rename to src/pix/client.rs index 9dd2903..ae67811 100644 --- a/src/pix_client.rs +++ b/src/pix/client.rs @@ -17,14 +17,14 @@ use color::Color; /// /// The client provides an interface for other logic to easily talk /// to the pixelflut panel. -pub struct PixClient { +pub struct Client { stream: BufStream, } -impl PixClient { +impl Client { /// Create a new client instance. - pub fn new(stream: TcpStream) -> PixClient { - PixClient { + pub fn new(stream: TcpStream) -> Client { + Client { stream: BufStream::new(stream), } } diff --git a/src/pix/mod.rs b/src/pix/mod.rs new file mode 100644 index 0000000..2f3a749 --- /dev/null +++ b/src/pix/mod.rs @@ -0,0 +1,2 @@ +pub mod canvas; +pub mod client;