|
| 1 | +// DHT Temperature & Humidity Unified Sensor Library |
| 2 | +// Copyright (c) 2014 Adafruit Industries |
| 3 | +// Author: Tony DiCola |
| 4 | + |
| 5 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | +// of this software and associated documentation files (the "Software"), to deal |
| 7 | +// in the Software without restriction, including without limitation the rights |
| 8 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | +// copies of the Software, and to permit persons to whom the Software is |
| 10 | +// furnished to do so, subject to the following conditions: |
| 11 | + |
| 12 | +// The above copyright notice and this permission notice shall be included in all |
| 13 | +// copies or substantial portions of the Software. |
| 14 | + |
| 15 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 21 | +// SOFTWARE. |
| 22 | +#include "DHT_U.h" |
| 23 | + |
| 24 | +DHT_Unified::DHT_Unified(uint8_t pin, uint8_t type, uint8_t count, int32_t tempSensorId, int32_t humiditySensorId): |
| 25 | + _dht(pin, type, count), |
| 26 | + _type(type), |
| 27 | + _temp(this, tempSensorId), |
| 28 | + _humidity(this, humiditySensorId) |
| 29 | +{} |
| 30 | + |
| 31 | +void DHT_Unified::begin() { |
| 32 | + _dht.begin(); |
| 33 | +} |
| 34 | + |
| 35 | +void DHT_Unified::setName(sensor_t* sensor) { |
| 36 | + switch(_type) { |
| 37 | + case DHT11: |
| 38 | + strncpy(sensor->name, "DHT11", sizeof(sensor->name) - 1); |
| 39 | + break; |
| 40 | + case DHT21: |
| 41 | + strncpy(sensor->name, "DHT21", sizeof(sensor->name) - 1); |
| 42 | + break; |
| 43 | + case DHT22: |
| 44 | + strncpy(sensor->name, "DHT22", sizeof(sensor->name) - 1); |
| 45 | + break; |
| 46 | + default: |
| 47 | + // TODO: Perhaps this should be an error? However main DHT library doesn't enforce |
| 48 | + // restrictions on the sensor type value. Pick a generic name for now. |
| 49 | + strncpy(sensor->name, "DHT?", sizeof(sensor->name) - 1); |
| 50 | + break; |
| 51 | + } |
| 52 | + sensor->name[sizeof(sensor->name)- 1] = 0; |
| 53 | +} |
| 54 | + |
| 55 | +void DHT_Unified::setMinDelay(sensor_t* sensor) { |
| 56 | + switch(_type) { |
| 57 | + case DHT11: |
| 58 | + sensor->min_delay = 1000000L; // 1 second (in microseconds) |
| 59 | + break; |
| 60 | + case DHT21: |
| 61 | + sensor->min_delay = 2000000L; // 2 seconds (in microseconds) |
| 62 | + break; |
| 63 | + case DHT22: |
| 64 | + sensor->min_delay = 2000000L; // 2 seconds (in microseconds) |
| 65 | + break; |
| 66 | + default: |
| 67 | + // Default to slowest sample rate in case of unknown type. |
| 68 | + sensor->min_delay = 2000000L; // 2 seconds (in microseconds) |
| 69 | + break; |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +DHT_Unified::Temperature::Temperature(DHT_Unified* parent, int32_t id): |
| 74 | + _parent(parent), |
| 75 | + _id(id) |
| 76 | +{} |
| 77 | + |
| 78 | +bool DHT_Unified::Temperature::getEvent(sensors_event_t* event) { |
| 79 | + // Clear event definition. |
| 80 | + memset(event, 0, sizeof(sensors_event_t)); |
| 81 | + // Populate sensor reading values. |
| 82 | + event->version = sizeof(sensors_event_t); |
| 83 | + event->sensor_id = _id; |
| 84 | + event->type = SENSOR_TYPE_AMBIENT_TEMPERATURE; |
| 85 | + event->timestamp = millis(); |
| 86 | + event->temperature = _parent->_dht.readTemperature(); |
| 87 | + |
| 88 | + return true; |
| 89 | +} |
| 90 | + |
| 91 | +void DHT_Unified::Temperature::getSensor(sensor_t* sensor) { |
| 92 | + // Clear sensor definition. |
| 93 | + memset(sensor, 0, sizeof(sensor_t)); |
| 94 | + // Set sensor name. |
| 95 | + _parent->setName(sensor); |
| 96 | + // Set version and ID |
| 97 | + sensor->version = DHT_SENSOR_VERSION; |
| 98 | + sensor->sensor_id = _id; |
| 99 | + // Set type and characteristics. |
| 100 | + sensor->type = SENSOR_TYPE_AMBIENT_TEMPERATURE; |
| 101 | + _parent->setMinDelay(sensor); |
| 102 | + switch (_parent->_type) { |
| 103 | + case DHT11: |
| 104 | + sensor->max_value = 50.0F; |
| 105 | + sensor->min_value = 0.0F; |
| 106 | + sensor->resolution = 2.0F; |
| 107 | + break; |
| 108 | + case DHT21: |
| 109 | + sensor->max_value = 80.0F; |
| 110 | + sensor->min_value = -40.0F; |
| 111 | + sensor->resolution = 0.1F; |
| 112 | + break; |
| 113 | + case DHT22: |
| 114 | + sensor->max_value = 125.0F; |
| 115 | + sensor->min_value = -40.0F; |
| 116 | + sensor->resolution = 0.1F; |
| 117 | + break; |
| 118 | + default: |
| 119 | + // Unknown type, default to 0. |
| 120 | + sensor->max_value = 0.0F; |
| 121 | + sensor->min_value = 0.0F; |
| 122 | + sensor->resolution = 0.0F; |
| 123 | + break; |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | +DHT_Unified::Humidity::Humidity(DHT_Unified* parent, int32_t id): |
| 128 | + _parent(parent), |
| 129 | + _id(id) |
| 130 | +{} |
| 131 | + |
| 132 | +bool DHT_Unified::Humidity::getEvent(sensors_event_t* event) { |
| 133 | + // Clear event definition. |
| 134 | + memset(event, 0, sizeof(sensors_event_t)); |
| 135 | + // Populate sensor reading values. |
| 136 | + event->version = sizeof(sensors_event_t); |
| 137 | + event->sensor_id = _id; |
| 138 | + event->type = SENSOR_TYPE_RELATIVE_HUMIDITY; |
| 139 | + event->timestamp = millis(); |
| 140 | + event->relative_humidity = _parent->_dht.readHumidity(); |
| 141 | + |
| 142 | + return true; |
| 143 | +} |
| 144 | + |
| 145 | +void DHT_Unified::Humidity::getSensor(sensor_t* sensor) { |
| 146 | + // Clear sensor definition. |
| 147 | + memset(sensor, 0, sizeof(sensor_t)); |
| 148 | + // Set sensor name. |
| 149 | + _parent->setName(sensor); |
| 150 | + // Set version and ID |
| 151 | + sensor->version = DHT_SENSOR_VERSION; |
| 152 | + sensor->sensor_id = _id; |
| 153 | + // Set type and characteristics. |
| 154 | + sensor->type = SENSOR_TYPE_RELATIVE_HUMIDITY; |
| 155 | + _parent->setMinDelay(sensor); |
| 156 | + switch (_parent->_type) { |
| 157 | + case DHT11: |
| 158 | + sensor->max_value = 80.0F; |
| 159 | + sensor->min_value = 20.0F; |
| 160 | + sensor->resolution = 5.0F; |
| 161 | + break; |
| 162 | + case DHT21: |
| 163 | + sensor->max_value = 100.0F; |
| 164 | + sensor->min_value = 0.0F; |
| 165 | + sensor->resolution = 0.1F; |
| 166 | + break; |
| 167 | + case DHT22: |
| 168 | + sensor->max_value = 100.0F; |
| 169 | + sensor->min_value = 0.0F; |
| 170 | + sensor->resolution = 0.1F; |
| 171 | + break; |
| 172 | + default: |
| 173 | + // Unknown type, default to 0. |
| 174 | + sensor->max_value = 0.0F; |
| 175 | + sensor->min_value = 0.0F; |
| 176 | + sensor->resolution = 0.0F; |
| 177 | + break; |
| 178 | + } |
| 179 | +} |
0 commit comments