1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689
//! BME280 driver with support for I2C and SPI buses.
//!
//! # Example
//!
//! ```
//! # let i2c = ehm::eh0::i2c::Mock::new(&[
//! # ehm::eh0::i2c::Transaction::write_read(0x76, vec![0x88], vec![0; 26]),
//! # ehm::eh0::i2c::Transaction::write_read(0x76, vec![0xE1], vec![0; 7]),
//! # ehm::eh0::i2c::Transaction::write(0x76, vec![0xF2, 0b100]),
//! # ehm::eh0::i2c::Transaction::write(0x76, vec![0xF4, 0b10010011]),
//! # ehm::eh0::i2c::Transaction::write(0x76, vec![0xF5, 0b10110000]),
//! # ehm::eh0::i2c::Transaction::write_read(0x76, vec![0xF7], vec![0; 8]),
//! # ]);
//! use bme280_multibus::{Address, Bme280, Sample, Standby};
//!
//! const SETTINGS: bme280_multibus::Settings = bme280_multibus::Settings {
//! config: bme280_multibus::Config::RESET
//! .set_standby_time(bme280_multibus::Standby::Millis1000)
//! .set_filter(bme280_multibus::Filter::X16),
//! ctrl_meas: bme280_multibus::CtrlMeas::RESET
//! .set_osrs_t(bme280_multibus::Oversampling::X8)
//! .set_osrs_p(bme280_multibus::Oversampling::X8)
//! .set_mode(bme280_multibus::Mode::Normal),
//! ctrl_hum: bme280_multibus::Oversampling::X8,
//! };
//!
//! let mut bme: Bme280<_> = Bme280::from_i2c0(i2c, Address::SdoGnd)?;
//! bme.settings(&SETTINGS)?;
//! let sample: Sample = bme.sample().unwrap();
//! # bme.free().free().done();
//! # Ok::<(), ehm::eh0::MockError>(())
//! ```
//!
//! # Features
//!
//! * `async`: Enable asynchronous implementations with `embedded-hal-async`.
//! Requires a nightly toolchain.
//! * `serde`: Implement `Serialize` and `Deserialize` for `Sample`.
#![no_std]
#![cfg_attr(feature = "async", allow(async_fn_in_trait))]
#![cfg_attr(docsrs, feature(doc_cfg), feature(doc_auto_cfg))]
#![forbid(unsafe_code)]
#![warn(missing_docs)]
use core::time::Duration;
pub use eh0;
pub use eh1;
#[cfg(feature = "async")]
pub use eha0a;
/// BME280 I2C bus implementation with embedded-hal version 0.2
pub mod i2c0;
/// BME280 I2C bus implementation with embedded-hal version 1
pub mod i2c1;
/// BME280 SPI bus implementation with embedded-hal version 0.2
pub mod spi0;
/// BME280 SPI bus implementation with embedded-hal version 1
pub mod spi1;
/// BME280 chip ID.
pub const CHIP_ID: u8 = 0x60;
const NUM_CALIB_REG: usize = 33;
const NUM_MEAS_REG: usize = 8;
/// Maximum SPI bus frequency in hertz.
pub const SPI_MAX_FREQ: u32 = 10_000_000;
/// BME280 calibration data.
#[derive(Debug)]
#[allow(missing_docs)]
pub struct Calibration {
t1: u16, // 0x88..0x89 buf[00:01]
t2: i16, // 0x8A..0x8B buf[02:03]
t3: i16, // 0x8C..0x8D buf[04:05]
p1: u16, // 0x8E..0x8F buf[06:07]
p2: i16, // 0x90..0x91 buf[08:09]
p3: i16, // 0x92..0x93 buf[10:11]
p4: i16, // 0x94..0x95 buf[12:13]
p5: i16, // 0x96..0x97 buf[14:15]
p6: i16, // 0x98..0x99 buf[16:17]
p7: i16, // 0x9A..0x9B buf[18:19]
p8: i16, // 0x9C..0x9D buf[20:21]
p9: i16, // 0x9E..0x9F buf[22:23]
// INTENTIONAL ONE BYTE GAP (see datasheet)
h1: u8, // 0xA1 buf[25]
h2: i16, // 0xE1..0xE2 buf[26:27]
h3: u8, // 0xE3 buf[28]
h4: i16, // 0xE4..0xE5[3:0] = H4 [11:4]..[3:0]
h5: i16, // 0xE5[7:4]..0xE6 = H5 [3:0]..[11:4]
h6: i8, // 0xE7 buf[32]
}
impl From<[u8; NUM_CALIB_REG]> for Calibration {
fn from(buf: [u8; NUM_CALIB_REG]) -> Self {
Calibration {
t1: u16::from_le_bytes([buf[0], buf[1]]),
t2: i16::from_le_bytes([buf[2], buf[3]]),
t3: i16::from_le_bytes([buf[4], buf[5]]),
p1: u16::from_le_bytes([buf[6], buf[7]]),
p2: i16::from_le_bytes([buf[8], buf[9]]),
p3: i16::from_le_bytes([buf[10], buf[11]]),
p4: i16::from_le_bytes([buf[12], buf[13]]),
p5: i16::from_le_bytes([buf[14], buf[15]]),
p6: i16::from_le_bytes([buf[16], buf[17]]),
p7: i16::from_le_bytes([buf[18], buf[19]]),
p8: i16::from_le_bytes([buf[20], buf[21]]),
p9: i16::from_le_bytes([buf[22], buf[23]]),
// INTENTIONAL ONE BYTE GAP (see datasheet)
h1: buf[25],
h2: i16::from_le_bytes([buf[26], buf[27]]),
h3: buf[28],
h4: ((buf[29] as i8 as i16) << 4 | (buf[30] as i8 as i16) & 0xF),
h5: (((buf[30] as i8 as i16) & 0xF0) >> 4) | ((buf[31] as i8 as i16) << 4),
h6: buf[32] as i8,
}
}
}
const RESET_MAGIC: u8 = 0xB6;
/// Register addresses.
///
/// from Table 18: Memory Map
#[allow(dead_code)]
mod reg {
pub const HUM_LSB: u8 = 0xFE;
pub const HUM_MSB: u8 = 0xFB;
pub const TEMP_XLSB: u8 = 0xFC;
pub const TEMP_LSB: u8 = 0xFB;
pub const TEMP_MSB: u8 = 0xFA;
pub const PRESS_XLSB: u8 = 0xF9;
pub const PRESS_LSB: u8 = 0xF8;
pub const PRESS_MSB: u8 = 0xF7;
pub const CONFIG: u8 = 0xF5;
pub const CTRL_MEAS: u8 = 0xF4;
pub const STATUS: u8 = 0xF3;
pub const CTRL_HUM: u8 = 0xF2;
pub const CALIB_41: u8 = 0xF0;
pub const CALIB_40: u8 = 0xEF;
pub const CALIB_39: u8 = 0xEE;
pub const CALIB_38: u8 = 0xED;
pub const CALIB_37: u8 = 0xEC;
pub const CALIB_36: u8 = 0xEB;
pub const CALIB_35: u8 = 0xEA;
pub const CALIB_34: u8 = 0xE9;
pub const CALIB_33: u8 = 0xE8;
pub const CALIB_32: u8 = 0xE7;
pub const CALIB_31: u8 = 0xE6;
pub const CALIB_30: u8 = 0xE5;
pub const CALIB_29: u8 = 0xE4;
pub const CALIB_28: u8 = 0xE3;
pub const CALIB_27: u8 = 0xE2;
pub const CALIB_26: u8 = 0xE1;
pub const RESET: u8 = 0xE0;
pub const ID: u8 = 0xD0;
pub const CALIB_25: u8 = 0xA1;
pub const CALIB_24: u8 = 0xA0;
pub const CALIB_23: u8 = 0x9F;
pub const CALIB_22: u8 = 0x9E;
pub const CALIB_21: u8 = 0x9D;
pub const CALIB_20: u8 = 0x9C;
pub const CALIB_19: u8 = 0x9B;
pub const CALIB_18: u8 = 0x9A;
pub const CALIB_17: u8 = 0x99;
pub const CALIB_16: u8 = 0x98;
pub const CALIB_15: u8 = 0x97;
pub const CALIB_14: u8 = 0x96;
pub const CALIB_13: u8 = 0x95;
pub const CALIB_12: u8 = 0x94;
pub const CALIB_11: u8 = 0x93;
pub const CALIB_10: u8 = 0x92;
pub const CALIB_09: u8 = 0x91;
pub const CALIB_08: u8 = 0x90;
pub const CALIB_07: u8 = 0x8F;
pub const CALIB_06: u8 = 0x8E;
pub const CALIB_05: u8 = 0x8D;
pub const CALIB_04: u8 = 0x8C;
pub const CALIB_03: u8 = 0x8B;
pub const CALIB_02: u8 = 0x8A;
pub const CALIB_01: u8 = 0x89;
pub const CALIB_00: u8 = 0x88;
}
/// Oversampling settings for temperature, pressure, and humidity data.
#[derive(Debug, PartialEq, Eq, Clone, Copy, PartialOrd, Ord, Hash)]
#[repr(u8)]
pub enum Oversampling {
/// Skipped, output set to `0x80000`.
Skip = 0b000,
/// Oversampling × 1
X1 = 0b001,
/// Oversampling × 2
X2 = 0b010,
/// Oversampling × 4
X4 = 0b011,
/// Oversampling × 8
X8 = 0b100,
/// Oversampling × 16
X16 = 0b101,
}
impl From<Oversampling> for u8 {
fn from(x: Oversampling) -> Self {
x as u8
}
}
impl Oversampling {
/// Reset value of the osrs fields.
///
/// # Example
///
/// ```
/// use bme280_multibus::Oversampling;
///
/// assert_eq!(Oversampling::RESET, Oversampling::Skip);
/// ```
pub const RESET: Self = Self::Skip;
}
impl Default for Oversampling {
fn default() -> Self {
Oversampling::RESET
}
}
/// Sensor mode.
#[derive(Debug, PartialEq, Eq, Clone, Copy, Hash)]
#[repr(u8)]
pub enum Mode {
/// Sleep mode.
Sleep = 0b00,
/// Forced mode.
Forced = 0b01,
/// Normal mode.
Normal = 0b11,
}
impl From<Mode> for u8 {
fn from(x: Mode) -> Self {
x as u8
}
}
impl Mode {
/// Reset value of the mode field in the [`CtrlMeas`] register.
///
/// # Example
///
/// ```
/// use bme280_multibus::Mode;
///
/// assert_eq!(Mode::RESET, Mode::Sleep);
/// ```
pub const RESET: Self = Mode::Sleep;
}
impl Default for Mode {
fn default() -> Self {
Mode::RESET
}
}
/// t<sub>standby</sub> settings.
#[derive(Debug, PartialEq, Eq, Clone, Copy, Hash)]
#[repr(u8)]
pub enum Standby {
/// 0.5 ms
Micros500 = 0b000,
/// 62.5 ms
Micros62500 = 0b001,
/// 125 ms
Millis125 = 0b010,
/// 250 ms
Millis250 = 0b011,
/// 500 ms
Millis500 = 0b100,
/// 1000 ms
Millis1000 = 0b101,
/// 10 ms
Millis10 = 0b110,
/// 20 ms
Millis20 = 0b111,
}
impl Standby {
/// Reset value of the standby field in the [`Config`] register.
///
/// # Example
///
/// ```
/// use bme280_multibus::Standby;
///
/// assert_eq!(Standby::RESET, Standby::Micros500);
/// ```
pub const RESET: Self = Standby::Micros500;
/// Convert the standby enumeration to a duration.
///
/// # Example
///
/// ```
/// use bme280_multibus::Standby;
/// use core::time::Duration;
///
/// assert_eq!(Standby::Micros500.duration(), Duration::from_micros(500));
/// assert_eq!(
/// Standby::Micros62500.duration(),
/// Duration::from_micros(62500)
/// );
/// assert_eq!(Standby::Millis125.duration(), Duration::from_millis(125));
/// assert_eq!(Standby::Millis250.duration(), Duration::from_millis(250));
/// assert_eq!(Standby::Millis500.duration(), Duration::from_millis(500));
/// assert_eq!(Standby::Millis1000.duration(), Duration::from_millis(1000));
/// assert_eq!(Standby::Millis10.duration(), Duration::from_millis(10));
/// assert_eq!(Standby::Millis20.duration(), Duration::from_millis(20));
/// ```
pub const fn duration(&self) -> Duration {
match self {
Standby::Micros500 => Duration::from_micros(500),
Standby::Micros62500 => Duration::from_micros(62500),
Standby::Millis125 => Duration::from_millis(125),
Standby::Millis250 => Duration::from_millis(250),
Standby::Millis500 => Duration::from_millis(500),
Standby::Millis1000 => Duration::from_millis(1000),
Standby::Millis10 => Duration::from_millis(10),
Standby::Millis20 => Duration::from_millis(20),
}
}
}
impl From<&Standby> for Duration {
fn from(s: &Standby) -> Self {
s.duration()
}
}
impl From<Standby> for Duration {
fn from(s: Standby) -> Self {
s.duration()
}
}
impl PartialOrd for Standby {
fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> {
Some(self.duration().cmp(&other.duration()))
}
}
impl Ord for Standby {
fn cmp(&self, other: &Self) -> core::cmp::Ordering {
self.duration().cmp(&other.duration())
}
}
impl Default for Standby {
fn default() -> Self {
Standby::RESET
}
}
/// Filter settings.
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Hash)]
#[repr(u8)]
pub enum Filter {
/// Filter off.
Off = 0b000,
/// Filter coefficient of 2.
X2 = 0b001,
/// Filter coefficient of 4.
X4 = 0b010,
/// Filter coefficient of 8.
X8 = 0b011,
/// Filter coefficient of 16.
X16 = 0b100,
}
impl Filter {
/// Reset value of the filter field in the [`Config`] register.
///
/// # Example
///
/// ```
/// use bme280_multibus::Filter;
///
/// assert_eq!(Filter::RESET, Filter::Off);
/// ```
pub const RESET: Self = Filter::Off;
}
impl Default for Filter {
fn default() -> Self {
Filter::RESET
}
}
impl From<Filter> for u8 {
fn from(x: Filter) -> Self {
x as u8
}
}
/// Config register.
///
/// This register sets the rate, filter, and interface options of the device.
/// Writes to the config register in normal mode may be ignored.
/// In sleep mode writes are not ignored.
///
/// All methods on this struct are constant so that you can create a
/// configuration value at compile time.
///
/// # Example
///
/// ```
/// use bme280_multibus::{Config, Filter, Standby};
///
/// const CONFIG: Config = Config::RESET
/// .set_standby_time(Standby::Millis1000)
/// .set_filter(Filter::X16)
/// .set_spi3w_en(false);
/// ```
#[derive(Debug, PartialEq, Eq, Clone, Copy, Hash)]
pub struct Config(u8);
impl Config {
/// Get the reset value of the config register.
///
/// # Example
///
/// ```
/// use bme280_multibus::Config;
///
/// assert_eq!(Config::RESET, Config::default());
/// ```
pub const RESET: Self = Config(0x00);
/// Set the inactive duration t<sub>standby</sub> in normal mode.
///
/// See [`Standby`] for settings, and chapter 3.3.4 in the [datasheet] for
/// details.
///
/// # Example
///
/// ```
/// use bme280_multibus::{Config, Standby};
///
/// let mut cfg: Config = Config::default();
/// assert_eq!(cfg.standby_time(), Standby::default());
/// cfg = cfg.set_standby_time(Standby::Micros500);
/// assert_eq!(cfg.standby_time(), Standby::Micros500);
/// cfg = cfg.set_standby_time(Standby::Micros62500);
/// assert_eq!(cfg.standby_time(), Standby::Micros62500);
/// cfg = cfg.set_standby_time(Standby::Millis125);
/// assert_eq!(cfg.standby_time(), Standby::Millis125);
/// cfg = cfg.set_standby_time(Standby::Millis250);
/// assert_eq!(cfg.standby_time(), Standby::Millis250);
/// cfg = cfg.set_standby_time(Standby::Millis500);
/// assert_eq!(cfg.standby_time(), Standby::Millis500);
/// cfg = cfg.set_standby_time(Standby::Millis1000);
/// assert_eq!(cfg.standby_time(), Standby::Millis1000);
/// cfg = cfg.set_standby_time(Standby::Millis10);
/// assert_eq!(cfg.standby_time(), Standby::Millis10);
/// cfg = cfg.set_standby_time(Standby::Millis20);
/// assert_eq!(cfg.standby_time(), Standby::Millis20);
/// ```
///
/// [datasheet]: https://www.bosch-sensortec.com/media/boschsensortec/downloads/datasheets/bst-bme280-ds002.pdf
#[must_use = "set_standby_time returns a modified Config"]
pub const fn set_standby_time(self, s: Standby) -> Config {
Config((self.0 & 0x1F) | ((s as u8) << 5))
}
/// Get the standby time.
pub const fn standby_time(&self) -> Standby {
match self.0 >> 5 {
0b000 => Standby::Micros500,
0b001 => Standby::Micros62500,
0b010 => Standby::Millis125,
0b011 => Standby::Millis250,
0b100 => Standby::Millis500,
0b101 => Standby::Millis1000,
0b110 => Standby::Millis10,
_ => Standby::Millis20,
}
}
/// Set the time constant of the IIR filter.
///
/// See [`Filter`] for settings, and chapter 3.4.4 in the [datasheet] for
/// details.
///
/// # Example
///
/// ```
/// use bme280_multibus::{Config, Filter};
///
/// let mut cfg: Config = Config::default();
/// assert_eq!(cfg.filter(), Filter::default());
/// cfg = cfg.set_filter(Filter::Off);
/// assert_eq!(cfg.filter(), Filter::Off);
/// cfg = cfg.set_filter(Filter::X2);
/// assert_eq!(cfg.filter(), Filter::X2);
/// cfg = cfg.set_filter(Filter::X4);
/// assert_eq!(cfg.filter(), Filter::X4);
/// cfg = cfg.set_filter(Filter::X8);
/// assert_eq!(cfg.filter(), Filter::X8);
/// cfg = cfg.set_filter(Filter::X16);
/// assert_eq!(cfg.filter(), Filter::X16);
/// ```
///
/// [datasheet]: https://www.bosch-sensortec.com/media/boschsensortec/downloads/datasheets/bst-bme280-ds002.pdf
#[must_use = "set_filter returns a modified Config"]
pub const fn set_filter(self, f: Filter) -> Config {
Config((self.0 & 0b11100011) | ((f as u8) << 2))
}
/// Get the filter coefficient.
pub const fn filter(&self) -> Filter {
match (self.0 >> 2) & 0b111 {
0b000 => Filter::Off,
0b001 => Filter::X2,
0b010 => Filter::X4,
0b011 => Filter::X8,
_ => Filter::X16,
}
}
/// Enables the 3-wire SPI itnerface when enabled.
///
/// See chapter 6.3 in the [datasheet] for details.
///
/// # Example
///
/// ```
/// use bme280_multibus::Config;
///
/// let mut cfg: Config = Config::default();
/// assert_eq!(cfg.spi3w_en(), false);
/// cfg = cfg.set_spi3w_en(true);
/// assert_eq!(cfg.spi3w_en(), true);
/// cfg = cfg.set_spi3w_en(false);
/// assert_eq!(cfg.spi3w_en(), false);
/// ```
///
/// [datasheet]: https://www.bosch-sensortec.com/media/boschsensortec/downloads/datasheets/bst-bme280-ds002.pdf
#[must_use = "set_spi3w_en returns a modified Config"]
pub const fn set_spi3w_en(self, en: bool) -> Config {
if en {
Config(self.0 | 0b1)
} else {
Config(self.0 & !0b1)
}
}
/// Returns `true` if 3-wire SPI is enabled.
pub const fn spi3w_en(&self) -> bool {
self.0 & 0b1 == 0b1
}
}
impl Default for Config {
fn default() -> Self {
Config::RESET
}
}
/// Measurement control register.
///
/// This configures the pressure and temperature data acquisition options of the
/// device.
#[derive(Debug, PartialEq, Eq, Clone, Copy, Hash)]
pub struct CtrlMeas(u8);
impl CtrlMeas {
/// Get the reset value of the ctrl_meas register.
///
/// # Example
///
/// ```
/// use bme280_multibus::CtrlMeas;
///
/// assert_eq!(CtrlMeas::RESET, CtrlMeas::default());
/// ```
pub const RESET: Self = CtrlMeas(0x00);
/// Set the oversampling for temperature data.
///
/// See [`Oversampling`] for settings, and chapter 3.4.3 in the [datasheet]
/// for details.
///
/// # Example
///
/// ```
/// use bme280_multibus::{CtrlMeas, Oversampling};
///
/// let mut ctrl_meas: CtrlMeas = CtrlMeas::default();
/// assert_eq!(ctrl_meas.osrs_t(), Oversampling::default());
/// ctrl_meas = ctrl_meas.set_osrs_t(Oversampling::Skip);
/// assert_eq!(ctrl_meas.osrs_t(), Oversampling::Skip);
/// ctrl_meas = ctrl_meas.set_osrs_t(Oversampling::X1);
/// assert_eq!(ctrl_meas.osrs_t(), Oversampling::X1);
/// ctrl_meas = ctrl_meas.set_osrs_t(Oversampling::X2);
/// assert_eq!(ctrl_meas.osrs_t(), Oversampling::X2);
/// ctrl_meas = ctrl_meas.set_osrs_t(Oversampling::X4);
/// assert_eq!(ctrl_meas.osrs_t(), Oversampling::X4);
/// ctrl_meas = ctrl_meas.set_osrs_t(Oversampling::X8);
/// assert_eq!(ctrl_meas.osrs_t(), Oversampling::X8);
/// ctrl_meas = ctrl_meas.set_osrs_t(Oversampling::X16);
/// assert_eq!(ctrl_meas.osrs_t(), Oversampling::X16);
/// ```
///
/// [datasheet]: https://www.bosch-sensortec.com/media/boschsensortec/downloads/datasheets/bst-bme280-ds002.pdf
#[must_use = "set_osrs_t returns a modified CtrlMeas"]
pub const fn set_osrs_t(self, os: Oversampling) -> CtrlMeas {
CtrlMeas((self.0 & 0b00011111) | ((os as u8) << 5))
}
/// Get the temperature data oversampling.
pub const fn osrs_t(&self) -> Oversampling {
match (self.0 >> 5) & 0b111 {
0b000 => Oversampling::Skip,
0b001 => Oversampling::X1,
0b010 => Oversampling::X2,
0b011 => Oversampling::X4,
0b100 => Oversampling::X8,
_ => Oversampling::X16,
}
}
/// Set the oversampling for pressure data.
///
/// See [`Oversampling`] for settings, and chapter 3.4.2 in the [datasheet]
/// for details.
///
/// # Example
///
/// ```
/// use bme280_multibus::{CtrlMeas, Oversampling};
///
/// let mut ctrl_meas: CtrlMeas = CtrlMeas::default();
/// assert_eq!(ctrl_meas.osrs_p(), Oversampling::default());
/// ctrl_meas = ctrl_meas.set_osrs_p(Oversampling::Skip);
/// assert_eq!(ctrl_meas.osrs_p(), Oversampling::Skip);
/// ctrl_meas = ctrl_meas.set_osrs_p(Oversampling::X1);
/// assert_eq!(ctrl_meas.osrs_p(), Oversampling::X1);
/// ctrl_meas = ctrl_meas.set_osrs_p(Oversampling::X2);
/// assert_eq!(ctrl_meas.osrs_p(), Oversampling::X2);
/// ctrl_meas = ctrl_meas.set_osrs_p(Oversampling::X4);
/// assert_eq!(ctrl_meas.osrs_p(), Oversampling::X4);
/// ctrl_meas = ctrl_meas.set_osrs_p(Oversampling::X8);
/// assert_eq!(ctrl_meas.osrs_p(), Oversampling::X8);
/// ctrl_meas = ctrl_meas.set_osrs_p(Oversampling::X16);
/// assert_eq!(ctrl_meas.osrs_p(), Oversampling::X16);
/// ```
///
/// [datasheet]: https://www.bosch-sensortec.com/media/boschsensortec/downloads/datasheets/bst-bme280-ds002.pdf
#[must_use = "set_osrs_p returns a modified CtrlMeas"]
pub const fn set_osrs_p(self, os: Oversampling) -> CtrlMeas {
CtrlMeas((self.0 & 0b11100011) | ((os as u8) << 2))
}
/// Get the pressure data oversampling.
pub const fn osrs_p(&self) -> Oversampling {
match (self.0 >> 2) & 0b111 {
0b000 => Oversampling::Skip,
0b001 => Oversampling::X1,
0b010 => Oversampling::X2,
0b011 => Oversampling::X4,
0b100 => Oversampling::X8,
_ => Oversampling::X16,
}
}
/// Set the sensor mode for the device.
///
/// See [`Mode`] for setting, and chapter 3.3 in the [datasheet] for details.
///
/// # Example
///
/// ```
/// use bme280_multibus::{CtrlMeas, Mode};
///
/// let mut ctrl_meas: CtrlMeas = CtrlMeas::default();
/// assert_eq!(ctrl_meas.mode(), Mode::default());
/// ctrl_meas = ctrl_meas.set_mode(Mode::Sleep);
/// assert_eq!(ctrl_meas.mode(), Mode::Sleep);
/// ctrl_meas = ctrl_meas.set_mode(Mode::Forced);
/// assert_eq!(ctrl_meas.mode(), Mode::Forced);
/// ctrl_meas = ctrl_meas.set_mode(Mode::Normal);
/// assert_eq!(ctrl_meas.mode(), Mode::Normal);
/// ```
///
/// [datasheet]: https://www.bosch-sensortec.com/media/boschsensortec/downloads/datasheets/bst-bme280-ds002.pdf
#[must_use = "set_osrs_p returns a modified CtrlMeas"]
pub const fn set_mode(self, m: Mode) -> CtrlMeas {
CtrlMeas((self.0 & 0xFC) | (m as u8))
}
/// Get the mode.
pub const fn mode(&self) -> Mode {
match self.0 & 0b11 {
0b00 => Mode::Sleep,
0b11 => Mode::Normal,
_ => Mode::Forced,
}
}
}
impl Default for CtrlMeas {
fn default() -> Self {
CtrlMeas::RESET
}
}
/// Status register
#[derive(Debug, PartialEq, Eq, Clone, Copy, Hash)]
pub struct Status(u8);
impl Status {
/// Get the reset value of the ctrl_meas register.
///
/// # Example
///
/// ```
/// use bme280_multibus::Status;
///
/// assert_eq!(Status::RESET, Status::default());
/// ```
pub const RESET: Self = Status(0x00);
/// Measuring field.
///
/// Automatically set to `true` whenever a conversion is running and back to
/// `false` when the results have been transferred to the data registers.
///
/// # Example
///
/// ```
/// assert!(!bme280_multibus::Status::RESET.measuring());
/// ```
pub const fn measuring(&self) -> bool {
self.0 & (1 << 3) != 0
}
/// im_update field.
///
/// Automatically set to `true` when the NVM data are being copied to image
/// registers and back to `false` when the copying is done.
/// The data is copied at power-on-reset and before every conversion.
///
/// # Example
///
/// ```
/// assert!(!bme280_multibus::Status::RESET.im_update());
/// ```
pub const fn im_update(&self) -> bool {
self.0 & 1 != 0
}
}
impl core::fmt::Display for Status {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.debug_struct("Status")
.field("measuring", &self.measuring())
.field("im_update", &self.im_update())
.finish()
}
}
impl Default for Status {
fn default() -> Self {
Status::RESET
}
}
/// BME280 initialization settings.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct Settings {
/// `config` register value.
pub config: Config,
/// `ctrl_meas` register value.
pub ctrl_meas: CtrlMeas,
/// `ctrl_hum` register value.
pub ctrl_hum: Oversampling,
}
impl Settings {
/// Create a new settings structure.
///
/// # Example
///
/// ```
/// use bme280_multibus::{Config, CtrlMeas, Filter, Mode, Oversampling, Settings, Standby};
///
/// const SETTINGS: Settings = Settings {
/// config: Config::RESET
/// .set_standby_time(Standby::Millis1000)
/// .set_filter(Filter::X16),
/// ctrl_meas: CtrlMeas::RESET
/// .set_osrs_t(Oversampling::X16)
/// .set_osrs_p(Oversampling::X16)
/// .set_mode(Mode::Normal),
/// ctrl_hum: Oversampling::X16,
/// };
/// ```
pub const fn new() -> Settings {
Settings {
config: Config::RESET,
ctrl_meas: CtrlMeas::RESET,
ctrl_hum: Oversampling::RESET,
}
}
}
impl Default for Settings {
fn default() -> Self {
Settings::new()
}
}
/// A sensor sample from the BME280.
#[derive(Debug, Clone, Copy)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Sample {
/// Temperature reading in celsius.
pub temperature: f32,
/// Pressure reading in pascal.
pub pressure: f32,
/// Humidity in perfect relative.
pub humidity: f32,
}
impl Sample {
fn from_raw<B>(buf: &[u8; NUM_MEAS_REG], cal: &Calibration) -> Result<Self, Error<B>> {
// The magical math and magical numbers come from the datasheet.
// I am not to blame for this.
// msb [7:0] = p[19:12]
// lsb [7:0] = p[11:4]
// xlsb[7:4] = p[3:0]
let p: u32 = ((buf[0] as u32) << 12) | ((buf[1] as u32) << 4) | ((buf[2] as u32) >> 4);
// msb [7:0] = t[19:12]
// lsb [7:0] = t[11:4]
// xlsb[7:4] = t[3:0]
let t: u32 = ((buf[3] as u32) << 12) | ((buf[4] as u32) << 4) | ((buf[5] as u32) >> 4);
// msb [7:0] = h[15:8]
// lsb [7:0] = h[7:0]
let h: u32 = ((buf[6] as u32) << 8) | (buf[7] as u32);
if t == 0x80000000 || p == 0x80000000 || h == 0x8000 {
return Err(Error::Sample);
}
let p: i32 = p as i32;
let t: i32 = t as i32;
let h: i32 = h as i32;
let var1: i32 = (((t >> 3) - ((cal.t1 as i32) << 1)) * (cal.t2 as i32)) >> 11;
let var2: i32 = (((((t >> 4) - (cal.t1 as i32)) * ((t >> 4) - (cal.t1 as i32))) >> 12)
* (cal.t3 as i32))
>> 14;
let t_fine: i32 = var1 + var2;
let temperatue: i32 = (t_fine * 5 + 128) >> 8;
let temperature: f32 = (temperatue as f32) / 100.0;
let var1: i64 = (t_fine as i64) - 128000;
let var2: i64 = var1 * var1 * (cal.p6 as i64);
let var2: i64 = var2 + ((var1 * (cal.p5 as i64)) << 17);
let var2: i64 = var2 + ((cal.p4 as i64) << 35);
let var1: i64 = ((var1 * var1 * (cal.p3 as i64)) >> 8) + ((var1 * (cal.p2 as i64)) << 12);
let var1: i64 = ((((1i64) << 47) + var1) * (cal.p1 as i64)) >> 33;
let pressure: f32 = if var1 == 0 {
0.0
} else {
let var3: i64 = 1048576 - (p as i64);
let var3: i64 = (((var3 << 31) - var2) * 3125) / var1;
let var1: i64 = ((cal.p9 as i64) * (var3 >> 13) * (var3 >> 13)) >> 25;
let var2: i64 = ((cal.p8 as i64) * var3) >> 19;
let var3: i64 = ((var3 + var1 + var2) >> 8) + ((cal.p7 as i64) << 4);
(var3 as f32) / 256.0
};
let var1: i32 = t_fine - 76800i32;
let var1: i32 =
((((h << 14) - ((cal.h4 as i32) << 20) - ((cal.h5 as i32) * var1)) + 16384i32) >> 15)
* (((((((var1 * (cal.h6 as i32)) >> 10)
* (((var1 * (cal.h3 as i32)) >> 11) + (32768i32)))
>> 10)
+ (2097152i32))
* (cal.h2 as i32)
+ 8192)
>> 14);
let var1: i32 = var1 - (((((var1 >> 15) * (var1 >> 15)) >> 7) * (cal.h1 as i32)) >> 4);
let var1: i32 = if var1 < 0 { 0 } else { var1 };
let var1: i32 = if var1 > 419430400 { 419430400 } else { var1 };
let humidity: f32 = ((var1 >> 12) as f32) / 1024.0;
Ok(Sample {
temperature,
pressure,
humidity,
})
}
}
/// I2C device address.
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Copy)]
#[repr(u8)]
pub enum Address {
/// SDO pin is connected to GND.
SdoGnd = 0x76,
/// SDO pin is connected to V<sub>DDIO</sub>
SdoVddio = 0x77,
}
/// Sampling error.
#[derive(Debug, PartialEq, Eq)]
pub enum Error<B> {
/// Bus error.
Bus(B),
/// Sample error.
///
/// This is returned when the sample registers hold the reset value.
/// This may indicate:
/// * The power-on-time delay was not observed.
/// * [`Oversampling::Skip`] was used for humidity, temperature, or pressure.
/// * The BME280 was not configured.
Sample,
}
/// BME280 bus.
pub trait Bme280Bus {
/// BME280 bus error.
type Error;
/// Read from the BME280.
///
/// # I2C
///
/// ```text
/// Read example (BME280 Datasheet Figure 10: I2C multiple byte read)
/// +-------+---------------+----+------+------------------+------+
/// | Start | Slave Address | RW | ACKS | Register Address | ACKS |
/// +-------+---------------+----+------+------------------+------+
/// | S | 111011x | 0 | | xxxxxxxx | |
/// +-------+---------------+----+------+------------------+------+
///
/// +-------+---------------+----+------+---------------+------+---------------+--------+------+
/// ... | Start | Slave Address | RW | ACKS | Register Data | ACKM | Register Data | NOACKM | Stop |
/// +-------+---------------+----+------+---------------+------+---------------+--------+------+
/// ... | S | 111011x | 1 | | xxxxxxxx | | xxxxxxxx | | P |
/// +-------+---------------+----+------+---------------+------+---------------+--------+------+
/// ```
///
/// # SPI
///
/// ```text
/// Read example (BME280 Datasheet Figure 13: SPI multiple byte read)
/// +-------+----+------------------+---------------+
/// | Start | RW | Register Address | Register Data |
/// +-------+----+------------------+---------------+
/// | CSB=0 | 1 | xxxxxxx | xxxxxxxx |
/// +-------+----+------------------+---------------+
///
/// +---------------+-------+
/// ... | Register Data | Stop |
/// +---------------+-------+
/// ... | xxxxxxxx | CSB=0 |
/// +---------------+-------+
/// ```
fn read_regs(&mut self, reg: u8, buf: &mut [u8]) -> Result<(), Self::Error>;
/// Write a single register to the BME280.
///
/// # I2C
///
/// ```text
/// Write example (BME280 Datasheet Figure 9: I2C multiple byte write)
/// +-------+---------------+----+------+------------------+------+---------------+------+
/// | Start | Slave Address | RW | ACKS | Register Address | ACKS | Register Data | ACKS |
/// +-------+---------------+----+------+------------------+------+---------------+------+
/// | S | 111011x | 0 | | xxxxxxxx | | xxxxxxxx | |
/// +-------+---------------+----+------+------------------+------+---------------+------+
///
/// +------------------+------+---------------+------+------+
/// ... | Register Address | ACKS | Register Data | ACKS | Stop |
/// +------------------+------+---------------+------+------+
/// ... | xxxxxxxx | | xxxxxxxx | | P |
/// +------------------+------+---------------+------+------+
/// ```
///
/// # SPI
///
/// ```text
/// Write example (BME280 Datasheet Figure 12: SPI multiple byte write)
/// +-------+----+------------------+---------------+
/// | Start | RW | Register Address | Register Data |
/// +-------+----+------------------+---------------+
/// | CSB=0 | 0 | xxxxxxx | xxxxxxxx |
/// +-------+----+------------------+---------------+
///
/// +----+------------------+---------------+-------+
/// ... | RW | Register Address | Register Data | Stop |
/// +----+------------------+---------------+-------+
/// ... | 0 | xxxxxxx | xxxxxxxx | CSB=0 |
/// +----+------------------+---------------+-------+
/// ```
fn write_reg(&mut self, reg: u8, data: u8) -> Result<(), Self::Error>;
/// Read the calibration from the chip.
fn calibration(&mut self) -> Result<Calibration, Self::Error> {
const FIRST: usize = (reg::CALIB_25 - reg::CALIB_00 + 1) as usize;
debug_assert_eq!(FIRST, 26);
const SECOND: usize = (reg::CALIB_32 - reg::CALIB_26 + 1) as usize;
debug_assert_eq!((FIRST + SECOND), NUM_CALIB_REG);
let mut buf: [u8; NUM_CALIB_REG] = [0; NUM_CALIB_REG];
self.read_regs(reg::CALIB_00, &mut buf[0..FIRST])?;
self.read_regs(reg::CALIB_26, &mut buf[FIRST..(FIRST + SECOND)])?;
Ok(buf.into())
}
}
/// Asynchronous BME280 bus.
#[cfg(feature = "async")]
pub trait Bme280BusAsync {
/// BME280 bus error.
type Error;
/// Read from the BME280.
///
/// See [`Bme280Bus::read_regs`] for more information.
async fn read_regs(&mut self, reg: u8, buf: &mut [u8]) -> Result<(), Self::Error>;
/// Write a single register to the BME280.
///
/// See [`Bme280Bus::write_reg`] for more information.
async fn write_reg(&mut self, reg: u8, data: u8) -> Result<(), Self::Error>;
/// Read the calibration from the chip.
async fn calibration(&mut self) -> Result<crate::Calibration, Self::Error> {
const FIRST: usize = (crate::reg::CALIB_25 - crate::reg::CALIB_00 + 1) as usize;
debug_assert_eq!(FIRST, 26);
const SECOND: usize = (crate::reg::CALIB_32 - crate::reg::CALIB_26 + 1) as usize;
debug_assert_eq!((FIRST + SECOND), crate::NUM_CALIB_REG);
let mut buf: [u8; crate::NUM_CALIB_REG] = [0; crate::NUM_CALIB_REG];
self.read_regs(crate::reg::CALIB_00, &mut buf[0..FIRST])
.await?;
self.read_regs(crate::reg::CALIB_26, &mut buf[FIRST..(FIRST + SECOND)])
.await?;
Ok(buf.into())
}
}
/// BME280 driver.
#[derive(Debug)]
pub struct Bme280<B> {
bus: B,
cal: Calibration,
}
impl<I2C, E> Bme280<crate::i2c0::Bme280Bus<I2C>>
where
I2C: eh0::blocking::i2c::Write<Error = E> + eh0::blocking::i2c::WriteRead<Error = E>,
{
/// Creates a new `Bme280` driver from a I2C peripheral, and an I2C
/// device address.
///
/// # Example
///
/// ```
/// # let i2c = ehm::eh0::i2c::Mock::new(&[
/// # ehm::eh0::i2c::Transaction::write_read(0x76, vec![0x88], vec![0; 26]),
/// # ehm::eh0::i2c::Transaction::write_read(0x76, vec![0xE1], vec![0; 7]),
/// # ]);
/// use bme280_multibus::{Address, Bme280};
///
/// let mut bme: Bme280<_> = Bme280::from_i2c0(i2c, Address::SdoGnd)?;
/// # bme.free().free().done();
/// # Ok::<(), ehm::eh0::MockError>(())
/// ```
pub fn from_i2c0(i2c: I2C, address: crate::Address) -> Result<Self, E> {
let bus = crate::i2c0::Bme280Bus::new(i2c, address);
Self::new(bus)
}
}
impl<I2C, E> Bme280<crate::i2c1::Bme280Bus<I2C>>
where
I2C: eh1::i2c::I2c<Error = E>,
{
/// Creates a new `Bme280` driver from a I2C peripheral, and an I2C
/// device address.
///
/// # Example
///
/// ```
/// # let i2c = ehm::eh1::i2c::Mock::new(&[
/// # ehm::eh1::i2c::Transaction::write_read(0x76, vec![0x88], vec![0; 26]),
/// # ehm::eh1::i2c::Transaction::write_read(0x76, vec![0xE1], vec![0; 7]),
/// # ]);
/// use bme280_multibus::{i2c1::Address, Bme280};
///
/// let mut bme: Bme280<_> = Bme280::from_i2c1(i2c, Address::SdoGnd)?;
/// # bme.free().free().done();
/// # Ok::<(), eh1::i2c::ErrorKind>(())
/// ```
pub fn from_i2c1(i2c: I2C, address: crate::i2c1::Address) -> Result<Self, E> {
let bus = crate::i2c1::Bme280Bus::new(i2c, address);
Self::new(bus)
}
}
impl<SPI, CS, SpiError, PinError> Bme280<crate::spi0::Bme280Bus<SPI, CS>>
where
SPI: eh0::blocking::spi::Transfer<u8, Error = SpiError>
+ eh0::blocking::spi::Write<u8, Error = SpiError>,
CS: eh0::digital::v2::OutputPin<Error = PinError>,
{
/// Creates a new `Bme280` driver from an embedded-hal version 0.2 SPI
/// peripheral and a chip select digital I/O pin.
///
/// # Safety
///
/// The chip select pin must be high before being passed to this function.
///
/// # Example
///
/// ```
/// # let spi = ehm::eh0::spi::Mock::new(&[
/// # ehm::eh0::spi::Transaction::write(vec![0x88]),
/// # ehm::eh0::spi::Transaction::transfer(vec![0; 26], vec![0; 26]),
/// # ehm::eh0::spi::Transaction::write(vec![0xE1]),
/// # ehm::eh0::spi::Transaction::transfer(vec![0; 7], vec![0; 7]),
/// # ]);
/// # let mut pin = ehm::eh0::pin::Mock::new(&[
/// # ehm::eh0::pin::Transaction::set(ehm::eh0::pin::State::High),
/// # ehm::eh0::pin::Transaction::set(ehm::eh0::pin::State::Low),
/// # ehm::eh0::pin::Transaction::set(ehm::eh0::pin::State::High),
/// # ehm::eh0::pin::Transaction::set(ehm::eh0::pin::State::Low),
/// # ehm::eh0::pin::Transaction::set(ehm::eh0::pin::State::High),
/// # ]);
/// use bme280_multibus::Bme280;
/// use eh0::digital::v2::OutputPin;
///
/// pin.set_high()?;
/// let mut bme: Bme280<_> = Bme280::from_spi0(spi, pin)?;
/// # let (mut spi, mut pin) = bme.free().free();
/// # spi.done(); pin.done();
/// # Ok::<(), bme280_multibus::spi0::Error<ehm::eh0::MockError, ehm::eh0::MockError>>(())
/// ```
#[allow(clippy::unnecessary_safety_doc)]
pub fn from_spi0(spi: SPI, cs: CS) -> Result<Self, crate::spi0::Error<SpiError, PinError>> {
let bus = crate::spi0::Bme280Bus::new(spi, cs);
Self::new(bus)
}
}
impl<SPI, E> Bme280<crate::spi1::Bme280Bus<SPI>>
where
SPI: eh1::spi::SpiDevice<Error = E>,
{
/// Creates a new `Bme280` driver from an embedded-hal version 1 SPI device.
///
/// # Example
///
/// ```
/// # let spi = ehm::eh1::spi::Mock::new(&[
/// # ehm::eh1::spi::Transaction::transaction_start(),
/// # ehm::eh1::spi::Transaction::write(0x88),
/// # ehm::eh1::spi::Transaction::read_vec(vec![0; 26]),
/// # ehm::eh1::spi::Transaction::transaction_end(),
/// # ehm::eh1::spi::Transaction::transaction_start(),
/// # ehm::eh1::spi::Transaction::write(0xE1),
/// # ehm::eh1::spi::Transaction::read_vec(vec![0; 7]),
/// # ehm::eh1::spi::Transaction::transaction_end(),
/// # ]);
/// use bme280_multibus::Bme280;
///
/// let mut bme: Bme280<_> = Bme280::from_spi1(spi)?;
/// # bme.free().free().done();
/// # Ok::<(), eh1::spi::ErrorKind>(())
/// ```
pub fn from_spi1(spi: SPI) -> Result<Self, E> {
let bus: crate::spi1::Bme280Bus<SPI> = crate::spi1::Bme280Bus::new(spi);
Self::new(bus)
}
}
#[cfg(feature = "async")]
impl<SPI, E> Bme280<crate::spi1::Bme280Bus<SPI>>
where
SPI: eha0a::spi::SpiDevice<Error = E>,
{
/// Creates a new `Bme280` driver from an embedded-hal-async SPI device.
///
/// # Example
///
/// ```
/// # #[tokio::main(flavor = "current_thread")]
/// # async fn main() -> Result<(), eh1::spi::ErrorKind> {
/// # let spi = ehm::eh1::spi::Mock::new(&[
/// # ehm::eh1::spi::Transaction::transaction_start(),
/// # ehm::eh1::spi::Transaction::write(0x88),
/// # ehm::eh1::spi::Transaction::read_vec(vec![0; 26]),
/// # ehm::eh1::spi::Transaction::transaction_end(),
/// # ehm::eh1::spi::Transaction::transaction_start(),
/// # ehm::eh1::spi::Transaction::write(0xE1),
/// # ehm::eh1::spi::Transaction::read_vec(vec![0; 7]),
/// # ehm::eh1::spi::Transaction::transaction_end(),
/// # ]);
/// use bme280_multibus::Bme280;
///
/// let bme: Bme280<_> = Bme280::from_spia0a(spi).await?;
/// # bme.free().free().done(); Ok(()) }
/// ```
pub async fn from_spia0a(spi: SPI) -> Result<Self, E> {
let bus = crate::spi1::Bme280Bus::new(spi);
Self::new_async(bus).await
}
}
#[cfg(feature = "async")]
impl<B, E> Bme280<B>
where
B: Bme280BusAsync<Error = E>,
{
/// Create a new BME280 from a [`Bme280BusAsync`].
///
/// # Example
///
/// ```
/// # #[tokio::main(flavor = "current_thread")]
/// # async fn main() -> Result<(), eh1::spi::ErrorKind> {
/// # let spi = ehm::eh1::spi::Mock::new(&[
/// # ehm::eh1::spi::Transaction::transaction_start(),
/// # ehm::eh1::spi::Transaction::write(0x88),
/// # ehm::eh1::spi::Transaction::read_vec(vec![0; 26]),
/// # ehm::eh1::spi::Transaction::transaction_end(),
/// # ehm::eh1::spi::Transaction::transaction_start(),
/// # ehm::eh1::spi::Transaction::write(0xE1),
/// # ehm::eh1::spi::Transaction::read_vec(vec![0; 7]),
/// # ehm::eh1::spi::Transaction::transaction_end(),
/// # ]);
/// use bme280_multibus::{spi1::Bme280Bus, Bme280};
///
/// let bus: Bme280Bus<_> = Bme280Bus::new(spi);
/// let bme: Bme280<_> = Bme280::new_async(bus).await?;
/// # bme.free().free().done(); Ok(()) }
/// ```
pub async fn new_async(mut bus: B) -> Result<Self, E> {
let cal: Calibration = bus.calibration().await?;
Ok(Self { bus, cal })
}
/// BME280 chip ID.
///
/// The return value is a constant, [`CHIP_ID`].
///
/// This register is useful as a sanity check to ensure communications are
/// working with the BME280.
///
/// # Example
///
/// ```
/// # #[tokio::main(flavor = "current_thread")]
/// # async fn main() -> Result<(), eh1::spi::ErrorKind> {
/// # let spi = ehm::eh1::spi::Mock::new(&[
/// # ehm::eh1::spi::Transaction::transaction_start(),
/// # ehm::eh1::spi::Transaction::write(0x88),
/// # ehm::eh1::spi::Transaction::read_vec(vec![0; 26]),
/// # ehm::eh1::spi::Transaction::transaction_end(),
/// # ehm::eh1::spi::Transaction::transaction_start(),
/// # ehm::eh1::spi::Transaction::write(0xE1),
/// # ehm::eh1::spi::Transaction::read_vec(vec![0; 7]),
/// # ehm::eh1::spi::Transaction::transaction_end(),
/// # ehm::eh1::spi::Transaction::transaction_start(),
/// # ehm::eh1::spi::Transaction::write(0xD0),
/// # ehm::eh1::spi::Transaction::read(0x60),
/// # ehm::eh1::spi::Transaction::transaction_end(),
/// # ]);
/// use bme280_multibus::{spi1::Bme280Bus, Bme280, CHIP_ID};
///
/// let mut bme: Bme280<_> = Bme280::from_spia0a(spi).await?;
/// let chip_id: u8 = bme.chip_id_async().await?;
/// assert_eq!(chip_id, CHIP_ID);
/// # bme.free().free().done(); Ok(()) }
/// ```
pub async fn chip_id_async(&mut self) -> Result<u8, E> {
let mut buf: [u8; 1] = [0];
self.bus.read_regs(reg::ID, &mut buf).await?;
Ok(buf[0])
}
/// Reset the BME280.
///
/// # Example
///
/// ```
/// # #[tokio::main(flavor = "current_thread")]
/// # async fn main() -> Result<(), eh1::spi::ErrorKind> {
/// # let spi = ehm::eh1::spi::Mock::new(&[
/// # ehm::eh1::spi::Transaction::transaction_start(),
/// # ehm::eh1::spi::Transaction::write(0x88),
/// # ehm::eh1::spi::Transaction::read_vec(vec![0; 26]),
/// # ehm::eh1::spi::Transaction::transaction_end(),
/// # ehm::eh1::spi::Transaction::transaction_start(),
/// # ehm::eh1::spi::Transaction::write(0xE1),
/// # ehm::eh1::spi::Transaction::read_vec(vec![0; 7]),
/// # ehm::eh1::spi::Transaction::transaction_end(),
/// # ehm::eh1::spi::Transaction::transaction_start(),
/// # ehm::eh1::spi::Transaction::write_vec(vec![0xE0 & !0x80, 0xB6]),
/// # ehm::eh1::spi::Transaction::transaction_end(),
/// # ]);
/// use bme280_multibus::{spi1::Bme280Bus, Bme280};
///
/// let mut bme: Bme280<_> = Bme280::from_spia0a(spi).await?;
/// bme.reset_async().await?;
/// # bme.free().free().done(); Ok(()) }
/// ```
pub async fn reset_async(&mut self) -> Result<(), E> {
self.bus.write_reg(reg::RESET, RESET_MAGIC).await
}
/// Get the status of the device.
///
/// # Example
///
/// ```
/// # #[tokio::main(flavor = "current_thread")]
/// # async fn main() -> Result<(), eh1::spi::ErrorKind> {
/// # let spi = ehm::eh1::spi::Mock::new(&[
/// # ehm::eh1::spi::Transaction::transaction_start(),
/// # ehm::eh1::spi::Transaction::write(0x88),
/// # ehm::eh1::spi::Transaction::read_vec(vec![0; 26]),
/// # ehm::eh1::spi::Transaction::transaction_end(),
/// # ehm::eh1::spi::Transaction::transaction_start(),
/// # ehm::eh1::spi::Transaction::write(0xE1),
/// # ehm::eh1::spi::Transaction::read_vec(vec![0; 7]),
/// # ehm::eh1::spi::Transaction::transaction_end(),
/// # ehm::eh1::spi::Transaction::transaction_start(),
/// # ehm::eh1::spi::Transaction::write(0xF3),
/// # ehm::eh1::spi::Transaction::read(0x00),
/// # ehm::eh1::spi::Transaction::transaction_end(),
/// # ]);
/// use bme280_multibus::{spi1::Bme280Bus, Bme280, Status};
///
/// let mut bme: Bme280<_> = Bme280::from_spia0a(spi).await?;
/// let status: Status = bme.status_async().await?;
/// # bme.free().free().done(); Ok(()) }
/// ```
pub async fn status_async(&mut self) -> Result<Status, E> {
let mut buf: [u8; 1] = [0];
self.bus.read_regs(reg::STATUS, &mut buf).await?;
Ok(Status(buf[0]))
}
/// Configure the BME280 settings.
///
/// # Example
///
/// ```
/// # #[tokio::main(flavor = "current_thread")]
/// # async fn main() -> Result<(), eh1::spi::ErrorKind> {
/// # let spi = ehm::eh1::spi::Mock::new(&[
/// # ehm::eh1::spi::Transaction::transaction_start(),
/// # ehm::eh1::spi::Transaction::write(0x88),
/// # ehm::eh1::spi::Transaction::read_vec(vec![0; 26]),
/// # ehm::eh1::spi::Transaction::transaction_end(),
/// # ehm::eh1::spi::Transaction::transaction_start(),
/// # ehm::eh1::spi::Transaction::write(0xE1),
/// # ehm::eh1::spi::Transaction::read_vec(vec![0; 7]),
/// # ehm::eh1::spi::Transaction::transaction_end(),
/// # ehm::eh1::spi::Transaction::transaction_start(),
/// # ehm::eh1::spi::Transaction::write_vec(vec![0xF2 & !0x80, 0b100]),
/// # ehm::eh1::spi::Transaction::transaction_end(),
/// # ehm::eh1::spi::Transaction::transaction_start(),
/// # ehm::eh1::spi::Transaction::write_vec(vec![0xF4 & !0x80, 0b10010011]),
/// # ehm::eh1::spi::Transaction::transaction_end(),
/// # ehm::eh1::spi::Transaction::transaction_start(),
/// # ehm::eh1::spi::Transaction::write_vec(vec![0xF5 & !0x80, 0b10110000]),
/// # ehm::eh1::spi::Transaction::transaction_end(),
/// # ]);
/// use bme280_multibus::{
/// spi1::Bme280Bus, Bme280, Config, CtrlMeas, Filter, Mode, Oversampling, Settings, Standby,
/// };
///
/// const SETTINGS: Settings = Settings {
/// config: Config::RESET
/// .set_standby_time(Standby::Millis1000)
/// .set_filter(Filter::X16),
/// ctrl_meas: CtrlMeas::RESET
/// .set_osrs_t(Oversampling::X8)
/// .set_osrs_p(Oversampling::X8)
/// .set_mode(Mode::Normal),
/// ctrl_hum: Oversampling::X8,
/// };
///
/// let mut bme: Bme280<_> = Bme280::from_spia0a(spi).await?;
/// bme.settings_async(&SETTINGS).await?;
/// # bme.free().free().done(); Ok(()) }
/// ```
pub async fn settings_async(&mut self, settings: &Settings) -> Result<(), E> {
self.bus
.write_reg(reg::CTRL_HUM, settings.ctrl_hum as u8)
.await?;
self.bus
.write_reg(reg::CTRL_MEAS, settings.ctrl_meas.0)
.await?;
self.bus.write_reg(reg::CONFIG, settings.config.0).await
}
/// Read a sample from the BME280.
///
/// ```
/// # #[tokio::main(flavor = "current_thread")]
/// # async fn main() -> Result<(), eh1::spi::ErrorKind> {
/// # let spi = ehm::eh1::spi::Mock::new(&[
/// # ehm::eh1::spi::Transaction::transaction_start(),
/// # ehm::eh1::spi::Transaction::write(0x88),
/// # ehm::eh1::spi::Transaction::read_vec(vec![0; 26]),
/// # ehm::eh1::spi::Transaction::transaction_end(),
/// # ehm::eh1::spi::Transaction::transaction_start(),
/// # ehm::eh1::spi::Transaction::write(0xE1),
/// # ehm::eh1::spi::Transaction::read_vec(vec![0; 7]),
/// # ehm::eh1::spi::Transaction::transaction_end(),
/// # ehm::eh1::spi::Transaction::transaction_start(),
/// # ehm::eh1::spi::Transaction::write_vec(vec![0xF2 & !0x80, 0b100]),
/// # ehm::eh1::spi::Transaction::transaction_end(),
/// # ehm::eh1::spi::Transaction::transaction_start(),
/// # ehm::eh1::spi::Transaction::write_vec(vec![0xF4 & !0x80, 0b10010011]),
/// # ehm::eh1::spi::Transaction::transaction_end(),
/// # ehm::eh1::spi::Transaction::transaction_start(),
/// # ehm::eh1::spi::Transaction::write_vec(vec![0xF5 & !0x80, 0b10110000]),
/// # ehm::eh1::spi::Transaction::transaction_end(),
/// # ehm::eh1::spi::Transaction::transaction_start(),
/// # ehm::eh1::spi::Transaction::write(0xF7),
/// # ehm::eh1::spi::Transaction::read_vec(vec![0; 8]),
/// # ehm::eh1::spi::Transaction::transaction_end(),
/// # ]);
/// use bme280_multibus::{
/// spi1::Bme280Bus, Bme280, Config, CtrlMeas, Filter, Mode, Oversampling, Sample, Settings,
/// Standby,
/// };
///
/// const SETTINGS: bme280_multibus::Settings = bme280_multibus::Settings {
/// config: bme280_multibus::Config::RESET
/// .set_standby_time(bme280_multibus::Standby::Millis1000)
/// .set_filter(bme280_multibus::Filter::X16),
/// ctrl_meas: bme280_multibus::CtrlMeas::RESET
/// .set_osrs_t(bme280_multibus::Oversampling::X8)
/// .set_osrs_p(bme280_multibus::Oversampling::X8)
/// .set_mode(bme280_multibus::Mode::Normal),
/// ctrl_hum: bme280_multibus::Oversampling::X8,
/// };
///
/// let mut bme: Bme280<_> = Bme280::from_spia0a(spi).await?;
/// bme.settings_async(&SETTINGS).await?;
/// let sample: Sample = bme.sample_async().await.unwrap();
/// # bme.free().free().done(); Ok(()) }
/// ```
pub async fn sample_async(&mut self) -> Result<Sample, Error<E>> {
let mut buf: [u8; NUM_MEAS_REG] = [0; NUM_MEAS_REG];
self.bus
.read_regs(reg::PRESS_MSB, &mut buf)
.await
.map_err(Error::Bus)?;
Sample::from_raw(&buf, &self.cal)
}
}
impl<B, E> Bme280<B>
where
B: Bme280Bus<Error = E>,
{
/// Create a new BME280 from a [`spi0::Bme280Bus`] or
/// a [`i2c0::Bme280Bus`].
///
/// # Example
///
/// ```
/// # let i2c = ehm::eh0::i2c::Mock::new(&[
/// # ehm::eh0::i2c::Transaction::write_read(0x76, vec![0x88], vec![0; 26]),
/// # ehm::eh0::i2c::Transaction::write_read(0x76, vec![0xE1], vec![0; 7]),
/// # ]);
/// use bme280_multibus::{
/// i2c0::{Address, Bme280Bus},
/// Bme280,
/// };
///
/// let bus: Bme280Bus<_> = Bme280Bus::new(i2c, Address::SdoGnd);
/// let bme: Bme280<_> = Bme280::new(bus)?;
/// # bme.free().free().done();
/// # Ok::<(), ehm::eh0::MockError>(())
/// ```
pub fn new(mut bus: B) -> Result<Self, E> {
let cal: Calibration = bus.calibration()?;
Ok(Self { bus, cal })
}
/// Free the I2C bus from the BME280.
///
/// # Example
///
/// ```
/// # let i2c = ehm::eh0::i2c::Mock::new(&[
/// # ehm::eh0::i2c::Transaction::write_read(0x76, vec![0x88], vec![0; 26]),
/// # ehm::eh0::i2c::Transaction::write_read(0x76, vec![0xE1], vec![0; 7]),
/// # ]);
/// use bme280_multibus::{
/// i2c0::{Address, Bme280Bus},
/// Bme280,
/// };
///
/// let bus: Bme280Bus<_> = Bme280Bus::new(i2c, Address::SdoGnd);
/// let bme: Bme280<_> = Bme280::new(bus)?;
/// let bus: Bme280Bus<_> = bme.free();
/// # bus.free().done();
/// # Ok::<(), ehm::eh0::MockError>(())
/// ```
#[inline]
pub fn free(self) -> B {
self.bus
}
/// BME280 chip ID.
///
/// The return value is a constant, [`CHIP_ID`].
///
/// This register is useful as a sanity check to ensure communications are
/// working with the BME280.
///
/// # Example
///
/// ```
/// # let i2c = ehm::eh0::i2c::Mock::new(&[
/// # ehm::eh0::i2c::Transaction::write_read(0x76, vec![0x88], vec![0; 26]),
/// # ehm::eh0::i2c::Transaction::write_read(0x76, vec![0xE1], vec![0; 7]),
/// # ehm::eh0::i2c::Transaction::write_read(0x76, vec![0xD0], vec![0x60]),
/// # ]);
/// use bme280_multibus::{Address, Bme280, CHIP_ID};
///
/// let mut bme: Bme280<_> = Bme280::from_i2c0(i2c, Address::SdoGnd)?;
/// let chip_id: u8 = bme.chip_id()?;
/// assert_eq!(chip_id, CHIP_ID);
/// # bme.free().free().done();
/// # Ok::<(), ehm::eh0::MockError>(())
/// ```
pub fn chip_id(&mut self) -> Result<u8, E> {
let mut buf: [u8; 1] = [0];
self.bus.read_regs(reg::ID, &mut buf)?;
Ok(buf[0])
}
/// Reset the BME280.
///
/// # Example
///
/// ```
/// # let i2c = ehm::eh0::i2c::Mock::new(&[
/// # ehm::eh0::i2c::Transaction::write_read(0x76, vec![0x88], vec![0; 26]),
/// # ehm::eh0::i2c::Transaction::write_read(0x76, vec![0xE1], vec![0; 7]),
/// # ehm::eh0::i2c::Transaction::write(0x76, vec![0xE0, 0xB6]),
/// # ]);
/// use bme280_multibus::{Address, Bme280};
///
/// let mut bme: Bme280<_> = Bme280::from_i2c0(i2c, Address::SdoGnd)?;
/// bme.reset()?;
/// # bme.free().free().done();
/// # Ok::<(), ehm::eh0::MockError>(())
/// ```
pub fn reset(&mut self) -> Result<(), E> {
self.bus.write_reg(reg::RESET, RESET_MAGIC)
}
/// Get the status of the device.
///
/// # Example
///
/// Check if a conversion is running.
///
/// ```
/// # let i2c = ehm::eh0::i2c::Mock::new(&[
/// # ehm::eh0::i2c::Transaction::write_read(0x76, vec![0x88], vec![0; 26]),
/// # ehm::eh0::i2c::Transaction::write_read(0x76, vec![0xE1], vec![0; 7]),
/// # ehm::eh0::i2c::Transaction::write_read(0x76, vec![0xF3], vec![0]),
/// # ]);
/// use bme280_multibus::{Address, Bme280, Status};
///
/// let mut bme: Bme280<_> = Bme280::from_i2c0(i2c, Address::SdoGnd)?;
/// let status: Status = bme.status()?;
/// # bme.free().free().done();
/// # Ok::<(), ehm::eh0::MockError>(())
/// ```
pub fn status(&mut self) -> Result<Status, E> {
let mut buf: [u8; 1] = [0];
self.bus.read_regs(reg::STATUS, &mut buf)?;
Ok(Status(buf[0]))
}
/// Configure the BME280 settings.
///
/// # Example
///
/// ```
/// # let i2c = ehm::eh0::i2c::Mock::new(&[
/// # ehm::eh0::i2c::Transaction::write_read(0x76, vec![0x88], vec![0; 26]),
/// # ehm::eh0::i2c::Transaction::write_read(0x76, vec![0xE1], vec![0; 7]),
/// # ehm::eh0::i2c::Transaction::write(0x76, vec![0xF2, 0b100]),
/// # ehm::eh0::i2c::Transaction::write(0x76, vec![0xF4, 0b10010011]),
/// # ehm::eh0::i2c::Transaction::write(0x76, vec![0xF5, 0b10110000]),
/// # ]);
/// use bme280_multibus::{
/// Address, Bme280, Config, CtrlMeas, Filter, Mode, Oversampling, Settings, Standby,
/// };
///
/// const SETTINGS: Settings = Settings {
/// config: Config::RESET
/// .set_standby_time(Standby::Millis1000)
/// .set_filter(Filter::X16),
/// ctrl_meas: CtrlMeas::RESET
/// .set_osrs_t(Oversampling::X8)
/// .set_osrs_p(Oversampling::X8)
/// .set_mode(Mode::Normal),
/// ctrl_hum: Oversampling::X8,
/// };
///
/// let mut bme: Bme280<_> = Bme280::from_i2c0(i2c, Address::SdoGnd)?;
/// bme.settings(&SETTINGS)?;
/// # bme.free().free().done();
/// # Ok::<(), ehm::eh0::MockError>(())
/// ```
pub fn settings(&mut self, settings: &Settings) -> Result<(), E> {
self.bus.write_reg(reg::CTRL_HUM, settings.ctrl_hum as u8)?;
self.bus.write_reg(reg::CTRL_MEAS, settings.ctrl_meas.0)?;
self.bus.write_reg(reg::CONFIG, settings.config.0)
}
/// Read a sample from the BME280.
///
/// # Example
///
/// ```
/// # let i2c = ehm::eh0::i2c::Mock::new(&[
/// # ehm::eh0::i2c::Transaction::write_read(0x76, vec![0x88], vec![0; 26]),
/// # ehm::eh0::i2c::Transaction::write_read(0x76, vec![0xE1], vec![0; 7]),
/// # ehm::eh0::i2c::Transaction::write(0x76, vec![0xF2, 0b100]),
/// # ehm::eh0::i2c::Transaction::write(0x76, vec![0xF4, 0b10010011]),
/// # ehm::eh0::i2c::Transaction::write(0x76, vec![0xF5, 0b10110000]),
/// # ehm::eh0::i2c::Transaction::write_read(0x76, vec![0xF7], vec![0; 8]),
/// # ]);
/// use bme280_multibus::{Address, Bme280, Sample};
///
/// const SETTINGS: bme280_multibus::Settings = bme280_multibus::Settings {
/// config: bme280_multibus::Config::RESET
/// .set_standby_time(bme280_multibus::Standby::Millis1000)
/// .set_filter(bme280_multibus::Filter::X16),
/// ctrl_meas: bme280_multibus::CtrlMeas::RESET
/// .set_osrs_t(bme280_multibus::Oversampling::X8)
/// .set_osrs_p(bme280_multibus::Oversampling::X8)
/// .set_mode(bme280_multibus::Mode::Normal),
/// ctrl_hum: bme280_multibus::Oversampling::X8,
/// };
///
/// let mut bme: Bme280<_> = Bme280::from_i2c0(i2c, Address::SdoGnd)?;
/// bme.settings(&SETTINGS)?;
/// let sample: Sample = bme.sample().unwrap();
/// # bme.free().free().done();
/// # Ok::<(), ehm::eh0::MockError>(())
/// ```
pub fn sample(&mut self) -> Result<Sample, Error<E>> {
let mut buf: [u8; NUM_MEAS_REG] = [0; NUM_MEAS_REG];
self.bus
.read_regs(reg::PRESS_MSB, &mut buf)
.map_err(Error::Bus)?;
Sample::from_raw(&buf, &self.cal)
}
}