-
Notifications
You must be signed in to change notification settings - Fork 0
/
full_rotary_wiringpi.py
executable file
·78 lines (59 loc) · 1.86 KB
/
full_rotary_wiringpi.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
#!/usr/bin/env python
import wiringpi as wpi
import datetime
pCLK = 3
pDT = 4
pSW = 5
globalCounter = 24
flag = 0
Last_DT_Status = 0
Current_DT_Status = 0
LastClearTime = 0
def setup():
wpi.wiringPiSetup()
wpi.pinMode(pCLK, wpi.INPUT)
wpi.pinMode(pDT, wpi.INPUT)
wpi.pinMode(pSW, wpi.INPUT)
wpi.pullUpDnControl(pSW, wpi.PUD_UP)
rotaryClear()
def rotaryDeal():
global flag
global Last_DT_Status
global Current_DT_Status
global globalCounter
# Last_DT_Status = GPIO.input(pDT)
Last_DT_Status = wpi.digitalRead(pDT)
while (not wpi.digitalRead(pCLK)):
Current_DT_Status = wpi.digitalRead(pDT)
flag = 1
if flag == 1:
flag = 0
if (Last_DT_Status == 0) and (Current_DT_Status == 1):
globalCounter = globalCounter + 1
print 'globalCounter = %d' % globalCounter
if (Last_DT_Status == 1) and (Current_DT_Status == 0):
globalCounter = globalCounter - 1
print 'globalCounter = %d' % globalCounter
def clear(ev=None):
global LastClearTime
if LastClearTime == 0 or (datetime.datetime.now() - LastClearTime) > datetime.timedelta(seconds=1):
globalCounter = 0
print 'globalCounter = %d' % globalCounter
LastClearTime = datetime.datetime.now()
def rotaryClear():
# GPIO.add_event_detect(RoSPin, GPIO.FALLING, callback=clear) # wait for falling
wpi.wiringPiISR(pSW, wpi.INT_EDGE_FALLING, clear)
def loop():
global globalCounter
while True:
rotaryDeal()
# print 'globalCounter = %d' % globalCounter
def destroy():
print "Program Over"
# GPIO.cleanup() # Release resource
if __name__ == '__main__': # Program start from here
setup()
try:
loop()
except KeyboardInterrupt: # When 'Ctrl+C' is pressed, the child program destroy() will be executed.
destroy()