wangzhibo
2026-10-08 220e4fab27b7f419b3e89e2f2d28ae789aac09d7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include "axp2101.h"
#include "board.h"
#include "display.h"
 
#include <esp_log.h>
 
#define TAG "Axp2101"
 
Axp2101::Axp2101(i2c_master_bus_handle_t i2c_bus, uint8_t addr) : I2cDevice(i2c_bus, addr) {
}
 
int Axp2101::GetBatteryCurrentDirection() {
    return (ReadReg(0x01) & 0b01100000) >> 5;
}
 
bool Axp2101::IsCharging() {
    return GetBatteryCurrentDirection() == 1;
}
 
bool Axp2101::IsDischarging() {
    return GetBatteryCurrentDirection() == 2;
}
 
bool Axp2101::IsChargingDone() {
    uint8_t value = ReadReg(0x01);
    return (value & 0b00000111) == 0b00000100;
}
 
int Axp2101::GetBatteryLevel() {
    return ReadReg(0xA4);
}
 
float Axp2101::GetTemperature() {
    return ReadReg(0xA5);
}
 
void Axp2101::PowerOff() {
    uint8_t value = ReadReg(0x10);
    value = value | 0x01;
    WriteReg(0x10, value);
}