This repository was archived by the owner on Mar 23, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrotary_encoder.py
133 lines (126 loc) · 4.74 KB
/
rotary_encoder.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#----------------------------------------------------------------------
# rotary_encoder.py from https://github.com/guyc/py-gaugette
# Guy Carpenter, Clearwater Software
#
# This is a class for reading quadrature rotary encoders
# like the PEC11 Series available from Adafruit:
# http://www.adafruit.com/products/377
# The datasheet for this encoder is here:
# http://www.adafruit.com/datasheets/pec11.pdf
#
# This library expects the common pin C to be connected
# to ground. Pins A and B will have their pull-up resistor
# pulled high.
#
# Usage:
#
# import gaugette.rotary_encoder
# A_PIN = 7 # use wiring pin numbers here
# B_PIN = 9
# encoder = gaugette.rotary_encoder.RotaryEncoder(A_PIN, B_PIN)
# while 1:
# delta = encoder.delta() # returns 0,1,or -1
# if delta!=0:
# print delta
import wiringpi
import math
import threading
import time
class RotaryEncoder(object):
#----------------------------------------------------------------------
# Pass the wiring pin numbers here. See:
# https://projects.drogon.net/raspberry-pi/wiringpi/pins/
#----------------------------------------------------------------------
def __init__(self, a_pin, b_pin):
self.a_pin = a_pin
self.b_pin = b_pin
self.gpio = wiringpi.GPIO(wiringpi.GPIO.WPI_MODE_PINS)
self.gpio.pinMode(self.a_pin, self.gpio.INPUT)
self.gpio.pullUpDnControl(self.a_pin, self.gpio.PUD_UP)
self.gpio.pinMode(self.b_pin, self.gpio.INPUT)
self.gpio.pullUpDnControl(self.b_pin, self.gpio.PUD_UP)
self.last_delta = 0
self.r_seq = self.rotation_sequence()
# Gets the 2-bit rotation state of the current position
# This is deprecated - we now use rotation_sequence instead.
def rotation_state(self):
a_state = self.gpio.digitalRead(self.a_pin)
b_state = self.gpio.digitalRead(self.b_pin)
r_state = a_state | b_state << 1
return r_state
# Returns the quadrature encoder state converted into
# a numerical sequence 0,1,2,3,0,1,2,3...
#
# Turning the encoder clockwise generates these
# values for switches B and A:
# B A
# 0 0
# 0 1
# 1 1
# 1 0
# We convert these to an ordinal sequence number by returning
# seq = (A ^ B) | B << 2
#
def rotation_sequence(self):
a_state = self.gpio.digitalRead(self.a_pin)
b_state = self.gpio.digitalRead(self.b_pin)
r_seq = (a_state ^ b_state) | b_state << 1
return r_seq
# Returns offset values of -2,-1,0,1,2
def get_delta(self):
delta = 0
r_seq = self.rotation_sequence()
if r_seq != self.r_seq:
delta = (r_seq - self.r_seq) % 4
if delta==3:
delta = -1
elif delta==2:
delta = int(math.copysign(delta, self.last_delta)) # same direction as previous, 2 steps
self.last_delta = delta
self.r_seq = r_seq
return delta
class Worker(threading.Thread):
def __init__(self, a_pin, b_pin):
threading.Thread.__init__(self)
self.lock = threading.Lock()
self.encoder = RotaryEncoder(a_pin, b_pin)
self.daemon = True
self.delta = 0
def run(self):
while True:
delta = self.encoder.get_delta()
with self.lock:
self.delta += delta
time.sleep(0.001)
def get_delta(self):
# revisit - should use locking
with self.lock:
delta = self.delta
self.delta = 0
return delta
# extension by [email protected]
class RotaryEncoder2(rotary_encoder.RotaryEncoder):
#----------------------------------------------------------------------
# Pass the wiring pin numbers here. See:
# https://projects.drogon.net/raspberry-pi/wiringpi/pins/
#----------------------------------------------------------------------
def __init__(self, a_pin, b_pin):
self.ao = self.bo = 1
super(RotaryEncoder2, self).__init__(a_pin, b_pin)
def rotation_sequence(self):
a_state = self.gpio.digitalRead(self.a_pin)
b_state = self.gpio.digitalRead(self.b_pin)
r_seq = (a_state << 1) | b_state
return r_seq
# Returns offset values of -1,0,1
def get_delta(self):
delta = 0
r_seq = self.rotation_sequence()
if r_seq != self.r_seq:
if self.r_seq == 3:
if r_seq == 1:
delta = 1
else:
delta = -1
self.r_seq = r_seq
return delta