From 054f27ebeeaa09d2e892fb69a1022f014c1c99fa Mon Sep 17 00:00:00 2001 From: Stefan Brand Date: Mon, 29 Mar 2021 17:23:16 +0200 Subject: [PATCH] Add Payload Helper Functions to AttSensor Class --- include/attsensor.h | 12 ++++++++++++ lib/BME280/BME280.cpp | 24 +++++------------------- lib/DS18B20/DS18B20.cpp | 4 ++-- lib/MHZ19C/MHZ19C.cpp | 6 ++---- lib/SENSAIRS8/SENSAIRS8.cpp | 4 +--- lib/SG112A/SG112A.cpp | 6 ++---- lib/SHT21/SHT21.cpp | 15 +++------------ 7 files changed, 27 insertions(+), 44 deletions(-) diff --git a/include/attsensor.h b/include/attsensor.h index 5be8118..f62f2cb 100644 --- a/include/attsensor.h +++ b/include/attsensor.h @@ -39,6 +39,18 @@ class AttSensor { // Return the number of Bytes added to the Payload virtual uint8_t numBytes(void) = 0; + + // Helper Functions to Put Values Into the Payload Array + static void int32ToPayload(int32_t value, char *payload, uint8_t startbyte) { + payload[startbyte] = (value) & 0XFF; + payload[startbyte+1] = (value >> 8) & 0XFF; + payload[startbyte+2] = (value >> 16) & 0XFF; + payload[startbyte+3] = (value >> 24) & 0XFF; + } + static void int16ToPayload(int16_t value, char *payload, uint8_t startbyte) { + payload[startbyte] = (value) & 0XFF; + payload[startbyte+1] = (value >> 8) & 0XFF; + } }; #endif \ No newline at end of file diff --git a/lib/BME280/BME280.cpp b/lib/BME280/BME280.cpp index 846027d..b2485dd 100644 --- a/lib/BME280/BME280.cpp +++ b/lib/BME280/BME280.cpp @@ -80,7 +80,7 @@ int32_t BME280::compensate_h(int32_t adc_H) uint8_t BME280::getSensorData(char *payload, uint8_t startbyte) { int32_t UP, UT, UH; - int32_t rawP, rawT, value; + int32_t rawP, rawT; // Trigger Measurement // Set Sensor Config @@ -104,27 +104,13 @@ uint8_t BME280::getSensorData(char *payload, uint8_t startbyte) { // Read Humidity UH = read16(0xFD); - // Temperature - value = compensate_t(UT); - payload[startbyte] = (value) & 0XFF; - payload[startbyte+1] = (value >> 8) & 0XFF; - payload[startbyte+2] = (value >> 16) & 0XFF; - payload[startbyte+3] = (value >> 24) & 0XFF; - + int32ToPayload(compensate_t(UT), payload, startbyte); // Humidity - value = compensate_h(UH); - payload[startbyte+4] = (value) & 0XFF; - payload[startbyte+5] = (value >> 8) & 0XFF; - payload[startbyte+6] = (value >> 16) & 0XFF; - payload[startbyte+7] = (value >> 24) & 0XFF; - + int32ToPayload(compensate_h(UH), payload, startbyte+4); // Pressure - value = compensate_p(UP); - payload[startbyte+8] = (value) & 0XFF; - payload[startbyte+9] = (value >> 8) & 0XFF; - payload[startbyte+10] = (value >> 16) & 0XFF; - payload[startbyte+11] = (value >> 24) & 0XFF; + int32ToPayload(compensate_p(UP), payload, startbyte+8); + return startbyte+12; } diff --git a/lib/DS18B20/DS18B20.cpp b/lib/DS18B20/DS18B20.cpp index 9a12f4e..c413afd 100644 --- a/lib/DS18B20/DS18B20.cpp +++ b/lib/DS18B20/DS18B20.cpp @@ -125,8 +125,8 @@ uint8_t DS18B20::getSensorData(char *payload, uint8_t startbyte){ value = -85; } // Add to Payload - payload[startbyte] = (value) & 0XFF; - payload[startbyte+1] = (value >> 8) & 0XFF; + int16ToPayload(value, payload, startbyte); + // Set next Startbyte startbyte += 2; delay(50); diff --git a/lib/MHZ19C/MHZ19C.cpp b/lib/MHZ19C/MHZ19C.cpp index a43b7db..f8d60b1 100644 --- a/lib/MHZ19C/MHZ19C.cpp +++ b/lib/MHZ19C/MHZ19C.cpp @@ -51,10 +51,8 @@ uint8_t MHZ19C::getSensorData(char * payload, uint8_t startbyte) { if (readBytes > 0) { switch(buffer[1]) { case 0x86: - uint16_t value = (buffer[2]*256) + buffer[3]; - payload[startbyte] = (value) & 0xFF; - payload[startbyte+1] = (value >> 8) & 0xFF; - break; + int16ToPayload((buffer[2]*256) + buffer[3], payload, startbyte); + break; } } return startbyte+2; diff --git a/lib/SENSAIRS8/SENSAIRS8.cpp b/lib/SENSAIRS8/SENSAIRS8.cpp index 3aa3879..ba8e38d 100644 --- a/lib/SENSAIRS8/SENSAIRS8.cpp +++ b/lib/SENSAIRS8/SENSAIRS8.cpp @@ -44,9 +44,7 @@ uint8_t SENSAIRS8::getSensorData(char *payload, uint8_t startbyte) { payload[startbyte] = 0x00; payload[startbyte+1] = 0x00; if (readBytes > 0) { - uint16_t value = (buffer[3]*256) + buffer[4]; - payload[startbyte] = (value) & 0xFF; - payload[startbyte+1] = (value >> 8) & 0xFF; + int16ToPayload((buffer[3]*256) + buffer[4], payload, startbyte); } return startbyte+2; } diff --git a/lib/SG112A/SG112A.cpp b/lib/SG112A/SG112A.cpp index e5fdd90..bfbab90 100644 --- a/lib/SG112A/SG112A.cpp +++ b/lib/SG112A/SG112A.cpp @@ -44,10 +44,8 @@ uint8_t SG112A::getSensorData(char *payload, uint8_t startbyte) { if (readBytes > 0) { switch(buffer[2]) { case 0x15: - uint16_t value = (buffer[5]*256) + buffer[4]; - payload[startbyte] = (value) & 0xFF; - payload[startbyte+1] = (value >> 8) & 0xFF; - break; + int16ToPayload((buffer[3]*256) + buffer[4], payload, startbyte); + break; } } return startbyte+2; diff --git a/lib/SHT21/SHT21.cpp b/lib/SHT21/SHT21.cpp index 60f496f..1ba31e0 100644 --- a/lib/SHT21/SHT21.cpp +++ b/lib/SHT21/SHT21.cpp @@ -54,18 +54,9 @@ uint16_t SHT21::sensorRead(uint8_t command) { uint8_t SHT21::getSensorData(char *payload, uint8_t startbyte) { // Temperature - int32_t value = (int32_t)((-46.85 + 175.72 / 65536.0 * (float)(sensorRead(SHT21_TEMPHOLD)))*100); - payload[startbyte] = (value) & 0XFF; - payload[startbyte+1] = (value >> 8) & 0XFF; - payload[startbyte+2] = (value >> 16) & 0XFF; - payload[startbyte+3] = (value >> 24) & 0XFF; - + int32ToPayload((int32_t)((-46.85 + 175.72 / 65536.0 * (float)(sensorRead(SHT21_TEMPHOLD)))*100), payload, startbyte); // Humidity - value = (int32_t)((-6.0 + 125.0 / 65536.0 * (float)(sensorRead(SHT21_HUMIHOLD)))*100); - payload[startbyte+4] = (value) & 0XFF; - payload[startbyte+5] = (value >> 8) & 0XFF; - payload[startbyte+6] = (value >> 16) & 0XFF; - payload[startbyte+7] = (value >> 24) & 0XFF; - + int32ToPayload((int32_t)((-6.0 + 125.0 / 65536.0 * (float)(sensorRead(SHT21_HUMIHOLD)))*100), payload, startbyte+4); + return startbyte+8; } \ No newline at end of file