forked from enesbcs/rpieasy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
_P210_MCP3008.py
133 lines (123 loc) · 4.16 KB
/
_P210_MCP3008.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
#!/usr/bin/env python3
#############################################################################
######################## MCP3008 plugin for RPIEasy #########################
#############################################################################
#
# Copyright (C) 2020 by Alexander Nagy - https://bitekmindenhol.blog.hu/
#
import plugin
import webserver
import rpieGlobals
import rpieTime
import misc
import lib.lib_mcp3008 as ADC
import gpios
class Plugin(plugin.PluginProto):
PLUGIN_ID = 210
PLUGIN_NAME = "Analog input - MCP3008/3208"
PLUGIN_VALUENAME1 = "Analog"
def __init__(self,taskindex): # general init
plugin.PluginProto.__init__(self,taskindex)
self.dtype = rpieGlobals.DEVICE_TYPE_SPI
self.vtype = rpieGlobals.SENSOR_TYPE_SINGLE
self.ports = 0
self.readinprogress = 0
self.valuecount = 1
self.senddataoption = True
self.timeroption = True
self.timeroptional = True
self.formulaoption = True
self.adc = None
self._nextdataservetime = 0
self.lastread = 0
self.samples = 3
self.preread = self.samples*1000 # 3 * 1 sec
self.TARR = []
def plugin_init(self,enableplugin=None):
plugin.PluginProto.plugin_init(self,enableplugin)
self.TARR = []
if self.enabled:
if self.interval>2:
nextr = self.interval-2
else:
nextr = self.interval
self._lastdataservetime = rpieTime.millis()-(nextr*1000)
self.preread = self.samples*1000
try:
if self.spi<0 or self.spidnum<0:
return
except:
self.spi = 0
self.spidnum = 0
self.ports = "SPI"+str(self.spi)+"/"+str(self.spidnum)+" CH"+str(self.taskdevicepluginconfig[2])
try:
if int(self.taskdevicepluginconfig[3]) == 0:
self.taskdevicepluginconfig[3] = 3008
except:
self.taskdevicepluginconfig[3] = 3008
try:
self.adc = ADC.request_adc_device(int(self.spi),int(self.spidnum),dtype=int(self.taskdevicepluginconfig[3]))
self.initialized = self.adc.initialized
except Exception as e:
misc.addLog(rpieGlobals.LOG_LEVEL_ERROR,"ADC can not be initialized! "+str(e))
self.initialized = False
if self.initialized == False or self.enabled==False:
self.ports = ""
def webform_load(self): # create html page for settings
options = ["MCP3008","MCP3208"]
optionvalues = [3008,3208]
webserver.addFormSelector("ADC type","p210_type",len(options),options,optionvalues,None,int(self.taskdevicepluginconfig[3]))
choice3 = self.taskdevicepluginconfig[2]
options = []
optionvalues = []
for i in range(8):
options.append("CH"+str(i))
optionvalues.append(int(i))
webserver.addFormSelector("Channel number","p210_chan",len(options),options,optionvalues,None,int(choice3))
webserver.addFormCheckBox("Oversampling","p210_over",self.timer1s)
return True
def webform_save(self,params): # process settings post reply
par = webserver.arg("p210_chan",params)
if par == "":
par = 0
self.taskdevicepluginconfig[2] = int(par)
par = webserver.arg("p210_type",params)
try:
self.taskdevicepluginconfig[3] = int(par)
except:
self.taskdevicepluginconfig[3] = 0
if self.taskdevicepluginconfig[3] == 0:
self.taskdevicepluginconfig[3] = 3008
if (webserver.arg("p210_over",params)=="on"):
self.timer1s = True
else:
self.timer1s = False
self.plugin_init()
return True
def plugin_read(self): # deal with data processing at specified time interval
result = False
if self.initialized and self.enabled:
self.p210_get_value()
if len(self.TARR)>0:
self.set_value(1,(sum(self.TARR) / float(len(self.TARR))),False)
self.plugin_senddata()
else:
misc.addLog(rpieGlobals.LOG_LEVEL_ERROR,"MCP3008 read failed!")
result = True
self.TARR = []
self._lastdataservetime = rpieTime.millis()
self._nextdataservetime = self._lastdataservetime + (self.interval*1000)
return result
def timer_once_per_second(self):
if self.initialized and self.enabled:
if self._nextdataservetime-rpieTime.millis()<=self.preread:
self.p210_get_value()
return self.timer1s
def p210_get_value(self):
val = -1
try:
val = self.adc.ADread(self.taskdevicepluginconfig[2])
except Exception as e:
val = -1
if val != -1:
self.TARR.append(val)