Skip to content
Snippets Groups Projects
Verified Commit d8b80f0f authored by João Magalhães's avatar João Magalhães :rocket:
Browse files

chore: made image buffer local variable

parent eefec196
No related branches found
No related tags found
1 merge request!23Support for serial data transfer 🔌
Pipeline #2516 failed
...@@ -118,7 +118,6 @@ pub struct PrinterDevice { ...@@ -118,7 +118,6 @@ pub struct PrinterDevice {
data: [u8; 0x280], data: [u8; 0x280],
image: [u8; 160 * 200], image: [u8; 160 * 200],
image_offset: u16, image_offset: u16,
image_buffer: Vec<u8>,
callback: fn(image_buffer: &Vec<u8>) callback: fn(image_buffer: &Vec<u8>)
} }
...@@ -136,7 +135,6 @@ impl PrinterDevice { ...@@ -136,7 +135,6 @@ impl PrinterDevice {
data: [0x00; 0x280], data: [0x00; 0x280],
image: [0x00; 160 * 200], image: [0x00; 160 * 200],
image_offset: 0, image_offset: 0,
image_buffer: Vec::new(),
callback: |_| {} callback: |_| {}
} }
} }
...@@ -153,48 +151,34 @@ impl PrinterDevice { ...@@ -153,48 +151,34 @@ impl PrinterDevice {
self.data = [0x00; 0x280]; self.data = [0x00; 0x280];
self.image = [0x00; 160 * 200]; self.image = [0x00; 160 * 200];
self.image_offset = 0; self.image_offset = 0;
self.clear_image_buffer()
} }
pub fn set_callback(&mut self, callback: fn(image_buffer: &Vec<u8>)) { pub fn set_callback(&mut self, callback: fn(image_buffer: &Vec<u8>)) {
self.callback = callback; self.callback = callback;
} }
pub fn image_buffer(&self) -> &Vec<u8> {
&self.image_buffer
}
pub fn image_buffer_mut(&mut self) -> &mut Vec<u8> {
&mut self.image_buffer
}
pub fn clear_image_buffer(&mut self) {
self.image_buffer.clear();
}
fn run_command(&mut self, command: PrinterCommand) { fn run_command(&mut self, command: PrinterCommand) {
match command { match command {
PrinterCommand::Init => { PrinterCommand::Init => {
self.status = 0x00; self.status = 0x00;
self.byte_out = self.status; self.byte_out = self.status;
self.image_offset = 0; self.image_offset = 0;
self.image_buffer.clear();
} }
PrinterCommand::Print => { PrinterCommand::Print => {
let mut image_buffer = Vec::new();
let palette_index = self.data[2]; let palette_index = self.data[2];
for index in 0..self.image_offset { for index in 0..self.image_offset {
let value = self.image[index as usize]; let value = self.image[index as usize];
let pixel_offset = (palette_index >> (value << 1)) & 0x03; let pixel_offset = (palette_index >> (value << 1)) & 0x03;
let pixel = PRINTER_PALETTE[pixel_offset as usize]; let pixel = PRINTER_PALETTE[pixel_offset as usize];
self.image_buffer.push(pixel[0]); image_buffer.push(pixel[0]);
self.image_buffer.push(pixel[1]); image_buffer.push(pixel[1]);
self.image_buffer.push(pixel[2]); image_buffer.push(pixel[2]);
self.image_buffer.push(pixel[3]); image_buffer.push(pixel[3]);
} }
(self.callback)(&self.image_buffer); (self.callback)(&image_buffer);
self.byte_out = self.status; self.byte_out = self.status;
self.status = 0x06; self.status = 0x06;
...@@ -222,8 +206,6 @@ impl PrinterDevice { ...@@ -222,8 +206,6 @@ impl PrinterDevice {
// mark it as done, resetting the status back to // mark it as done, resetting the status back to
// the original value // the original value
if self.status == 0x06 { if self.status == 0x06 {
// @TODO: check if this value should be 0x04 instead
// this seems to be a bug with the print demo
self.status = 0x00; self.status = 0x00;
} }
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment