SensorLib 0.5.0
Multi-platform sensor driver library for Arduino, PlatformIO, and ESP-IDF
Loading...
Searching...
No Matches
SensorPlatform.hpp
Go to the documentation of this file.
1
30#pragma once
31
32#if defined(ARDUINO)
37#elif !defined(ARDUINO) && defined(ESP_PLATFORM)
41#endif
45#include <assert.h>
46
52
53template<typename CommType, typename HalType, typename... Args>
55 std::unique_ptr<SensorCommBase> &comm,
56 std::unique_ptr<SensorCommStatic> &staticComm,
57 std::unique_ptr<SensorHal> &hal,
58 Args &&... args)
59{
60 hal = std::make_unique<HalType>();
61 if (!hal) {
62 return false;
63 }
64 comm = std::make_unique<CommType>(std::forward<Args>(args)..., hal.get());
65 if (!comm) {
66 hal.reset();
67 return false;
68 }
69 if (!comm->init()) {
70 return false;
71 }
72 staticComm = std::make_unique<SensorCommStatic>(comm.get(), hal.get());
73 if (!staticComm) {
74 return false;
75 }
76 return true;
77}
78
79template<typename CommType, typename HalType, typename... Args>
81 std::unique_ptr<SensorCommBase> &comm,
82 std::unique_ptr<SensorHal> &hal,
83 Args &&... args)
84{
85 hal = std::make_unique<HalType>();
86 if (!hal) {
87 return false;
88 }
89 comm = std::make_unique<CommType>(std::forward<Args>(args)..., hal.get());
90 if (!comm) {
91 hal.reset();
92 return false;
93 }
94 return comm->init();
95}
96
97template <typename CommType, typename HalType>
99 typename CommType::CustomCallback callback,
100 typename HalType::CustomHalCallback hal_callback,
101 uint8_t addr,
102 std::unique_ptr<SensorCommBase> &comm,
103 std::unique_ptr<SensorHal> &hal)
104{
105 hal = std::make_unique<HalType>(hal_callback);
106 if (!hal) {
107 return false;
108 }
109 comm = std::make_unique<CommType>(callback, addr);
110 if (!comm) {
111 return false;
112 }
113 return comm->init();
114}
115
116inline void addrToBeBuf(uint32_t addr, uint8_t buf[4])
117{
118 buf[0] = (addr >> 24) & 0xFF;
119 buf[1] = (addr >> 16) & 0xFF;
120 buf[2] = (addr >> 8) & 0xFF;
121 buf[3] = addr & 0xFF;
122}
123
124inline void addrToBeBuf(uint16_t addr, uint8_t buf[2])
125{
126 buf[0] = (addr >> 8) & 0xFF;
127 buf[1] = addr & 0xFF;
128}
@license MIT License
@license MIT License
@license MIT License
@license MIT License
@license MIT License
@license MIT License
@license MIT License
@license MIT License
@license MIT License
bool beginCommon(std::unique_ptr< SensorCommBase > &comm, std::unique_ptr< SensorHal > &hal, Args &&... args)
bool beginCommCustomCallback(CommInterface interface, typename CommType::CustomCallback callback, typename HalType::CustomHalCallback hal_callback, uint8_t addr, std::unique_ptr< SensorCommBase > &comm, std::unique_ptr< SensorHal > &hal)
void addrToBeBuf(uint32_t addr, uint8_t buf[4])
bool beginCommonStatic(std::unique_ptr< SensorCommBase > &comm, std::unique_ptr< SensorCommStatic > &staticComm, std::unique_ptr< SensorHal > &hal, Args &&... args)
CommInterface
@ COMM_CUSTOM
@ COMM_SPI
@ COMM_I2C
uint32_t hal_callback(SensorCommCustomHal::Operation op, void *param1, void *param2)
char buf[64]