Fix tests
This commit is contained in:
parent
ad552acc25
commit
975670c884
@ -8,7 +8,7 @@ pub struct LedPanel {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Stores color as a tuple of (Red, Green, Blue)
|
/// 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 {
|
impl LedPanel {
|
||||||
pub fn new(num_leds: u32) -> LedPanel {
|
pub fn new(num_leds: u32) -> LedPanel {
|
||||||
@ -28,7 +28,8 @@ impl LedPanel {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn write(&mut self) {
|
fn write(&mut self) {
|
||||||
let output = self.buffer
|
let output = self
|
||||||
|
.buffer
|
||||||
.drain(..)
|
.drain(..)
|
||||||
.flat_map(|val| LedPanel::to_ws2812b_bytes(val).to_vec())
|
.flat_map(|val| LedPanel::to_ws2812b_bytes(val).to_vec())
|
||||||
.collect::<Vec<u8>>();
|
.collect::<Vec<u8>>();
|
||||||
@ -37,11 +38,10 @@ impl LedPanel {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn set_leds(&mut self, hex_codes: &[RGB]) {
|
pub fn set_leds(&mut self, hex_codes: &[RGB]) {
|
||||||
hex_codes
|
hex_codes.iter().for_each(|hex_code| {
|
||||||
.iter()
|
|
||||||
.for_each(|hex_code| {
|
|
||||||
// Swapping here from RGB to the GRB expected by the LED panel
|
// 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.buffer
|
||||||
|
.extend_from_slice(&[hex_code.1, hex_code.0, hex_code.2]);
|
||||||
});
|
});
|
||||||
self.write();
|
self.write();
|
||||||
}
|
}
|
||||||
@ -61,51 +61,57 @@ impl LedPanel {
|
|||||||
* 1 -> 110
|
* 1 -> 110
|
||||||
* 0 -> 100
|
* 0 -> 100
|
||||||
*/
|
*/
|
||||||
fn to_ws2812b_bytes(byte: u8) -> [u8; 3] {
|
pub fn to_ws2812b_bytes(byte: u8) -> [u8; 3] {
|
||||||
let mut ret: u32 = 0b0;
|
let mut ret: u32 = 0b0;
|
||||||
|
|
||||||
println!("Adding byte {:#b}", byte);
|
println!("Adding byte {:#b}", byte);
|
||||||
// Each bit in byte
|
// Each bit in byte
|
||||||
// for i in (0..8).rev() // Correct order
|
// for i in (0..8).rev() // Correct order
|
||||||
for i in 0..8 { // Reverse order
|
for i in 0..8 {
|
||||||
// println!(" Pushing bit {:}", (u32::from(byte) >> i) & 0b1);
|
// Reverse order
|
||||||
// println!(" Now {:#b}", ret);
|
println!(" Pushing bit {:} ({:})", (u32::from(byte) >> i) & 0b1, i);
|
||||||
|
println!(" Now {:#b}", ret);
|
||||||
// 0b -> 0b00
|
// 0b -> 0b00
|
||||||
ret <<= 2;
|
ret <<= 2;
|
||||||
// 0b00 -> 0b1<bit>
|
// 0b00 -> 0b1<bit>
|
||||||
// println!(" Now {:#b}", ret);
|
println!(" Now {:#b}", ret);
|
||||||
ret |= 0b10 | ((u32::from(byte) >> i) & 0b1);
|
ret |= 0b10 | ((u32::from(byte) >> i) & 0b1);
|
||||||
// println!(" Now {:#b}", ret);
|
println!(" Now {:#b}", ret);
|
||||||
// 0b1<bit> -> 0b1<bit>0
|
// 0b1<bit> -> 0b1<bit>0
|
||||||
ret <<= 1;
|
ret <<= 1;
|
||||||
// println!(" Now {:#b}", ret);
|
println!(" Now {:#b}", ret);
|
||||||
}
|
}
|
||||||
let bytes: [u8; 4] = ret.to_be_bytes();
|
let bytes: [u8; 4] = ret.to_be_bytes();
|
||||||
[bytes[1], bytes[2], bytes[3]]
|
[bytes[1], bytes[2], bytes[3]]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
|
use super::LedPanel;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn convert_0() {
|
fn convert_0() {
|
||||||
let mut output: Vec<u8> = Vec::new();
|
assert_eq!(
|
||||||
super::to_ws2812b_bytes(&mut output, 0b00000000);
|
vec![0b10010010, 0b01001001, 0b00100100],
|
||||||
assert_eq!(vec![0b10010010, 0b01001001, 0b00100100], output);
|
LedPanel::to_ws2812b_bytes(0b00000000)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn convert_number() {
|
fn convert_number() {
|
||||||
let mut output: Vec<u8> = Vec::new();
|
// assert_eq!(vec![0b10011010, 0b01101001, 0b10110100], LedPanel::to_ws2812b_bytes(0b01010110)); // Regular
|
||||||
super::to_ws2812b_bytes(&mut output, 0b01010110);
|
assert_eq!(
|
||||||
// assert_eq!(vec![0b10011010, 0b01101001, 0b10110100], output); // Regular
|
vec![0b10011011, 0b01001101, 0b00110100],
|
||||||
assert_eq!(vec![0b10011011, 0b01001101, 0b00110100], output); // Reversed
|
LedPanel::to_ws2812b_bytes(0b01010110)
|
||||||
|
); // Reversed
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn convert_max() {
|
fn convert_max() {
|
||||||
let mut output: Vec<u8> = Vec::new();
|
assert_eq!(
|
||||||
super::to_ws2812b_bytes(&mut output, 0b11111111);
|
vec![0b11011011, 0b01101101, 0b10110110],
|
||||||
assert_eq!(vec![0b11011011, 0b01101101, 0b10110110], output);
|
LedPanel::to_ws2812b_bytes(0b11111111)
|
||||||
}
|
);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user