mirror of
https://github.com/adafruit/DHT-sensor-library.git
synced 2023-10-23 22:20:38 +03:00
86115884c7
A preprocessor conditional directive intended to only evaluate as true when compiling for the Arduino Nano 33 BLE board was also evaluating as true when compiling for any board that didn't define the compared identifiers because undefined identifiers are replaced with 0 by the C/C++ preprocessor, making them equal. From the C++11 standard section 16.1, paragraph 4: > After all replacements due to macro expansion and the defined unary operator have been performed, all remaining identifiers and keywords 148 , except for true and false, are replaced with the pp-number 0 The fix is to modify the directive to check whether the macro has been defined.
110 lines
3.1 KiB
C++
110 lines
3.1 KiB
C++
/*!
|
|
* @file DHT.h
|
|
*
|
|
* This is a library for DHT series of low cost temperature/humidity sensors.
|
|
*
|
|
* You must have Adafruit Unified Sensor Library library installed to use this
|
|
* class.
|
|
*
|
|
* Adafruit invests time and resources providing this open source code,
|
|
* please support Adafruit andopen-source hardware by purchasing products
|
|
* from Adafruit!
|
|
*
|
|
* Written by Adafruit Industries.
|
|
*
|
|
* MIT license, all text above must be included in any redistribution
|
|
*/
|
|
|
|
#ifndef DHT_H
|
|
#define DHT_H
|
|
|
|
#include "Arduino.h"
|
|
|
|
/* Uncomment to enable printing out nice debug messages. */
|
|
//#define DHT_DEBUG
|
|
|
|
#define DEBUG_PRINTER \
|
|
Serial /**< Define where debug output will be printed. \
|
|
*/
|
|
|
|
/* Setup debug printing macros. */
|
|
#ifdef DHT_DEBUG
|
|
#define DEBUG_PRINT(...) \
|
|
{ DEBUG_PRINTER.print(__VA_ARGS__); }
|
|
#define DEBUG_PRINTLN(...) \
|
|
{ DEBUG_PRINTER.println(__VA_ARGS__); }
|
|
#else
|
|
#define DEBUG_PRINT(...) \
|
|
{} /**< Debug Print Placeholder if Debug is disabled */
|
|
#define DEBUG_PRINTLN(...) \
|
|
{} /**< Debug Print Line Placeholder if Debug is disabled */
|
|
#endif
|
|
|
|
/* Define types of sensors. */
|
|
#define DHT11 11 /**< DHT TYPE 11 */
|
|
#define DHT12 12 /**< DHY TYPE 12 */
|
|
#define DHT22 22 /**< DHT TYPE 22 */
|
|
#define DHT21 21 /**< DHT TYPE 21 */
|
|
#define AM2301 21 /**< AM2301 */
|
|
|
|
#if defined(TARGET_NAME) && (TARGET_NAME == ARDUINO_NANO33BLE)
|
|
#ifndef microsecondsToClockCycles
|
|
/*!
|
|
* As of 7 Sep 2020 the Arduino Nano 33 BLE boards do not have
|
|
* microsecondsToClockCycles defined.
|
|
*/
|
|
#define microsecondsToClockCycles(a) ((a) * (SystemCoreClock / 1000000L))
|
|
#endif
|
|
#endif
|
|
|
|
/*!
|
|
* @brief Class that stores state and functions for DHT
|
|
*/
|
|
class DHT {
|
|
public:
|
|
DHT(uint8_t pin, uint8_t type, uint8_t count = 6);
|
|
void begin(uint8_t usec = 55);
|
|
float readTemperature(bool S = false, bool force = false);
|
|
float convertCtoF(float);
|
|
float convertFtoC(float);
|
|
float computeHeatIndex(bool isFahrenheit = true);
|
|
float computeHeatIndex(float temperature, float percentHumidity,
|
|
bool isFahrenheit = true);
|
|
float readHumidity(bool force = false);
|
|
bool read(bool force = false);
|
|
|
|
private:
|
|
uint8_t data[5];
|
|
uint8_t _pin, _type;
|
|
#ifdef __AVR
|
|
// Use direct GPIO access on an 8-bit AVR so keep track of the port and
|
|
// bitmask for the digital pin connected to the DHT. Other platforms will use
|
|
// digitalRead.
|
|
uint8_t _bit, _port;
|
|
#endif
|
|
uint32_t _lastreadtime, _maxcycles;
|
|
bool _lastresult;
|
|
uint8_t pullTime; // Time (in usec) to pull up data line before reading
|
|
|
|
uint32_t expectPulse(bool level);
|
|
};
|
|
|
|
/*!
|
|
* @brief Class that defines Interrupt Lock Avaiability
|
|
*/
|
|
class InterruptLock {
|
|
public:
|
|
InterruptLock() {
|
|
#if !defined(ARDUINO_ARCH_NRF52)
|
|
noInterrupts();
|
|
#endif
|
|
}
|
|
~InterruptLock() {
|
|
#if !defined(ARDUINO_ARCH_NRF52)
|
|
interrupts();
|
|
#endif
|
|
}
|
|
};
|
|
|
|
#endif
|