From e375a0b11f72e45f2a43f353c14c40e508885db7 Mon Sep 17 00:00:00 2001 From: edwin Date: Tue, 6 Dec 2022 15:00:19 +0800 Subject: [PATCH 1/3] define an empty constructor and overload DHT::begin --- DHT.cpp | 40 ++++++++++++++++++++++++++++++++++++++++ DHT.h | 2 ++ 2 files changed, 42 insertions(+) diff --git a/DHT.cpp b/DHT.cpp index a48ac74..f8fa3d4 100644 --- a/DHT.cpp +++ b/DHT.cpp @@ -30,6 +30,24 @@ UINT32_MAX /**< Used programmatically for timeout. \ Not a timeout duration. Type: uint32_t. */ +/** + * @brief Instantiates a default DHT class + * + */ +DHT::DHT() { + _pin = 2; // pin 2 set as the default data pin + _type = DHT11; // using the DHT11 sensor as the default sensor type. + // Note that pin 2 and DHT11 are just default values to + // initialize members _pin and _type in the class definition. + // They can be modified via DHT::begin(uint8_t pin, uint8_t type, uint8_t usec). +#ifdef __AVR + _bit = digitalPinToBitMask(pin); + _port = digitalPinToPort(pin); +#endif + _maxcycles = microsecondsToClockCycles(1000); // 1 millisecond timeout for + // reading pulses from DHT sensor. +} + /*! * @brief Instantiates a new DHT class * @param pin @@ -72,6 +90,28 @@ void DHT::begin(uint8_t usec) { pullTime = usec; } +/** + * @brief Setup sensor pins and set pull timings + * + * @param pin + * pin number that sensor is connected + * @param type + * type of sensor + * @param usec + * Optionally pass pull-up time (in microseconds) before DHT reading + * starts. Default is 55 (see function declaration in DHT.h). + */ +void DHT::begin(uint8_t pin, uint8_t type, uint8_t usec) +{ + _pin = pin; + _type = type; +#ifdef __AVR + _bit = digitalPinToBitMask(pin); + _port = digitalPinToPort(pin); +#endif + begin(usec); +} + /*! * @brief Read temperature * @param S diff --git a/DHT.h b/DHT.h index 95570e1..5e3db94 100644 --- a/DHT.h +++ b/DHT.h @@ -62,8 +62,10 @@ static const uint8_t AM2301{21}; /**< AM2301 */ */ class DHT { public: + DHT(); DHT(uint8_t pin, uint8_t type, uint8_t count = 6); void begin(uint8_t usec = 55); + void begin(uint8_t pin, uint8_t type, uint8_t usec = 55); float readTemperature(bool S = false, bool force = false); float convertCtoF(float); float convertFtoC(float); From 97d11f3fd5fc2b3aa1157a9afb8b9f2920a89682 Mon Sep 17 00:00:00 2001 From: edwin Date: Tue, 6 Dec 2022 15:21:41 +0800 Subject: [PATCH 2/3] define an empty constructor and overload DHT::begin --- DHT.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/DHT.cpp b/DHT.cpp index f8fa3d4..730d6ee 100644 --- a/DHT.cpp +++ b/DHT.cpp @@ -41,8 +41,8 @@ DHT::DHT() { // initialize members _pin and _type in the class definition. // They can be modified via DHT::begin(uint8_t pin, uint8_t type, uint8_t usec). #ifdef __AVR - _bit = digitalPinToBitMask(pin); - _port = digitalPinToPort(pin); + _bit = digitalPinToBitMask(_pin); + _port = digitalPinToPort(_pin); #endif _maxcycles = microsecondsToClockCycles(1000); // 1 millisecond timeout for // reading pulses from DHT sensor. From 3f20e8520200243e3fe7eae6af5780ae2008c135 Mon Sep 17 00:00:00 2001 From: edWin-m Date: Tue, 6 Dec 2022 18:06:23 +0800 Subject: [PATCH 3/3] format check with clang --- DHT.cpp | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/DHT.cpp b/DHT.cpp index 730d6ee..089f9ba 100644 --- a/DHT.cpp +++ b/DHT.cpp @@ -32,20 +32,21 @@ /** * @brief Instantiates a default DHT class - * + * */ DHT::DHT() { - _pin = 2; // pin 2 set as the default data pin + _pin = 2; // pin 2 set as the default data pin _type = DHT11; // using the DHT11 sensor as the default sensor type. - // Note that pin 2 and DHT11 are just default values to - // initialize members _pin and _type in the class definition. - // They can be modified via DHT::begin(uint8_t pin, uint8_t type, uint8_t usec). + // Note that pin 2 and DHT11 are just default values to initialize members + // _pin and _type in the class definition. They can be modified via + // DHT::begin(uint8_t pin, uint8_t type, uint8_t usec). #ifdef __AVR _bit = digitalPinToBitMask(_pin); _port = digitalPinToPort(_pin); #endif - _maxcycles = microsecondsToClockCycles(1000); // 1 millisecond timeout for - // reading pulses from DHT sensor. + _maxcycles = + microsecondsToClockCycles(1000); // 1 millisecond timeout for + // reading pulses from DHT sensor. } /*! @@ -92,17 +93,16 @@ void DHT::begin(uint8_t usec) { /** * @brief Setup sensor pins and set pull timings - * - * @param pin + * + * @param pin * pin number that sensor is connected - * @param type + * @param type * type of sensor - * @param usec - * Optionally pass pull-up time (in microseconds) before DHT reading + * @param usec + * Optionally pass pull-up time (in microseconds) before DHT reading * starts. Default is 55 (see function declaration in DHT.h). */ -void DHT::begin(uint8_t pin, uint8_t type, uint8_t usec) -{ +void DHT::begin(uint8_t pin, uint8_t type, uint8_t usec) { _pin = pin; _type = type; #ifdef __AVR