SensorLib 0.5.0
Multi-platform sensor driver library for Arduino, PlatformIO, and ESP-IDF
Loading...
Searching...
No Matches
SensorBase.hpp
Go to the documentation of this file.
1
35#pragma once
36
37#include "../SensorPlatform.hpp"
38#include "SensorDefs.hpp"
39#include <functional>
40#include <vector>
41#include <cstring>
42
43
48{
49public:
56 static const char *typeToString(SensorType type)
57 {
58 switch (type) {
59 case SensorType::ACCELEROMETER: return "ACCELEROMETER";
60 case SensorType::GYROSCOPE: return "GYROSCOPE";
61 case SensorType::MAGNETOMETER: return "MAGNETOMETER";
62 case SensorType::PRESSURE: return "PRESSURE";
63 case SensorType::TEMPERATURE: return "TEMPERATURE";
64 case SensorType::HUMIDITY: return "HUMIDITY";
65 case SensorType::MULTI_AXIS: return "MULTI_AXIS";
66 default: return "UNKNOWN";
67 }
68 }
69};
70
81template<typename T>
83{
84protected:
93 {
94 _info.type = sensor_type;
95 }
96
100 virtual ~SensorBase() = default;
101
102public:
103 // Delete copy operations to prevent slicing
104 SensorBase(const SensorBase &) = delete;
105 SensorBase &operator=(const SensorBase &) = delete;
106
114 void setOffset(int16_t x, int16_t y, int16_t z)
115 {
116 _x_offset = x;
117 _y_offset = y;
118 _z_offset = z;
119 }
120
126 float getSensitivity() const
127 {
128 return _sensitivity;
129 }
130
137 {
138 return _info;
139 }
140
141 // Pure virtual interface functions
142
150 virtual bool readData(T &data) = 0;
151
158 virtual bool isDataReady() = 0;
159
166 virtual bool reset() = 0;
167
174 virtual bool selfTest() = 0;
175
181 virtual SensorConfig getConfig() const
182 {
183 return _config;
184 }
185
186private:
194 virtual bool initImpl(uint8_t addr) = 0;
195
196protected:
199 int16_t _x_offset;
200 int16_t _y_offset;
201 int16_t _z_offset;
203};
@license MIT License
SensorType
Enumeration of sensor types.
@ MULTI_AXIS
Multi-axis sensor.
@ HUMIDITY
Humidity sensor.
@ GYROSCOPE
Gyroscope sensor.
@ PRESSURE
Pressure sensor.
@ UNKNOWN
Unknown sensor type.
@ ACCELEROMETER
Accelerometer sensor.
@ TEMPERATURE
Temperature sensor.
@ MAGNETOMETER
Magnetometer sensor.
Template base class for all sensor implementations.
void setOffset(int16_t x, int16_t y, int16_t z)
Set sensor calibration offsets.
virtual SensorConfig getConfig() const
Get current sensor configuration.
virtual bool isDataReady()=0
Check if new data is available.
SensorConfig _config
Current configuration.
virtual ~SensorBase()=default
Virtual destructor for proper polymorphic cleanup.
float getSensitivity() const
Get sensor sensitivity.
virtual bool readData(T &data)=0
Read data from sensor.
SensorBase(SensorType sensor_type=SensorType::UNKNOWN)
Construct a new SensorBase object.
int16_t _x_offset
int16_t _z_offset
SensorBase(const SensorBase &)=delete
SensorBase & operator=(const SensorBase &)=delete
virtual bool selfTest()=0
Perform sensor self-test.
SensorInfo _info
Sensor information.
int16_t _y_offset
float _sensitivity
virtual bool reset()=0
Reset sensor to default state.
SensorInfo getSensorInfo() const
Get sensor information.
Utility class providing static sensor-related helper functions.
static const char * typeToString(SensorType type)
Convert sensor type enumeration to string representation.
Structure representing sensor configuration parameters.
Structure containing sensor identification information.
SensorType type
Sensor type.
int16_t x[5]
int16_t y[5]