nrfcxx  0.1.0
C++-17 Framework for Nordic nRF5 Devices
hts221.hpp
Go to the documentation of this file.
1 /* SPDX-License-Identifier: Apache-2.0 */
2 /* Copyright 2018-2019 Peter A. Bigot */
3 
8 #ifndef NRFCXX_SENSOR_HTS221_HPP
9 #define NRFCXX_SENSOR_HTS221_HPP
10 #pragma once
11 
12 #include <climits>
13 
14 #include <nrfcxx/gpio.hpp>
15 #include <nrfcxx/lpm.hpp>
16 
17 namespace nrfcxx {
18 namespace sensor {
19 
36 class hts221 : public lpm::lpsm_capable
37 {
38  using super = lpm::lpsm_capable;
39 public:
45  {
48 
54  int8_t drdy_psel;
55 
60  uint8_t address;
61 
62  iface_config_type (const iface_config_type&) = delete;
63  iface_config_type& operator= (const iface_config_type&) = delete;
65  iface_config_type& operator= (iface_config_type&&) = delete;
66  };
67 
70  {
71  return iface_config_;
72  }
73 
82  {
84  int16_t cal_cCel[2];
85 
87  uint16_t cal_pptt[2];
88 
90  int16_t td_cCel;
91 
93  int16_t hd_pptt;
94 
96  int16_t t0_out;
97 
99  int16_t h0_out;
100 
102  int16_t td_out;
103 
106  int16_t hd_out;
107 
109  int16_t conv_cCel (int16_t t_out) const
110  {
111  return cal_cCel[0] + td_cCel * (t_out - t0_out) / td_out;
112  }
113 
115  uint16_t conv_pptt (int16_t h_out) const
116  {
117  return cal_pptt[0] + hd_pptt * (h_out - h0_out) / hd_out;
118  }
119  };
120 
125  {
126  return calibration_;
127  }
128 
130  enum STATUS_REG_e : uint8_t
131  {
133  SR_T_DA = 0x01,
134 
136  SR_H_DA = 0x02,
137 
140  };
141 
143  int status () const;
144 
146  static constexpr uint16_t INVALID_OBSERVATION = 30000;
147 
150  {
156 
162  };
163 
166  {
167  return observations_;
168  }
169 
174  enum ODR_e : uint8_t
175  {
179  ODR_OneShot = 0x00,
180 
182  ODR_1_Hz = 0x01,
183 
185  ODR_7_Hz = 0x02,
186 
188  ODR_12p5_Hz = 0x03,
189  };
190 
192  static constexpr auto ODR_DEFAULT = ODR_1_Hz;
193 
199  uint8_t odr () const
200  {
201  return odr_;
202  }
203 
210  int odr (uint8_t dr);
211 
219  hts221 (notifier_type notify,
220  iface_config_type& ifc);
221 
223  bool drdy_asserted () const
224  {
225  return nrf5::GPIO->IN & (1U << iface_config_.drdy_psel);
226  }
227 
230  int lpsm_sample () override;
231 
234  uint16_t lost_drdy () const
235  {
236  return lost_drdy_;
237  }
238 
239 private:
240  int lpsm_process_ (int& delay,
241  process_flags_type& pf) override;
242 
243  void drdy_callback_ (const periph::GPIOTE::sense_status_type*);
244 
245  iface_config_type& iface_config_;
246  calibration_type calibration_{};
247  observations_type observations_{};
248  periph::GPIOTE::sense_listener drdy_listener_;
249  uint8_t odr_ = ODR_DEFAULT;
250  uint16_t lost_drdy_ = 0;
251 };
252 
253 } // ns sensor
254 } // ns nrfcxx
255 
256 #endif /* NRFCXX_SENSOR_HTS221_HPP */
nrfcxx::sensor::hts221::calibration_type::conv_pptt
uint16_t conv_pptt(int16_t h_out) const
Convert a measured raw relative humidity value to parts-per-ten-thousand.
Definition: hts221.hpp:115
nrfcxx::sensor::hts221::calibration_type::hd_out
int16_t hd_out
Pre-calculated two-point raw relative humidity measurement difference.
Definition: hts221.hpp:106
nrfcxx::sensor::hts221::calibration_type::t0_out
int16_t t0_out
Raw measurement corresponding to cal_cCel[0].
Definition: hts221.hpp:96
nrfcxx::sensor::hts221::status
int status() const
Return the value of the STATUS register.
nrfcxx::sensor::hts221::STATUS_REG_e
STATUS_REG_e
Bit fields in the HTS221 STATUS_REG register.
Definition: hts221.hpp:130
nrfcxx::sensor::hts221::SR_H_DA
Bit set to indicate that new humidity data is available.
Definition: hts221.hpp:136
lpm.hpp
Material supporting low-power-mode operations.
nrfcxx::sensor::hts221::lpsm_sample
int lpsm_sample() override
Post-extend to signal machine if start failed due to undetected drdy.
nrfcxx::sensor::hts221::iface_config
const iface_config_type & iface_config() const
Access the interface configuration for the sensor.
Definition: hts221.hpp:69
nrfcxx::periph::TWI
Wrapper around the nRF51 TWI peripheral.
Definition: periph.hpp:1528
gpio.hpp
Core GPIO functionality.
nrfcxx::sensor::hts221::INVALID_OBSERVATION
static constexpr uint16_t INVALID_OBSERVATION
Value used to indicate that observations are not valid.
Definition: hts221.hpp:146
nrfcxx::sensor::hts221::hts221
hts221(notifier_type notify, iface_config_type &ifc)
Instantiate the device.
nrfcxx::sensor::hts221::lost_drdy
uint16_t lost_drdy() const
Count of the number of times a DRDY signal was detected to be lost.
Definition: hts221.hpp:234
nrfcxx::sensor::hts221::observations
const observations_type & observations() const
Access the most recent completed observation.
Definition: hts221.hpp:165
nrfcxx::sensor::hts221::calibration_type::cal_cCel
int16_t cal_cCel[2]
Temperatures used for two-point calibration.
Definition: hts221.hpp:84
nrfcxx::sensor::hts221::odr
uint8_t odr() const
Retrieve the configured output data rate.
Definition: hts221.hpp:199
nrfcxx::sensor::hts221::iface_config_type::twi
periph::TWI & twi
Reference to TWI device used to communicate with sensor.
Definition: hts221.hpp:47
nrfcxx::sensor::hts221::SR_READY
Value when all expected data is available.
Definition: hts221.hpp:139
nrfcxx::sensor::hts221::calibration_type::hd_pptt
int16_t hd_pptt
Pre-calculated two-point relative humidity difference.
Definition: hts221.hpp:93
nrfcxx::sensor::hts221::calibration_type::td_out
int16_t td_out
Pre-calculated two-point raw temperature measurement difference.
Definition: hts221.hpp:102
nrfcxx::sensor::hts221::iface_config_type::address
uint8_t address
The I2C address used to communicate with the device.
Definition: hts221.hpp:60
nrfcxx::sensor::hts221
Interface to the ST HTS221 capacitive digital sensor for relative humidity and temperature.
Definition: hts221.hpp:36
nrfcxx::sensor::hts221::ODR_12p5_Hz
Observations generated at 12.5 Hz.
Definition: hts221.hpp:188
nrfcxx::sensor::hts221::drdy_asserted
bool drdy_asserted() const
Programmatic test for whether DRDY is asserted.
Definition: hts221.hpp:223
nrfcxx::sensor::hts221::calibration_type::conv_cCel
int16_t conv_cCel(int16_t t_out) const
Convert a measured raw temperature value to centi-Celsius.
Definition: hts221.hpp:109
nrfcxx::sensor::hts221::ODR_OneShot
On-demand observations.
Definition: hts221.hpp:179
nrfcxx::sensor::hts221::ODR_1_Hz
Observations generated at 1 Hz.
Definition: hts221.hpp:182
nrfcxx::periph::GPIOTE::sense_status_type
Structure used to convey information about pin levels to sense_listener callbacks.
Definition: periph.hpp:672
nrfcxx::notifier_type
std::function< void()> notifier_type
Type used to hold a notifier.
Definition: core.hpp:514
nrfcxx::sensor::hts221::ODR_DEFAULT
static constexpr auto ODR_DEFAULT
The default output data rate.
Definition: hts221.hpp:192
nrfcxx::sensor::hts221::observations_type::temperature_cCel
int16_t temperature_cCel
A measured temperature in centi-Celsius.
Definition: hts221.hpp:155
nrfcxx::lpm::lpsm_capable
Base (or mixin) class for anything that supports a state_machine.
Definition: lpm.hpp:426
nrfcxx::sensor::hts221::calibration_type::h0_out
int16_t h0_out
Raw measurement corresponding to cal_pptt[0].
Definition: hts221.hpp:99
nrfcxx::sensor::hts221::calibration_type::td_cCel
int16_t td_cCel
Pre-calculated two-point temperature difference.
Definition: hts221.hpp:90
nrfcxx::sensor::hts221::calibration_type
Structure for calibration information.
Definition: hts221.hpp:81
nrfcxx::sensor::hts221::observations_type
Structure used to return sampled values.
Definition: hts221.hpp:149
nrfcxx::sensor::hts221::iface_config_type::drdy_psel
int8_t drdy_psel
GPIO pin selector usable for interrupt-driven notification of available measurements.
Definition: hts221.hpp:54
nrfcxx::sensor::hts221::iface_config_type
Information required to communicate with a sensor instance.
Definition: hts221.hpp:44
nrfcxx::sensor::hts221::observations_type::humidity_pptt
uint16_t humidity_pptt
A measured relative humidity in parts-per-ten-thousand (c%).
Definition: hts221.hpp:161
nrfcxx::sensor::hts221::calibration
const calibration_type & calibration() const
Access the calibration constants.
Definition: hts221.hpp:124
nrfcxx::sensor::hts221::ODR_e
ODR_e
Bit field values in CTRL_REG1 for observation data rate.
Definition: hts221.hpp:174
nrfcxx::sensor::hts221::ODR_7_Hz
Observations generated at 7 Hz.
Definition: hts221.hpp:185
nrfcxx
Primary namespace for nrfcxx functionality.
Definition: clock.hpp:17
nrfcxx::sensor::hts221::calibration_type::cal_pptt
uint16_t cal_pptt[2]
Relative humidity measures used for two-point calibration.
Definition: hts221.hpp:87
nrfcxx::sensor::hts221::SR_T_DA
Bit set to indicate that new temperature data is available.
Definition: hts221.hpp:133