refactor: simplify LED type system to support only WS2812B and SK6812 chips

This commit is contained in:
2025-07-08 22:32:33 +08:00
parent 2c6b777fa6
commit 2d502fcd6c
4 changed files with 28 additions and 24 deletions

View File

@ -18,13 +18,13 @@ pub enum Border {
#[derive(Clone, Copy, Serialize, Deserialize, Debug, PartialEq)] #[derive(Clone, Copy, Serialize, Deserialize, Debug, PartialEq)]
pub enum LedType { pub enum LedType {
RGB, WS2812B,
RGBW, SK6812,
} }
impl Default for LedType { impl Default for LedType {
fn default() -> Self { fn default() -> Self {
LedType::RGB LedType::WS2812B
} }
} }
@ -151,7 +151,7 @@ impl LedStripConfigGroup {
}, },
start_pos: j + i * 4 * 30, start_pos: j + i * 4 * 30,
len: 30, len: 30,
led_type: LedType::RGB, led_type: LedType::WS2812B,
}; };
configs.push(item); configs.push(item);
strips.push(item); strips.push(item);

View File

@ -322,12 +322,12 @@ impl LedColorsPublisher {
let led_type = if group_index < strips.len() { let led_type = if group_index < strips.len() {
strips[group_index].led_type strips[group_index].led_type
} else { } else {
LedType::RGB // fallback to RGB LedType::WS2812B // fallback to WS2812B
}; };
let bytes_per_led = match led_type { let bytes_per_led = match led_type {
LedType::RGB => 3, LedType::WS2812B => 3,
LedType::RGBW => 4, LedType::SK6812 => 4,
}; };
let mut buffer = Vec::<u8>::with_capacity(group_size * bytes_per_led); let mut buffer = Vec::<u8>::with_capacity(group_size * bytes_per_led);
@ -348,7 +348,7 @@ impl LedColorsPublisher {
for i in start_index..end_index { for i in start_index..end_index {
if i < colors.len() { if i < colors.len() {
let bytes = match led_type { let bytes = match led_type {
LedType::RGB => { LedType::WS2812B => {
let calibration_bytes = color_calibration.to_bytes(); let calibration_bytes = color_calibration.to_bytes();
let color_bytes = colors[i].as_bytes(); let color_bytes = colors[i].as_bytes();
// Apply calibration to RGB values // Apply calibration to RGB values
@ -358,7 +358,7 @@ impl LedColorsPublisher {
((color_bytes[2] as f32 * calibration_bytes[2] as f32 / 255.0) as u8), ((color_bytes[2] as f32 * calibration_bytes[2] as f32 / 255.0) as u8),
] ]
} }
LedType::RGBW => { LedType::SK6812 => {
let calibration_bytes = color_calibration.to_bytes_rgbw(); let calibration_bytes = color_calibration.to_bytes_rgbw();
let color_bytes = colors[i].as_bytes(); let color_bytes = colors[i].as_bytes();
// Apply calibration to RGB values and use calibrated W // Apply calibration to RGB values and use calibrated W
@ -375,8 +375,8 @@ impl LedColorsPublisher {
log::warn!("Index {} out of bounds for colors array of length {}", i, colors.len()); log::warn!("Index {} out of bounds for colors array of length {}", i, colors.len());
// Add black color as fallback // Add black color as fallback
match led_type { match led_type {
LedType::RGB => buffer.extend_from_slice(&[0, 0, 0]), LedType::WS2812B => buffer.extend_from_slice(&[0, 0, 0]),
LedType::RGBW => buffer.extend_from_slice(&[0, 0, 0, 0]), LedType::SK6812 => buffer.extend_from_slice(&[0, 0, 0, 0]),
} }
} }
} }
@ -396,7 +396,7 @@ impl LedColorsPublisher {
for i in (start_index..end_index).rev() { for i in (start_index..end_index).rev() {
if i < colors.len() { if i < colors.len() {
let bytes = match led_type { let bytes = match led_type {
LedType::RGB => { LedType::WS2812B => {
let calibration_bytes = color_calibration.to_bytes(); let calibration_bytes = color_calibration.to_bytes();
let color_bytes = colors[i].as_bytes(); let color_bytes = colors[i].as_bytes();
// Apply calibration to RGB values // Apply calibration to RGB values
@ -406,7 +406,7 @@ impl LedColorsPublisher {
((color_bytes[2] as f32 * calibration_bytes[2] as f32 / 255.0) as u8), ((color_bytes[2] as f32 * calibration_bytes[2] as f32 / 255.0) as u8),
] ]
} }
LedType::RGBW => { LedType::SK6812 => {
let calibration_bytes = color_calibration.to_bytes_rgbw(); let calibration_bytes = color_calibration.to_bytes_rgbw();
let color_bytes = colors[i].as_bytes(); let color_bytes = colors[i].as_bytes();
// Apply calibration to RGB values and use calibrated W // Apply calibration to RGB values and use calibrated W
@ -423,8 +423,8 @@ impl LedColorsPublisher {
log::warn!("Index {} out of bounds for colors array of length {}", i, colors.len()); log::warn!("Index {} out of bounds for colors array of length {}", i, colors.len());
// Add black color as fallback // Add black color as fallback
match led_type { match led_type {
LedType::RGB => buffer.extend_from_slice(&[0, 0, 0]), LedType::WS2812B => buffer.extend_from_slice(&[0, 0, 0]),
LedType::RGBW => buffer.extend_from_slice(&[0, 0, 0, 0]), LedType::SK6812 => buffer.extend_from_slice(&[0, 0, 0, 0]),
} }
} }
} }

View File

@ -19,13 +19,17 @@ pub struct TestEffectConfig {
#[derive(Debug, Clone, Serialize, Deserialize)] #[derive(Debug, Clone, Serialize, Deserialize)]
pub enum LedType { pub enum LedType {
RGB, WS2812B,
RGBW, SK6812,
} }
pub struct LedTestEffects; pub struct LedTestEffects;
impl LedTestEffects { impl LedTestEffects {
/// Check if LED type supports white channel (RGBW)
fn is_rgbw_type(led_type: &LedType) -> bool {
matches!(led_type, LedType::SK6812)
}
/// Generate LED colors for a specific test effect at a given time /// Generate LED colors for a specific test effect at a given time
pub fn generate_colors(config: &TestEffectConfig, time_ms: u64) -> Vec<u8> { pub fn generate_colors(config: &TestEffectConfig, time_ms: u64) -> Vec<u8> {
let time_seconds = time_ms as f64 / 1000.0; let time_seconds = time_ms as f64 / 1000.0;
@ -60,7 +64,7 @@ impl LedTestEffects {
buffer.push(rgb.1); buffer.push(rgb.1);
buffer.push(rgb.2); buffer.push(rgb.2);
if matches!(led_type, LedType::RGBW) { if Self::is_rgbw_type(&led_type) {
buffer.push(0); // White channel buffer.push(0); // White channel
} }
} }
@ -93,7 +97,7 @@ impl LedTestEffects {
buffer.push(color.1); buffer.push(color.1);
buffer.push(color.2); buffer.push(color.2);
if matches!(led_type, LedType::RGBW) { if Self::is_rgbw_type(&led_type) {
buffer.push(0); // White channel buffer.push(0); // White channel
} }
} }
@ -114,7 +118,7 @@ impl LedTestEffects {
buffer.push(255); buffer.push(255);
buffer.push(255); buffer.push(255);
if matches!(led_type, LedType::RGBW) { if Self::is_rgbw_type(&led_type) {
buffer.push(255); // White channel buffer.push(255); // White channel
} }
} else { } else {
@ -123,7 +127,7 @@ impl LedTestEffects {
buffer.push(0); buffer.push(0);
buffer.push(0); buffer.push(0);
if matches!(led_type, LedType::RGBW) { if Self::is_rgbw_type(&led_type) {
buffer.push(0); // White channel buffer.push(0); // White channel
} }
} }
@ -144,7 +148,7 @@ impl LedTestEffects {
buffer.push(brightness); buffer.push(brightness);
buffer.push(brightness); buffer.push(brightness);
if matches!(led_type, LedType::RGBW) { if Self::is_rgbw_type(&led_type) {
buffer.push(brightness); // White channel buffer.push(brightness); // White channel
} }
} }

View File

@ -324,8 +324,8 @@ async fn stop_led_test_effect(board_address: String, led_count: u32, led_type: l
// Turn off all LEDs // Turn off all LEDs
let bytes_per_led = match led_type { let bytes_per_led = match led_type {
led_test_effects::LedType::RGB => 3, led_test_effects::LedType::WS2812B => 3,
led_test_effects::LedType::RGBW => 4, led_test_effects::LedType::SK6812 => 4,
}; };
let buffer = vec![0u8; (led_count * bytes_per_led) as usize]; let buffer = vec![0u8; (led_count * bytes_per_led) as usize];