mirror of
https://github.com/skaringa/water-counter.git
synced 2024-11-24 10:24:24 +01:00
Use interrupt with edge triggering (epoll)
This commit is contained in:
parent
a67be3af05
commit
6a362159f7
2
setup.sh
2
setup.sh
@ -1 +1,3 @@
|
|||||||
|
gpio unexport 9
|
||||||
gpio export 9 in
|
gpio export 9 in
|
||||||
|
gpio edge 9 rising
|
||||||
|
40
wairc.py
40
wairc.py
@ -22,16 +22,16 @@
|
|||||||
# You should have received a copy of the GNU General Public License
|
# You should have received a copy of the GNU General Public License
|
||||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
import serial
|
|
||||||
import time
|
import time
|
||||||
import sys
|
import sys
|
||||||
import os
|
import os
|
||||||
import re
|
import re
|
||||||
|
import select
|
||||||
import argparse
|
import argparse
|
||||||
import rrdtool
|
import rrdtool
|
||||||
|
|
||||||
# GPIO value files (wiringPi)
|
# GPIO pin to read from
|
||||||
gpin = '/sys/class/gpio/gpio9/value'
|
gpio_pin = 9
|
||||||
|
|
||||||
# counter unit: 1 revolution = x m^3
|
# counter unit: 1 revolution = x m^3
|
||||||
trigger_step = 0.001
|
trigger_step = 0.001
|
||||||
@ -78,6 +78,12 @@ def last_rrd_count():
|
|||||||
handle.close()
|
handle.close()
|
||||||
return val
|
return val
|
||||||
|
|
||||||
|
|
||||||
|
# Setup gpio edge interrupt triggering
|
||||||
|
def setup_gpio(pin):
|
||||||
|
os.system("gpio export {} in".format(pin))
|
||||||
|
os.system("gpio edge {} rising".format(pin))
|
||||||
|
|
||||||
# Main
|
# Main
|
||||||
def main():
|
def main():
|
||||||
# Check command args
|
# Check command args
|
||||||
@ -88,29 +94,29 @@ def main():
|
|||||||
if args.create:
|
if args.create:
|
||||||
create_rrd()
|
create_rrd()
|
||||||
|
|
||||||
trigger_state = 0
|
|
||||||
counter = last_rrd_count()
|
counter = last_rrd_count()
|
||||||
print "restoring counter to %f" % counter
|
print "restoring counter to %f" % counter
|
||||||
|
|
||||||
while(1==1):
|
# open gpio input value file
|
||||||
# read gpio input value file
|
# and register handler for edge trigger
|
||||||
f = open(gpin, 'r')
|
setup_gpio(gpio_pin)
|
||||||
line = f.read().strip()
|
f = open("/sys/class/gpio/gpio{}/value".format(gpio_pin), 'r')
|
||||||
f.close()
|
epoll = select.epoll()
|
||||||
|
epoll.register(f, select.EPOLLIN | select.EPOLLET)
|
||||||
|
events = epoll.poll() # eat the first edge
|
||||||
|
|
||||||
old_state = trigger_state
|
while(1==1):
|
||||||
if line == '1':
|
events = epoll.poll()
|
||||||
trigger_state = 1
|
f.seek(0)
|
||||||
elif line == '0':
|
value = f.read(1)
|
||||||
trigger_state = 0
|
if value == '1':
|
||||||
if old_state == 1 and trigger_state == 0:
|
# trigger edge detected -> update count rrd
|
||||||
# trigger active -> update count rrd
|
|
||||||
counter += trigger_step
|
counter += trigger_step
|
||||||
update = "N:%.3f:%.3f" % (counter, trigger_step)
|
update = "N:%.3f:%.3f" % (counter, trigger_step)
|
||||||
#print update
|
#print update
|
||||||
rrdtool.update(count_rrd, update)
|
rrdtool.update(count_rrd, update)
|
||||||
time.sleep(0.1)
|
|
||||||
|
|
||||||
|
f.close()
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
main()
|
||||||
|
Loading…
Reference in New Issue
Block a user