diff options
| author | Robert Williams <[email protected]> | 2025-10-27 21:44:53 +0000 |
|---|---|---|
| committer | Robert Williams <[email protected]> | 2025-11-03 21:37:09 +0000 |
| commit | 07f45bd12a355a9775e1dac472ee127e47ed3624 (patch) | |
| tree | 77f1fc573c4e43fcc053f8d59f575b06c1a44a49 /embassy-rp | |
| parent | 2546d26cf2748a37b74aac4dbdab3ff4df460cc1 (diff) | |
fix: split traits to avoid inference problems
Diffstat (limited to 'embassy-rp')
| -rw-r--r-- | embassy-rp/src/pio_programs/ws2812.rs | 42 |
1 files changed, 19 insertions, 23 deletions
diff --git a/embassy-rp/src/pio_programs/ws2812.rs b/embassy-rp/src/pio_programs/ws2812.rs index 3c16367e6..38f95facb 100644 --- a/embassy-rp/src/pio_programs/ws2812.rs +++ b/embassy-rp/src/pio_programs/ws2812.rs | |||
| @@ -16,48 +16,44 @@ const T2: u8 = 5; // data bit | |||
| 16 | const T3: u8 = 3; // stop bit | 16 | const T3: u8 = 3; // stop bit |
| 17 | const CYCLES_PER_BIT: u32 = (T1 + T2 + T3) as u32; | 17 | const CYCLES_PER_BIT: u32 = (T1 + T2 + T3) as u32; |
| 18 | 18 | ||
| 19 | /// This trait defines the contract for color ordering | 19 | /// Color orders for WS2812B, type RGB8 |
| 20 | pub trait ColorOrder { | 20 | pub trait RgbColorOrder { |
| 21 | type ColorType; | 21 | fn pack(color: RGB8) -> u32; |
| 22 | |||
| 23 | // Pack the type specific struct into a u32 word in the correct order | ||
| 24 | fn pack(color: Self::ColorType) -> u32; | ||
| 25 | } | 22 | } |
| 26 | 23 | ||
| 27 | /// Color orders for WS2812B, type RGB8 | ||
| 28 | /// Green, Red, Blue order is the common default for WS2812B | 24 | /// Green, Red, Blue order is the common default for WS2812B |
| 29 | pub struct Grb; | 25 | pub struct Grb; |
| 30 | impl ColorOrder for Grb { | 26 | impl RgbColorOrder for Grb { |
| 31 | type ColorType = RGB8; | 27 | fn pack(color: RGB8) -> u32 { |
| 32 | fn pack(color: Self::ColorType) -> u32 { | ||
| 33 | (u32::from(color.g) << 24) | (u32::from(color.r) << 16) | (u32::from(color.b) << 8) | 28 | (u32::from(color.g) << 24) | (u32::from(color.r) << 16) | (u32::from(color.b) << 8) |
| 34 | } | 29 | } |
| 35 | } | 30 | } |
| 36 | 31 | ||
| 37 | /// Red, Green, Blue is used by some WS2812B implementations | 32 | /// Red, Green, Blue is used by some WS2812B implementations |
| 38 | pub struct Rgb; | 33 | pub struct Rgb; |
| 39 | impl ColorOrder for Rgb { | 34 | impl RgbColorOrder for Rgb { |
| 40 | type ColorType = RGB8; | 35 | fn pack(color: RGB8) -> u32 { |
| 41 | fn pack(color: Self::ColorType) -> u32 { | ||
| 42 | (u32::from(color.r) << 24) | (u32::from(color.g) << 16) | (u32::from(color.b) << 8) | 36 | (u32::from(color.r) << 24) | (u32::from(color.g) << 16) | (u32::from(color.b) << 8) |
| 43 | } | 37 | } |
| 44 | } | 38 | } |
| 45 | 39 | ||
| 46 | /// Color orders RGBW strips | 40 | /// Color orders RGBW strips |
| 41 | pub trait RgbwColorOrder { | ||
| 42 | fn pack(color: RGBW<u8>) -> u32; | ||
| 43 | } | ||
| 44 | |||
| 47 | /// Green, Red, Blue, White order is the common default for RGBW strips | 45 | /// Green, Red, Blue, White order is the common default for RGBW strips |
| 48 | pub struct Grbw; | 46 | pub struct Grbw; |
| 49 | impl ColorOrder for Grbw { | 47 | impl RgbwColorOrder for Grbw { |
| 50 | type ColorType = RGBW<u8>; | 48 | fn pack(color: RGBW<u8>) -> u32 { |
| 51 | fn pack(color: Self::ColorType) -> u32 { | ||
| 52 | (u32::from(color.g) << 24) | (u32::from(color.r) << 16) | (u32::from(color.b) << 8) | u32::from(color.a.0) | 49 | (u32::from(color.g) << 24) | (u32::from(color.r) << 16) | (u32::from(color.b) << 8) | u32::from(color.a.0) |
| 53 | } | 50 | } |
| 54 | } | 51 | } |
| 55 | 52 | ||
| 56 | /// Red, Green, Blue, White order | 53 | /// Red, Green, Blue, White order |
| 57 | pub struct Rgbw; | 54 | pub struct Rgbw; |
| 58 | impl ColorOrder for Rgbw { | 55 | impl RgbwColorOrder for Rgbw { |
| 59 | type ColorType = RGBW<u8>; | 56 | fn pack(color: RGBW<u8>) -> u32 { |
| 60 | fn pack(color: Self::ColorType) -> u32 { | ||
| 61 | (u32::from(color.r) << 24) | (u32::from(color.g) << 16) | (u32::from(color.b) << 8) | u32::from(color.a.0) | 57 | (u32::from(color.r) << 24) | (u32::from(color.g) << 16) | (u32::from(color.b) << 8) | u32::from(color.a.0) |
| 62 | } | 58 | } |
| 63 | } | 59 | } |
| @@ -100,7 +96,7 @@ impl<'a, PIO: Instance> PioWs2812Program<'a, PIO> { | |||
| 100 | /// Const N is the number of ws2812 leds attached to this pin | 96 | /// Const N is the number of ws2812 leds attached to this pin |
| 101 | pub struct PioWs2812<'d, P: Instance, const S: usize, const N: usize, ORDER = Grb> | 97 | pub struct PioWs2812<'d, P: Instance, const S: usize, const N: usize, ORDER = Grb> |
| 102 | where | 98 | where |
| 103 | ORDER: ColorOrder<ColorType = RGB8>, | 99 | ORDER: RgbColorOrder, |
| 104 | { | 100 | { |
| 105 | dma: Peri<'d, AnyChannel>, | 101 | dma: Peri<'d, AnyChannel>, |
| 106 | sm: StateMachine<'d, P, S>, | 102 | sm: StateMachine<'d, P, S>, |
| @@ -109,7 +105,7 @@ where | |||
| 109 | 105 | ||
| 110 | impl<'d, P: Instance, const S: usize, const N: usize, ORDER = Grb> PioWs2812<'d, P, S, N, ORDER> | 106 | impl<'d, P: Instance, const S: usize, const N: usize, ORDER = Grb> PioWs2812<'d, P, S, N, ORDER> |
| 111 | where | 107 | where |
| 112 | ORDER: ColorOrder<ColorType = RGB8>, | 108 | ORDER: RgbColorOrder, |
| 113 | { | 109 | { |
| 114 | /// Configure a pio state machine to use the loaded ws2812 program. | 110 | /// Configure a pio state machine to use the loaded ws2812 program. |
| 115 | pub fn new( | 111 | pub fn new( |
| @@ -173,7 +169,7 @@ where | |||
| 173 | /// Const N is the number of ws2812 leds attached to this pin | 169 | /// Const N is the number of ws2812 leds attached to this pin |
| 174 | pub struct RgbwPioWs2812<'d, P: Instance, const S: usize, const N: usize, ORDER = Grbw> | 170 | pub struct RgbwPioWs2812<'d, P: Instance, const S: usize, const N: usize, ORDER = Grbw> |
| 175 | where | 171 | where |
| 176 | ORDER: ColorOrder<ColorType = RGBW<u8>>, | 172 | ORDER: RgbwColorOrder, |
| 177 | { | 173 | { |
| 178 | dma: Peri<'d, AnyChannel>, | 174 | dma: Peri<'d, AnyChannel>, |
| 179 | sm: StateMachine<'d, P, S>, | 175 | sm: StateMachine<'d, P, S>, |
| @@ -182,7 +178,7 @@ where | |||
| 182 | 178 | ||
| 183 | impl<'d, P: Instance, const S: usize, const N: usize, ORDER = Grbw> RgbwPioWs2812<'d, P, S, N, ORDER> | 179 | impl<'d, P: Instance, const S: usize, const N: usize, ORDER = Grbw> RgbwPioWs2812<'d, P, S, N, ORDER> |
| 184 | where | 180 | where |
| 185 | ORDER: ColorOrder<ColorType = RGBW<u8>>, | 181 | ORDER: RgbwColorOrder, |
| 186 | { | 182 | { |
| 187 | /// Configure a pio state machine to use the loaded ws2812 program. | 183 | /// Configure a pio state machine to use the loaded ws2812 program. |
| 188 | pub fn new( | 184 | pub fn new( |
