50
50
_MAX1704X_STATUS_REG = const (0x1A )
51
51
_MAX1704X_CMD_REG = const (0xFE )
52
52
53
+ ALERTFLAG_SOC_CHANGE = 0x20
54
+ ALERTFLAG_SOC_LOW = 0x10
55
+ ALERTFLAG_VOLTAGE_RESET = 0x08
56
+ ALERTFLAG_VOLTAGE_LOW = 0x04
57
+ ALERTFLAG_VOLTAGE_HIGH = 0x02
58
+ ALERTFLAG_RESET_INDICATOR = 0x01
59
+
53
60
class MAX17048 :
54
61
"""Driver for the MAX1704X battery fuel gauge.
55
62
:param ~busio.I2C i2c_bus: The I2C bus the MAX1704X is connected to.
@@ -58,18 +65,28 @@ class MAX17048:
58
65
59
66
chip_version = ROUnaryStruct (_MAX1704X_VERSION_REG , ">H" )
60
67
_config = ROUnaryStruct (_MAX1704X_CONFIG_REG , ">H" )
68
+ # expose the config bits
69
+ sleep = RWBit (_MAX1704X_CONFIG_REG + 1 , 7 , register_width = 2 , lsb_first = False )
70
+ _alert_status = RWBit (_MAX1704X_CONFIG_REG + 1 , 5 , register_width = 2 , lsb_first = False )
71
+ enable_sleep = RWBit (_MAX1704X_MODE_REG , 5 )
72
+ hibernating = ROBit (_MAX1704X_MODE_REG , 4 )
73
+
61
74
_cmd = UnaryStruct (_MAX1704X_CMD_REG , ">H" )
62
75
_status = ROUnaryStruct (_MAX1704X_STATUS_REG , ">B" )
63
76
_cell_voltage = ROUnaryStruct (_MAX1704X_VCELL_REG , ">H" )
64
77
_cell_SOC = ROUnaryStruct (_MAX1704X_SOC_REG , ">B" )
65
78
_hibrt_actthr = UnaryStruct (_MAX1704X_HIBRT_REG + 1 , ">B" )
66
79
_hibrt_hibthr = UnaryStruct (_MAX1704X_HIBRT_REG , ">B" )
80
+ _valrt_min = UnaryStruct (_MAX1704X_VALERT_REG , ">B" )
81
+ _valrt_max = UnaryStruct (_MAX1704X_VALERT_REG + 1 , ">B" )
82
+
67
83
# expose the alert bits
68
- reset_alert = ROBit (_MAX1704X_STATUS_REG , 0 )
69
- voltage_high_alert = ROBit (_MAX1704X_STATUS_REG , 1 )
70
- voltage_low_alert = ROBit (_MAX1704X_STATUS_REG , 2 )
71
- voltage_reset_alert = ROBit (_MAX1704X_STATUS_REG , 3 )
72
- SOC_change_alert = ROBit (_MAX1704X_STATUS_REG , 5 )
84
+ reset_alert = RWBit (_MAX1704X_STATUS_REG , 0 )
85
+ voltage_high_alert = RWBit (_MAX1704X_STATUS_REG , 1 )
86
+ voltage_low_alert = RWBit (_MAX1704X_STATUS_REG , 2 )
87
+ voltage_reset_alert = RWBit (_MAX1704X_STATUS_REG , 3 )
88
+ SOC_low_alert = RWBit (_MAX1704X_STATUS_REG , 4 )
89
+ SOC_change_alert = RWBit (_MAX1704X_STATUS_REG , 5 )
73
90
74
91
chip_id = ROUnaryStruct (_MAX1704X_CHIPID_REG , ">B" )
75
92
@@ -80,14 +97,18 @@ def __init__(self, i2c_bus, address=MAX1704X_I2CADDR_DEFAULT):
80
97
if self .chip_version & 0xFFF0 != 0x0010 :
81
98
raise RuntimeError ("Failed to find MAX1704X - check your wiring!" )
82
99
self .reset ()
100
+ self .enable_sleep = False
101
+ self .sleep = False
83
102
84
103
def reset (self ):
85
104
try :
86
105
self ._cmd = 0x5400
87
106
except OSError :
88
107
# aha! we NACKed, which is CORRECT!
89
- return self .reset_alert # we should have reset
90
- raise RuntimeException ("Reset did not succeed" )
108
+ pass
109
+ else :
110
+ raise RuntimeError ("Reset did not succeed" )
111
+ self .reset_alert = False # clean up RI alert
91
112
92
113
@property
93
114
def cell_voltage (self ):
@@ -97,6 +118,36 @@ def cell_voltage(self):
97
118
def cell_percent (self ):
98
119
return self ._cell_SOC
99
120
121
+
122
+ @property
123
+ def voltage_alert_min (self ):
124
+ return self ._valrt_min * .02 # 20mV / LSB
125
+
126
+ @voltage_alert_min .setter
127
+ def voltage_alert_min (self , minvoltage ):
128
+ if (not 0 <= minvoltage <= (255 * 0.02 )):
129
+ raise ValueError ("Alert voltage must be between 0 and 5.1 Volts" )
130
+ self ._valrt_min = int (minvoltage / .02 ) # 20mV / LSB
131
+
132
+ @property
133
+ def voltage_alert_max (self ):
134
+ return self ._valrt_max * .02 # 20mV / LSB
135
+
136
+ @voltage_alert_max .setter
137
+ def voltage_alert_max (self , maxvoltage ):
138
+ if (not 0 <= maxvoltage <= (255 * 0.02 )):
139
+ raise ValueError ("Alert voltage must be between 0 and 5.1 Volts" )
140
+ self ._valrt_max = int (maxvoltage / .02 ) # 20mV / LSB
141
+
142
+ @property
143
+ def active_alert (self ):
144
+ return self ._alert_status
145
+
146
+ @property
147
+ def alert_reason (self ):
148
+ return self ._status & 0x3F
149
+
150
+
100
151
@property
101
152
def activity_threshold (self ):
102
153
return self ._hibrt_actthr * 0.00125 # 1.25mV per LSB
@@ -118,3 +169,19 @@ def hibernation_threshold(self, threshold_percent):
118
169
raise ValueError ("Activity percentage/hour change must be between 0 and 53%" )
119
170
self ._hibrt_hibthr = int (threshold_percent / 0.208 ) # 0.208% per hour
120
171
172
+
173
+ def hibernate (self ):
174
+ """Setup thresholds for hibernation to go into hibernation mode immediately.
175
+ See datasheet: HIBRT Register (0x0A) To disable hibernate mode, set HIBRT = 0x0000. To
176
+ always use hibernate mode, set HIBRT = 0xFFFF. Can check status with `self.hibernating`"""
177
+ self ._hibrt_hibthr = 0xFF
178
+ self ._hibrt_actthr = 0xFF
179
+
180
+ def disable_hibernation (self ):
181
+ """Setup thresholds for hibernation to leave hibernation mode immediately.
182
+ See datasheet: HIBRT Register (0x0A) To disable hibernate mode, set HIBRT = 0x0000. To
183
+ always use hibernate mode, set HIBRT = 0xFFFF. Can check status with `self.hibernating`"""
184
+ self ._hibrt_hibthr = 0
185
+ self ._hibrt_actthr = 0
186
+
187
+
0 commit comments