-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathIP2368.cpp
More file actions
72 lines (55 loc) · 1.55 KB
/
IP2368.cpp
File metadata and controls
72 lines (55 loc) · 1.55 KB
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include "IP2368.h"
#define IP2368_ADDRESS 0x75
// Registers
#define SOC_CAP_DATA 0x30
#define RSTATE_CTL0 0x31
#define RSTATE_CTL1 0x32
#define STATE_CTL2 0x33
#define RTYPEC_STATE 0x34
IP2368::IP2368() {}
void IP2368::begin() {
Wire.begin();
}
uint8_t IP2368::readRegister(uint8_t regAddress) {
delay(2);
Wire.beginTransmission(IP2368_ADDRESS);
Wire.write(regAddress);
uint8_t errorCode = Wire.endTransmission(false); // 不發送停止信號
if (errorCode) {
//Serial.print("Error during transmission: ");
//Serial.println(errorCode);
return -1;
}
uint8_t bytesRead = Wire.requestFrom(IP2368_ADDRESS, (uint8_t)1, (bool)true); // 請求1個字節,並發送停止信號
if (bytesRead != 1) {
//Serial.println("Failed to read from device!");
return -1;
}
return Wire.read();
}
uint8_t IP2368::getBatteryPercentage() {
return readRegister(SOC_CAP_DATA);
}
bool IP2368::isCharging() {
return readRegister(RSTATE_CTL0) & (1 << 5);
}
bool IP2368::isChargeFull() {
return readRegister(RSTATE_CTL0) & (1 << 4);
}
bool IP2368::isDischarging() {
return readRegister(RSTATE_CTL0) & (1 << 3);
}
IP2368::ChargeState IP2368::getChargeState() {
uint8_t data = readRegister(RSTATE_CTL0);
uint8_t stateBits = data & 0x07;
return static_cast<ChargeState>(stateBits);
}
bool IP2368::isVbusPresent() {
return readRegister(STATE_CTL2) & (1 << 7);
}
bool IP2368::isVbusOvervoltage() {
return readRegister(STATE_CTL2) & (1 << 6);
}
bool IP2368::isTypeCSinkConnected() {
return readRegister(RTYPEC_STATE) & (1 << 7);
}