SensorLib 0.5.0
Multi-platform sensor driver library for Arduino, PlatformIO, and ESP-IDF
Loading...
Searching...
No Matches
AXP1xxAdc_impl.hpp
Go to the documentation of this file.
1
30#pragma once
31
32#include "AXP1xxAdc.hpp"
33
34namespace axp1xx
35{
36
37template<typename CoreType, typename RegTraits>
39{
40 uint8_t en1 = 0, en2 = 0;
41 bool hasSpecial = false;
42
43 // Translate each Channel bit to chip-specific register mask
44 forEachChannel(mask, [&](uint32_t bit) {
45 auto ch = static_cast<PmicAdcBase::Channel>(bit);
46 uint32_t hwMask = RegTraits::getChannelMask(ch);
47 en1 |= hwMask & 0xFF;
48 en2 |= (hwMask >> 8) & 0xFF;
49 if (RegTraits::hasSpecialEnablement(ch)) {
50 hasSpecial = true;
51 }
52 });
53
54 if (en1 || en2) {
55 if (en1) {
56 int val = _core.readReg(RegTraits::ADC_EN1_ADDR);
57 if (val < 0) return false;
58 val |= en1;
59 if (_core.writeReg(RegTraits::ADC_EN1_ADDR, static_cast<uint8_t>(val)) < 0) {
60 return false;
61 }
62 }
63 if (en2) {
64 int val = _core.readReg(RegTraits::ADC_EN2_ADDR);
65 if (val < 0) return false;
66 val |= en2;
67 if (_core.writeReg(RegTraits::ADC_EN2_ADDR, static_cast<uint8_t>(val)) < 0) {
68 return false;
69 }
70 }
71 } else {
72 return false; // No supported channels in mask
73 }
74
75 // Handle special enablement requirements (e.g., TS pin)
76 if (hasSpecial) {
77 forEachChannel(mask, [&](uint32_t bit) {
78 auto ch = static_cast<PmicAdcBase::Channel>(bit);
79 if (RegTraits::hasSpecialEnablement(ch)) {
80 RegTraits::enableSpecialChannel(_core, ch);
81 }
82 });
83 }
84
85 return true;
86}
87
88template<typename CoreType, typename RegTraits>
90{
91 uint8_t en1 = 0, en2 = 0;
92 bool hasSpecial = false;
93
94 forEachChannel(mask, [&](uint32_t bit) {
95 auto ch = static_cast<PmicAdcBase::Channel>(bit);
96 uint32_t hwMask = RegTraits::getChannelMask(ch);
97 en1 |= hwMask & 0xFF;
98 en2 |= (hwMask >> 8) & 0xFF;
99 if (RegTraits::hasSpecialEnablement(ch)) {
100 hasSpecial = true;
101 }
102 });
103
104 if (en1 || en2) {
105 if (en1) {
106 int val = _core.readReg(RegTraits::ADC_EN1_ADDR);
107 if (val < 0) return false;
108 val &= ~en1;
109 if (_core.writeReg(RegTraits::ADC_EN1_ADDR, static_cast<uint8_t>(val)) < 0) {
110 return false;
111 }
112 }
113 if (en2) {
114 int val = _core.readReg(RegTraits::ADC_EN2_ADDR);
115 if (val < 0) return false;
116 val &= ~en2;
117 if (_core.writeReg(RegTraits::ADC_EN2_ADDR, static_cast<uint8_t>(val)) < 0) {
118 return false;
119 }
120 }
121 } else {
122 return false;
123 }
124
125 if (hasSpecial) {
126 forEachChannel(mask, [&](uint32_t bit) {
127 auto ch = static_cast<PmicAdcBase::Channel>(bit);
128 if (RegTraits::hasSpecialEnablement(ch)) {
129 RegTraits::disableSpecialChannel(_core, ch);
130 }
131 });
132 }
133
134 return true;
135}
136
137template<typename CoreType, typename RegTraits>
138uint16_t AXP1xxAdc<CoreType, RegTraits>::readH8L4(uint8_t regH, uint8_t regL)
139{
140 int h = _core.readReg(regH);
141 int l = _core.readReg(regL);
142 if (h < 0 || l < 0) return 0;
143 return (static_cast<uint16_t>(h) << 4) | (l & 0x0F);
144}
145
146template<typename CoreType, typename RegTraits>
147uint16_t AXP1xxAdc<CoreType, RegTraits>::readH8L5(uint8_t regH, uint8_t regL)
148{
149 int h = _core.readReg(regH);
150 int l = _core.readReg(regL);
151 if (h < 0 || l < 0) return 0;
152 return (static_cast<uint16_t>(h) << 5) | (l & 0x1F);
153}
154
155template<typename CoreType, typename RegTraits>
157{
158 out = NAN;
159
160 // Delegate channel-specific read logic to RegTraits
161 return RegTraits::readChannel(_core, ch, out);
162}
163
164} // namespace axp1xx
@license MIT License
Channel
ADC channel bitmask.
bool disableChannels(uint32_t mask) override
Disable one or more ADC channels.
bool enableChannels(uint32_t mask) override
Enable one or more ADC channels.
bool read(Channel ch, float &out) override
Read a single ADC channel value.