Refactored Sensor Libraries to use Common Interface

This commit is contained in:
seiichiro 2021-02-01 21:21:03 +01:00
parent 9aa59b3fe9
commit 45fa51c071
9 changed files with 66 additions and 51 deletions

7
.gitignore vendored
View file

@ -1 +1,6 @@
config.h .pio
.vscode/.browse.c_cpp.db*
.vscode/c_cpp_properties.json
.vscode/launch.json
.vscode/ipch
src/config.h

7
.vscode/extensions.json vendored Normal file
View file

@ -0,0 +1,7 @@
{
// See http://go.microsoft.com/fwlink/?LinkId=827846
// for the documentation about the extensions.json format
"recommendations": [
"platformio.platformio-ide"
]
}

View file

@ -78,7 +78,7 @@ int32_t BME280::compensate_h(int32_t adc_H)
return (uint32_t)((v_x1_u32r>>12)/10); 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 UP, UT, UH;
int32_t rawP, rawT; int32_t rawP, rawT;
@ -106,9 +106,9 @@ void BME280::getData(int32_t *t, int32_t *p, int32_t *h) {
UH = read16(0xFD); UH = read16(0xFD);
// Compensate Values and Return // Compensate Values and Return
*t = compensate_t(UT); loradata.temperature = compensate_t(UT);
*p = compensate_p(UP); loradata.pressure = compensate_p(UP);
*h = compensate_h(UH); loradata.humidity = compensate_h(UH);
} }
uint8_t BME280::read8(uint8_t addr) { uint8_t BME280::read8(uint8_t addr) {

View file

@ -5,6 +5,13 @@
#define BME280_I2CADDR 0x76 #define BME280_I2CADDR 0x76
struct lora_data {
uint8_t bat;
int32_t temperature;
int32_t humidity;
int32_t pressure;
} __attribute__ ((packed));
class BME280 class BME280
{ {
private: private:
@ -36,7 +43,6 @@ public:
// Get Calibration Data from Sensor // Get Calibration Data from Sensor
void getCalData(void); void getCalData(void);
// Read Pressure From Sensor // Read Pressure From Sensor
void getData(int32_t *t, int32_t *p, int32_t *h); void getSensorData(lora_data &loradata);
}; };
#endif #endif

View file

@ -5,7 +5,11 @@ void GA112A(void) {
} }
uint16_t getPPM(void) { void SG112A::getSensorData(lora_data &loradata) {
}
uint16_t SG112A::getPPM(void) {
} }

View file

@ -1,13 +1,19 @@
#ifndef SG112A_H #ifndef SG112A_H
#define SG112A_H #define SG112A_H
struct lora_data {
uint8_t bat;
int16_t ppm;
} __attribute__ ((packed));
class SG112A { class SG112A {
private: private:
void sendCmd(uint8_t *cmd, uint8_t len); void sendCmd(uint8_t *cmd, uint8_t len);
uint16_t crc16(uint8_t *cmd, uint8_t len); uint16_t crc16(uint8_t *cmd, uint8_t len);
uint16_t getPPM(void);
public: public:
SG112A(void); SG112A(void);
uint16_t getPPM(void); void getSensorData(lora_data &loradata);
}; };
#endif #endif

View file

@ -52,10 +52,7 @@ uint16_t SHT21::sensorRead(uint8_t command) {
return result; return result;
} }
float SHT21::getTemperature(void) { void SHT21::getSensorData(lora_data &loradata) {
return (-46.85 + 175.72 / 65536.0 * (float)(sensorRead(SHT21_TEMPHOLD))); 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);
float SHT21::getHumidity(void) {
return (-6.0 + 125.0 / 65536.0 * (float)(sensorRead(SHT21_HUMIHOLD)));
} }

View file

@ -37,6 +37,12 @@
#define SHT21_HUMINOHOLD 0xF5 #define SHT21_HUMINOHOLD 0xF5
#define SHT21_SOFTRESET 0xFE #define SHT21_SOFTRESET 0xFE
struct lora_data {
uint8_t bat;
int32_t temperature;
int32_t humidity;
} __attribute__ ((packed));
class SHT21 class SHT21
{ {
private: private:
@ -44,9 +50,7 @@ class SHT21
public: public:
SHT21(void); SHT21(void);
void getSensorData(lora_data &loradata);
float getTemperature(void);
float getHumidity(void);
}; };
#endif #endif

View file

@ -31,14 +31,15 @@ void blink(uint8_t num) {
#define BLINK_LED(COUNT) #define BLINK_LED(COUNT)
#endif #endif
#ifdef HAS_BME280 #if defined HAS_BME280
#include <BME280.h> #include <BME280.h>
BME280 sensor; BME280 sensor;
#endif #elif defined HAS_SHT21
#ifdef HAS_SHT21
#include <SHT21.h> #include <SHT21.h>
SHT21 sensor; SHT21 sensor;
#elif defined HAS_SG112A
#include <SG112A.h>
SG112A sensor;
#endif #endif
// Define some LMIC Callbacks and Variables // Define some LMIC Callbacks and Variables
@ -147,21 +148,9 @@ void do_send(osjob_t* j) {
#ifdef HAS_NO_SENSOR #ifdef HAS_NO_SENSOR
struct lora_data { struct lora_data {
uint8_t bat; uint8_t bat;
} __attribute ((packed)) data; } __attribute ((packed));
#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;
#endif #endif
lora_data data; // The struct is defined in the sensor class (or above for use without a sensor)
if (LMIC.opmode & OP_TXRXPEND) { if (LMIC.opmode & OP_TXRXPEND) {
delay(1); delay(1);
@ -172,12 +161,9 @@ void do_send(osjob_t* j) {
if (batv % 20 > 9) if (batv % 20 > 9)
data.bat += 1; data.bat += 1;
// Take Measurements depending on Sensor // Get Sensor Readings Into Data Paket
#ifdef HAS_SHT21 #ifndef HAS_NO_SENSOR
data.temperature = (int32_t)(sensor.getTemperature()*100); sensor.getSensorData(data);
data.humidity = (int32_t)(sensor.getHumidity()*100);
#elif defined HAS_BME280
sensor.getData(&data.temperature, &data.pressure, &data.humidity);
#endif #endif
// Queue Packet for Sending // Queue Packet for Sending