Skip to content

Commit 5f04c0a

Browse files
authored
Merge pull request #5 from sokratisvas/add-type-annotations
Add type annotations
2 parents 333a949 + 3fb87e2 commit 5f04c0a

File tree

1 file changed

+24
-19
lines changed

1 file changed

+24
-19
lines changed

adafruit_max1704x.py

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@
3333
from adafruit_register.i2c_bit import RWBit, ROBit
3434
from adafruit_register.i2c_bits import RWBits
3535

36+
try:
37+
from busio import I2C
38+
except ImportError:
39+
pass
40+
3641
__version__ = "0.0.0-auto.0"
3742
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_MAX1704x.git"
3843

@@ -100,7 +105,7 @@ class MAX17048:
100105
_reset_voltage = RWBits(7, _MAX1704X_VRESET_REG, 1)
101106
comparator_disabled = RWBit(_MAX1704X_VRESET_REG, 0)
102107

103-
def __init__(self, i2c_bus, address=MAX1704X_I2CADDR_DEFAULT):
108+
def __init__(self, i2c_bus: I2C, address: int = MAX1704X_I2CADDR_DEFAULT) -> None:
104109
# pylint: disable=no-member
105110
self.i2c_device = i2c_device.I2CDevice(i2c_bus, address)
106111

@@ -110,7 +115,7 @@ def __init__(self, i2c_bus, address=MAX1704X_I2CADDR_DEFAULT):
110115
self.enable_sleep = False
111116
self.sleep = False
112117

113-
def reset(self):
118+
def reset(self) -> None:
114119
"""Perform a soft reset of the chip"""
115120
try:
116121
self._cmd = 0x5400
@@ -122,91 +127,91 @@ def reset(self):
122127
self.reset_alert = False # clean up RI alert
123128

124129
@property
125-
def cell_voltage(self):
130+
def cell_voltage(self) -> float:
126131
"""The state of charge of the battery, in volts"""
127132
return self._cell_voltage * 78.125 / 1_000_000
128133

129134
@property
130-
def cell_percent(self):
135+
def cell_percent(self) -> float:
131136
"""The state of charge of the battery, in percentage of 'fullness'"""
132137
return self._cell_SOC / 256.0
133138

134139
@property
135-
def charge_rate(self):
140+
def charge_rate(self) -> float:
136141
"""Charge or discharge rate of the battery in percent/hour"""
137142
return self._cell_crate * 0.208
138143

139144
@property
140-
def reset_voltage(self):
145+
def reset_voltage(self) -> float:
141146
"""The voltage that will determine whether the chip will consider it a reset/swap"""
142147
return self._reset_voltage * 0.04 # 40mV / LSB
143148

144149
@reset_voltage.setter
145-
def reset_voltage(self, reset_v):
150+
def reset_voltage(self, reset_v: float) -> None:
146151
if not 0 <= reset_v <= (127 * 0.04):
147152
raise ValueError("Reset voltage must be between 0 and 5.1 Volts")
148153
self._reset_voltage = int(reset_v / 0.04) # 40mV / LSB
149154

150155
@property
151-
def voltage_alert_min(self):
156+
def voltage_alert_min(self) -> float:
152157
"""The lower-limit voltage for the voltage alert"""
153158
return self._valrt_min * 0.02 # 20mV / LSB
154159

155160
@voltage_alert_min.setter
156-
def voltage_alert_min(self, minvoltage):
161+
def voltage_alert_min(self, minvoltage: float) -> None:
157162
if not 0 <= minvoltage <= (255 * 0.02):
158163
raise ValueError("Alert voltage must be between 0 and 5.1 Volts")
159164
self._valrt_min = int(minvoltage / 0.02) # 20mV / LSB
160165

161166
@property
162-
def voltage_alert_max(self):
167+
def voltage_alert_max(self) -> float:
163168
"""The upper-limit voltage for the voltage alert"""
164169
return self._valrt_max * 0.02 # 20mV / LSB
165170

166171
@voltage_alert_max.setter
167-
def voltage_alert_max(self, maxvoltage):
172+
def voltage_alert_max(self, maxvoltage: float) -> None:
168173
if not 0 <= maxvoltage <= (255 * 0.02):
169174
raise ValueError("Alert voltage must be between 0 and 5.1 Volts")
170175
self._valrt_max = int(maxvoltage / 0.02) # 20mV / LSB
171176

172177
@property
173-
def active_alert(self):
178+
def active_alert(self) -> bool:
174179
"""Whether there is an active alert to be checked"""
175180
return self._alert_status
176181

177182
@property
178-
def alert_reason(self):
183+
def alert_reason(self) -> int:
179184
"""The 7 bits of alert-status that can be checked at once for flags"""
180185
return self._status & 0x3F
181186

182187
@property
183-
def activity_threshold(self):
188+
def activity_threshold(self) -> float:
184189
"""The absolute change in battery voltage that will trigger hibernation"""
185190
return self._hibrt_actthr * 0.00125 # 1.25mV per LSB
186191

187192
@activity_threshold.setter
188-
def activity_threshold(self, threshold_voltage):
193+
def activity_threshold(self, threshold_voltage: float) -> None:
189194
if not 0 <= threshold_voltage <= (255 * 0.00125):
190195
raise ValueError(
191196
"Activity voltage change must be between 0 and 0.31875 Volts"
192197
)
193198
self._hibrt_actthr = int(threshold_voltage / 0.00125) # 1.25mV per LSB
194199

195200
@property
196-
def hibernation_threshold(self):
201+
def hibernation_threshold(self) -> float:
197202
"""The absolute-value percent-per-hour change in charge rate
198203
that will trigger hibernation"""
199204
return self._hibrt_hibthr * 0.208 # 0.208% per hour
200205

201206
@hibernation_threshold.setter
202-
def hibernation_threshold(self, threshold_percent):
207+
def hibernation_threshold(self, threshold_percent: float) -> None:
203208
if not 0 <= threshold_percent <= (255 * 0.208):
204209
raise ValueError(
205210
"Activity percentage/hour change must be between 0 and 53%"
206211
)
207212
self._hibrt_hibthr = int(threshold_percent / 0.208) # 0.208% per hour
208213

209-
def hibernate(self):
214+
def hibernate(self) -> None:
210215
"""Setup thresholds for hibernation to go into hibernation mode immediately.
211216
212217
See datasheet: HIBRT Register (0x0A) To disable hibernate mode, set
@@ -217,7 +222,7 @@ def hibernate(self):
217222
self._hibrt_hibthr = 0xFF
218223
self._hibrt_actthr = 0xFF
219224

220-
def wake(self):
225+
def wake(self) -> None:
221226
"""Setup thresholds for hibernation to leave hibernation mode immediately.
222227
223228
See datasheet: HIBRT Register (0x0A) To disable hibernate mode, set

0 commit comments

Comments
 (0)