3434In order to be coherent with the datasheet, the channel index in the below API start at 1.
3535Value of ``channel`` parameter must be ``1``, ``2`` or ``3``. **Do not use** ``0``
3636
37- Memory usage (tested with CircuitPython 4.0.0beta5 on CircuitPlayground Express):
37+ Memory usage (tested with CircuitPython 4.0.0-rc.1 on CircuitPlayground Express):
3838
3939* from barbudor_ina3221 import INA3221 --> 6592 bytes
4040* ina3221 = INA3221(i2c_bus) --> 112 bytes
6969C_REG_CONFIG = const (0x00 )
7070
7171C_RESET = const (0x8000 )
72- C_ENABLE_CH1 = const (0x4000 ) # default set
73- C_ENABLE_CH2 = const (0x2000 ) # default set
74- C_ENABLE_CH3 = const (0x1000 ) # default set
72+ C_ENABLE_CH = (None ,const (0x4000 ),const (0x2000 ),const (0x1000 )) # default set
7573
7674C_AVERAGING_MASK = const (0x0E00 )
7775C_AVERAGING_NONE = const (0x0000 ) # 1 sample, default
114112C_MODE_SHUNT_AND_BUS_CONTINOUS = const (0x0007 ) # Shunt and bus, continuous (default)
115113
116114# Other registers
117- C_REG_SHUNT_VOLTAGE_CH1 = const (0x01 )
118- C_REG_BUS_VOLTAGE_CH1 = const (0x02 )
119- C_REG_SHUNT_VOLTAGE_CH2 = const (0x03 )
120- C_REG_BUS_VOLTAGE_CH2 = const (0x04 )
121- C_REG_SHUNT_VOLTAGE_CH3 = const (0x05 )
122- C_REG_BUS_VOLTAGE_CH3 = const (0x06 )
123- C_REG_CRITICAL_ALERT_LIMIT_CH1 = const (0x07 )
124- C_REG_WARNING_ALERT_LIMIT_CH1 = const (0x08 )
125- C_REG_CRITICAL_ALERT_LIMIT_CH2 = const (0x09 )
126- C_REG_WARNING_ALERT_LIMIT_CH2 = const (0x0A )
127- C_REG_CRITICAL_ALERT_LIMIT_CH3 = const (0x0B )
128- C_REG_WARNING_ALERT_LIMIT_CH3 = const (0x0C )
115+ C_REG_SHUNT_VOLTAGE_CH = (None , const (0x01 ), const (0x03 ), const (0x05 ))
116+ C_REG_BUS_VOLTAGE_CH = (None , const (0x02 ), const (0x04 ), const (0x06 ))
117+ C_REG_CRITICAL_ALERT_LIMIT_CH = (None , const (0x07 ), const (0x09 ), const (0x0B ))
118+ C_REG_WARNING_ALERT_LIMIT_CH = (None , const (0x08 ), const (0x0A ), const (0x0C ))
129119C_REG_SHUNT_VOLTAGE_SUM = const (0x0D )
130120C_REG_SHUNT_VOLTAGE_SUM_LIMIT = const (0x0E )
131121
132122# Mask/enable register
133123C_REG_MASK_ENABLE = const (0x0F )
134- C_SUM_CONTROL_CH1 = const (0x4000 ) # default not set
135- C_SUM_CONTROL_CH2 = const (0x2000 ) # default not set
136- C_SUM_CONTROL_CH3 = const (0x1000 ) # default not set
124+ C_SUM_CONTROL_CH = (None ,const (0x4000 ),const (0x2000 ),const (0x1000 )) # def. not set
137125C_WARNING_LATCH_ENABLE = const (0x0800 ) # default not set
138126C_CRITICAL_LATCH_ENABLE = const (0x0400 ) # default not set
139- C_CRITICAL_FLAG_CH1 = const (0x0200 )
140- C_CRITICAL_FLAG_CH2 = const (0x0100 )
141- C_CRITICAL_FLAG_CH3 = const (0x0080 )
127+ C_CRITICAL_FLAG_CH = (None ,const (0x0200 ),const (0x0100 ),const (0x0080 ))
142128C_SUM_ALERT_FLAG = const (0x0040 )
143- C_WARNING_FLAG_CH1 = const (0x0020 )
144- C_WARNING_FLAG_CH2 = const (0x0010 )
145- C_WARNING_FLAG_CH3 = const (0x0008 )
129+ C_WARNING_FLAG_CH = (None ,const (0x0020 ),const (0x0010 ),const (0x0008 ))
146130C_POWER_ALERT_FLAG = const (0x0004 )
147131C_TIMING_ALERT_FLAG = const (0x0002 )
148132C_CONV_READY_FLAG = const (0x0001 )
@@ -209,12 +193,14 @@ def __init__(self, i2c_bus, i2c_addr = _DEFAULT_ADDRESS, shunt_resistor = (0.1,
209193
210194 def is_channel_enabled (self , channel = 1 ):
211195 """Returns if a given channel is enabled or not"""
212- bit = C_ENABLE_CH1 >> (channel - 1 )
196+ assert 1 <= channel <= 3 , "channel argument must be 1, 2, or 3"
197+ bit = C_ENABLE_CH [channel ]
213198 return self .read (C_REG_CONFIG ) & bit != 0
214199
215200 def enable_channel (self , channel = 1 , enable = True ):
216201 """Enables or disable a given channel"""
217- bit = C_ENABLE_CH1 >> (channel - 1 )
202+ assert 1 <= channel <= 3 , "channel argument must be 1, 2, or 3"
203+ bit = C_ENABLE_CH [channel ]
218204 value = 0
219205 if enable :
220206 value = bit
@@ -223,7 +209,7 @@ def enable_channel(self, channel=1, enable=True):
223209 def shunt_voltage (self , channel = 1 ):
224210 """Returns the channel's shunt voltage in Volts"""
225211 assert 1 <= channel <= 3 , "channel argument must be 1, 2, or 3"
226- value = self ._to_signed (self .read (C_REG_SHUNT_VOLTAGE_CH1 + channel - 1 )) / 8.0
212+ value = self ._to_signed (self .read (C_REG_SHUNT_VOLTAGE_CH [ channel ] )) / 8.0
227213 # convert to volts - LSB = 40uV
228214 return value * 0.00004
229215
@@ -235,32 +221,32 @@ def current(self, channel=1):
235221 def bus_voltage (self , channel = 1 ):
236222 """Returns the channel's bus voltage in Volts"""
237223 assert 1 <= channel <= 3 , "channel argument must be 1, 2, or 3"
238- value = self ._to_signed (self .read (C_REG_BUS_VOLTAGE_CH1 + channel - 1 )) / 8
224+ value = self ._to_signed (self .read (C_REG_BUS_VOLTAGE_CH [ channel ] )) / 8
239225 # convert to volts - LSB = 8mV
240226 return value * 0.008
241227
242228 def shunt_critical_alert_limit (self , channel = 1 ):
243229 """Returns the channel's shunt voltage critical alert limit in Volts"""
244230 assert 1 <= channel <= 3 , "channel argument must be 1, 2, or 3"
245- value = self ._to_signed (self .read (C_REG_CRITICAL_ALERT_LIMIT_CH1 + channel - 1 )) / 8
231+ value = self ._to_signed (self .read (C_REG_CRITICAL_ALERT_LIMIT_CH [ channel ] )) / 8
246232 # convert to volts - LSB = 40uV
247233 return value * 0.00004
248234
249235 def set_shunt_critical_alert_limit (self , channel , voltage ):
250236 """Sets the channel's shunt voltage critical alert limit in Volts"""
251237 assert 1 <= channel <= 3 , "channel argument must be 1, 2, or 3"
252238 value = self ._to_unsigned (round (voltage * 0.00004 ) * 8 )
253- self .write (C_REG_CRITICAL_ALERT_LIMIT_CH1 + channel - 1 , value )
239+ self .write (C_REG_CRITICAL_ALERT_LIMIT_CH [ channel ] , value )
254240
255241 def shunt_warning_alert_limit (self , channel = 1 ):
256242 """Returns the channel's shunt voltage warning alert limit in Volts"""
257243 assert 1 <= channel <= 3 , "channel argument must be 1, 2, or 3"
258- value = self ._to_signed (self .read (C_REG_WARNING_ALERT_LIMIT_CH1 + channel - 1 )) / 8
244+ value = self ._to_signed (self .read (C_REG_WARNING_ALERT_LIMIT_CH [ channel ] )) / 8
259245 # convert to volts - LSB = 40uV
260246 return value * 0.00004
261247
262248 def set_shunt_warning_alert_limit (self , channel , voltage ):
263249 """Sets the channel's shunt voltage warning alert limit in Volts"""
264250 assert 1 <= channel <= 3 , "channel argument must be 1, 2, or 3"
265251 value = self ._to_unsigned (round (voltage * 0.00004 ) * 8 )
266- self .write (C_REG_WARNING_ALERT_LIMIT_CH1 + channel - 1 , value )
252+ self .write (C_REG_WARNING_ALERT_LIMIT_CH [ channel ] , value )
0 commit comments