From 99e90c269869b4320006d2274a16df368035a83f Mon Sep 17 00:00:00 2001 From: piclaw Date: Thu, 5 Feb 2026 09:32:02 -0500 Subject: [PATCH] Fix clang-format CI: add .clang-format, update workflow, format code --- .clang-format | 13 +++ .github/workflows/githubci.yml | 20 ++++ DHT.cpp | 108 +++++++++--------- DHT.h | 34 +++--- DHT_U.cpp | 196 +++++++++++++++++---------------- DHT_U.h | 40 ++++--- 6 files changed, 231 insertions(+), 180 deletions(-) create mode 100644 .clang-format diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..63a90a9 --- /dev/null +++ b/.clang-format @@ -0,0 +1,13 @@ +Language: Cpp +BasedOnStyle: Google +IndentWidth: 2 +ColumnLimit: 80 +AllowShortFunctionsOnASingleLine: Empty +AllowShortIfStatementsOnASingleLine: false +AllowShortLoopsOnASingleLine: false +BinPackArguments: true +BinPackParameters: true +BreakBeforeBraces: Attach +DerivePointerAlignment: false +PointerAlignment: Left +SpacesBeforeTrailingComments: 1 diff --git a/.github/workflows/githubci.yml b/.github/workflows/githubci.yml index ee3fd16..beb1893 100644 --- a/.github/workflows/githubci.yml +++ b/.github/workflows/githubci.yml @@ -3,8 +3,28 @@ name: Arduino Library CI on: [pull_request, push, repository_dispatch] jobs: + clang-format: + runs-on: ubuntu-latest + + steps: + - uses: actions/setup-python@v4 + with: + python-version: '3.x' + - uses: actions/checkout@v3 + - uses: actions/checkout@v3 + with: + repository: adafruit/ci-arduino + path: ci + + - name: pre-install + run: bash ci/actions_install.sh + + - name: clang + run: python3 ci/run-clang-format.py -e "ci/*" -e "bin/*" -r . + build: runs-on: ubuntu-latest + needs: clang-format steps: - uses: actions/setup-python@v4 diff --git a/DHT.cpp b/DHT.cpp index a48ac74..ba3b434 100644 --- a/DHT.cpp +++ b/DHT.cpp @@ -26,8 +26,8 @@ #include "DHT.h" #define MIN_INTERVAL 2000 /**< min interval value */ -#define TIMEOUT \ - UINT32_MAX /**< Used programmatically for timeout. \ +#define TIMEOUT \ + UINT32_MAX /**< Used programmatically for timeout. \ Not a timeout duration. Type: uint32_t. */ /*! @@ -87,37 +87,37 @@ float DHT::readTemperature(bool S, bool force) { if (read(force)) { switch (_type) { - case DHT11: - f = data[2]; - if (data[3] & 0x80) { - f = -1 - f; - } - f += (data[3] & 0x0f) * 0.1; - if (S) { - f = convertCtoF(f); - } - break; - case DHT12: - f = data[2]; - f += (data[3] & 0x0f) * 0.1; - if (data[2] & 0x80) { - f *= -1; - } - if (S) { - f = convertCtoF(f); - } - break; - case DHT22: - case DHT21: - f = ((word)(data[2] & 0x7F)) << 8 | data[3]; - f *= 0.1; - if (data[2] & 0x80) { - f *= -1; - } - if (S) { - f = convertCtoF(f); - } - break; + case DHT11: + f = data[2]; + if (data[3] & 0x80) { + f = -1 - f; + } + f += (data[3] & 0x0f) * 0.1; + if (S) { + f = convertCtoF(f); + } + break; + case DHT12: + f = data[2]; + f += (data[3] & 0x0f) * 0.1; + if (data[2] & 0x80) { + f *= -1; + } + if (S) { + f = convertCtoF(f); + } + break; + case DHT22: + case DHT21: + f = ((word)(data[2] & 0x7F)) << 8 | data[3]; + f *= 0.1; + if (data[2] & 0x80) { + f *= -1; + } + if (S) { + f = convertCtoF(f); + } + break; } } return f; @@ -129,7 +129,9 @@ float DHT::readTemperature(bool S, bool force) { * value in Celcius * @return float value in Fahrenheit */ -float DHT::convertCtoF(float c) { return c * 1.8 + 32; } +float DHT::convertCtoF(float c) { + return c * 1.8 + 32; +} /*! * @brief Converts Fahrenheit to Celcius @@ -137,7 +139,9 @@ float DHT::convertCtoF(float c) { return c * 1.8 + 32; } * value in Fahrenheit * @return float value in Celcius */ -float DHT::convertFtoC(float f) { return (f - 32) * 0.55555; } +float DHT::convertFtoC(float f) { + return (f - 32) * 0.55555; +} /*! * @brief Read Humidity @@ -149,15 +153,15 @@ float DHT::readHumidity(bool force) { float f = NAN; if (read(force)) { switch (_type) { - case DHT11: - case DHT12: - f = data[0] + data[1] * 0.1; - break; - case DHT22: - case DHT21: - f = ((word)data[0]) << 8 | data[1]; - f *= 0.1; - break; + case DHT11: + case DHT12: + f = data[0] + data[1] * 0.1; + break; + case DHT22: + case DHT21: + f = ((word)data[0]) << 8 | data[1]; + f *= 0.1; + break; } } return f; @@ -256,14 +260,14 @@ bool DHT::read(bool force) { pinMode(_pin, OUTPUT); digitalWrite(_pin, LOW); switch (_type) { - case DHT22: - case DHT21: - delayMicroseconds(1100); // data sheet says "at least 1ms" - break; - case DHT11: - default: - delay(20); // data sheet says at least 18ms, 20ms just to be safe - break; + case DHT22: + case DHT21: + delayMicroseconds(1100); // data sheet says "at least 1ms" + break; + case DHT11: + default: + delay(20); // data sheet says at least 18ms, 20ms just to be safe + break; } uint32_t cycles[80]; diff --git a/DHT.h b/DHT.h index 95570e1..c339469 100644 --- a/DHT.h +++ b/DHT.h @@ -21,23 +21,29 @@ #include "Arduino.h" /* Uncomment to enable printing out nice debug messages. */ -//#define DHT_DEBUG +// #define DHT_DEBUG -#define DEBUG_PRINTER \ - Serial /**< Define where debug output will be printed. \ +#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__); } +#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 */ +#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. */ @@ -61,7 +67,7 @@ static const uint8_t AM2301{21}; /**< AM2301 */ * @brief Class that stores state and functions for DHT */ class DHT { -public: + 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); @@ -73,7 +79,7 @@ public: float readHumidity(bool force = false); bool read(bool force = false); -private: + private: uint8_t data[5]; uint8_t _pin, _type; #ifdef __AVR @@ -93,7 +99,7 @@ private: * @brief Class that defines Interrupt Lock Avaiability */ class InterruptLock { -public: + public: InterruptLock() { #if !defined(ARDUINO_ARCH_NRF52) noInterrupts(); diff --git a/DHT_U.cpp b/DHT_U.cpp index 4182ed9..ab8852a 100644 --- a/DHT_U.cpp +++ b/DHT_U.cpp @@ -29,39 +29,43 @@ */ DHT_Unified::DHT_Unified(uint8_t pin, uint8_t type, uint8_t count, int32_t tempSensorId, int32_t humiditySensorId) - : _dht(pin, type, count), _type(type), _temp(this, tempSensorId), + : _dht(pin, type, count), + _type(type), + _temp(this, tempSensorId), _humidity(this, humiditySensorId) {} /*! * @brief Setup sensor (calls begin on It) */ -void DHT_Unified::begin() { _dht.begin(); } +void DHT_Unified::begin() { + _dht.begin(); +} /*! * @brief Sets sensor name * @param sensor * Sensor that will be set */ -void DHT_Unified::setName(sensor_t *sensor) { +void DHT_Unified::setName(sensor_t* sensor) { switch (_type) { - case DHT11: - strncpy(sensor->name, "DHT11", sizeof(sensor->name) - 1); - break; - case DHT12: - strncpy(sensor->name, "DHT12", sizeof(sensor->name) - 1); - break; - case DHT21: - strncpy(sensor->name, "DHT21", sizeof(sensor->name) - 1); - break; - case DHT22: - strncpy(sensor->name, "DHT22", sizeof(sensor->name) - 1); - break; - default: - // TODO: Perhaps this should be an error? However main DHT library doesn't - // enforce restrictions on the sensor type value. Pick a generic name for - // now. - strncpy(sensor->name, "DHT?", sizeof(sensor->name) - 1); - break; + case DHT11: + strncpy(sensor->name, "DHT11", sizeof(sensor->name) - 1); + break; + case DHT12: + strncpy(sensor->name, "DHT12", sizeof(sensor->name) - 1); + break; + case DHT21: + strncpy(sensor->name, "DHT21", sizeof(sensor->name) - 1); + break; + case DHT22: + strncpy(sensor->name, "DHT22", sizeof(sensor->name) - 1); + break; + default: + // TODO: Perhaps this should be an error? However main DHT library + // doesn't enforce restrictions on the sensor type value. Pick a generic + // name for now. + strncpy(sensor->name, "DHT?", sizeof(sensor->name) - 1); + break; } sensor->name[sizeof(sensor->name) - 1] = 0; } @@ -71,24 +75,24 @@ void DHT_Unified::setName(sensor_t *sensor) { * @param sensor * Sensor that will be set */ -void DHT_Unified::setMinDelay(sensor_t *sensor) { +void DHT_Unified::setMinDelay(sensor_t* sensor) { switch (_type) { - case DHT11: - sensor->min_delay = 1000000L; // 1 second (in microseconds) - break; - case DHT12: - sensor->min_delay = 2000000L; // 2 second (in microseconds) - break; - case DHT21: - sensor->min_delay = 2000000L; // 2 seconds (in microseconds) - break; - case DHT22: - sensor->min_delay = 2000000L; // 2 seconds (in microseconds) - break; - default: - // Default to slowest sample rate in case of unknown type. - sensor->min_delay = 2000000L; // 2 seconds (in microseconds) - break; + case DHT11: + sensor->min_delay = 1000000L; // 1 second (in microseconds) + break; + case DHT12: + sensor->min_delay = 2000000L; // 2 second (in microseconds) + break; + case DHT21: + sensor->min_delay = 2000000L; // 2 seconds (in microseconds) + break; + case DHT22: + sensor->min_delay = 2000000L; // 2 seconds (in microseconds) + break; + default: + // Default to slowest sample rate in case of unknown type. + sensor->min_delay = 2000000L; // 2 seconds (in microseconds) + break; } } @@ -99,7 +103,7 @@ void DHT_Unified::setMinDelay(sensor_t *sensor) { * @param id * Sensor id */ -DHT_Unified::Temperature::Temperature(DHT_Unified *parent, int32_t id) +DHT_Unified::Temperature::Temperature(DHT_Unified* parent, int32_t id) : _parent(parent), _id(id) {} /*! @@ -107,7 +111,7 @@ DHT_Unified::Temperature::Temperature(DHT_Unified *parent, int32_t id) * @param event * @return always returns true */ -bool DHT_Unified::Temperature::getEvent(sensors_event_t *event) { +bool DHT_Unified::Temperature::getEvent(sensors_event_t* event) { // Clear event definition. memset(event, 0, sizeof(sensors_event_t)); // Populate sensor reading values. @@ -124,7 +128,7 @@ bool DHT_Unified::Temperature::getEvent(sensors_event_t *event) { * @brief Provides the sensor_t data for this sensor * @param sensor */ -void DHT_Unified::Temperature::getSensor(sensor_t *sensor) { +void DHT_Unified::Temperature::getSensor(sensor_t* sensor) { // Clear sensor definition. memset(sensor, 0, sizeof(sensor_t)); // Set sensor name. @@ -136,32 +140,32 @@ void DHT_Unified::Temperature::getSensor(sensor_t *sensor) { sensor->type = SENSOR_TYPE_AMBIENT_TEMPERATURE; _parent->setMinDelay(sensor); switch (_parent->_type) { - case DHT11: - sensor->max_value = 50.0F; - sensor->min_value = 0.0F; - sensor->resolution = 2.0F; - break; - case DHT12: - sensor->max_value = 60.0F; - sensor->min_value = -20.0F; - sensor->resolution = 0.5F; - break; - case DHT21: - sensor->max_value = 80.0F; - sensor->min_value = -40.0F; - sensor->resolution = 0.1F; - break; - case DHT22: - sensor->max_value = 125.0F; - sensor->min_value = -40.0F; - sensor->resolution = 0.1F; - break; - default: - // Unknown type, default to 0. - sensor->max_value = 0.0F; - sensor->min_value = 0.0F; - sensor->resolution = 0.0F; - break; + case DHT11: + sensor->max_value = 50.0F; + sensor->min_value = 0.0F; + sensor->resolution = 2.0F; + break; + case DHT12: + sensor->max_value = 60.0F; + sensor->min_value = -20.0F; + sensor->resolution = 0.5F; + break; + case DHT21: + sensor->max_value = 80.0F; + sensor->min_value = -40.0F; + sensor->resolution = 0.1F; + break; + case DHT22: + sensor->max_value = 125.0F; + sensor->min_value = -40.0F; + sensor->resolution = 0.1F; + break; + default: + // Unknown type, default to 0. + sensor->max_value = 0.0F; + sensor->min_value = 0.0F; + sensor->resolution = 0.0F; + break; } } @@ -172,7 +176,7 @@ void DHT_Unified::Temperature::getSensor(sensor_t *sensor) { * @param id * Sensor id */ -DHT_Unified::Humidity::Humidity(DHT_Unified *parent, int32_t id) +DHT_Unified::Humidity::Humidity(DHT_Unified* parent, int32_t id) : _parent(parent), _id(id) {} /*! @@ -180,7 +184,7 @@ DHT_Unified::Humidity::Humidity(DHT_Unified *parent, int32_t id) * @param event * @return always returns true */ -bool DHT_Unified::Humidity::getEvent(sensors_event_t *event) { +bool DHT_Unified::Humidity::getEvent(sensors_event_t* event) { // Clear event definition. memset(event, 0, sizeof(sensors_event_t)); // Populate sensor reading values. @@ -197,7 +201,7 @@ bool DHT_Unified::Humidity::getEvent(sensors_event_t *event) { * @brief Provides the sensor_t data for this sensor * @param sensor */ -void DHT_Unified::Humidity::getSensor(sensor_t *sensor) { +void DHT_Unified::Humidity::getSensor(sensor_t* sensor) { // Clear sensor definition. memset(sensor, 0, sizeof(sensor_t)); // Set sensor name. @@ -209,31 +213,31 @@ void DHT_Unified::Humidity::getSensor(sensor_t *sensor) { sensor->type = SENSOR_TYPE_RELATIVE_HUMIDITY; _parent->setMinDelay(sensor); switch (_parent->_type) { - case DHT11: - sensor->max_value = 80.0F; - sensor->min_value = 20.0F; - sensor->resolution = 5.0F; - break; - case DHT12: - sensor->max_value = 95.0F; - sensor->min_value = 20.0F; - sensor->resolution = 5.0F; - break; - case DHT21: - sensor->max_value = 100.0F; - sensor->min_value = 0.0F; - sensor->resolution = 0.1F; - break; - case DHT22: - sensor->max_value = 100.0F; - sensor->min_value = 0.0F; - sensor->resolution = 0.1F; - break; - default: - // Unknown type, default to 0. - sensor->max_value = 0.0F; - sensor->min_value = 0.0F; - sensor->resolution = 0.0F; - break; + case DHT11: + sensor->max_value = 80.0F; + sensor->min_value = 20.0F; + sensor->resolution = 5.0F; + break; + case DHT12: + sensor->max_value = 95.0F; + sensor->min_value = 20.0F; + sensor->resolution = 5.0F; + break; + case DHT21: + sensor->max_value = 100.0F; + sensor->min_value = 0.0F; + sensor->resolution = 0.1F; + break; + case DHT22: + sensor->max_value = 100.0F; + sensor->min_value = 0.0F; + sensor->resolution = 0.1F; + break; + default: + // Unknown type, default to 0. + sensor->max_value = 0.0F; + sensor->min_value = 0.0F; + sensor->resolution = 0.0F; + break; } } diff --git a/DHT_U.h b/DHT_U.h index 3d4ba83..0b322cc 100644 --- a/DHT_U.h +++ b/DHT_U.h @@ -43,7 +43,7 @@ * DHT_Unified. */ class DHT_Unified { -public: + public: DHT_Unified(uint8_t pin, uint8_t type, uint8_t count = 6, int32_t tempSensorId = -1, int32_t humiditySensorId = -1); void begin(); @@ -52,13 +52,13 @@ public: * @brief Class that stores state and functions about Temperature */ class Temperature : public Adafruit_Sensor { - public: - Temperature(DHT_Unified *parent, int32_t id); - bool getEvent(sensors_event_t *event); - void getSensor(sensor_t *sensor); + public: + Temperature(DHT_Unified* parent, int32_t id); + bool getEvent(sensors_event_t* event); + void getSensor(sensor_t* sensor); - private: - DHT_Unified *_parent; + private: + DHT_Unified* _parent; int32_t _id; }; @@ -66,13 +66,13 @@ public: * @brief Class that stores state and functions about Humidity */ class Humidity : public Adafruit_Sensor { - public: - Humidity(DHT_Unified *parent, int32_t id); - bool getEvent(sensors_event_t *event); - void getSensor(sensor_t *sensor); + public: + Humidity(DHT_Unified* parent, int32_t id); + bool getEvent(sensors_event_t* event); + void getSensor(sensor_t* sensor); - private: - DHT_Unified *_parent; + private: + DHT_Unified* _parent; int32_t _id; }; @@ -80,22 +80,26 @@ public: * @brief Returns temperature stored in _temp * @return Temperature value */ - Temperature temperature() { return _temp; } + Temperature temperature() { + return _temp; + } /*! * @brief Returns humidity stored in _humidity * @return Humidity value */ - Humidity humidity() { return _humidity; } + Humidity humidity() { + return _humidity; + } -private: + private: DHT _dht; uint8_t _type; Temperature _temp; Humidity _humidity; - void setName(sensor_t *sensor); - void setMinDelay(sensor_t *sensor); + void setName(sensor_t* sensor); + void setMinDelay(sensor_t* sensor); }; #endif