forked from enesbcs/rpieasy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
_P003_Pulse.py
166 lines (157 loc) · 5.29 KB
/
_P003_Pulse.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
#!/usr/bin/env python3
#############################################################################
##################### Pulse counter plugin for RPIEasy ######################
#############################################################################
#
# Based on the original ESPEasy P003 plugin.
#
# Copyright (C) 2019 by Alexander Nagy - https://bitekmindenhol.blog.hu/
#
import plugin
import rpieGlobals
import rpieTime
import time
import misc
import gpios
import webserver
class Plugin(plugin.PluginProto):
PLUGIN_ID = 3
PLUGIN_NAME = "Input - Pulse Counter"
PLUGIN_VALUENAME1 = "Count"
PLUGIN_VALUENAME2 = "Total"
PLUGIN_VALUENAME3 = "Time"
def __init__(self,taskindex): # general init
plugin.PluginProto.__init__(self,taskindex)
self.dtype = rpieGlobals.DEVICE_TYPE_SINGLE
self.vtype = rpieGlobals.SENSOR_TYPE_TRIPLE
self.valuecount = 3
self.senddataoption = True
self.timeroption = True
self.inverselogicoption = False
self.recdataoption = False
self.pulsecounter = 0
self.pulsetotalcounter = 0
self.pulsetime = 0
self.pulsetimeprevious = 0
self.timer100ms = False
self.readinprogress = False
self.irqinprogress = False
self.prevval = -1
self.formulaoption = True
def webform_load(self): # create html page for settings
webserver.addFormNote("Select an input pin.")
webserver.addFormNumericBox("Debounce Time (mSec)","p003",self.taskdevicepluginconfig[0])
choice1 = self.taskdevicepluginconfig[1]
options = ["Delta","Delta/Total/Time","Total","Delta/Total"]
optionvalues = [0,1,2,3]
webserver.addFormSelector("Counter Type","p003_countertype",len(options),options,optionvalues,None,choice1)
choice2 = self.taskdevicepluginconfig[2]
options = ["BOTH","RISING","FALLING"]
optionvalues = [gpios.BOTH,gpios.RISING,gpios.FALLING]
webserver.addFormSelector("Mode Type","p003_raisetype",len(options),options,optionvalues,None,choice2)
return True
def webform_save(self,params): # process settings post reply
pchanged = False
par1 = webserver.arg("p003",params)
try:
self.taskdevicepluginconfig[0] = int(par1)
except:
self.taskdevicepluginconfig[0] = 0
par1 = webserver.arg("p003_countertype",params)
try:
if str(self.taskdevicepluginconfig[1]) != str(par1):
self.taskdevicepluginconfig[1] = int(par1)
pchanged = True
except:
self.taskdevicepluginconfig[1] = 0
par1 = webserver.arg("p003_raisetype",params)
try:
if str(self.taskdevicepluginconfig[2]) != str(par1):
self.taskdevicepluginconfig[2] = int(par1)
pchanged = True
except:
self.taskdevicepluginconfig[2] = 0
if pchanged:
self.plugin_init()
return True
def __del__(self):
try:
gpios.HWPorts.remove_event_detect(int(self.taskdevicepin[0]))
except:
pass
def plugin_exit(self):
self.__del__()
return True
def plugin_init(self,enableplugin=None):
plugin.PluginProto.plugin_init(self,enableplugin)
self.decimals[0]=0
self.decimals[1]=0
self.decimals[2]=0
self.initialized=False
try:
self.prevval = -1
except:
self.prevval = -1
if self.enabled and self.taskdevicepin[0]>0:
self.__del__()
self.readinprogress = False
self.pulsecounter = 0
self.pulsetimeprevious = rpieTime.millis()
self.irqinprogress = False
time.sleep(0.1)
try:
gpios.HWPorts.add_event_detect(int(self.taskdevicepin[0]),self.taskdevicepluginconfig[2],self.p003_handler)
self.initialized = True
except Exception as e:
misc.addLog(rpieGlobals.LOG_LEVEL_ERROR,"GPIO event handlers can not be created "+str(e))
if self.initialized:
if self.taskdevicepluginconfig[1] == 0:
self.vtype = rpieGlobals.SENSOR_TYPE_SINGLE
elif self.taskdevicepluginconfig[1] == 1:
self.vtype = rpieGlobals.SENSOR_TYPE_TRIPLE
elif self.taskdevicepluginconfig[1] == 2:
self.vtype = rpieGlobals.SENSOR_TYPE_SINGLE
elif self.taskdevicepluginconfig[1] == 3:
self.vtype = rpieGlobals.SENSOR_TYPE_DUAL
def plugin_read(self):
result = False
if self.initialized and self.enabled:
if self.taskdevicepluginconfig[1] == 0:
self.set_value(1,self.pulsecounter,False)
elif self.taskdevicepluginconfig[1] == 1:
self.set_value(1,self.pulsecounter,False)
self.set_value(2,self.pulsetotalcounter,False)
self.set_value(3,self.pulsetime,False)
elif self.taskdevicepluginconfig[1] == 2:
self.set_value(1,self.pulsetotalcounter,False)
elif self.taskdevicepluginconfig[1] == 3:
self.set_value(1,self.pulsecounter,False)
self.set_value(2,self.pulsetotalcounter,False)
self.plugin_senddata()
self._lastdataservetime = rpieTime.millis()
self.pulsecounter = 0
result = True
return result
def p003_handler(self,channel):
if (self.irqinprogress == False):
self.irqinprogress = True
atime = rpieTime.millis()
ptime = atime - self.pulsetimeprevious
aval = gpios.HWPorts.input(channel)
if (ptime > int(self.taskdevicepluginconfig[0])):
ok = False
if self.taskdevicepluginconfig[2] == gpios.BOTH:
ok = True
elif self.taskdevicepluginconfig[2] == gpios.RISING:
if self.prevval<aval:
ok = True
elif self.taskdevicepluginconfig[2] == gpios.FALLING:
if self.prevval>aval:
ok = True
if ok:
self.pulsecounter += 1
self.pulsetotalcounter += 1
self.pulsetime = ptime
self.pulsetimeprevious = atime
self.prevval = aval
self.irqinprogress = False