@@ -6,10 +6,22 @@ const CRC: crc::Crc<u8> = crc::Crc::<u8>::new(&crc::CRC_8_OPENSAFETY);
66
77/// Image slot ID.
88///
9- /// Valid values from 0x00 to 0x06.
9+ /// Valid values from 0x00 to 0x06 for 7 slots maximum.
10+ /// Represented as a 3-bit wide value for use in [State].
11+ ///
12+ /// 0b111 is deemed an invalid value because [State] can never be only 1's.
1013#[ derive( Debug , Clone , Copy , PartialEq ) ]
1114#[ cfg_attr( feature = "defmt" , derive( defmt:: Format ) ) ]
12- pub struct Slot ( u8 ) ;
15+ #[ repr( u8 ) ]
16+ pub enum Slot {
17+ S0 = 0x00 ,
18+ S1 = 0x01 ,
19+ S2 = 0x02 ,
20+ S3 = 0x03 ,
21+ S4 = 0x04 ,
22+ S5 = 0x05 ,
23+ S6 = 0x06 ,
24+ }
1325
1426#[ derive( Debug ) ]
1527#[ cfg_attr( feature = "defmt" , derive( defmt:: Format ) ) ]
@@ -19,17 +31,22 @@ impl TryFrom<u8> for Slot {
1931 type Error = TooManyBits ;
2032
2133 fn try_from ( val : u8 ) -> Result < Slot , Self :: Error > {
22- if val >= MAX_SLOT_COUNT as u8 {
23- Err ( TooManyBits )
24- } else {
25- Ok ( Slot ( val) )
26- }
34+ Ok ( match val {
35+ 0x00 => Slot :: S0 ,
36+ 0x01 => Slot :: S1 ,
37+ 0x02 => Slot :: S2 ,
38+ 0x03 => Slot :: S3 ,
39+ 0x04 => Slot :: S4 ,
40+ 0x05 => Slot :: S5 ,
41+ 0x06 => Slot :: S6 ,
42+ _ => return Err ( TooManyBits ) ,
43+ } )
2744 }
2845}
2946
3047impl From < Slot > for u8 {
3148 fn from ( val : Slot ) -> Self {
32- val. 0
49+ val as u8
3350 }
3451}
3552
@@ -99,8 +116,8 @@ impl State {
99116 pub const fn new ( status : Status , target : Slot , backup : Slot ) -> Self {
100117 let mut data = 0u8 ;
101118 data |= ( status as u8 ) << 6 ;
102- data |= backup. 0 << 3 ;
103- data |= target. 0 ;
119+ data |= ( backup as u8 ) << 3 ;
120+ data |= target as u8 ;
104121
105122 let crc = CRC . checksum ( & [ data] ) ;
106123
@@ -221,8 +238,8 @@ mod tests {
221238 /// Try a few handpicked [State] values and assert Crc value.
222239 #[ test]
223240 fn state_validity_crc ( ) {
224- let slot_a = Slot :: try_from ( 1 ) . unwrap ( ) ;
225- let slot_b = Slot :: try_from ( 2 ) . unwrap ( ) ;
241+ let slot_a = Slot :: S1 ;
242+ let slot_b = Slot :: S2 ;
226243
227244 let state = State :: new ( Status :: Initial , slot_b, slot_a) ;
228245 assert_eq ! ( state. 0 [ 1 ] , 12 ) ; // Crc
0 commit comments