SensorLib 0.5.0
Multi-platform sensor driver library for Arduino, PlatformIO, and ESP-IDF
Loading...
Searching...
No Matches
AXP2101Pwron.cpp
Go to the documentation of this file.
1
31#include "../../../SensorBuildOpt.h"
32#if !SENSORLIB_EXCLUDE_PMIC_AXP2101
33
34#include "AXP2101Pwron.hpp"
35#include "AXP2101Regs.hpp"
36
37
39{
40}
41
42bool AXP2101Pwron::updateBits(uint8_t mask, uint8_t value)
43{
45 if (v < 0) return false;
46 uint8_t cur = static_cast<uint8_t>(v);
47 uint8_t next = (cur & ~mask) | (value & mask);
48 if (next == cur) return true;
49 return _core.writeReg(axp2101_regs::pmu::IRQ_OFF_ON_LEVEL, next) >= 0;
50}
51
53{
54 uint8_t idx = (ms >= 2000) ? 3 :
55 (ms >= 1000) ? 2 :
56 (ms >= 512) ? 1 : 0;
57 return updateBits(0x03, idx);
58}
59
60bool AXP2101Pwron::getOnDurationMs(uint16_t &ms) const
61{
62 const uint16_t kOnLevelMs[] = {128, 512, 1000, 2000};
64 if (v < 0) return false;
65 ms = kOnLevelMs[static_cast<uint8_t>(v) & 0x03];
66 return true;
67}
68
70{
71 uint8_t idx = (ms >= 10000) ? 3 :
72 (ms >= 8000) ? 2 :
73 (ms >= 6000) ? 1 : 0;
74 return updateBits(0x0C, idx << 2);
75}
76
77bool AXP2101Pwron::getOffDurationMs(uint16_t &ms) const
78{
79 const uint16_t kOffLevelMs[] = {4000, 6000, 8000, 10000};
81 if (v < 0) return false;
82 ms = kOffLevelMs[(static_cast<uint8_t>(v) >> 2) & 0x03];
83 return true;
84}
85
87{
88 uint8_t idx = (ms >= 2500) ? 3 :
89 (ms >= 2000) ? 2 :
90 (ms >= 1500) ? 1 : 0;
91 return updateBits(0x30, idx << 4);
92}
93
94bool AXP2101Pwron::getIrqDurationMs(uint16_t &ms) const
95{
96 const uint16_t kIrqLevelMs[] = {1000, 1500, 2000, 2500};
98 if (v < 0) return false;
99 ms = kIrqLevelMs[(static_cast<uint8_t>(v) >> 4) & 0x03];
100 return true;
101}
102
103#endif
@license MIT License
Core I2C communication layer for the AXP2101 PMIC.
bool getIrqDurationMs(uint16_t &ms) const override
Get the short-press / IRQ trigger delay.
bool setIrqDurationMs(uint16_t ms) override
Set the short-press / IRQ trigger delay.
bool setOffDurationMs(uint16_t ms) override
Set the power-off delay (held duration to turn off).
bool setOnDurationMs(uint16_t ms) override
Set the power-on delay (held duration to turn on).
bool getOnDurationMs(uint16_t &ms) const override
Get the power-on delay setting.
AXP2101Pwron(AXP2101Core &core)
bool getOffDurationMs(uint16_t &ms) const override
Get the power-off delay setting.
int readReg(uint8_t reg) const
int writeReg(uint8_t reg, uint8_t val)
static constexpr uint8_t IRQ_OFF_ON_LEVEL