SensorLib 0.5.0
Multi-platform sensor driver library for Arduino, PlatformIO, and ESP-IDF
Loading...
Searching...
No Matches
ComplexStaticDeviceWithHal.hpp
Go to the documentation of this file.
1
30#pragma once
31#include "DeviceBeginCommon.hpp"
32
34{
35public:
36#if defined(ARDUINO)
46 bool begin(TwoWire &wire, uint8_t addr, int sda = -1, int scl = -1)
47 {
48 return beginLifecycle(addr, COMM_I2C, addr, [&]() {
49 return beginCommonStatic<SensorCommI2C, HalArduino>(comm, staticComm, hal, wire, addr, sda, scl);
50 });
51 }
52
62 bool begin(SPIClass &spi, uint8_t csPin, int mosi = -1, int miso = -1, int sck = -1)
63 {
64 return beginLifecycle(0, COMM_SPI, COMM_SPI, [&]() {
65 return beginCommonStatic<SensorCommSPI, HalArduino>(comm, staticComm, hal, spi, csPin, mosi, miso, sck);
66 });
67 }
68
69#elif defined(ESP_PLATFORM)
70#if defined(SENSORLIB_USE_I2C_LEGACY)
79 bool begin(i2c_port_t port_num, uint8_t addr, int sda = -1, int scl = -1)
80 {
81 return beginLifecycle(addr, COMM_I2C, addr, [&]() {
82 return beginCommonStatic<SensorCommI2C, HalEspIDF>(comm, staticComm, hal, port_num, addr, sda, scl);
83 });
84 }
85#else
92 bool begin(i2c_master_bus_handle_t handle, uint8_t addr)
93 {
94 return beginLifecycle(addr, COMM_I2C, addr, [&]() {
95 return beginCommonStatic<SensorCommI2C, HalEspIDF>(comm, staticComm, hal, handle, addr);
96 });
97 }
98#endif
109 bool begin(spi_host_device_t host, spi_device_handle_t handle,
110 uint8_t csPin, int mosi = -1, int miso = -1, int sck = -1)
111 {
112 return beginLifecycle(0, COMM_SPI, COMM_SPI, [&]() {
113 return beginCommonStatic<SensorCommSPI, HalEspIDF>(comm, staticComm, hal, host, handle, csPin, mosi, miso, sck);
114 });
115 }
116#endif
126 bool begin(CommInterface interface,
129 uint8_t addr = 0)
130 {
131 return beginLifecycle(addr, interface, interface, [&]() {
132 return beginCommCustomCallback<SensorCommCustom, SensorCommCustomHal>(
133 interface, callback, hal_callback, addr, comm, hal);
134 });
135 }
136protected:
141 bool ensureValid() const override
142 {
143 if (!comm || !hal) return false;
144 return true;
145 }
146};
@license MIT License
CommInterface
@ COMM_SPI
@ COMM_I2C
bool begin(CommInterface interface, SensorCommCustom::CustomCallback callback, SensorCommCustomHal::CustomHalCallback hal_callback, uint8_t addr=0)
Initialize the sensor using custom callback interface.
bool ensureValid() const override
Ensure the communication interface is valid.
std::unique_ptr< SensorCommBase > comm
bool beginLifecycle(uint8_t addr, uint8_t iface, uint8_t initParam, CreateComm createComm)
std::unique_ptr< SensorCommStatic > staticComm
std::unique_ptr< SensorHal > hal
uint32_t(*)(Operation op, void *param1, void *param2) CustomHalCallback
bool(*)(uint8_t addr, uint8_t reg, uint8_t *buf, size_t len, bool writeReg, bool isWrite) CustomCallback
uint32_t hal_callback(SensorCommCustomHal::Operation op, void *param1, void *param2)