Fix tests

This commit is contained in:
Austen Adler 2020-12-22 11:26:03 -05:00
parent ad552acc25
commit 975670c884

View File

@ -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::<Vec<u8>>();
@ -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<bit>
// println!(" Now {:#b}", ret);
println!(" Now {:#b}", ret);
ret |= 0b10 | ((u32::from(byte) >> i) & 0b1);
// println!(" Now {:#b}", ret);
println!(" Now {:#b}", ret);
// 0b1<bit> -> 0b1<bit>0
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<u8> = 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<u8> = 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<u8> = 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)
);
}
}