-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy path18_rotaryEncoder.py
More file actions
executable file
·61 lines (50 loc) · 1.4 KB
/
18_rotaryEncoder.py
File metadata and controls
executable file
·61 lines (50 loc) · 1.4 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
#!/usr/bin/env python
import RPi.GPIO as GPIO
import time
RoAPin = 11 # pin11
RoBPin = 12 # pin12
BtnPin = 13 # Button Pin
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(BtnPin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.add_event_detect(BtnPin, GPIO.FALLING, callback=btnISR)
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
if (Last_RoB_Status == 1) and (Current_RoB_Status == 0):
globalCounter = globalCounter + 1
def btnISR(channel):
global globalCounter
globalCounter = 0
def loop():
global globalCounter
tmp = 0 # Rotary Temperary
while True:
rotaryDeal()
if tmp != globalCounter:
print 'globalCounter = %d' % globalCounter
tmp = 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()