-
Notifications
You must be signed in to change notification settings - Fork 31
/
rotary.py
82 lines (64 loc) · 2.24 KB
/
rotary.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
import pyb
class rotary():
def __init__(self,Apin='X21',Bpin='X22'):
self.B = pyb.Pin(Bpin)
self.A = pyb.Pin(Apin)
self.prevA = self.A.value()
self.prevB = self.B.value()
self.CWcount = 0
self.CCWcount = 0
self.position = 0
self.Bint = pyb.ExtInt(self.B,pyb.ExtInt.IRQ_RISING_FALLING,pyb.Pin.PULL_UP,self.callback)
self.Aint = pyb.ExtInt(self.A,pyb.ExtInt.IRQ_RISING_FALLING,pyb.Pin.PULL_UP,self.callback)
def callback(self,line):
# self.Bint.disable()
# self.Aint.disable()
A = self.A.value()
B = self.B.value()
#previous state 11
if self.prevA==1 and self.prevB==1:
if A==1 and B==0:
#print( "CCW 11 to 10")
self.CCWcount += 1
self.prevA = A
self.prevB = B
elif A==0 and B==0:
#print ("CW 11 to 00")
self.CWcount += 1
self.prevA = A
self.prevB = B
#previous state 10
elif self.prevA==1 and self.prevB==0:
if A==1 and B==1:
#print ("CW 10 to 11")
self.CWcount += 1
self.prevA = A
self.prevB = B
elif A==0 and B==0:
#print ("CCW 10 to 00")
self.CCWcount += 1
self.prevA = A
self.prevB = B
#previous state 00
elif self.prevA==0 and self.prevB==0:
if A==1 and B==1:
#print ("CCW 00 to 11")
self.CCWcount += 1
self.prevA = A
self.prevB = B
elif A==1 and B==0:
#print ("CW 00 to 10")
self.CWcount+=1
self.prevA = A
self.prevB = B
# self.Bint.enable()
# self.Aint.enable()
if A==1 and B==1:
if self.CWcount>=3 and self.CWcount>self.CCWcount:
self.position+=1
print (self.position)
if self.CCWcount>=3 and self.CCWcount>self.CWcount:
self.position-=1
print(self.position)
self.CCWcount = 0
self.CWcount = 0