-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path08_rotaryEncoder.py
More file actions
executable file
·64 lines (52 loc) · 1.51 KB
/
08_rotaryEncoder.py
File metadata and controls
executable file
·64 lines (52 loc) · 1.51 KB
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
#!/usr/bin/env python
import RPi.GPIO as GPIO
import time
RoAPin = 11 # pin11
RoBPin = 12 # pin12
RoSPin = 13 # pin13
globalCounter = 0
flag = 0
Last_RoB_Status = 0
Current_RoB_Status = 0
def setup():
GPIO.setmode(GPIO.BOARD) # Numbers GPIOs by physical location
GPIO.setup(RoAPin, GPIO.IN) # input mode
GPIO.setup(RoBPin, GPIO.IN)
GPIO.setup(RoSPin,GPIO.IN,pull_up_down=GPIO.PUD_UP)
rotaryClear()
def rotaryDeal():
global flag
global Last_RoB_Status
global Current_RoB_Status
global globalCounter
Last_RoB_Status = GPIO.input(RoBPin)
while(not GPIO.input(RoAPin)):
Current_RoB_Status = GPIO.input(RoBPin)
flag = 1
if flag == 1:
flag = 0
if (Last_RoB_Status == 0) and (Current_RoB_Status == 1):
globalCounter = globalCounter + 1
print 'globalCounter = %d' % globalCounter
if (Last_RoB_Status == 1) and (Current_RoB_Status == 0):
globalCounter = globalCounter - 1
print 'globalCounter = %d' % globalCounter
def clear(ev=None):
globalCounter = 0
print 'globalCounter = %d' % globalCounter
time.sleep(1)
def rotaryClear():
GPIO.add_event_detect(RoSPin, GPIO.FALLING, callback=clear) # wait for falling
def loop():
global globalCounter
while True:
rotaryDeal()
# print 'globalCounter = %d' % globalCounter
def destroy():
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()