From 975670c88494b655be17f75e4c9622bc3b0266f7 Mon Sep 17 00:00:00 2001 From: Austen Adler Date: Tue, 22 Dec 2020 11:26:03 -0500 Subject: [PATCH] Fix tests --- src/wire_protocol.rs | 80 ++++++++++++++++++++++++-------------------- 1 file changed, 43 insertions(+), 37 deletions(-) diff --git a/src/wire_protocol.rs b/src/wire_protocol.rs index 02acf7c..6e55d8b 100644 --- a/src/wire_protocol.rs +++ b/src/wire_protocol.rs @@ -8,7 +8,7 @@ pub struct LedPanel { } /// Stores color as a tuple of (Red, Green, Blue) -pub struct RGB (pub u8, pub u8, pub u8); +pub struct RGB(pub u8, pub u8, pub u8); impl LedPanel { pub fn new(num_leds: u32) -> LedPanel { @@ -28,7 +28,8 @@ impl LedPanel { } fn write(&mut self) { - let output = self.buffer + let output = self + .buffer .drain(..) .flat_map(|val| LedPanel::to_ws2812b_bytes(val).to_vec()) .collect::>(); @@ -37,12 +38,11 @@ impl LedPanel { } pub fn set_leds(&mut self, hex_codes: &[RGB]) { - hex_codes - .iter() - .for_each(|hex_code| { - // Swapping here from RGB to the GRB expected by the LED panel - self.buffer.extend_from_slice(&[hex_code.1, hex_code.0, hex_code.2]); - }); + hex_codes.iter().for_each(|hex_code| { + // Swapping here from RGB to the GRB expected by the LED panel + self.buffer + .extend_from_slice(&[hex_code.1, hex_code.0, hex_code.2]); + }); self.write(); } @@ -61,51 +61,57 @@ impl LedPanel { * 1 -> 110 * 0 -> 100 */ - fn to_ws2812b_bytes(byte: u8) -> [u8; 3] { + pub fn to_ws2812b_bytes(byte: u8) -> [u8; 3] { let mut ret: u32 = 0b0; println!("Adding byte {:#b}", byte); // Each bit in byte // for i in (0..8).rev() // Correct order - for i in 0..8 { // Reverse order - // println!(" Pushing bit {:}", (u32::from(byte) >> i) & 0b1); - // println!(" Now {:#b}", ret); + for i in 0..8 { + // Reverse order + println!(" Pushing bit {:} ({:})", (u32::from(byte) >> i) & 0b1, i); + println!(" Now {:#b}", ret); // 0b -> 0b00 ret <<= 2; // 0b00 -> 0b1 - // println!(" Now {:#b}", ret); + println!(" Now {:#b}", ret); ret |= 0b10 | ((u32::from(byte) >> i) & 0b1); - // println!(" Now {:#b}", ret); + println!(" Now {:#b}", ret); // 0b1 -> 0b10 ret <<= 1; - // println!(" Now {:#b}", ret); + println!(" Now {:#b}", ret); } let bytes: [u8; 4] = ret.to_be_bytes(); [bytes[1], bytes[2], bytes[3]] } } - #[cfg(test)] - mod tests { - #[test] - fn convert_0() { - let mut output: Vec = Vec::new(); - super::to_ws2812b_bytes(&mut output, 0b00000000); - assert_eq!(vec![0b10010010, 0b01001001, 0b00100100], output); - } +#[cfg(test)] +mod tests { + use super::LedPanel; - #[test] - fn convert_number() { - let mut output: Vec = Vec::new(); - super::to_ws2812b_bytes(&mut output, 0b01010110); - // assert_eq!(vec![0b10011010, 0b01101001, 0b10110100], output); // Regular - assert_eq!(vec![0b10011011, 0b01001101, 0b00110100], output); // Reversed - } - - #[test] - fn convert_max() { - let mut output: Vec = Vec::new(); - super::to_ws2812b_bytes(&mut output, 0b11111111); - assert_eq!(vec![0b11011011, 0b01101101, 0b10110110], output); - } + #[test] + fn convert_0() { + assert_eq!( + vec![0b10010010, 0b01001001, 0b00100100], + LedPanel::to_ws2812b_bytes(0b00000000) + ); } + + #[test] + fn convert_number() { + // assert_eq!(vec![0b10011010, 0b01101001, 0b10110100], LedPanel::to_ws2812b_bytes(0b01010110)); // Regular + assert_eq!( + vec![0b10011011, 0b01001101, 0b00110100], + LedPanel::to_ws2812b_bytes(0b01010110) + ); // Reversed + } + + #[test] + fn convert_max() { + assert_eq!( + vec![0b11011011, 0b01101101, 0b10110110], + LedPanel::to_ws2812b_bytes(0b11111111) + ); + } +}