SensorLib 0.5.0
Multi-platform sensor driver library for Arduino, PlatformIO, and ESP-IDF
Loading...
Searching...
No Matches
AXP517Pwron.cpp
Go to the documentation of this file.
1
30#include "../../../SensorBuildOpt.h"
31#if !SENSORLIB_EXCLUDE_PMIC_AXP517
32
33#include "AXP517Pwron.hpp"
34#include "AXP517Regs.hpp"
35
36
38 : _core(core)
39{
40}
41
42bool AXP517Pwron::updateBits(uint8_t mask, uint8_t value)
43{
45 if (v < 0) return false;
46 uint8_t cur = (uint8_t)v;
47 uint8_t next = (uint8_t)((cur & ~mask) | (value & mask));
48 if (next == cur) return true;
49 return _core.writeReg(axp517_regs::ctrl::PWRON_CFG, next) >= 0;
50}
51
52
54{
55 uint8_t idx = (ms >= 2000) ? 3 :
56 (ms >= 1000) ? 2 :
57 (ms >= 512) ? 1 : 0;
58 return updateBits(0x03, idx);
59}
60
61bool AXP517Pwron::getOnDurationMs(uint16_t &ms) const
62{
63 // ONLEVEL: 00=128ms, 01=512ms, 10=1000ms, 11=2000ms
64 const uint16_t kOnLevelMs[] = {128, 512, 1000, 2000};
66 if (v < 0) return false;
67 ms = kOnLevelMs[((uint8_t)v) & 0x03];
68 return true;
69}
70
72{
73 uint8_t idx = (ms >= 10000) ? 3 :
74 (ms >= 8000) ? 2 :
75 (ms >= 6000) ? 1 : 0;
76 return updateBits(0x0C, idx << 2);
77}
78
79bool AXP517Pwron::getOffDurationMs(uint16_t &ms) const
80{
81 // OFFLEVEL: 00=4000ms, 01=6000ms, 10=8000ms, 11=10000ms
82 const uint16_t kOffLevelMs[] = {4000, 6000, 8000, 10000};
84 if (v < 0) return false;
85 ms = kOffLevelMs[(((uint8_t)v) >> 2) & 0x03];
86 return true;
87}
88
89
91{
92 uint8_t idx = (ms >= 2500) ? 3 :
93 (ms >= 2000) ? 2 :
94 (ms >= 1500) ? 1 : 0;
95 return updateBits(0x30, idx << 4);
96}
97
98bool AXP517Pwron::getIrqDurationMs(uint16_t &ms) const
99{
100 // IRQLEVEL: 00=1000ms, 01=1500ms, 10=2000ms, 11=2500ms
101 const uint16_t kIrqLevelMs[] = {1000, 1500, 2000, 2500};
103 if (v < 0) return false;
104 ms = kIrqLevelMs[(((uint8_t)v) >> 4) & 0x03];
105 return true;
106}
107
108#endif
@license MIT License
@license MIT License
AXP517 low-level core driver.
AXP517Pwron(AXP517Core &core)
Construct an AXP517 PWRON button driver.
bool setOffDurationMs(uint16_t ms) override
Set the power-off debounce duration.
bool setOnDurationMs(uint16_t ms) override
Set the power-on debounce duration.
bool getOnDurationMs(uint16_t &ms) const override
Get the current power-on debounce duration.
bool getIrqDurationMs(uint16_t &ms) const override
Get the current button IRQ trigger duration.
bool getOffDurationMs(uint16_t &ms) const override
Get the current power-off debounce duration.
bool setIrqDurationMs(uint16_t ms) override
Set the button IRQ trigger duration.
int readReg(uint8_t reg) const
int writeReg(uint8_t reg, uint8_t val)
static constexpr uint8_t PWRON_CFG
PWRON configure.