11
2- // use embedded_hal::{
3- // blocking::delay::{DelayUs, DelayMs},
4- // };
5-
6- use super :: { SensorInterface , PACKET_HEADER_LENGTH } ;
2+ use super :: { SensorInterface , SensorCommon , PACKET_HEADER_LENGTH } ;
3+ use crate :: Error ;
4+ use embedded_hal:: blocking:: delay:: DelayMs ;
75
86/// the i2c address normally used by BNO080
97pub const DEFAULT_ADDRESS : u8 = 0x4A ;
@@ -24,6 +22,9 @@ pub struct I2cInterface<I2C> {
2422 address : u8 ,
2523 /// buffer for receiving segments of packets from the sensor hub
2624 seg_recv_buf : [ u8 ; SEG_RECV_BUF_LEN ] ,
25+
26+ /// number of packets received
27+ received_packet_count : usize ,
2728}
2829
2930impl < I2C , CommE > I2cInterface < I2C >
@@ -36,42 +37,22 @@ impl<I2C, CommE> I2cInterface<I2C>
3637 i2c_port : i2c,
3738 address : addr,
3839 seg_recv_buf : [ 0 ; SEG_RECV_BUF_LEN ] ,
40+ received_packet_count : 0 ,
3941 }
4042 }
41- }
42-
43-
44- #[ derive( Debug ) ]
45- pub enum I2cCommError < E > {
46- /// I2C bus error
47- I2c ( E ) ,
48- }
49-
50- impl < I2C , CommE > SensorInterface for I2cInterface < I2C >
51- where
52- I2C : embedded_hal:: blocking:: i2c:: Write < Error = CommE > +
53- embedded_hal:: blocking:: i2c:: Read < Error = CommE >
54- {
55- type SensorError = I2cCommError < CommE > ;
5643
57- fn send_packet ( & mut self , packet : & [ u8 ] ) -> Result < ( ) , Self :: SensorError > {
58- self . i2c_port . write ( self . address ,
59- & packet) . map_err ( Self :: SensorError :: I2c ) ?;
60- Ok ( ( ) )
61- }
62-
63- fn read_packet_header ( & mut self , recv_buf : & mut [ u8 ] ) -> Result < ( ) , Self :: SensorError > {
64- recv_buf[ 0 ] = 0 ;
65- recv_buf[ 1 ] = 0 ;
66- self . i2c_port . read ( self . address , & mut recv_buf[ ..PACKET_HEADER_LENGTH ] ) . map_err ( Self :: SensorError :: I2c ) ?;
44+ fn read_packet_header ( & mut self ) -> Result < ( ) , Error < CommE , ( ) > > {
45+ self . seg_recv_buf [ 0 ] = 0 ;
46+ self . seg_recv_buf [ 1 ] = 0 ;
47+ self . i2c_port . read ( self . address , & mut self . seg_recv_buf [ ..PACKET_HEADER_LENGTH ] ) . map_err ( Error :: Comm ) ?;
6748 Ok ( ( ) )
6849 }
6950
7051 /// Read the remainder of the packet after the packet header, if any
7152 fn read_sized_packet ( & mut self ,
7253 total_packet_len : usize ,
7354 packet_recv_buf : & mut [ u8 ]
74- ) -> Result < usize , Self :: SensorError > {
55+ ) -> Result < usize , Error < CommE , ( ) > > {
7556 //iprintln!("sized: {}", total_packet_len).unwrap();
7657 let mut remaining_body_len: usize = total_packet_len - PACKET_HEADER_LENGTH ;
7758 let mut already_read_len: usize = 0 ;
@@ -83,7 +64,7 @@ impl<I2C, CommE> SensorInterface for I2cInterface<I2C>
8364 if total_packet_len > 0 {
8465 self . i2c_port
8566 . read ( self . address , & mut packet_recv_buf[ ..total_packet_len] )
86- . map_err ( Self :: SensorError :: I2c ) ?;
67+ . map_err ( Error :: Comm ) ?;
8768 //let packet_declared_len = Self::parse_packet_header(&self.packet_recv_buf[..PACKET_HEADER_LENGTH]);
8869 already_read_len = total_packet_len;
8970 }
@@ -97,7 +78,7 @@ impl<I2C, CommE> SensorInterface for I2cInterface<I2C>
9778
9879 self . seg_recv_buf [ 0 ] = 0 ;
9980 self . seg_recv_buf [ 1 ] = 0 ;
100- self . i2c_port . read ( self . address , & mut self . seg_recv_buf [ ..segment_read_len] ) . map_err ( Self :: SensorError :: I2c ) ?;
81+ self . i2c_port . read ( self . address , & mut self . seg_recv_buf [ ..segment_read_len] ) . map_err ( Error :: Comm ) ?;
10182 //let packet_declared_len = Self::parse_packet_header(&self.seg_recv_buf[..PACKET_HEADER_LENGTH]);
10283
10384 //if we've never read any segments, transcribe the first packet header;
@@ -116,6 +97,96 @@ impl<I2C, CommE> SensorInterface for I2cInterface<I2C>
11697
11798 Ok ( already_read_len)
11899 }
100+ }
101+
102+
103+ impl < I2C , CommE > SensorInterface for I2cInterface < I2C >
104+ where
105+ I2C : embedded_hal:: blocking:: i2c:: Write < Error = CommE > +
106+ embedded_hal:: blocking:: i2c:: Read < Error = CommE >
107+ {
108+ type SensorError = Error < CommE , ( ) > ;
109+
110+ fn setup ( & mut self , _delay_source : & mut impl DelayMs < u8 > ) -> Result < ( ) , Self :: SensorError > {
111+ Ok ( ( ) )
112+ }
113+
114+ fn wait_for_data_available ( & mut self , _max_ms : u8 , _delay_source : & mut impl DelayMs < u8 > ) -> bool {
115+ let rc = self . read_packet_header ( ) ;
116+ if rc. is_err ( ) {
117+ return false ;
118+ }
119+ let packet_len = SensorCommon :: parse_packet_header ( & self . seg_recv_buf [ ..PACKET_HEADER_LENGTH ] ) ;
120+ packet_len > 0
121+ }
122+
123+ fn send_packet ( & mut self , packet : & [ u8 ] ) -> Result < ( ) , Self :: SensorError > {
124+ self . i2c_port . write ( self . address ,
125+ & packet) . map_err ( Error :: Comm ) ?;
126+ Ok ( ( ) )
127+ }
128+
129+ /// Read one packet into the receive buffer
130+ fn read_packet ( & mut self , recv_buf : & mut [ u8 ] ) -> Result < usize , Self :: SensorError > {
131+ self . read_packet_header ( ) ?;
132+ let packet_len = SensorCommon :: parse_packet_header ( & self . seg_recv_buf [ ..PACKET_HEADER_LENGTH ] ) ;
133+
134+ let received_len =
135+ if packet_len > PACKET_HEADER_LENGTH {
136+ self . read_sized_packet ( packet_len, recv_buf) ?
137+ }
138+ else {
139+ packet_len
140+ } ;
141+
142+ if packet_len > 0 {
143+ self . received_packet_count += 1 ;
144+ }
145+
146+ Ok ( received_len)
147+ }
148+
149+
150+
151+ }
152+
153+ #[ cfg( test) ]
154+ mod tests {
155+ use crate :: interface:: mock_i2c_port:: FakeI2cPort ;
156+ use crate :: interface:: I2cInterface ;
157+ use crate :: interface:: i2c:: DEFAULT_ADDRESS ;
158+ use crate :: wrapper:: BNO080 ;
159+
160+ #[ test]
161+ fn test_multi_segment_receive_packet ( ) {
162+ let mut mock_i2c_port = FakeI2cPort :: new ( ) ;
163+
164+ let packet = ADVERTISING_PACKET_FULL ;
165+ mock_i2c_port. add_available_packet ( & packet) ;
166+
167+ let mut shub = BNO080 :: new_with_interface (
168+ I2cInterface :: new ( mock_i2c_port, DEFAULT_ADDRESS ) ) ;
169+ let rc = shub. receive_packet ( ) ;
170+
171+ assert ! ( rc. is_ok( ) ) ;
172+ let next_packet_size = rc. unwrap_or ( 0 ) ;
173+ assert_eq ! ( next_packet_size, packet. len( ) , "wrong length" ) ;
174+ }
175+
119176
177+ // Actual advertising packet received from sensor:
178+ pub const ADVERTISING_PACKET_FULL : [ u8 ; 276 ] = [
179+ 0x14 , 0x81 , 0x00 , 0x01 ,
180+ 0x00 , 0x01 , 0x04 , 0x00 , 0x00 , 0x00 , 0x00 , 0x80 , 0x06 , 0x31 , 0x2e , 0x30 , 0x2e , 0x30 , 0x00 , 0x02 , 0x02 , 0x00 , 0x01 , 0x03 , 0x02 , 0xff , 0x7f , 0x04 , 0x02 , 0x00 , 0x01 , 0x05 ,
181+ 0x02 , 0xff , 0x7f , 0x08 , 0x05 , 0x53 , 0x48 , 0x54 , 0x50 , 0x00 , 0x06 , 0x01 , 0x00 , 0x09 , 0x08 , 0x63 , 0x6f , 0x6e , 0x74 , 0x72 , 0x6f , 0x6c , 0x00 , 0x01 , 0x04 , 0x01 , 0x00 , 0x00 ,
182+ 0x00 , 0x08 , 0x0b , 0x65 , 0x78 , 0x65 , 0x63 , 0x75 , 0x74 , 0x61 , 0x62 , 0x6c , 0x65 , 0x00 , 0x06 , 0x01 , 0x01 , 0x09 , 0x07 , 0x64 , 0x65 , 0x76 , 0x69 , 0x63 , 0x65 , 0x00 , 0x01 , 0x04 ,
183+ 0x02 , 0x00 , 0x00 , 0x00 , 0x08 , 0x0a , 0x73 , 0x65 , 0x6e , 0x73 , 0x6f , 0x72 , 0x68 , 0x75 , 0x62 , 0x00 , 0x06 , 0x01 , 0x02 , 0x09 , 0x08 , 0x63 , 0x6f , 0x6e , 0x74 , 0x72 , 0x6f , 0x6c ,
184+ 0x00 , 0x06 , 0x01 , 0x03 , 0x09 , 0x0c , 0x69 , 0x6e , 0x70 , 0x75 , 0x74 , 0x4e , 0x6f , 0x72 , 0x6d , 0x61 , 0x6c , 0x00 , 0x07 , 0x01 , 0x04 , 0x09 , 0x0a , 0x69 , 0x6e , 0x70 , 0x75 , 0x74 ,
185+ 0x57 , 0x61 , 0x6b , 0x65 , 0x00 , 0x06 , 0x01 , 0x05 , 0x09 , 0x0c , 0x69 , 0x6e , 0x70 , 0x75 , 0x74 , 0x47 , 0x79 , 0x72 , 0x6f , 0x52 , 0x76 , 0x00 , 0x80 , 0x06 , 0x31 , 0x2e , 0x31 , 0x2e ,
186+ 0x30 , 0x00 , 0x81 , 0x64 , 0xf8 , 0x10 , 0xf5 , 0x04 , 0xf3 , 0x10 , 0xf1 , 0x10 , 0xfb , 0x05 , 0xfa , 0x05 , 0xfc , 0x11 , 0xef , 0x02 , 0x01 , 0x0a , 0x02 , 0x0a , 0x03 , 0x0a , 0x04 , 0x0a ,
187+ 0x05 , 0x0e , 0x06 , 0x0a , 0x07 , 0x10 , 0x08 , 0x0c , 0x09 , 0x0e , 0x0a , 0x08 , 0x0b , 0x08 , 0x0c , 0x06 , 0x0d , 0x06 , 0x0e , 0x06 , 0x0f , 0x10 , 0x10 , 0x05 , 0x11 , 0x0c , 0x12 , 0x06 ,
188+ 0x13 , 0x06 , 0x14 , 0x10 , 0x15 , 0x10 , 0x16 , 0x10 , 0x17 , 0x00 , 0x18 , 0x08 , 0x19 , 0x06 , 0x1a , 0x00 , 0x1b , 0x00 , 0x1c , 0x06 , 0x1d , 0x00 , 0x1e , 0x10 , 0x1f , 0x00 , 0x20 , 0x00 ,
189+ 0x21 , 0x00 , 0x22 , 0x00 , 0x23 , 0x00 , 0x24 , 0x00 , 0x25 , 0x00 , 0x26 , 0x00 , 0x27 , 0x00 , 0x28 , 0x0e , 0x29 , 0x0c , 0x2a , 0x0e
190+ ] ;
120191}
121192
0 commit comments