Add Payload Helper Functions to AttSensor Class

This commit is contained in:
seiichiro 2021-03-29 17:23:16 +02:00
parent b498364a57
commit 054f27ebee
7 changed files with 27 additions and 44 deletions

View file

@ -39,6 +39,18 @@ class AttSensor {
// Return the number of Bytes added to the Payload // Return the number of Bytes added to the Payload
virtual uint8_t numBytes(void) = 0; 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 #endif

View file

@ -80,7 +80,7 @@ int32_t BME280::compensate_h(int32_t adc_H)
uint8_t BME280::getSensorData(char *payload, uint8_t startbyte) { uint8_t BME280::getSensorData(char *payload, uint8_t startbyte) {
int32_t UP, UT, UH; int32_t UP, UT, UH;
int32_t rawP, rawT, value; int32_t rawP, rawT;
// Trigger Measurement // Trigger Measurement
// Set Sensor Config // Set Sensor Config
@ -104,27 +104,13 @@ uint8_t BME280::getSensorData(char *payload, uint8_t startbyte) {
// Read Humidity // Read Humidity
UH = read16(0xFD); UH = read16(0xFD);
// Temperature // Temperature
value = compensate_t(UT); int32ToPayload(compensate_t(UT), payload, startbyte);
payload[startbyte] = (value) & 0XFF;
payload[startbyte+1] = (value >> 8) & 0XFF;
payload[startbyte+2] = (value >> 16) & 0XFF;
payload[startbyte+3] = (value >> 24) & 0XFF;
// Humidity // Humidity
value = compensate_h(UH); int32ToPayload(compensate_h(UH), payload, startbyte+4);
payload[startbyte+4] = (value) & 0XFF;
payload[startbyte+5] = (value >> 8) & 0XFF;
payload[startbyte+6] = (value >> 16) & 0XFF;
payload[startbyte+7] = (value >> 24) & 0XFF;
// Pressure // Pressure
value = compensate_p(UP); int32ToPayload(compensate_p(UP), payload, startbyte+8);
payload[startbyte+8] = (value) & 0XFF;
payload[startbyte+9] = (value >> 8) & 0XFF;
payload[startbyte+10] = (value >> 16) & 0XFF;
payload[startbyte+11] = (value >> 24) & 0XFF;
return startbyte+12; return startbyte+12;
} }

View file

@ -125,8 +125,8 @@ uint8_t DS18B20::getSensorData(char *payload, uint8_t startbyte){
value = -85; value = -85;
} }
// Add to Payload // Add to Payload
payload[startbyte] = (value) & 0XFF; int16ToPayload(value, payload, startbyte);
payload[startbyte+1] = (value >> 8) & 0XFF;
// Set next Startbyte // Set next Startbyte
startbyte += 2; startbyte += 2;
delay(50); delay(50);

View file

@ -51,10 +51,8 @@ uint8_t MHZ19C::getSensorData(char * payload, uint8_t startbyte) {
if (readBytes > 0) { if (readBytes > 0) {
switch(buffer[1]) { switch(buffer[1]) {
case 0x86: case 0x86:
uint16_t value = (buffer[2]*256) + buffer[3]; int16ToPayload((buffer[2]*256) + buffer[3], payload, startbyte);
payload[startbyte] = (value) & 0xFF; break;
payload[startbyte+1] = (value >> 8) & 0xFF;
break;
} }
} }
return startbyte+2; return startbyte+2;

View file

@ -44,9 +44,7 @@ uint8_t SENSAIRS8::getSensorData(char *payload, uint8_t startbyte) {
payload[startbyte] = 0x00; payload[startbyte] = 0x00;
payload[startbyte+1] = 0x00; payload[startbyte+1] = 0x00;
if (readBytes > 0) { if (readBytes > 0) {
uint16_t value = (buffer[3]*256) + buffer[4]; int16ToPayload((buffer[3]*256) + buffer[4], payload, startbyte);
payload[startbyte] = (value) & 0xFF;
payload[startbyte+1] = (value >> 8) & 0xFF;
} }
return startbyte+2; return startbyte+2;
} }

View file

@ -44,10 +44,8 @@ uint8_t SG112A::getSensorData(char *payload, uint8_t startbyte) {
if (readBytes > 0) { if (readBytes > 0) {
switch(buffer[2]) { switch(buffer[2]) {
case 0x15: case 0x15:
uint16_t value = (buffer[5]*256) + buffer[4]; int16ToPayload((buffer[3]*256) + buffer[4], payload, startbyte);
payload[startbyte] = (value) & 0xFF; break;
payload[startbyte+1] = (value >> 8) & 0xFF;
break;
} }
} }
return startbyte+2; return startbyte+2;

View file

@ -54,18 +54,9 @@ uint16_t SHT21::sensorRead(uint8_t command) {
uint8_t SHT21::getSensorData(char *payload, uint8_t startbyte) { uint8_t SHT21::getSensorData(char *payload, uint8_t startbyte) {
// Temperature // Temperature
int32_t value = (int32_t)((-46.85 + 175.72 / 65536.0 * (float)(sensorRead(SHT21_TEMPHOLD)))*100); int32ToPayload((int32_t)((-46.85 + 175.72 / 65536.0 * (float)(sensorRead(SHT21_TEMPHOLD)))*100), payload, startbyte);
payload[startbyte] = (value) & 0XFF;
payload[startbyte+1] = (value >> 8) & 0XFF;
payload[startbyte+2] = (value >> 16) & 0XFF;
payload[startbyte+3] = (value >> 24) & 0XFF;
// Humidity // Humidity
value = (int32_t)((-6.0 + 125.0 / 65536.0 * (float)(sensorRead(SHT21_HUMIHOLD)))*100); int32ToPayload((int32_t)((-6.0 + 125.0 / 65536.0 * (float)(sensorRead(SHT21_HUMIHOLD)))*100), payload, startbyte+4);
payload[startbyte+4] = (value) & 0XFF;
payload[startbyte+5] = (value >> 8) & 0XFF;
payload[startbyte+6] = (value >> 16) & 0XFF;
payload[startbyte+7] = (value >> 24) & 0XFF;
return startbyte+8; return startbyte+8;
} }