chore: moving to more dynamic structure

This commit is contained in:
Myk
2024-09-22 02:52:22 +03:00
parent 258fa0097d
commit 72a6ceb30c
14 changed files with 95710 additions and 439 deletions

View File

@@ -1,21 +1,26 @@
// BMP280Sensor.h
#ifndef BMP280SENSOR_H
#define BMP280SENSOR_H
#define BMP280_ADDR (0x76)
#include "i2cSensor.h"
#include <Adafruit_BME280.h> // Adafruit library for BMP280
#define BMP280_ADDR (0x76)
#define SEALEVELPRESSURE_HPA (1013.25)
#include <Adafruit_BME280.h>
class BMP280Sensor {
class BMP280Sensor : public i2cSensor {
public:
BMP280Sensor(uint8_t address = BMP280_ADDR); // Constructor
void init();
void read_values(float* temperature, float* humidity, float* pressure, float* altitude);
BMP280Sensor(uint8_t address = BMP280_ADDR); // Constructor
// Override init method from i2cSensor
void init() override;
// Override methods from Sensor
std::string getName() const override;
std::vector<ReadingType> getFeatures() const override;
float readValue(ReadingType feature) override;
private:
uint8_t _address; // BMP280 wake pin
Adafruit_BME280 bmp280; // BMP280 object
Adafruit_BME280 bmp280; // BMP280 object
};
#endif // BMP280SENSOR_H

View File

@@ -1,23 +1,26 @@
// CCS811Sensor.h
#ifndef CCS811SENSOR_H
#define CCS811SENSOR_H
#define CCS811_WAK 23
#include "i2cSensor.h"
#include <ccs811.h> // Include CCS811 library
class CCS811Sensor {
class CCS811Sensor : public i2cSensor {
public:
CCS811Sensor(uint8_t wakePin = CCS811_WAK); // Constructor
void init(double* temperature, double* humidity); // temp and hum needed to correct env
void init(); // Initialize without temperature and humidity
void read_values(uint16_t* eco2, uint16_t* etvoc, uint16_t* errstat, uint16_t* raw);
explicit CCS811Sensor(uint8_t wakePin = 23); // Constructor with default wake pin
void init() override; // Override init method from i2cSensor
void init(double* temperature, double* humidity); // Initialize with environmental data
// Override methods from Sensor
std::string getName() const override;
std::vector<ReadingType> getFeatures() const override;
float readValue(ReadingType feature) override;
private:
uint8_t _wakePin; // CCS811 wake pin
CCS811 ccs811; // CCS811 object
double* _temperaturePtr; // Pointer to temperature value
double* _humidityPtr; // Pointer to humidity value
uint8_t wakePin_; // CCS811 wake pin
CCS811 ccs811_; // CCS811 object
double* temperaturePtr_; // Pointer to temperature value
double* humidityPtr_; // Pointer to humidity value
};
#endif // CCS811SENSOR_H

View File

@@ -1,20 +1,22 @@
// HDC1080Sensor.h
#ifndef HDC1080SENSOR_H
#define HDC1080SENSOR_H
#define HDC1080_ADDR 0x40
#include "ClosedCube_HDC1080.h"
#include "i2cSensor.h"
#include "ClosedCube_HDC1080.h" // Include the HDC1080 library
class HDC1080Sensor {
class HDC1080Sensor : public i2cSensor {
public:
HDC1080Sensor(uint8_t address = HDC1080_ADDR); // Constructor
void init(); // Initialize HDC1080 sensor
void read_values(double* temperature, double* humidity, bool* success); // Read temperature and humidity values
explicit HDC1080Sensor(uint8_t address = 0x40); // Constructor with default I2C address
void init() override; // Override init method from i2cSensor
// Override methods from Sensor
std::string getName() const override;
std::vector<ReadingType> getFeatures() const override;
float readValue(ReadingType feature) override;
private:
uint8_t _address; // HDC1080 I2C address
ClosedCube_HDC1080 hdc1080; // HDC1080 object
ClosedCube_HDC1080 hdc1080_; // HDC1080 object
};
#endif // HDC1080SENSOR_H

34
include/ReadingType.h Normal file
View File

@@ -0,0 +1,34 @@
#ifndef READING_TYPE_LIST_H
#define READING_TYPE_LIST_H
#include <string>
#include <iostream>
// Define a macro to list all the enum values with their names
#define READING_TYPE_LIST \
X(Temperature) \
X(Humidity) \
X(Pressure) \
X(Altitude) \
X(eCO2) \
X(eTVOC) \
X(ErrorStatus)
// Enum class for different types of readings
enum class ReadingType {
#define X(name) name,
READING_TYPE_LIST
#undef X
};
// Inline function to convert ReadingType to a string
inline std::string readingTypeToString(ReadingType type) {
switch (type) {
#define X(name) case ReadingType::name: return #name;
READING_TYPE_LIST
#undef X
default: return "Unknown";
}
}
#endif // READING_TYPE_LIST_H

17
include/Sensor.h Normal file
View File

@@ -0,0 +1,17 @@
#ifndef SENSOR_H
#define SENSOR_H
#include <string>
#include <vector>
#include "ReadingType.h"
// Abstract base class for sensors
class Sensor {
public:
virtual ~Sensor() {}
virtual std::string getName() const = 0;
virtual std::vector<ReadingType> getFeatures() const = 0;
virtual float readValue(ReadingType feature) = 0;
};
#endif // SENSOR_H

24
include/i2cSensor.h Normal file
View File

@@ -0,0 +1,24 @@
#ifndef I2CSENSOR_H
#define I2CSENSOR_H
#include "Sensor.h"
#include <Wire.h> // Include the Wire library for I2C
// Abstract base class for I2C sensors
class i2cSensor : public Sensor {
public:
virtual ~i2cSensor() {}
// Initializes the sensor
virtual void init() = 0;
// Returns the I2C address of the sensor
uint8_t getAddress() const {
return i2c_address;
}
protected:
uint8_t i2c_address; // I2C address of the sensor
};
#endif // I2CSENSOR_H