From 45fa51c07138527b5ad8d46b194420da40b4d1a0 Mon Sep 17 00:00:00 2001 From: Stefan Brand Date: Mon, 1 Feb 2021 21:21:03 +0100 Subject: [PATCH] Refactored Sensor Libraries to use Common Interface --- .gitignore | 7 ++++++- .vscode/extensions.json | 7 +++++++ lib/BME280/BME280.cpp | 10 +++++----- lib/BME280/BME280.h | 10 ++++++++-- lib/SG112A/SG112A.cpp | 6 +++++- lib/SG112A/SG112A.h | 10 ++++++++-- lib/SHT21/SHT21.cpp | 11 ++++------- lib/SHT21/SHT21.h | 12 +++++++---- src/main.cpp | 44 ++++++++++++++--------------------------- 9 files changed, 66 insertions(+), 51 deletions(-) create mode 100644 .vscode/extensions.json diff --git a/.gitignore b/.gitignore index 0e56cf2..4d37315 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,6 @@ -config.h +.pio +.vscode/.browse.c_cpp.db* +.vscode/c_cpp_properties.json +.vscode/launch.json +.vscode/ipch +src/config.h \ No newline at end of file diff --git a/.vscode/extensions.json b/.vscode/extensions.json new file mode 100644 index 0000000..0f0d740 --- /dev/null +++ b/.vscode/extensions.json @@ -0,0 +1,7 @@ +{ + // See http://go.microsoft.com/fwlink/?LinkId=827846 + // for the documentation about the extensions.json format + "recommendations": [ + "platformio.platformio-ide" + ] +} diff --git a/lib/BME280/BME280.cpp b/lib/BME280/BME280.cpp index 67be7a4..9204d20 100644 --- a/lib/BME280/BME280.cpp +++ b/lib/BME280/BME280.cpp @@ -78,7 +78,7 @@ int32_t BME280::compensate_h(int32_t adc_H) return (uint32_t)((v_x1_u32r>>12)/10); } -void BME280::getData(int32_t *t, int32_t *p, int32_t *h) { +void BME280::getSensorData(lora_data &loradata) { int32_t UP, UT, UH; int32_t rawP, rawT; @@ -106,9 +106,9 @@ void BME280::getData(int32_t *t, int32_t *p, int32_t *h) { UH = read16(0xFD); // Compensate Values and Return - *t = compensate_t(UT); - *p = compensate_p(UP); - *h = compensate_h(UH); + loradata.temperature = compensate_t(UT); + loradata.pressure = compensate_p(UP); + loradata.humidity = compensate_h(UH); } uint8_t BME280::read8(uint8_t addr) { @@ -147,4 +147,4 @@ void BME280::write8(uint8_t addr, uint8_t data) { Wire.write(addr); Wire.write(data); Wire.endTransmission(); -} +} \ No newline at end of file diff --git a/lib/BME280/BME280.h b/lib/BME280/BME280.h index 69341db..184ff44 100644 --- a/lib/BME280/BME280.h +++ b/lib/BME280/BME280.h @@ -5,6 +5,13 @@ #define BME280_I2CADDR 0x76 +struct lora_data { + uint8_t bat; + int32_t temperature; + int32_t humidity; + int32_t pressure; +} __attribute__ ((packed)); + class BME280 { private: @@ -36,7 +43,6 @@ public: // Get Calibration Data from Sensor void getCalData(void); // Read Pressure From Sensor - void getData(int32_t *t, int32_t *p, int32_t *h); - + void getSensorData(lora_data &loradata); }; #endif diff --git a/lib/SG112A/SG112A.cpp b/lib/SG112A/SG112A.cpp index b23a49e..ccf3add 100644 --- a/lib/SG112A/SG112A.cpp +++ b/lib/SG112A/SG112A.cpp @@ -5,7 +5,11 @@ void GA112A(void) { } -uint16_t getPPM(void) { +void SG112A::getSensorData(lora_data &loradata) { + +} + +uint16_t SG112A::getPPM(void) { } diff --git a/lib/SG112A/SG112A.h b/lib/SG112A/SG112A.h index 2c1a9c6..bd7c617 100644 --- a/lib/SG112A/SG112A.h +++ b/lib/SG112A/SG112A.h @@ -1,13 +1,19 @@ #ifndef SG112A_H #define SG112A_H +struct lora_data { + uint8_t bat; + int16_t ppm; +} __attribute__ ((packed)); + class SG112A { private: void sendCmd(uint8_t *cmd, uint8_t len); uint16_t crc16(uint8_t *cmd, uint8_t len); + uint16_t getPPM(void); public: SG112A(void); - uint16_t getPPM(void); + void getSensorData(lora_data &loradata); }; -#endif +#endif \ No newline at end of file diff --git a/lib/SHT21/SHT21.cpp b/lib/SHT21/SHT21.cpp index c55568a..ba3b659 100644 --- a/lib/SHT21/SHT21.cpp +++ b/lib/SHT21/SHT21.cpp @@ -52,10 +52,7 @@ uint16_t SHT21::sensorRead(uint8_t command) { return result; } -float SHT21::getTemperature(void) { - return (-46.85 + 175.72 / 65536.0 * (float)(sensorRead(SHT21_TEMPHOLD))); -} - -float SHT21::getHumidity(void) { - return (-6.0 + 125.0 / 65536.0 * (float)(sensorRead(SHT21_HUMIHOLD))); -} +void SHT21::getSensorData(lora_data &loradata) { + loradata.temperature = (int32_t)((-46.85 + 175.72 / 65536.0 * (float)(sensorRead(SHT21_TEMPHOLD)))*100); + loradata.humidity = (int32_t)((-6.0 + 125.0 / 65536.0 * (float)(sensorRead(SHT21_HUMIHOLD)))*100); +} \ No newline at end of file diff --git a/lib/SHT21/SHT21.h b/lib/SHT21/SHT21.h index a1210c5..17becde 100644 --- a/lib/SHT21/SHT21.h +++ b/lib/SHT21/SHT21.h @@ -37,6 +37,12 @@ #define SHT21_HUMINOHOLD 0xF5 #define SHT21_SOFTRESET 0xFE +struct lora_data { + uint8_t bat; + int32_t temperature; + int32_t humidity; +} __attribute__ ((packed)); + class SHT21 { private: @@ -44,9 +50,7 @@ class SHT21 public: SHT21(void); - - float getTemperature(void); - float getHumidity(void); + void getSensorData(lora_data &loradata); }; -#endif +#endif \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 6710147..9dfb6e4 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -31,14 +31,15 @@ void blink(uint8_t num) { #define BLINK_LED(COUNT) #endif -#ifdef HAS_BME280 -#include -BME280 sensor; -#endif - -#ifdef HAS_SHT21 -#include -SHT21 sensor; +#if defined HAS_BME280 + #include + BME280 sensor; +#elif defined HAS_SHT21 + #include + SHT21 sensor; +#elif defined HAS_SG112A + #include + SG112A sensor; #endif // Define some LMIC Callbacks and Variables @@ -147,21 +148,9 @@ void do_send(osjob_t* j) { #ifdef HAS_NO_SENSOR struct lora_data { uint8_t bat; - } __attribute ((packed)) data; - #elif defined HAS_SHT21 - struct lora_data { - uint8_t bat; - int32_t temperature; - int32_t humidity; - } __attribute__ ((packed)) data; - #elif defined HAS_BME280 - struct lora_data { - uint8_t bat; - int32_t temperature; - int32_t humidity; - int32_t pressure; - } __attribute__ ((packed)) data; + } __attribute ((packed)); #endif + lora_data data; // The struct is defined in the sensor class (or above for use without a sensor) if (LMIC.opmode & OP_TXRXPEND) { delay(1); @@ -172,12 +161,9 @@ void do_send(osjob_t* j) { if (batv % 20 > 9) data.bat += 1; - // Take Measurements depending on Sensor - #ifdef HAS_SHT21 - data.temperature = (int32_t)(sensor.getTemperature()*100); - data.humidity = (int32_t)(sensor.getHumidity()*100); - #elif defined HAS_BME280 - sensor.getData(&data.temperature, &data.pressure, &data.humidity); + // Get Sensor Readings Into Data Paket + #ifndef HAS_NO_SENSOR + sensor.getSensorData(data); #endif // Queue Packet for Sending @@ -238,4 +224,4 @@ void loop() { // Only Run the LMIC loop here. Actual Sending Code is in do_send() os_runloop_once(); -} +} \ No newline at end of file