nrfcxx  0.1.0
C++-17 Framework for Nordic nRF5 Devices
sht21.hpp
Go to the documentation of this file.
1 /* SPDX-License-Identifier: Apache-2.0 */
2 /* Copyright 2015-2019 Peter A. Bigot */
3 
7 #ifndef NRFCXX_SENSOR_SHT21_HPP
8 #define NRFCXX_SENSOR_SHT21_HPP
9 #pragma once
10 
11 #include <nrfcxx/clock.hpp>
12 #include <nrfcxx/lpm.hpp>
13 #include <nrfcxx/periph.hpp>
14 
15 namespace nrfcxx {
16 namespace sensor {
17 
30 class sht21 : public lpm::lpsm_capable
31 {
32  using super = lpm::lpsm_capable;
33 public:
35  using eic_type = uint8_t[8];
36 
39  constexpr static unsigned int RESET_DELAY_ms = 15;
40 
45  constexpr static unsigned int SAMPLE14_DELAY_ms = 85;
46 
51  constexpr static unsigned int SAMPLE12_DELAY_ms = 22;
52 
55  constexpr static uint8_t CONFIG_RES_H12T14 = 0x00;
56 
59  constexpr static uint8_t CONFIG_RES_H8T12 = 0x01;
60 
63  constexpr static uint8_t CONFIG_RES_H10T13 = 0x80;
64 
67  constexpr static uint8_t CONFIG_RES_H11T11 = 0x81;
68 
71  constexpr static uint8_t CONFIG_RES_Msk = 0x81;
72 
75  constexpr static uint8_t CONFIG_OTPRn = 0x02;
76 
79  constexpr static uint8_t CONFIG_HEATER = 0x04;
80 
82  constexpr static uint8_t CONFIG_EOB = 0x40;
83 
88  constexpr static int INVALID_OBSERVATION = -30000;
89 
92  {
96 
100  };
101 
107  {
110 
115  uint8_t address;
116 
117  iface_config_type (const iface_config_type&) = delete;
118  iface_config_type& operator= (const iface_config_type&) = delete;
120  iface_config_type& operator= (iface_config_type&&) = delete;
121  };
122 
125  {
126  return iface_config_;
127  }
128 
130  sht21 (notifier_type setter,
131  iface_config_type& ifc);
132 
133  /* No copying or moving. */
134  sht21 (const sht21&) = delete;
135  sht21& operator= (const sht21&) = delete;
136  sht21 (sht21&& ) = delete;
137  sht21& operator= (sht21&) = delete;
138 
146  int reset ();
147 
161  int configure (int config);
162 
168  int read_eic (eic_type& eic);
169 
172  static bool eic_is_sht21 (const eic_type& eic)
173  {
174  return ((0x00 == eic[0]) && (0x80 == eic[1]));
175  }
176 
179  static bool eic_is_htu21d (const eic_type& eic)
180  {
181  return (('H' == eic[0]) && ('T' == eic[1]));
182  }
183 
192  {
193  return trigger_(false);
194  }
195 
204  {
205  return trigger_(true);
206  }
207 
212 
220  {
221  return fetch_(false);
222  }
223 
231  {
232  return fetch_(true);
233  }
234 
238  {
239  return observations_;
240  }
241 
242 protected:
243  int lpsm_process_ (int& delay,
244  process_flags_type& pf) override;
245 
246  int trigger_ (bool humidity);
247 
254  int fetch_ (bool humidity);
255 
256  iface_config_type& iface_config_;
257  observations_type observations_;
258 };
259 
260 } // ns sensor
261 } // ns nrfcxx
262 
263 #endif /* NRFCXX_SENSOR_SHT21_HPP */
nrfcxx::sensor::sht21::trigger_temperature
int trigger_temperature()
Initiate a temperature measurement.
Definition: sht21.hpp:191
nrfcxx::sensor::sht21::eic_is_sht21
static bool eic_is_sht21(const eic_type &eic)
Return true iff the eic indicates a Sensirion SHT21 device.
Definition: sht21.hpp:172
nrfcxx::sensor::sht21::eic_type
uint8_t[8] eic_type
Storage for the Electronic Identification Code.
Definition: sht21.hpp:35
nrfcxx::sensor::sht21::temperature_cK
int temperature_cK()
Retrieve the last measurement as a temperature measurement.
Definition: sht21.hpp:219
nrfcxx::sensor::sht21::fetch_
int fetch_(bool humidity)
Fetch the latest result as a raw value.
nrfcxx::sensor::sht21::iface_config
const iface_config_type & iface_config() const
Access the interface configuration for the sensor.
Definition: sht21.hpp:124
nrfcxx::sensor::sht21::observations_type::temperature_cK
int16_t temperature_cK
A recently calculated valid temperature_cK() or INVALID_OBSERVATION.
Definition: sht21.hpp:95
nrfcxx::sensor::sht21::iface_config_type
Information required to communicate with a sensor instance.
Definition: sht21.hpp:106
nrfcxx::periph::details::comm_error_support::ERR_ANACK
constexpr static unsigned int ERR_ANACK
NRF_TWI_Type::ERRORSRC bit indicating NACK received during address transmission.
Definition: periph.hpp:1402
nrfcxx::sensor::sht21::trigger_humidity
int trigger_humidity()
Initiate a temperature measurement.
Definition: sht21.hpp:203
nrfcxx::sensor::sht21::CONFIG_RES_H10T13
constexpr static uint8_t CONFIG_RES_H10T13
Bits for parameter to configure() specifying 10-bit humidity and 13-bit temperature measurements.
Definition: sht21.hpp:63
clock.hpp
Core clock-related functionality.
lpm.hpp
Material supporting low-power-mode operations.
nrfcxx::periph::TWI
Wrapper around the nRF51 TWI peripheral.
Definition: periph.hpp:1528
nrfcxx::sensor::sht21::read_eic
int read_eic(eic_type &eic)
Read the Electronic Identification Code from the device.
nrfcxx::sensor::sht21::SAMPLE12_DELAY_ms
constexpr static unsigned int SAMPLE12_DELAY_ms
The maximum time required for a 12-bit measurement, in milliseconds.
Definition: sht21.hpp:51
nrfcxx::sensor::sht21::SAMPLE14_DELAY_ms
constexpr static unsigned int SAMPLE14_DELAY_ms
The maximum time required for a 14-bit measurement, in milliseconds.
Definition: sht21.hpp:45
nrfcxx::sensor::sht21::CONFIG_EOB
constexpr static uint8_t CONFIG_EOB
Bit set in configure() result indicating Vdd is below 2.25V.
Definition: sht21.hpp:82
nrfcxx::sensor::sht21::observations_type::humidity_pptt
int16_t humidity_pptt
A recently calculated valid humidity_pptt() or INVALID_OBSERVATION.
Definition: sht21.hpp:99
nrfcxx::sensor::sht21::configure
int configure(int config)
Configure resolution and heater.
nrfcxx::sensor::sht21::CONFIG_RES_H11T11
constexpr static uint8_t CONFIG_RES_H11T11
Bits for parameter to configure() specifying for 11-bit humidity and 11-bit temperature measurements.
Definition: sht21.hpp:67
nrfcxx::sensor::sht21::RESET_DELAY_ms
constexpr static unsigned int RESET_DELAY_ms
The maximum time required by the sensor to stabilize after reset(), in milliseconds.
Definition: sht21.hpp:39
nrfcxx::sensor::sht21::reset
int reset()
Send a soft-reset command to the SHT21.
nrfcxx::sensor::sht21::NOT_READY
constexpr static int NOT_READY
The error code returned from temperature_cK() and humidity_pptt() when the device is still processing...
Definition: sht21.hpp:211
nrfcxx::sensor::sht21::sht21
sht21(notifier_type setter, iface_config_type &ifc)
Construct a sensor interface around a TWI device and events.
nrfcxx::notifier_type
std::function< void()> notifier_type
Type used to hold a notifier.
Definition: core.hpp:514
nrfcxx::periph::details::comm_error_support::error_encoded
constexpr static ssize_type error_encoded(error_type ec)
Pack an error value into a negative return value.
Definition: periph.hpp:1449
nrfcxx::sensor::sht21::eic_is_htu21d
static bool eic_is_htu21d(const eic_type &eic)
Return true iff the eic indicates a Measurement Specialties HTU21D device.
Definition: sht21.hpp:179
nrfcxx::lpm::lpsm_capable
Base (or mixin) class for anything that supports a state_machine.
Definition: lpm.hpp:426
nrfcxx::sensor::sht21::CONFIG_RES_Msk
constexpr static uint8_t CONFIG_RES_Msk
Mask to isolate the bits of the SHT21 user register that identify sampling resolution.
Definition: sht21.hpp:71
nrfcxx::sensor::sht21::humidity_pptt
int humidity_pptt()
Retrieve the last measurement as a humidity measurement.
Definition: sht21.hpp:230
nrfcxx::sensor::sht21::CONFIG_OTPRn
constexpr static uint8_t CONFIG_OTPRn
Bit set in configure() parameter/result to disable configuration reset on each measurement.
Definition: sht21.hpp:75
nrfcxx::sensor::sht21::INVALID_OBSERVATION
constexpr static int INVALID_OBSERVATION
Value for observations that are known to be invalid.
Definition: sht21.hpp:88
nrfcxx::sensor::sht21::CONFIG_RES_H8T12
constexpr static uint8_t CONFIG_RES_H8T12
Bits for parameter to configure() specifying 8-bit humidity and 12-bit temperature measurements.
Definition: sht21.hpp:59
nrfcxx::sensor::sht21::iface_config_type::address
uint8_t address
The I2C address used to communicate with the device.
Definition: sht21.hpp:115
nrfcxx::sensor::sht21::iface_config_type::twi
periph::TWI & twi
Reference to TWI device used to communicate with sensor.
Definition: sht21.hpp:109
nrfcxx::sensor::sht21::CONFIG_RES_H12T14
constexpr static uint8_t CONFIG_RES_H12T14
Bits for parameter to configure() specifying 12-bit humidity and 14-bit temperature measurements.
Definition: sht21.hpp:55
nrfcxx::sensor::sht21::observations_type
Storage for cached results.
Definition: sht21.hpp:91
nrfcxx::sensor::sht21::CONFIG_HEATER
constexpr static uint8_t CONFIG_HEATER
Bit set in configure() parameter/result to enable the on-chip heater.
Definition: sht21.hpp:79
periph.hpp
Abstraction of Nordic device peripherals.
nrfcxx
Primary namespace for nrfcxx functionality.
Definition: clock.hpp:17
nrfcxx::sensor::sht21::observations
const observations_type & observations() const
Retrieve the most recent observations calculated through the LPM infrastructure.
Definition: sht21.hpp:237
nrfcxx::sensor::sht21
Abstraction around SHT21/HTU21D temperature/humidity sensor.
Definition: sht21.hpp:30