Skip to content

Commit 25d801e

Browse files
committed
lintblack
1 parent 5eb7028 commit 25d801e

File tree

3 files changed

+69
-47
lines changed

3 files changed

+69
-47
lines changed

adafruit_max1704x.py

Lines changed: 40 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
* Adafruit's Register library: https://github.com/adafruit/Adafruit_CircuitPython_Register
2727
"""
2828

29-
import time
3029
from micropython import const
3130
from adafruit_bus_device import i2c_device
3231
from adafruit_register.i2c_struct import ROUnaryStruct, UnaryStruct
@@ -58,6 +57,7 @@
5857
ALERTFLAG_VOLTAGE_HIGH = 0x02
5958
ALERTFLAG_RESET_INDICATOR = 0x01
6059

60+
6161
class MAX17048:
6262
"""Driver for the MAX1704X battery fuel gauge.
6363
:param ~busio.I2C i2c_bus: The I2C bus the MAX1704X is connected to.
@@ -69,8 +69,10 @@ class MAX17048:
6969

7070
_config = ROUnaryStruct(_MAX1704X_CONFIG_REG, ">H")
7171
# 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+
)
7476
enable_sleep = RWBit(_MAX1704X_MODE_REG, 5)
7577
hibernating = ROBit(_MAX1704X_MODE_REG, 4)
7678
quick_start = RWBit(_MAX1704X_MODE_REG, 6)
@@ -81,10 +83,10 @@ class MAX17048:
8183
_cell_SOC = ROUnaryStruct(_MAX1704X_SOC_REG, ">H")
8284
_cell_crate = ROUnaryStruct(_MAX1704X_CRATE_REG, ">h")
8385
_vreset = ROUnaryStruct(_MAX1704X_VRESET_REG, ">B")
84-
_hibrt_actthr = UnaryStruct(_MAX1704X_HIBRT_REG+1, ">B")
86+
_hibrt_actthr = UnaryStruct(_MAX1704X_HIBRT_REG + 1, ">B")
8587
_hibrt_hibthr = UnaryStruct(_MAX1704X_HIBRT_REG, ">B")
8688
_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")
8890

8991
# expose the alert bits
9092
reset_alert = RWBit(_MAX1704X_STATUS_REG, 0)
@@ -108,88 +110,100 @@ def __init__(self, i2c_bus, address=MAX1704X_I2CADDR_DEFAULT):
108110
self.sleep = False
109111

110112
def reset(self):
113+
"""Perform a soft reset of the chip"""
111114
try:
112-
self._cmd = 0x5400
115+
self._cmd = 0x5400
113116
except OSError:
114117
# aha! we NACKed, which is CORRECT!
115118
pass
116119
else:
117120
raise RuntimeError("Reset did not succeed")
118-
self.reset_alert = False # clean up RI alert
121+
self.reset_alert = False # clean up RI alert
119122

120123
@property
121124
def cell_voltage(self):
125+
"""The state of charge of the battery, in volts"""
122126
return self._cell_voltage * 78.125 / 1_000_000
123127

124128
@property
125129
def cell_percent(self):
130+
"""The state of charge of the battery, in percentage of 'fullness'"""
126131
return self._cell_SOC / 256.0
127132

128133
@property
129134
def charge_rate(self):
130135
"""Charge or discharge rate of the battery in percent/hour"""
131-
return self._cell_crate * 0.208
136+
return self._cell_crate * 0.208
132137

133138
@property
134139
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
136142

137143
@reset_voltage.setter
138144
def reset_voltage(self, reset_v):
139-
if (not 0 <= reset_v <= (127 * 0.04)):
145+
if not 0 <= reset_v <= (127 * 0.04):
140146
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
142148

143149
@property
144150
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
146153

147154
@voltage_alert_min.setter
148155
def voltage_alert_min(self, minvoltage):
149-
if (not 0 <= minvoltage <= (255 * 0.02)):
156+
if not 0 <= minvoltage <= (255 * 0.02):
150157
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
152159

153160
@property
154161
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
156164

157165
@voltage_alert_max.setter
158166
def voltage_alert_max(self, maxvoltage):
159-
if (not 0 <= maxvoltage <= (255 * 0.02)):
167+
if not 0 <= maxvoltage <= (255 * 0.02):
160168
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
162170

163171
@property
164172
def active_alert(self):
173+
"""Whether there is an active alert to be checked"""
165174
return self._alert_status
166175

167176
@property
168177
def alert_reason(self):
178+
"""The 7 bits of alert-status that can be checked at once for flags"""
169179
return self._status & 0x3F
170180

171-
172181
@property
173182
def activity_threshold(self):
183+
"""The absolute change in battery voltage that will trigger hibernation"""
174184
return self._hibrt_actthr * 0.00125 # 1.25mV per LSB
175185

176186
@activity_threshold.setter
177187
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
182193

183194
@property
184195
def hibernation_threshold(self):
196+
"""The absolute-value percent-per-hour change in charge rate
197+
that will trigger hibernation"""
185198
return self._hibrt_hibthr * 0.208 # 0.208% per hour
186199

187200
@hibernation_threshold.setter
188201
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
193207

194208
def hibernate(self):
195209
"""Setup thresholds for hibernation to go into hibernation mode immediately.
@@ -204,5 +218,3 @@ def wake(self):
204218
always use hibernate mode, set HIBRT = 0xFFFF. Can check status with `self.hibernating`"""
205219
self._hibrt_hibthr = 0
206220
self._hibrt_actthr = 0
207-
208-

examples/max1704x_advanced.py

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,17 @@
44

55
import time
66
import board
7-
import adafruit_max1704x
7+
import adafruit_max1704x
88

99
i2c = board.I2C() # uses board.SCL and board.SDA
1010
max17 = adafruit_max1704x.MAX17048(i2c)
1111

12-
print("Found MAX1704x with chip version", hex(max17.chip_version), "and id", hex(max17.chip_id))
12+
print(
13+
"Found MAX1704x with chip version",
14+
hex(max17.chip_version),
15+
"and id",
16+
hex(max17.chip_id),
17+
)
1318

1419
# Quick starting allows an instant 'auto-calibration' of the battery. However, its a bad idea
1520
# to do this right when the battery is first plugged in or if there's a lot of load on the battery
@@ -19,7 +24,7 @@
1924

2025
# The reset voltage is what the chip considers 'battery has been removed and replaced'
2126
# The default is 3.0 Volts but you can change it here:
22-
#max17.reset_voltage = 2.5
27+
# max17.reset_voltage = 2.5
2328
print("MAX1704x reset voltage = %0.1f V" % max17.reset_voltage)
2429

2530
# The analog comparator is used to detect the rest voltage, if you don't think the battery
@@ -32,16 +37,16 @@
3237

3338
# Hibernation mode reduces how often the ADC is read, for power reduction. There is an automatic
3439
# enter/exit mode but you can also customize the activity threshold both as voltage and charge rate
35-
#max17.activity_threshold = 0.15
40+
# max17.activity_threshold = 0.15
3641
print("MAX1704x activity threshold = %0.2f V" % max17.activity_threshold)
3742

38-
#max17.hibernation_threshold = 5
43+
# max17.hibernation_threshold = 5
3944
print("MAX1704x hibernation threshold = %0.2f %%" % max17.hibernation_threshold)
4045

4146
# You can also 'force' hibernation mode!
42-
#max17.hibernate()
47+
# max17.hibernate()
4348
# ...or force it to wake up!
44-
#max17.wake()
49+
# max17.wake()
4550

4651
# The alert pin can be used to detect when the voltage of the battery goes below or
4752
# above a voltage, you can also query the alert in the loop.
@@ -52,8 +57,8 @@
5257

5358
print("")
5459
while True:
55-
print(f'Battery voltage: {max17.cell_voltage:.2f} Volts')
56-
print(f'Battery state : {max17.cell_percent:.1f} %')
60+
print(f"Battery voltage: {max17.cell_voltage:.2f} Volts")
61+
print(f"Battery state : {max17.cell_percent:.1f} %")
5762

5863
# we can check if we're hibernating or not
5964
if max17.hibernating:
@@ -63,26 +68,26 @@
6368
print("Alert!")
6469
if max17.reset_alert:
6570
print(" Reset indicator")
66-
max17.reset_alert = False # clear the alert
71+
max17.reset_alert = False # clear the alert
6772

6873
if max17.voltage_high_alert:
6974
print(" Voltage high")
70-
max17.voltage_high_alert = False # clear the alert
75+
max17.voltage_high_alert = False # clear the alert
7176

7277
if max17.voltage_low_alert:
7378
print(" Voltage low")
74-
max17.voltage_low_alert = False # clear the alert
79+
max17.voltage_low_alert = False # clear the alert
7580

7681
if max17.voltage_reset_alert:
7782
print(" Voltage reset")
78-
max17.voltage_reset_alert = False # clear the alert
83+
max17.voltage_reset_alert = False # clear the alert
7984

8085
if max17.SOC_low_alert:
8186
print(" Charge low")
82-
max17.SOC_low_alert = False # clear the alert
87+
max17.SOC_low_alert = False # clear the alert
8388

8489
if max17.SOC_change_alert:
8590
print(" Charge changed")
86-
max17.SOC_change_alert = False # clear the alert
91+
max17.SOC_change_alert = False # clear the alert
8792
print("")
8893
time.sleep(1)

examples/max1704x_simpletest.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,17 @@
44

55
import time
66
import board
7-
import adafruit_max1704x
7+
import adafruit_max1704x
88

99
i2c = board.I2C() # uses board.SCL and board.SDA
1010
max17 = adafruit_max1704x.MAX17048(i2c)
1111

12-
print("Found MAX1704x with chip version", hex(max17.chip_version), "and id", hex(max17.chip_id))
12+
print(
13+
"Found MAX1704x with chip version",
14+
hex(max17.chip_version),
15+
"and id",
16+
hex(max17.chip_id),
17+
)
1318

1419
# Quick starting allows an instant 'auto-calibration' of the battery. However, its a bad idea
1520
# to do this right when the battery is first plugged in or if there's a lot of load on the battery
@@ -18,7 +23,7 @@
1823
# max17.quick_start = True
1924

2025
while True:
21-
print(f'Battery voltage: {max17.cell_voltage:.2f} Volts')
22-
print(f'Battery state : {max17.cell_percent:.1f} %')
26+
print(f"Battery voltage: {max17.cell_voltage:.2f} Volts")
27+
print(f"Battery state : {max17.cell_percent:.1f} %")
2328
print("")
2429
time.sleep(1)

0 commit comments

Comments
 (0)