Files
esphome-lsm6ds/esphome/components/lsm6ds_base/lsm6ds.h
T
NonaSuomy b7b3b06e92 [lsm6ds] Restructure component with _base, _i2c, _spi pattern
Major restructure to follow ESPHome conventions for multi-interface components:

Component Structure Changes:
- Renamed lsm6ds/ to lsm6ds_base/ to follow _base naming convention
- Created lsm6ds_i2c/ for I2C interface implementation
- Created lsm6ds_spi/ for SPI interface implementation
- Both interfaces use AUTO_LOAD to automatically include base component
- Updated C++ namespaces to match component folder names (lsm6ds_base, lsm6ds_i2c, lsm6ds_spi)

Features:
- Support for LSM6DSOX, LSM6DS3TR-C, LSM6DSO32 variants
- Full 3-axis accelerometer and gyroscope support
- Temperature sensor support
- Configurable ranges: Accel (2G-16G), Gyro (125-2000 DPS)
- Configurable output data rate (12.5Hz - 6.66kHz)
- Two usage patterns: all-in-one and hub+sensor platform

Implementation Details:
- Base component (lsm6ds_base) contains shared code and abstract LSM6DSComponent class
- I2C variant implements LSM6DSI2CDevice with I2C read/write methods
- SPI variant implements LSM6DSSPIDevice with SPI read/write methods (MSB-based protocol)
- Both variants support optional sensor.py for hub+sensor pattern
- Added README_EXAMPLE.md files with usage examples for both interfaces
- Added component test files for validation

Documentation:
- Added COMPONENT_STRUCTURE.md explaining architecture and usage patterns
- Included example configurations for both I2C and SPI
- Documented all configuration options and sensor variants
2026-06-04 03:45:55 -04:00

81 lines
2.5 KiB
C++

#pragma once
#include "esphome/core/component.h"
#include "esphome/components/sensor/sensor.h"
namespace esphome {
namespace lsm6ds_base {
enum LSM6DSAccelRange {
LSM6DS_ACCEL_RANGE_2G = 0x00,
LSM6DS_ACCEL_RANGE_16G = 0x01,
LSM6DS_ACCEL_RANGE_4G = 0x02,
LSM6DS_ACCEL_RANGE_8G = 0x03,
};
enum LSM6DSGyroRange {
LSM6DS_GYRO_RANGE_250DPS = 0x00,
LSM6DS_GYRO_RANGE_500DPS = 0x01,
LSM6DS_GYRO_RANGE_1000DPS = 0x02,
LSM6DS_GYRO_RANGE_2000DPS = 0x03,
LSM6DS_GYRO_RANGE_125DPS = 0x04,
};
enum LSM6DSODR {
LSM6DS_ODR_OFF = 0x00,
LSM6DS_ODR_12_5HZ = 0x01,
LSM6DS_ODR_26HZ = 0x02,
LSM6DS_ODR_52HZ = 0x03,
LSM6DS_ODR_104HZ = 0x04,
LSM6DS_ODR_208HZ = 0x05,
LSM6DS_ODR_416HZ = 0x06,
LSM6DS_ODR_833HZ = 0x07,
LSM6DS_ODR_1_66KHZ = 0x08,
LSM6DS_ODR_3_33KHZ = 0x09,
LSM6DS_ODR_6_66KHZ = 0x0A,
};
class LSM6DSComponent : public PollingComponent {
public:
void setup() override;
void dump_config() override;
void update() override;
float get_setup_priority() const override { return setup_priority::DATA; }
void set_accel_x_sensor(sensor::Sensor *accel_x) { accel_x_sensor_ = accel_x; }
void set_accel_y_sensor(sensor::Sensor *accel_y) { accel_y_sensor_ = accel_y; }
void set_accel_z_sensor(sensor::Sensor *accel_z) { accel_z_sensor_ = accel_z; }
void set_gyro_x_sensor(sensor::Sensor *gyro_x) { gyro_x_sensor_ = gyro_x; }
void set_gyro_y_sensor(sensor::Sensor *gyro_y) { gyro_y_sensor_ = gyro_y; }
void set_gyro_z_sensor(sensor::Sensor *gyro_z) { gyro_z_sensor_ = gyro_z; }
void set_temperature_sensor(sensor::Sensor *temp) { temperature_sensor_ = temp; }
void set_accel_range(LSM6DSAccelRange range) { accel_range_ = range; }
void set_gyro_range(LSM6DSGyroRange range) { gyro_range_ = range; }
void set_accel_odr(LSM6DSODR odr) { accel_odr_ = odr; }
void set_gyro_odr(LSM6DSODR odr) { gyro_odr_ = odr; }
protected:
virtual bool read_register(uint8_t reg, uint8_t *data, size_t len) = 0;
virtual bool write_register(uint8_t reg, uint8_t data) = 0;
sensor::Sensor *accel_x_sensor_{nullptr};
sensor::Sensor *accel_y_sensor_{nullptr};
sensor::Sensor *accel_z_sensor_{nullptr};
sensor::Sensor *gyro_x_sensor_{nullptr};
sensor::Sensor *gyro_y_sensor_{nullptr};
sensor::Sensor *gyro_z_sensor_{nullptr};
sensor::Sensor *temperature_sensor_{nullptr};
LSM6DSAccelRange accel_range_;
LSM6DSGyroRange gyro_range_;
LSM6DSODR accel_odr_;
LSM6DSODR gyro_odr_;
float accel_sensitivity_;
float gyro_sensitivity_;
};
} // namespace lsm6ds_base
} // namespace esphome