arduinoprojects

git clone https://git.tarina.org/arduinoprojects
Log | Files | Refs

DHT.h (3146B)


      1 /*!
      2  *  @file DHT.h
      3  *
      4  *  This is a library for DHT series of low cost temperature/humidity sensors.
      5  *
      6  *  You must have Adafruit Unified Sensor Library library installed to use this
      7  * class.
      8  *
      9  *  Adafruit invests time and resources providing this open source code,
     10  *  please support Adafruit andopen-source hardware by purchasing products
     11  *  from Adafruit!
     12  *
     13  *  Written by Adafruit Industries.
     14  *
     15  *  MIT license, all text above must be included in any redistribution
     16  */
     17 
     18 #ifndef DHT_H
     19 #define DHT_H
     20 
     21 #include "Arduino.h"
     22 
     23 /* Uncomment to enable printing out nice debug messages. */
     24 //#define DHT_DEBUG
     25 
     26 #define DEBUG_PRINTER                                                          \
     27   Serial /**< Define where debug output will be printed.                       \
     28           */
     29 
     30 /* Setup debug printing macros. */
     31 #ifdef DHT_DEBUG
     32 #define DEBUG_PRINT(...)                                                       \
     33   { DEBUG_PRINTER.print(__VA_ARGS__); }
     34 #define DEBUG_PRINTLN(...)                                                     \
     35   { DEBUG_PRINTER.println(__VA_ARGS__); }
     36 #else
     37 #define DEBUG_PRINT(...)                                                       \
     38   {} /**< Debug Print Placeholder if Debug is disabled */
     39 #define DEBUG_PRINTLN(...)                                                     \
     40   {} /**< Debug Print Line Placeholder if Debug is disabled */
     41 #endif
     42 
     43 /* Define types of sensors. */
     44 #define DHT11 11  /**< DHT TYPE 11 */
     45 #define DHT12 12  /**< DHY TYPE 12 */
     46 #define DHT22 22  /**< DHT TYPE 22 */
     47 #define DHT21 21  /**< DHT TYPE 21 */
     48 #define AM2301 21 /**< AM2301 */
     49 
     50 #if defined(TARGET_NAME) && (TARGET_NAME == ARDUINO_NANO33BLE)
     51 #ifndef microsecondsToClockCycles
     52 /*!
     53  * As of 7 Sep 2020 the Arduino Nano 33 BLE boards do not have
     54  * microsecondsToClockCycles defined.
     55  */
     56 #define microsecondsToClockCycles(a) ((a) * (SystemCoreClock / 1000000L))
     57 #endif
     58 #endif
     59 
     60 /*!
     61  *  @brief  Class that stores state and functions for DHT
     62  */
     63 class DHT {
     64 public:
     65   DHT(uint8_t pin, uint8_t type, uint8_t count = 6);
     66   void begin(uint8_t usec = 55);
     67   float readTemperature(bool S = false, bool force = false);
     68   float convertCtoF(float);
     69   float convertFtoC(float);
     70   float computeHeatIndex(bool isFahrenheit = true);
     71   float computeHeatIndex(float temperature, float percentHumidity,
     72                          bool isFahrenheit = true);
     73   float readHumidity(bool force = false);
     74   bool read(bool force = false);
     75 
     76 private:
     77   uint8_t data[5];
     78   uint8_t _pin, _type;
     79 #ifdef __AVR
     80   // Use direct GPIO access on an 8-bit AVR so keep track of the port and
     81   // bitmask for the digital pin connected to the DHT.  Other platforms will use
     82   // digitalRead.
     83   uint8_t _bit, _port;
     84 #endif
     85   uint32_t _lastreadtime, _maxcycles;
     86   bool _lastresult;
     87   uint8_t pullTime; // Time (in usec) to pull up data line before reading
     88 
     89   uint32_t expectPulse(bool level);
     90 };
     91 
     92 /*!
     93  *  @brief  Class that defines Interrupt Lock Avaiability
     94  */
     95 class InterruptLock {
     96 public:
     97   InterruptLock() {
     98 #if !defined(ARDUINO_ARCH_NRF52)
     99     noInterrupts();
    100 #endif
    101   }
    102   ~InterruptLock() {
    103 #if !defined(ARDUINO_ARCH_NRF52)
    104     interrupts();
    105 #endif
    106   }
    107 };
    108 
    109 #endif