aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRobert Williams <[email protected]>2025-10-27 21:44:53 +0000
committerRobert Williams <[email protected]>2025-11-03 21:37:09 +0000
commit07f45bd12a355a9775e1dac472ee127e47ed3624 (patch)
tree77f1fc573c4e43fcc053f8d59f575b06c1a44a49
parent2546d26cf2748a37b74aac4dbdab3ff4df460cc1 (diff)
fix: split traits to avoid inference problems
-rw-r--r--embassy-rp/src/pio_programs/ws2812.rs42
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
16const T3: u8 = 3; // stop bit 16const T3: u8 = 3; // stop bit
17const CYCLES_PER_BIT: u32 = (T1 + T2 + T3) as u32; 17const 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
20pub trait ColorOrder { 20pub 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
29pub struct Grb; 25pub struct Grb;
30impl ColorOrder for Grb { 26impl 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
38pub struct Rgb; 33pub struct Rgb;
39impl ColorOrder for Rgb { 34impl 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
41pub 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
48pub struct Grbw; 46pub struct Grbw;
49impl ColorOrder for Grbw { 47impl 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
57pub struct Rgbw; 54pub struct Rgbw;
58impl ColorOrder for Rgbw { 55impl 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
101pub struct PioWs2812<'d, P: Instance, const S: usize, const N: usize, ORDER = Grb> 97pub struct PioWs2812<'d, P: Instance, const S: usize, const N: usize, ORDER = Grb>
102where 98where
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
110impl<'d, P: Instance, const S: usize, const N: usize, ORDER = Grb> PioWs2812<'d, P, S, N, ORDER> 106impl<'d, P: Instance, const S: usize, const N: usize, ORDER = Grb> PioWs2812<'d, P, S, N, ORDER>
111where 107where
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
174pub struct RgbwPioWs2812<'d, P: Instance, const S: usize, const N: usize, ORDER = Grbw> 170pub struct RgbwPioWs2812<'d, P: Instance, const S: usize, const N: usize, ORDER = Grbw>
175where 171where
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
183impl<'d, P: Instance, const S: usize, const N: usize, ORDER = Grbw> RgbwPioWs2812<'d, P, S, N, ORDER> 179impl<'d, P: Instance, const S: usize, const N: usize, ORDER = Grbw> RgbwPioWs2812<'d, P, S, N, ORDER>
184where 180where
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(