-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathgpio-counter.py
executable file
·105 lines (74 loc) · 2.59 KB
/
gpio-counter.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
#!/usr/bin/env python
import RPi.GPIO as GPIO
import datetime
import sys
import signal
#verbose = True # global variable
############################################################################################################
############################################################################################################
def printusage(progname):
print progname + ' <gpio-pin-number> <filename> [debug]'
print 'Example usage: '
print progname + ' 23 /path/to/mylogfile'
print progname + ' 23 /path/to/mylogfile debug'
sys.exit(-1)
def signal_handler(signal, frame):
if verbose:
print('You pressed Ctrl+C, so exiting')
GPIO.cleanup()
sys.exit(0)
def readvalue(myworkfile):
try:
f = open(myworkfile, 'ab+') # open for reading. If it does not exist, create it
value = int(f.readline().rstrip()) # read the first line; it should be an integer value
except:
value = 0 # if something went wrong, reset to 0
#print "old value is", value
f.close() # close for reading
return value
def writevalue(myworkfile,value):
f = open(myworkfile, 'w')
f.write((str(value)+ '\n')) # the value
f.write((str(datetime.datetime.now())+ '\n')) # timestamp
f.close()
############################################################################################################
############################################################################################################
######### Initialization
#### get input parameters:
try:
mygpiopin = int(sys.argv[1])
logfile = sys.argv[2]
except:
printusage(sys.argv[0])
verbose = False
try:
if sys.argv[3] == 'debug':
verbose = True
print "Verbose is On"
else:
printusage(sys.argv[0])
except:
pass
#### if verbose, print some info to stdout
if verbose:
print "GPIO is " + str(mygpiopin)
print "Logfile is " + logfile
print "Current value is " + str(readvalue(logfile))
#### setup
GPIO.setmode(GPIO.BCM)
GPIO.setup(mygpiopin, GPIO.IN, pull_up_down = GPIO.PUD_DOWN)
signal.signal(signal.SIGINT, signal_handler) # SIGINT = interrupt by CTRL-C
########## Main Loop
while True:
# wait for pin going up
GPIO.wait_for_edge(mygpiopin, GPIO.RISING)
# read value from file
counter=readvalue(logfile) + 1
if verbose:
print "New value is", counter
# write value to file
writevalue(logfile,counter)
# and wait for pin going down
GPIO.wait_for_edge(mygpiopin, GPIO.FALLING)
############################################################################################################
############################################################################################################