26
26
* Adafruit's Register library: https://github.com/adafruit/Adafruit_CircuitPython_Register
27
27
"""
28
28
29
- import time
30
29
from micropython import const
31
30
from adafruit_bus_device import i2c_device
32
31
from adafruit_register .i2c_struct import ROUnaryStruct , UnaryStruct
58
57
ALERTFLAG_VOLTAGE_HIGH = 0x02
59
58
ALERTFLAG_RESET_INDICATOR = 0x01
60
59
60
+
61
61
class MAX17048 :
62
62
"""Driver for the MAX1704X battery fuel gauge.
63
63
:param ~busio.I2C i2c_bus: The I2C bus the MAX1704X is connected to.
@@ -69,8 +69,10 @@ class MAX17048:
69
69
70
70
_config = ROUnaryStruct (_MAX1704X_CONFIG_REG , ">H" )
71
71
# expose the config bits
72
- sleep = RWBit (_MAX1704X_CONFIG_REG + 1 , 7 , register_width = 2 , lsb_first = False )
73
- _alert_status = RWBit (_MAX1704X_CONFIG_REG + 1 , 5 , register_width = 2 , lsb_first = False )
72
+ sleep = RWBit (_MAX1704X_CONFIG_REG + 1 , 7 , register_width = 2 , lsb_first = False )
73
+ _alert_status = RWBit (
74
+ _MAX1704X_CONFIG_REG + 1 , 5 , register_width = 2 , lsb_first = False
75
+ )
74
76
enable_sleep = RWBit (_MAX1704X_MODE_REG , 5 )
75
77
hibernating = ROBit (_MAX1704X_MODE_REG , 4 )
76
78
quick_start = RWBit (_MAX1704X_MODE_REG , 6 )
@@ -81,10 +83,10 @@ class MAX17048:
81
83
_cell_SOC = ROUnaryStruct (_MAX1704X_SOC_REG , ">H" )
82
84
_cell_crate = ROUnaryStruct (_MAX1704X_CRATE_REG , ">h" )
83
85
_vreset = ROUnaryStruct (_MAX1704X_VRESET_REG , ">B" )
84
- _hibrt_actthr = UnaryStruct (_MAX1704X_HIBRT_REG + 1 , ">B" )
86
+ _hibrt_actthr = UnaryStruct (_MAX1704X_HIBRT_REG + 1 , ">B" )
85
87
_hibrt_hibthr = UnaryStruct (_MAX1704X_HIBRT_REG , ">B" )
86
88
_valrt_min = UnaryStruct (_MAX1704X_VALERT_REG , ">B" )
87
- _valrt_max = UnaryStruct (_MAX1704X_VALERT_REG + 1 , ">B" )
89
+ _valrt_max = UnaryStruct (_MAX1704X_VALERT_REG + 1 , ">B" )
88
90
89
91
# expose the alert bits
90
92
reset_alert = RWBit (_MAX1704X_STATUS_REG , 0 )
@@ -108,88 +110,100 @@ def __init__(self, i2c_bus, address=MAX1704X_I2CADDR_DEFAULT):
108
110
self .sleep = False
109
111
110
112
def reset (self ):
113
+ """Perform a soft reset of the chip"""
111
114
try :
112
- self ._cmd = 0x5400
115
+ self ._cmd = 0x5400
113
116
except OSError :
114
117
# aha! we NACKed, which is CORRECT!
115
118
pass
116
119
else :
117
120
raise RuntimeError ("Reset did not succeed" )
118
- self .reset_alert = False # clean up RI alert
121
+ self .reset_alert = False # clean up RI alert
119
122
120
123
@property
121
124
def cell_voltage (self ):
125
+ """The state of charge of the battery, in volts"""
122
126
return self ._cell_voltage * 78.125 / 1_000_000
123
127
124
128
@property
125
129
def cell_percent (self ):
130
+ """The state of charge of the battery, in percentage of 'fullness'"""
126
131
return self ._cell_SOC / 256.0
127
132
128
133
@property
129
134
def charge_rate (self ):
130
135
"""Charge or discharge rate of the battery in percent/hour"""
131
- return self ._cell_crate * 0.208
136
+ return self ._cell_crate * 0.208
132
137
133
138
@property
134
139
def reset_voltage (self ):
135
- return self ._reset_voltage * .04 # 40mV / LSB
140
+ """The voltage that will determine whether the chip will consider it a reset/swap"""
141
+ return self ._reset_voltage * 0.04 # 40mV / LSB
136
142
137
143
@reset_voltage .setter
138
144
def reset_voltage (self , reset_v ):
139
- if ( not 0 <= reset_v <= (127 * 0.04 ) ):
145
+ if not 0 <= reset_v <= (127 * 0.04 ):
140
146
raise ValueError ("Reset voltage must be between 0 and 5.1 Volts" )
141
- self ._reset_voltage = int (reset_v / .04 ) # 40mV / LSB
147
+ self ._reset_voltage = int (reset_v / 0 .04 ) # 40mV / LSB
142
148
143
149
@property
144
150
def voltage_alert_min (self ):
145
- return self ._valrt_min * .02 # 20mV / LSB
151
+ """The lower-limit voltage for the voltage alert"""
152
+ return self ._valrt_min * 0.02 # 20mV / LSB
146
153
147
154
@voltage_alert_min .setter
148
155
def voltage_alert_min (self , minvoltage ):
149
- if ( not 0 <= minvoltage <= (255 * 0.02 ) ):
156
+ if not 0 <= minvoltage <= (255 * 0.02 ):
150
157
raise ValueError ("Alert voltage must be between 0 and 5.1 Volts" )
151
- self ._valrt_min = int (minvoltage / .02 ) # 20mV / LSB
158
+ self ._valrt_min = int (minvoltage / 0 .02 ) # 20mV / LSB
152
159
153
160
@property
154
161
def voltage_alert_max (self ):
155
- return self ._valrt_max * .02 # 20mV / LSB
162
+ """The upper-limit voltage for the voltage alert"""
163
+ return self ._valrt_max * 0.02 # 20mV / LSB
156
164
157
165
@voltage_alert_max .setter
158
166
def voltage_alert_max (self , maxvoltage ):
159
- if ( not 0 <= maxvoltage <= (255 * 0.02 ) ):
167
+ if not 0 <= maxvoltage <= (255 * 0.02 ):
160
168
raise ValueError ("Alert voltage must be between 0 and 5.1 Volts" )
161
- self ._valrt_max = int (maxvoltage / .02 ) # 20mV / LSB
169
+ self ._valrt_max = int (maxvoltage / 0 .02 ) # 20mV / LSB
162
170
163
171
@property
164
172
def active_alert (self ):
173
+ """Whether there is an active alert to be checked"""
165
174
return self ._alert_status
166
175
167
176
@property
168
177
def alert_reason (self ):
178
+ """The 7 bits of alert-status that can be checked at once for flags"""
169
179
return self ._status & 0x3F
170
180
171
-
172
181
@property
173
182
def activity_threshold (self ):
183
+ """The absolute change in battery voltage that will trigger hibernation"""
174
184
return self ._hibrt_actthr * 0.00125 # 1.25mV per LSB
175
185
176
186
@activity_threshold .setter
177
187
def activity_threshold (self , threshold_voltage ):
178
- if (not 0 <= threshold_voltage <= (255 * 0.00125 )):
179
- raise ValueError ("Activity voltage change must be between 0 and 0.31875 Volts" )
180
- self ._hibrt_actthr = int (threshold_voltage / 0.00125 ) # 1.25mV per LSB
181
-
188
+ if not 0 <= threshold_voltage <= (255 * 0.00125 ):
189
+ raise ValueError (
190
+ "Activity voltage change must be between 0 and 0.31875 Volts"
191
+ )
192
+ self ._hibrt_actthr = int (threshold_voltage / 0.00125 ) # 1.25mV per LSB
182
193
183
194
@property
184
195
def hibernation_threshold (self ):
196
+ """The absolute-value percent-per-hour change in charge rate
197
+ that will trigger hibernation"""
185
198
return self ._hibrt_hibthr * 0.208 # 0.208% per hour
186
199
187
200
@hibernation_threshold .setter
188
201
def hibernation_threshold (self , threshold_percent ):
189
- if (not 0 <= threshold_percent <= (255 * 0.208 )):
190
- raise ValueError ("Activity percentage/hour change must be between 0 and 53%" )
191
- self ._hibrt_hibthr = int (threshold_percent / 0.208 ) # 0.208% per hour
192
-
202
+ if not 0 <= threshold_percent <= (255 * 0.208 ):
203
+ raise ValueError (
204
+ "Activity percentage/hour change must be between 0 and 53%"
205
+ )
206
+ self ._hibrt_hibthr = int (threshold_percent / 0.208 ) # 0.208% per hour
193
207
194
208
def hibernate (self ):
195
209
"""Setup thresholds for hibernation to go into hibernation mode immediately.
@@ -204,5 +218,3 @@ def wake(self):
204
218
always use hibernate mode, set HIBRT = 0xFFFF. Can check status with `self.hibernating`"""
205
219
self ._hibrt_hibthr = 0
206
220
self ._hibrt_actthr = 0
207
-
208
-
0 commit comments