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);
}
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) {

View file

@ -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

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
#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

View file

@ -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);
}

View file

@ -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

View file

@ -31,14 +31,15 @@ void blink(uint8_t num) {
#define BLINK_LED(COUNT)
#endif
#ifdef HAS_BME280
#if defined HAS_BME280
#include <BME280.h>
BME280 sensor;
#endif
#ifdef HAS_SHT21
#elif defined HAS_SHT21
#include <SHT21.h>
SHT21 sensor;
#elif defined HAS_SG112A
#include <SG112A.h>
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