nrfcxx  0.1.0
C++-17 Framework for Nordic nRF5 Devices
bme280.hpp
Go to the documentation of this file.
1 /* SPDX-License-Identifier: Apache-2.0 */
2 /* Copyright 2017-2019 Peter A. Bigot */
3 
8 #ifndef NRFCXX_SENSOR_BME280_HPP
9 #define NRFCXX_SENSOR_BME280_HPP
10 #pragma once
11 
12 #include <climits>
13 
14 #include "bme280.h"
15 
16 #include <nrfcxx/lpm.hpp>
17 #include <nrfcxx/periph.hpp>
18 
19 namespace nrfcxx {
20 namespace sensor {
21 
40 class bme280 : public lpm::lpsm_capable
41 {
42  using super = lpm::lpsm_capable;
43 public:
48  static constexpr unsigned int SAMPLE_DELAY_utt = clock::uptime::from_ms(10);
49 
51  static constexpr uint16_t INVALID_TEMPHUMID = 30000;
52 
57  static constexpr unsigned int INVALID_PRESSURE = 0x00C00000;
58 
64 
68 
71  };
72 
82  bme280 (notifier_type notify,
84  int csn_psel);
85 
95  bme280 (notifier_type notify,
97  bool addr_sec = false);
98 
113  int initialize ();
114 
124  int sample ()
125  {
126  return ::bme280_set_sensor_mode(BME280_FORCED_MODE, &dev);
127  }
128 
135  int fetch (observations_type& obs);
136 
140  {
141  return observations_;
142  }
143 
144 private:
145  /* Initialization shared between SPI and TWI interfaces. */
146  void init_dev_ (bool spi, bool addr_sec = false);
147 
148  /* Pointer to the SPI peripheral, if a SPI instance exists. */
149  static periph::SPI* spip_;
150 
151  /* Pointer to the TWI peripheral, if an I2C interface exists. */
152  static periph::TWI* twip_;
153 
154  /* Pin selector for SPI CSn signal. */
155  static uint8_t csn_psel_;
156 
157  /* Driver functions are static so they can peek at #spip_, #twip_,
158  * and #csn_psel_ to do what they need to do. */
159  static int8_t spi_read (uint8_t dev_id,
160  uint8_t reg_addr,
161  uint8_t* data,
162  uint16_t len);
163 
164  static int8_t spi_write (uint8_t dev_id,
165  uint8_t reg_addr,
166  uint8_t* data,
167  uint16_t len);
168 
169  static int8_t twi_read (uint8_t dev_id,
170  uint8_t reg_addr,
171  uint8_t* data,
172  uint16_t len);
173 
174  static int8_t twi_write (uint8_t dev_id,
175  uint8_t reg_addr,
176  uint8_t* data,
177  uint16_t len);
178 
179  /* Instance-specific device configuration. */
180  struct bme280_dev dev;
181 
182  int lpsm_process_ (int& delay,
183  process_flags_type& pf) override;
184 
185  /* Last observations captured by LPM. */
186  observations_type observations_;
187 };
188 
189 } // ns sensor
190 } // ns nrfcxx
191 
192 #endif /* NRFCXX_SENSOR_BME280_HPP */
nrfcxx::sensor::bme280::SAMPLE_DELAY_utt
static constexpr unsigned int SAMPLE_DELAY_utt
Lower bound for wait after sample() before get_sensor_data().
Definition: bme280.hpp:48
nrfcxx::sensor::bme280::observations
const observations_type & observations() const
Retrieve the most recent observations calculated through the LPM infrastructure.
Definition: bme280.hpp:139
nrfcxx::sensor::bme280::sample
int sample()
Request a new sample from the device.
Definition: bme280.hpp:124
nrfcxx::sensor::bme280::observations_type::pressure_cPa
unsigned int pressure_cPa
A recently retrieved valid pressure or INVALID_PRESSURE.
Definition: bme280.hpp:70
nrfcxx::periph::SPI
Wrapper around the nRF51 SPI peripheral.
Definition: periph.hpp:1748
lpm.hpp
Material supporting low-power-mode operations.
nrfcxx::periph::TWI
Wrapper around the nRF51 TWI peripheral.
Definition: periph.hpp:1528
nrfcxx::sensor::bme280::observations_type
Structure used to return sampled values.
Definition: bme280.hpp:60
nrfcxx::sensor::bme280::fetch
int fetch(observations_type &obs)
Retrieve the results from the most recent observation.
nrfcxx::sensor::bme280::bme280
bme280(notifier_type notify, nrfcxx::periph::SPI &spi, int csn_psel)
Construct an instance that controls through the SPI interface.
nrfcxx::sensor::bme280::INVALID_PRESSURE
static constexpr unsigned int INVALID_PRESSURE
Value used for invalid pressure observations.
Definition: bme280.hpp:57
nrfcxx::sensor::bme280
Abstraction around BME280 temperature/humidity/pressure sensor.
Definition: bme280.hpp:40
nrfcxx::sensor::bme280::INVALID_TEMPHUMID
static constexpr uint16_t INVALID_TEMPHUMID
Value used for invalid temperature and humidity observations.
Definition: bme280.hpp:51
nrfcxx::sensor::bme280::initialize
int initialize()
Initialize the BME280.
nrfcxx::notifier_type
std::function< void()> notifier_type
Type used to hold a notifier.
Definition: core.hpp:514
nrfcxx::sensor::bme280::observations_type::humidity_pptt
uint16_t humidity_pptt
A recently retrieved valid relative humidity or INVALID_TEMPHUMID.
Definition: bme280.hpp:67
nrfcxx::lpm::lpsm_capable
Base (or mixin) class for anything that supports a state_machine.
Definition: lpm.hpp:426
nrfcxx::clock::uptime::from_ms
constexpr static int64_t from_ms(int64_t ms)
Convert integral milliseconds to uptime ticks (rounding down).
Definition: clock.hpp:422
nrfcxx::sensor::bme280::observations_type::temperature_cCel
int16_t temperature_cCel
A recently retrieved valid temperature or INVALID_TEMPHUMID.
Definition: bme280.hpp:63
periph.hpp
Abstraction of Nordic device peripherals.
nrfcxx
Primary namespace for nrfcxx functionality.
Definition: clock.hpp:17