libyggdrasil  v1.0.0
humidity_sensor.hpp
Go to the documentation of this file.
1  /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2  * _____.___. .___ .__.__ *
3  * \__ | | ____ ____ __| _/___________ _____|__| | *
4  * / | |/ ___\ / ___\ / __ |\_ __ \__ \ / ___/ | | *
5  * \____ / /_/ > /_/ > /_/ | | | \// __ \_\___ \| | |__ *
6  * / ______\___ /\___ /\____ | |__| (____ /____ >__|____/ *
7  * \/ /_____//_____/ \/ \/ \/ *
8  * - Yggdrasil - *
9  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
10  * This software can be used by students and other personal of the *
11  * Bern University of Applied Sciences under the terms of the MIT *
12  * license. *
13  * For other persons this software is under the terms of the GNU *
14  * General Public License version 2. *
15  * *
16  * Copyright © 2021, Bern University of Applied Sciences. *
17  * All rights reserved. *
18  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
26 #pragma once
27 
29 #include <cpp/common/types.hpp>
30 #include <cpp/common/utils.hpp>
31 
32 namespace bsp::ygg::prph {
33 
38  public:
39  HumiditySensor() = delete;
40 
45  enum class Heat : u8 {
46  _200mWFor1s = 0x39,
47  _200mWFor0p1s = 0x32,
48  _110mWFor1s = 0x2F,
49  _110mWFor0p1s = 0x24,
50  _20mWFor1s = 0x1E,
51  _20mWFor0p1s = 0x15,
52  };
53 
54  enum class Precision : u8 {
55  High = 0xFD,
56  Medium = 0xF6,
57  Low = 0xE0,
58  };
59 
66  static bool init() {
67  bsp::I2CA::write(DeviceAddress, enumValue(Command::SoftReset));
68  core::delay(2);
69  return true;
70  }
71 
79  static float getTemperature(Precision precision = Precision::High){
80  return getSensorData(precision).sensorTemperature;
81  }
82 
90  static float getHumidity(Precision precision = Precision::High){
91  return getSensorData(precision).humidity;
92  }
93 
100  static void enableHeater(Heat level) {
101  bsp::I2CA::write(DeviceAddress, enumValue(level));
102  }
103 
104  private:
105 
109  struct SensorData{
110  float humidity;
111  float sensorTemperature;
112  };
113 
121  static SensorData getSensorData(Precision precision = Precision::High) {
122  bsp::I2CA::write(DeviceAddress, enumValue(precision));
123  core::delay(10); // Delay until the measurement is done, there is no other option
124 
125  auto rawData = bsp::I2CA::read<SensorDataRaw>(DeviceAddress);
126 
127  SensorData data;
128  data.sensorTemperature = -45 + 175 * float((u16(rawData.th) << 8) | rawData.tl) / 0xFFFF;
129  data.humidity = -6 + 125 * float((u16(rawData.rhh) << 8) | rawData.rhl) / 0xFFFF;
130 
131  if (data.humidity > 100) data.humidity = 100;
132  else if(data.humidity < 0) data.humidity = 0;
133 
134  return data;
135 
136  }
137 
141  struct SensorDataRaw {
142  u8 th;
143  u8 tl;
144  u8 tcrc8;
145  u8 rhh;
146  u8 rhl;
147  u8 rhcrc8;
148  };
149 
153  enum class Command : u8 {
154  ReadSerial = 0x89,
155  SoftReset = 0x94,
156  };
157 
158  constexpr static inline u8 DeviceAddress = 0x88;
159 
160  };
161 
162 }
bsp::ygg::prph::HumiditySensor::Precision::Low
@ Low
measure T & RH with lowest precision (low repeatability)
bsp::ygg::prph::HumiditySensor::getTemperature
static float getTemperature(Precision precision=Precision::High)
Get the temperature without using the heater.
Definition: humidity_sensor.hpp:79
bsp::ygg::prph::HumiditySensor::Heat::_110mWFor0p1s
@ _110mWFor0p1s
110mW @ 3.3V for 0.1s
bsp::ygg::prph::HumiditySensor::Precision::Medium
@ Medium
measure T & RH with medium precision (medium repeatability)
bsp::ygg::prph::HumiditySensor::HumiditySensor
HumiditySensor()=delete
u16
uint16_t u16
Definition: types.h:37
utils.hpp
Commonly used helper functions.
u8
uint8_t u8
Unsigned integer definitions.
Definition: types.h:36
types.hpp
Commonly used type definitions and helper templates.
bsp::ygg::prph::HumiditySensor::getHumidity
static float getHumidity(Precision precision=Precision::High)
Get the temperature without using the heater.
Definition: humidity_sensor.hpp:90
bsp::ygg::prph::HumiditySensor::init
static bool init()
Initialization of the SHT40-AD1B-R2 relative humidity and temperature sensor.
Definition: humidity_sensor.hpp:66
bsp::ygg::prph::HumiditySensor::Heat::_20mWFor0p1s
@ _20mWFor0p1s
20mW @ 3.3V for 0.1s
bsp::enumValue
auto enumValue(T value)
Casts a scoped enum type into its underlying value.
Definition: utils.hpp:151
bsp::ygg::prph::HumiditySensor::Heat
Heat
Heater commands for the SHT40-AD1B-R2 sensor.
Definition: humidity_sensor.hpp:45
bsp::ygg::prph::HumiditySensor::Precision::High
@ High
measure T & RH with High precision (High repeatability)
bsp::ygg::prph::HumiditySensor::Precision
Precision
Definition: humidity_sensor.hpp:54
bsp::ygg::prph::HumiditySensor::Heat::_20mWFor1s
@ _20mWFor1s
20mW @ 3.3V for 1s
bsp::ygg::prph::HumiditySensor::Heat::_110mWFor1s
@ _110mWFor1s
110mW @ 3.3V for 1s
bsp::ygg::prph::HumiditySensor::enableHeater
static void enableHeater(Heat level)
Enable the heater module on the sensor.
Definition: humidity_sensor.hpp:100
attributes.hpp
Commonly used C++ and GNU attributes.
bsp::ygg::prph::HumiditySensor::Heat::_200mWFor1s
@ _200mWFor1s
200mW @ 3.3V for 1s
bsp::ygg::prph::HumiditySensor::Heat::_200mWFor0p1s
@ _200mWFor0p1s
200mW @ 3.3V for 0.1s
bsp::ygg::prph::HumiditySensor
Humidity sensor SHT40-AD1B-R2 driver.
Definition: humidity_sensor.hpp:37
bsp::core::delay
ALWAYS_INLINE void delay(u32 ms)
Delays execution by a certain number of milliseconds.
Definition: cortex.hpp:39
bsp::ygg::prph
Definition: color_sensor.hpp:32