@@ -22,7 +22,7 @@ pub mod cmd;
22
22
pub mod measurement;
23
23
pub mod types;
24
24
25
- const SPS_MAX : usize = 90_000 ;
25
+ const SPS_MAX : usize = 100_000 ;
26
26
27
27
#[ derive( Error , Debug ) ]
28
28
/// PPK2 communication or data parsing error.
@@ -97,12 +97,31 @@ impl Ppk2 {
97
97
Ok ( response)
98
98
}
99
99
100
- /// Get the device metadata.
101
- pub fn get_metadata ( & mut self ) -> Result < Metadata > {
100
+ fn try_get_metadata ( & mut self ) -> Result < Metadata > {
102
101
let response = self . send_command ( Command :: GetMetaData ) ?;
103
102
Metadata :: from_bytes ( & response)
104
103
}
105
104
105
+ /// Get the device metadata.
106
+ pub fn get_metadata ( & mut self ) -> Result < Metadata > {
107
+ let mut result: Result < Metadata > = Err ( Error :: Parse ( "Metadata" . to_string ( ) ) ) ;
108
+
109
+ // Retry a few times, as the metadata command sometimes fails
110
+ for _ in 0 ..3 {
111
+ match self . try_get_metadata ( ) {
112
+ Ok ( metadata) => {
113
+ result = Ok ( metadata) ;
114
+ break ;
115
+ }
116
+ Err ( e) => {
117
+ tracing:: warn!( "Error fetching metadata: {:?}. Retrying.." , e) ;
118
+ }
119
+ }
120
+ }
121
+
122
+ result
123
+ }
124
+
106
125
/// Enable or disable the device power.
107
126
pub fn set_device_power ( & mut self , power : DevicePower ) -> Result < ( ) > {
108
127
self . send_command ( Command :: DeviceRunningSet ( power) ) ?;
@@ -161,7 +180,13 @@ impl Ppk2 {
161
180
. wait_while ( lock. lock ( ) . unwrap ( ) , |ready| !* ready)
162
181
. unwrap ( ) ;
163
182
164
- let mut buf = [ 0u8 ; 1024 ] ;
183
+ /* 4 bytes is the size of a single sample, and the PPK pushes 100,000 samples per second.
184
+ Having size of `buf` at eg.1024 blocks port.read() until the buffer is full with 1024 bytes (128 samples).
185
+ The measurement returned will be the average of the 128 samples. But we want to get every single sample when
186
+ requested sps is 100,000. Hence, we set the buffer size to 4 bytes, and read the port in a loop,
187
+ feeding the accumulator with the data.
188
+ */
189
+ let mut buf = [ 0u8 ; 4 ] ;
165
190
let mut measurement_buf = VecDeque :: with_capacity ( SPS_MAX ) ;
166
191
let mut missed = 0 ;
167
192
loop {
0 commit comments