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