-
-
Notifications
You must be signed in to change notification settings - Fork 33
/
_P002_PiAnalog.py
199 lines (191 loc) · 6.78 KB
/
_P002_PiAnalog.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#!/usr/bin/env python3
#############################################################################
####################### PiAnalog plugin for RPIEasy #########################
#############################################################################
#
# Based on the Pi_Analog library:
# https://github.com/simonmonk/pi_analog
#
# Modified to support RockPIS integrated ADC_IN
#
# Copyright (C) 2019 by Alexander Nagy - https://bitekmindenhol.blog.hu/
#
import plugin
import rpieGlobals
import rpieTime
import time
import misc
import webserver
analogpins = []
realanalog = False
try:
import gpios
analogpins = gpios.HWPorts.getanaloglist()
realanalog = True
except:
pass
try:
import lib.pi_analog.PiAnalog as Analog
rpiok = True
except:
rpiok = False
class Plugin(plugin.PluginProto):
PLUGIN_ID = 2
PLUGIN_NAME = "Input - PiAnalog (EXPERIMENTAL)"
PLUGIN_VALUENAME1 = "Analog"
def __init__(self,taskindex): # general init
plugin.PluginProto.__init__(self,taskindex)
if realanalog:
self.dtype = rpieGlobals.DEVICE_TYPE_DUMMY
else:
self.dtype = rpieGlobals.DEVICE_TYPE_DUAL
self.vtype = rpieGlobals.SENSOR_TYPE_SINGLE
self.valuecount = 1
self.senddataoption = True
self.timeroption = True
self.inverselogicoption = False
self.recdataoption = False
self.timer100ms = False
self.readinprogress = False
self.adc = None
self.formulaoption = True
def webform_load(self): # create html page for settings
global analogpins,realanalog,rpiok
if realanalog:
self.taskdevicepluginconfig[0] = 0
webserver.addHtml("<tr><td>Analog input pin:<td>")
webserver.addSelector_Head("p002_ain",False)
for d in range(len(analogpins)):
webserver.addSelector_Item(analogpins[d][1],analogpins[d][0],(self.taskdevicepluginconfig[1]==analogpins[d][0]),False)
webserver.addSelector_Foot()
elif rpiok:
webserver.addFormNote("Pin1 is A, Pin2 is B, for wiring, see <a href='https://github.com/simonmonk/pi_analog'>https://github.com/simonmonk/pi_analog</a>")
choice0 = self.taskdevicepluginconfig[0]
options = ["Analog","Resistance","Thermistor"]
optionvalues = [0,1,2]
webserver.addFormSelector("Result Type","p002_type",len(options),options,optionvalues,None,choice0)
webserver.addFormFloatNumberBox("C1 capacitor", "p002_c1", self.taskdevicepluginconfig[1], 0, 1000000.0)
webserver.addUnit("uF")
webserver.addFormNumericBox("R1 resistor","p002_r1",self.taskdevicepluginconfig[2])
webserver.addUnit("Ohm")
webserver.addFormFloatNumberBox("Vt voltage (digital HIGH level)", "p002_vt", self.taskdevicepluginconfig[3], 0, 3.3)
webserver.addUnit("V")
webserver.addFormNote("Settings below are only valid for thermistor type!")
webserver.addFormNumericBox("Thermistor resistance","p002_tr",self.taskdevicepluginconfig[4])
webserver.addUnit("Ohm")
webserver.addFormNumericBox("Thermistor Beta","p002_tb",self.taskdevicepluginconfig[5])
return True
def webform_save(self,params): # process settings post reply
global realanalog,rpiok
if realanalog:
self.taskdevicepluginconfig[0] = 0
par1 = webserver.arg("p002_ain",params)
try:
self.taskdevicepluginconfig[1] = int(par1)
except:
self.taskdevicepluginconfig[1] = 0
elif rpiok:
par1 = webserver.arg("p002_type",params)
try:
self.taskdevicepluginconfig[0] = int(par1)
except:
self.taskdevicepluginconfig[0] = 0
par1 = webserver.arg("p002_c1",params)
try:
self.taskdevicepluginconfig[1] = float(par1)
except:
self.taskdevicepluginconfig[1] = 0.33
par1 = webserver.arg("p002_r1",params)
try:
self.taskdevicepluginconfig[2] = int(par1)
except:
self.taskdevicepluginconfig[2] = 10000
par1 = webserver.arg("p002_vt",params)
try:
self.taskdevicepluginconfig[3] = float(par1)
except:
self.taskdevicepluginconfig[3] = 1.35
par1 = webserver.arg("p002_tr",params)
try:
self.taskdevicepluginconfig[4] = int(par1)
except:
self.taskdevicepluginconfig[4] = 1000
par1 = webserver.arg("p002_tb",params)
try:
self.taskdevicepluginconfig[5] = int(par1)
except:
self.taskdevicepluginconfig[5] = 3800
self.plugin_init()
return True
def plugin_init(self,enableplugin=None):
global realanalog,rpiok
plugin.PluginProto.plugin_init(self,enableplugin)
self.decimals[0]=0
self.initialized=False
if realanalog:
try:
self.adc = virtADC(int(self.taskdevicepluginconfig[1]))
self.initialized = True
except Exception as e:
misc.addLog(rpieGlobals.LOG_LEVEL_ERROR,"Analog init error: "+str(e))
elif rpiok:
try:
if str(self.taskdevicepluginconfig[1])=="" or str(self.taskdevicepluginconfig[1])=="0":
self.taskdevicepluginconfig[1]=0.33
except:
self.taskdevicepluginconfig[1]=0.33
try:
if str(self.taskdevicepluginconfig[2])=="" or str(self.taskdevicepluginconfig[2])=="0":
self.taskdevicepluginconfig[2]=10000
except:
self.taskdevicepluginconfig[2]=10000
try:
if str(self.taskdevicepluginconfig[3])=="" or str(self.taskdevicepluginconfig[3])=="0":
self.taskdevicepluginconfig[3]=1.35
except:
self.taskdevicepluginconfig[3]=1.35
try:
if str(self.taskdevicepluginconfig[4])=="" or str(self.taskdevicepluginconfig[4])=="0":
self.taskdevicepluginconfig[4]=1000
except:
self.taskdevicepluginconfig[4]=1000
try:
if str(self.taskdevicepluginconfig[5])=="" or str(self.taskdevicepluginconfig[5])=="0":
self.taskdevicepluginconfig[5]=3800
except:
self.taskdevicepluginconfig[5]=3800
if self.enabled and self.taskdevicepin[0]>=0 and self.taskdevicepin[1]>=0:
self.readinprogress = False
try:
self.adc = Analog.PiAnalog(gpioinit=False,a_pin=self.taskdevicepin[0],b_pin=self.taskdevicepin[1],C=self.taskdevicepluginconfig[1],R1=self.taskdevicepluginconfig[2],Vt=self.taskdevicepluginconfig[3])
self.initialized = True
except Exception as e:
misc.addLog(rpieGlobals.LOG_LEVEL_ERROR,"PiAnalog init error: "+str(e))
def plugin_read(self):
result = False
if self.initialized and self.enabled and self.readinprogress==False and (self.adc is not None):
self.readinprogress = True
try:
if self.taskdevicepluginconfig[0] == 0:
self.set_value(1,self.adc.analog_read(),False)
elif self.taskdevicepluginconfig[0] == 1:
self.set_value(1,self.adc.read_resistance(),False)
elif self.taskdevicepluginconfig[0] == 2:
self.set_value(1,self.adc.read_temp_c(self.taskdevicepluginconfig[5],self.taskdevicepluginconfig[4]),False)
except:
pass
self.plugin_senddata()
self._lastdataservetime = rpieTime.millis()
self.readinprogress = False
result = True
return result
class virtADC:
def __init__(self,channel):
self.channel = channel
def analog_read(self):
res = None
try:
res = gpios.HWPorts.analog_read(self.channel)
except Exception as e:
pass
return res