-
Notifications
You must be signed in to change notification settings - Fork 10
/
LiFuelGauge.cpp
executable file
·201 lines (179 loc) · 5.71 KB
/
LiFuelGauge.cpp
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/**
* Name: LiFuelGauge
* Author: Nick Lamprianidis <nlamprian@gmail.com>
* Version: 1.0
* Description: A library for interfacing the MAXIM MAX17043/MAX17044
* Li+ fuel gauges. These ICs report the relative state of charge
* of the connected Lithium Ion Polymer battery, and the library
* can help you configure them and communicate with them
* Source: https://github.com/nlamprian/LiFuelGauge
* License: Copyright (c) 2014 Nick Lamprianidis
* This library is licensed under the MIT license
* http://www.opensource.org/licenses/mit-license.php
*
* Filename: LiFuelGauge.cpp
* File description: Implementations of methods for the LiFuelGauge library
*/
#include "LiFuelGauge.h"
// Initializes variables and the Wire library
LiFuelGauge::LiFuelGauge(gaugeType ic) : _ic(ic), _f(NULL) { Wire.begin(); }
// Initializes varaibles and the Wire library
// Assigns ISR f to interrupt intr (for Alert Interrupt)
LiFuelGauge::LiFuelGauge(gaugeType ic, uint8_t intr, func f) : _ic(ic), _f(f)
{
attachInterrupt(intr, f, FALLING);
Wire.begin();
}
// Returns a measurement of the voltage of the connected LiIon Polymer battery
// 0-5V range w/ 1.25mV resolution for the MAX17043
// 0-10V range w/ 2.5mV resolution for the MAX17044
double LiFuelGauge::getVoltage()
{
Wire.beginTransmission(MAX1704X_ADDR);
Wire.write(MAX1704X_VCELL_ADDR);
Wire.endTransmission(false);
Wire.requestFrom(MAX1704X_ADDR, (uint8_t)2);
return ( (Wire.read() << 4) + (Wire.read() >> 4) ) * 0.00125 * _ic;
}
// Returns the relative state of charge of the connected LiIon Polymer battery
// as a percentage of the full capacity w/ resolution 1/256%
double LiFuelGauge::getSOC()
{
Wire.beginTransmission(MAX1704X_ADDR);
Wire.write(MAX1704X_SOC_ADDR);
Wire.endTransmission(false);
Wire.requestFrom(MAX1704X_ADDR, (uint8_t)2);
return Wire.read() + (double) Wire.read() / 256;
}
// Returns the production version of the IC
uint16_t LiFuelGauge::getVersion()
{
Wire.beginTransmission(MAX1704X_ADDR);
Wire.write(MAX1704X_VERSION_ADDR);
Wire.endTransmission(false);
Wire.requestFrom(MAX1704X_ADDR, (uint8_t)2);
return ( Wire.read() << 8 ) + Wire.read();
}
// Returns a value used to optimize IC performance
// to different operating conditions
uint8_t LiFuelGauge::getCompensation()
{
Wire.beginTransmission(MAX1704X_ADDR);
Wire.write(MAX1704X_RCOMP_ADDR);
Wire.endTransmission(false);
Wire.requestFrom(MAX1704X_ADDR, (uint8_t)1);
return Wire.read();
}
// Returns the alert threshold as a percentage
// below which an alert interrupt is generated
uint8_t LiFuelGauge::getAlertThreshold()
{
return ( ~getStatus() & 0x1F ) + 1;
}
// Helper method. Returns the LSByte of the CONFIG register
uint8_t LiFuelGauge::getStatus()
{
Wire.beginTransmission(MAX1704X_ADDR);
Wire.write(MAX1704X_ATHRD_ADDR);
Wire.endTransmission(false);
Wire.requestFrom(MAX1704X_ADDR, (uint8_t)1);
return Wire.read();
}
// Sets a value to the MSByte of the CONFIG register used to optimize
// IC performance to different operating conditions
// Returns the status of transmission
uint8_t LiFuelGauge::setCompensation(uint8_t comp)
{
uint8_t status = getStatus();
Wire.beginTransmission(MAX1704X_ADDR);
Wire.write(MAX1704X_CONFIG_ADDR);
Wire.write(comp);
Wire.write(status);
return Wire.endTransmission();
}
// Sets the alert threshold below which an alert interrupt is generated
// The acceptable range is 1-32%. Default threshold is 4%
// Returns the status of transmission
uint8_t LiFuelGauge::setAlertThreshold(uint8_t thrd)
{
if ( thrd > 32 ) thrd = 32;
else if ( thrd < 1 ) thrd = 1;
thrd = ( ~thrd + 1 ) & 0x1F;
uint8_t comp, sleepBit;
Wire.beginTransmission(MAX1704X_ADDR);
Wire.write(MAX1704X_CONFIG_ADDR);
Wire.endTransmission(false);
Wire.requestFrom(MAX1704X_ADDR, (uint8_t)2);
comp = Wire.read();
sleepBit = Wire.read() & 0x80;
Wire.beginTransmission(MAX1704X_ADDR);
Wire.write(MAX1704X_CONFIG_ADDR);
Wire.write(comp);
Wire.write(sleepBit | thrd);
return Wire.endTransmission();
}
// After an alert interrupt has been generated,
// it clears the alert bit on the CONFIG register
// Returns the status of transmission
uint8_t LiFuelGauge::clearAlertInterrupt()
{
uint8_t comp = getCompensation();
uint8_t status = getStatus();
Wire.beginTransmission(MAX1704X_ADDR);
Wire.write(MAX1704X_CONFIG_ADDR);
Wire.write(comp);
Wire.write(0xDF & status);
return Wire.endTransmission();
}
// It puts the MAX1704X to sleep
// All IC operations are halted
// Returns the status of transmission
uint8_t LiFuelGauge::sleep()
{
uint8_t comp = getCompensation();
uint8_t thrd = getAlertThreshold();
Wire.beginTransmission(MAX1704X_ADDR);
Wire.write(MAX1704X_CONFIG_ADDR);
Wire.write(comp);
Wire.write(0x80 | thrd);
return Wire.endTransmission();
}
// It wakes the MAX1704X from sleep mode
// Returns the status of transmission
uint8_t LiFuelGauge::wake()
{
uint8_t comp = getCompensation();
uint8_t thrd = getAlertThreshold();
Wire.beginTransmission(MAX1704X_ADDR);
Wire.write(MAX1704X_CONFIG_ADDR);
Wire.write(comp);
Wire.write(0x7F & thrd);
return Wire.endTransmission();
}
// Returns a value corresponding to
// whether the MAX1704X is in sleep mode
boolean LiFuelGauge::sleeping()
{
return ( getStatus() & 0x80 ) == 0x80;
}
// It forces the MAX1704X to
// restart fuel-gauge calculations
// Returns the status of transmission
uint8_t LiFuelGauge::quickStart()
{
Wire.beginTransmission(MAX1704X_ADDR);
Wire.write(MAX1704X_MODE_ADDR);
Wire.write(0x40);
Wire.write(0x00);
return Wire.endTransmission();
}
// It forces the MAX1704X to completely reset
// Returns the status of transmission
uint8_t LiFuelGauge::reset()
{
Wire.beginTransmission(MAX1704X_ADDR);
Wire.write(MAX1704X_COMMAND_ADDR);
Wire.write(0x54);
Wire.write(0x00);
return Wire.endTransmission();
}