@@ -37,6 +37,7 @@ def __init__(self,taskindex): # general init
3737 self .lastread = 0
3838 self .ina = None
3939 self .decimals = [3 ,3 ,3 ,0 ]
40+ self .shuntohms = 0.1
4041
4142 def plugin_init (self ,enableplugin = None ):
4243 plugin .PluginProto .plugin_init (self ,enableplugin )
@@ -60,23 +61,46 @@ def plugin_init(self,enableplugin=None):
6061 self ._lastdataservetime = rpieTime .millis ()- (nextr * 1000 )
6162 self .lastread = 0
6263 try :
64+ shunt = self .shuntohms
65+ except :
6366 shunt = 0.1
67+ self .shuntohms = shunt
68+
69+ try :
70+ pgain = int (self .taskdevicepluginconfig [6 ])
71+ except :
72+ pgain = - 1
73+ try :
74+ padc = int (self .taskdevicepluginconfig [7 ])
75+ except :
76+ padc = 3
77+ try :
6478 amps = int (self .taskdevicepluginconfig [1 ])
6579 if amps < 1 :
6680 amps = None
81+ else :
82+ try :
83+ amps = float (amps ) / 1000
84+ except :
85+ amps = None
6786 vrange = int (self .taskdevicepluginconfig [2 ])
6887 if vrange not in [0 ,1 ]:
6988 vrange = 1
7089 if int (self .taskdevicepluginconfig [0 ])> 0x39 :
7190 self .ina = INA219 (shunt ,busnum = i2cl , address = int (self .taskdevicepluginconfig [0 ]), max_expected_amps = amps )
72- self .ina .configure (voltage_range = vrange )
91+ self .ina .configure (voltage_range = vrange , gain = pgain , bus_adc = padc , shunt_adc = padc )
7392 self .initialized = True
7493 except Exception as e :
7594 misc .addLog (rpieGlobals .LOG_LEVEL_ERROR ,"INA219 can not be initialized: " + str (e ))
7695 self .ina = None
7796 self .initialized = False
7897
7998 def webform_load (self ): # create html page for settings
99+ try :
100+ shunt = self .shuntohms
101+ except :
102+ shunt = 0.1
103+ self .shuntohms = shunt
80104 choice1 = self .taskdevicepluginconfig [0 ]
81105 optionvalues = [0x40 ,0x41 ,0x44 ,0x45 ,0x42 ,0x43 ,0x46 ,0x47 ,0x48 ,0x49 ,0x4A ,0x4B ,0x4C ,0x4D ,0x4E ,0x4F ]
82106 options = []
@@ -97,6 +121,18 @@ def webform_load(self): # create html page for settings
97121 webserver .addFormSelector ("Max voltage" ,"plugin_027_volt" ,len (options ),options ,optionvalues ,None ,int (choice3 ))
98122 webserver .addUnit ("V" )
99123
124+ webserver .addFormFloatNumberBox ("Shunt resistor" ,"plugin_027_shuntohm" ,self .shuntohms ,0 ,30000000.0 )
125+ webserver .addUnit ("Ohm" )
126+ if self .ina is not None :
127+ choice7 = self .taskdevicepluginconfig [6 ]
128+ options = ["AUTO" ,"40mV" ,"80mV" ,"160mV" ,"320mV" ]
129+ optionvalues = [ self .ina .GAIN_AUTO , self .ina .GAIN_1_40MV , self .ina .GAIN_2_80MV , self .ina .GAIN_4_160MV , self .ina .GAIN_8_320MV ]
130+ webserver .addFormSelector ("Gain" ,"plugin_027_gain" ,len (options ),options ,optionvalues ,None ,int (choice7 ))
131+ choice8 = self .taskdevicepluginconfig [7 ]
132+ options = ["9bit" ,"10bit" ,"11bit" ,"12bit" ,"2SAMP" ,"4SAMP" ,"8SAMP" ,"16SAMP" ,"32SAMP" ,"64SAMP" ,"128SAMP" ]
133+ optionvalues = [self .ina .ADC_9BIT ,self .ina .ADC_10BIT ,self .ina .ADC_11BIT ,self .ina .ADC_12BIT ,self .ina .ADC_2SAMP ,self .ina .ADC_4SAMP ,self .ina .ADC_8SAMP ,self .ina .ADC_16SAMP ,self .ina .ADC_32SAMP ,self .ina .ADC_64SAMP ,self .ina .ADC_128SAMP ]
134+ webserver .addFormSelector ("ADC resolution" ,"plugin_027_adc" ,len (options ),options ,optionvalues ,None ,int (choice8 ))
135+
100136 choice4 = self .taskdevicepluginconfig [3 ]
101137 options = ["None" ,"Voltage" ,"Current" ,"Power" ]
102138 optionvalues = [0 ,1 ,2 ,3 ]
@@ -108,6 +144,25 @@ def webform_load(self): # create html page for settings
108144 return True
109145
110146 def webform_save (self ,params ): # process settings post reply
147+ try :
148+ shunt = float (webserver .arg ("plugin_027_shuntohm" ,params ))
149+ if shunt <= 0 :
150+ shunt = 0.1
151+ except :
152+ shunt = 0.1
153+ self .shuntohms = shunt
154+
155+ try :
156+ par = int (webserver .arg ("plugin_027_gain" ,params ))
157+ self .taskdevicepluginconfig [6 ] = par
158+ except :
159+ pass
160+ try :
161+ par = int (webserver .arg ("plugin_027_adc" ,params ))
162+ self .taskdevicepluginconfig [7 ] = par
163+ except :
164+ pass
165+
111166 par = webserver .arg ("plugin_027_addr" ,params )
112167 if par == "" :
113168 par = 0x40
0 commit comments