@@ -10,7 +10,9 @@ duplicate! {
10
10
]
11
11
#[ cfg( feature=_FEATURE_) ]
12
12
pub mod _MOD_ {
13
- use crate :: { register:: Register , tri, Address , BMP180Error , Calibration , Mode , Id } ;
13
+ //! Device definition and implementation.
14
+
15
+ use crate :: { register:: Register , tri, address:: Address , error:: BMP180Error , calibration:: Calibration , mode:: Mode , id:: Id } ;
14
16
15
17
/// Builder for an uninitialized `BMP180` device.
16
18
///
@@ -171,16 +173,16 @@ duplicate! {
171
173
I2C : _I2C_T_,
172
174
DELAY : _DELAY_T_,
173
175
{
174
- /// Split the device into its parts.
175
- pub fn release( self ) -> ( I2C , DELAY ) {
176
- ( self . i2c, self . delay)
177
- }
178
-
179
176
/// Device I2C address.
180
177
pub fn addr( & self ) -> Address {
181
178
self . addr
182
179
}
183
180
181
+ /// Device I2C address as `u8`.
182
+ fn addr_u8( & self ) -> u8 {
183
+ self . addr. into( )
184
+ }
185
+
184
186
/// Device operating mode.
185
187
pub fn mode( & self ) -> Mode {
186
188
self . mode
@@ -312,6 +314,148 @@ duplicate! {
312
314
313
315
44330.0 * ( 1.0 - libm:: powf( pressure as f32 / sea_level_pressure, 0.1903 ) )
314
316
}
317
+
318
+ /// Read raw temperature.
319
+ async fn read_raw_temperature( & mut self ) -> Result <i16 , BMP180Error <I2C :: Error >> {
320
+ tri!(
321
+ self . i2c
322
+ . write(
323
+ self . addr_u8( ) ,
324
+ & [ Register :: Control as u8 , Register :: ReadTempCmd as u8 ]
325
+ )
326
+ _await
327
+ . map_err( BMP180Error :: I2C )
328
+ ) ;
329
+
330
+ self . delay. delay_ms( 5 ) _await;
331
+
332
+ let mut data = [ 0u8 ; 2 ] ;
333
+
334
+ tri!(
335
+ self . i2c
336
+ . write_read(
337
+ self . addr_u8( ) ,
338
+ & [ Register :: TempPressureData as u8 ] ,
339
+ & mut data
340
+ )
341
+ _await
342
+ . map_err( BMP180Error :: I2C )
343
+ ) ;
344
+
345
+ let raw_temperature = ( ( data[ 0 ] as i16 ) << 8 ) | data[ 1 ] as i16 ;
346
+
347
+ Ok ( raw_temperature)
348
+ }
349
+
350
+ /// Read raw pressure.
351
+ async fn read_raw_pressure( & mut self ) -> Result <i32 , BMP180Error <I2C :: Error >> {
352
+ let mode = self . mode( ) ;
353
+
354
+ tri!(
355
+ self . i2c
356
+ . write(
357
+ self . addr_u8( ) ,
358
+ & [
359
+ Register :: Control as u8 ,
360
+ Register :: ReadPressureCmd as u8 + ( ( mode as u8 ) << 6 )
361
+ ] ,
362
+ )
363
+ _await
364
+ . map_err( BMP180Error :: I2C )
365
+ ) ;
366
+
367
+ self . delay. delay_ms( mode. delay_ms( ) ) _await;
368
+
369
+ let mut data = [ 0u8 ; 3 ] ;
370
+
371
+ tri!(
372
+ self . i2c
373
+ . write_read(
374
+ self . addr_u8( ) ,
375
+ & [ Register :: TempPressureData as u8 ] ,
376
+ & mut data
377
+ )
378
+ _await
379
+ . map_err( BMP180Error :: I2C )
380
+ ) ;
381
+
382
+ let raw_pressure =
383
+ ( ( ( data[ 0 ] as i32 ) << 16 ) + ( ( data[ 1 ] as i32 ) << 8 ) + data[ 2 ] as i32 )
384
+ >> ( 8 - mode as u8 ) ;
385
+
386
+ Ok ( raw_pressure)
387
+ }
388
+
389
+ /// Update temperature in `self`.
390
+ pub async fn update_temperature( & mut self ) -> Result <( ) , BMP180Error <I2C :: Error >> {
391
+ let raw_temperature = tri!( self . read_raw_temperature( ) _await) ;
392
+
393
+ self . temperature = self . compute_temperature( raw_temperature) ;
394
+
395
+ Ok ( ( ) )
396
+ }
397
+
398
+ /// Update pressure in `self`.
399
+ pub async fn update_pressure( & mut self ) -> Result <( ) , BMP180Error <I2C :: Error >> {
400
+ let raw_temperature = tri!( self . read_raw_temperature( ) _await) ;
401
+ let raw_pressure = tri!( self . read_raw_pressure( ) _await) ;
402
+
403
+ self . pressure = self . compute_pressure( raw_temperature, raw_pressure) ;
404
+
405
+ Ok ( ( ) )
406
+ }
407
+
408
+ /// Update both temperature and pressure in `self`.
409
+ pub async fn update( & mut self ) -> Result <( ) , BMP180Error <I2C :: Error >> {
410
+ let raw_temperature = tri!( self . read_raw_temperature( ) _await) ;
411
+ let raw_pressure = tri!( self . read_raw_pressure( ) _await) ;
412
+
413
+ self . temperature = self . compute_temperature( raw_temperature) ;
414
+ self . pressure = self . compute_pressure( raw_temperature, raw_pressure) ;
415
+
416
+ Ok ( ( ) )
417
+ }
418
+ }
419
+
420
+ #[ cfg( feature = "i-know-what-i-am-doing" ) ]
421
+ impl <I2C , DELAY > BMP180 <I2C , DELAY > {
422
+ /// Split the BMP180 device into its parts.
423
+ ///
424
+ /// Only available when the `i-know-what-i-am-doing` feature is enabled.
425
+ pub fn into_parts( self ) -> ( Address , Mode , Calibration , i32 , i32 , I2C , DELAY ) {
426
+ (
427
+ self . addr,
428
+ self . mode,
429
+ self . calibration,
430
+ self . temperature,
431
+ self . pressure,
432
+ self . i2c,
433
+ self . delay,
434
+ )
435
+ }
436
+
437
+ /// Create a BMP180 device from its parts.
438
+ ///
439
+ /// Only available when the `i-know-what-i-am-doing` feature is enabled.
440
+ pub fn from_parts(
441
+ addr: Address ,
442
+ mode: Mode ,
443
+ calibration: Calibration ,
444
+ temperature: i32 ,
445
+ pressure: i32 ,
446
+ i2c: I2C ,
447
+ delay: DELAY ,
448
+ ) -> Self {
449
+ Self {
450
+ addr,
451
+ mode,
452
+ calibration,
453
+ temperature,
454
+ pressure,
455
+ i2c,
456
+ delay,
457
+ }
458
+ }
315
459
}
316
460
}
317
461
0 commit comments