Move pix modules to sub module
This commit is contained in:
140
src/pix/canvas.rs
Normal file
140
src/pix/canvas.rs
Normal file
@@ -0,0 +1,140 @@
|
||||
use std::io::Error;
|
||||
use std::net::TcpStream;
|
||||
use std::sync::mpsc;
|
||||
use std::sync::mpsc::{Sender, Receiver};
|
||||
use std::thread;
|
||||
|
||||
use image::DynamicImage;
|
||||
|
||||
use painter::Painter;
|
||||
use painter_handle::PainterHandle;
|
||||
use pix::client::Client;
|
||||
use rect::Rect;
|
||||
|
||||
|
||||
|
||||
/// A pixflut instance
|
||||
pub struct Canvas {
|
||||
host: String,
|
||||
painter_count: usize,
|
||||
painter_handles: Vec<PainterHandle>,
|
||||
size: (u32, u32),
|
||||
offset: (u32, u32),
|
||||
}
|
||||
|
||||
impl Canvas {
|
||||
/// Create a new pixelflut canvas.
|
||||
pub fn new(
|
||||
host: &str,
|
||||
painter_count: usize,
|
||||
size: (u32, u32),
|
||||
offset: (u32, u32),
|
||||
) -> Canvas {
|
||||
// Initialize the object
|
||||
let mut canvas = Canvas {
|
||||
host: host.to_string(),
|
||||
painter_count,
|
||||
painter_handles: Vec::with_capacity(painter_count),
|
||||
size,
|
||||
offset,
|
||||
};
|
||||
|
||||
// Show a status message
|
||||
println!("Starting painter threads...");
|
||||
|
||||
// Spawn some painters
|
||||
canvas.spawn_painters();
|
||||
|
||||
// Return the canvas
|
||||
canvas
|
||||
}
|
||||
|
||||
/// Spawn the painters for this canvas
|
||||
fn spawn_painters(&mut self) {
|
||||
// Spawn some painters
|
||||
for i in 0..self.painter_count {
|
||||
// Determine the slice width
|
||||
let width = self.size.0 / (self.painter_count as u32);
|
||||
|
||||
// Define the area to paint per thread
|
||||
let painter_area = Rect::from(
|
||||
(i as u32) * width,
|
||||
0,
|
||||
width,
|
||||
self.size.1,
|
||||
);
|
||||
|
||||
// Spawn the painter
|
||||
self.spawn_painter(painter_area);
|
||||
}
|
||||
}
|
||||
|
||||
/// Spawn a single painter in a thread.
|
||||
fn spawn_painter(&mut self, area: Rect) {
|
||||
// Get the host that will be used
|
||||
let host = self.host.to_string();
|
||||
|
||||
// Redefine the offset to make it usable in the thread
|
||||
let offset = (self.offset.0, self.offset.1);
|
||||
|
||||
// Create a channel to push new images
|
||||
let (tx, rx): (Sender<DynamicImage>, Receiver<DynamicImage>)
|
||||
= mpsc::channel();
|
||||
|
||||
// Create the painter thread
|
||||
let thread = thread::spawn(move || {
|
||||
// Create a new stream
|
||||
let stream = create_stream(host)
|
||||
.expect("failed to open stream to pixelflut");
|
||||
|
||||
// Create a new client
|
||||
let client = Client::new(stream);
|
||||
|
||||
// Create a painter
|
||||
let mut painter = Painter::new(
|
||||
client,
|
||||
area,
|
||||
offset,
|
||||
None
|
||||
);
|
||||
|
||||
// Do some work
|
||||
loop {
|
||||
// Work
|
||||
painter.work()
|
||||
.expect("Painter failed to perform work");
|
||||
|
||||
// Update the image to paint
|
||||
if let Ok(image) = rx.try_recv() {
|
||||
painter.set_image(image);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
// Create a new painter handle, pust it to the list
|
||||
self.painter_handles.push(
|
||||
PainterHandle::new(
|
||||
thread,
|
||||
area,
|
||||
tx,
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
// Update the image that is being rendered for all painters.
|
||||
pub fn update_image(&mut self, image: &mut DynamicImage) {
|
||||
// Update the image for each specific painter handle
|
||||
for handle in &self.painter_handles {
|
||||
handle.update_image(image);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/// Create a stream to talk to the pixelflut server.
|
||||
///
|
||||
/// The stream is returned as result.
|
||||
fn create_stream(host: String) -> Result<TcpStream, Error> {
|
||||
TcpStream::connect(host)
|
||||
}
|
||||
75
src/pix/client.rs
Normal file
75
src/pix/client.rs
Normal file
@@ -0,0 +1,75 @@
|
||||
extern crate bufstream;
|
||||
|
||||
use std::io::Error;
|
||||
use std::io::prelude::*;
|
||||
use std::net::TcpStream;
|
||||
|
||||
use self::bufstream::BufStream;
|
||||
|
||||
use color::Color;
|
||||
|
||||
|
||||
|
||||
/// A pixelflut client.
|
||||
///
|
||||
/// This client uses a stream to talk to a pixelflut panel.
|
||||
/// It allows to write pixels to the panel, and read some status.
|
||||
///
|
||||
/// The client provides an interface for other logic to easily talk
|
||||
/// to the pixelflut panel.
|
||||
pub struct Client {
|
||||
stream: BufStream<TcpStream>,
|
||||
}
|
||||
|
||||
impl Client {
|
||||
/// Create a new client instance.
|
||||
pub fn new(stream: TcpStream) -> Client {
|
||||
Client {
|
||||
stream: BufStream::new(stream),
|
||||
}
|
||||
}
|
||||
|
||||
/// Write a pixel to the given stream.
|
||||
pub fn write_pixel(&mut self, x: u32, y: u32, color: &Color) -> Result<(), Error> {
|
||||
// Write the command to set a pixel
|
||||
self.write_command(
|
||||
format!("PX {} {} {}", x, y, color.as_hex()),
|
||||
)
|
||||
}
|
||||
|
||||
// /// Read the size of the screen.
|
||||
// fn read_screen_size(&mut self) {
|
||||
// // Read the screen size
|
||||
// let size = self
|
||||
// .write_read_command("SIZE".into())
|
||||
// .expect("Failed to read screen size");
|
||||
//
|
||||
// // TODO: Remove this after debugging
|
||||
// println!("Read size: {}", size);
|
||||
// }
|
||||
|
||||
/// Write the given command to the given stream.
|
||||
fn write_command(&mut self, cmd: String) -> Result<(), Error> {
|
||||
// Write the pixels and a new line
|
||||
self.stream.write(cmd.as_bytes())?;
|
||||
self.stream.write("\n".as_bytes())?;
|
||||
|
||||
// Everything seems to be ok
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// /// Write the given command to the given stream, and read the output.
|
||||
// fn write_read_command(&mut self, cmd: String) -> Result<String, Error> {
|
||||
// // Write the command
|
||||
// self.write_command(cmd);
|
||||
//
|
||||
// // Read the output
|
||||
// let mut buffer = String::with_capacity(CMD_READ_BUFFER_SIZE);
|
||||
// println!("Reading line...");
|
||||
// self.stream.read_line(&mut buffer)?;
|
||||
// println!("Done reading");
|
||||
//
|
||||
// // Return the read string
|
||||
// Ok(buffer)
|
||||
// }
|
||||
}
|
||||
2
src/pix/mod.rs
Normal file
2
src/pix/mod.rs
Normal file
@@ -0,0 +1,2 @@
|
||||
pub mod canvas;
|
||||
pub mod client;
|
||||
Reference in New Issue
Block a user