-
Notifications
You must be signed in to change notification settings - Fork 1
/
encoder.py
223 lines (175 loc) · 6.19 KB
/
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File: encoder.py
# Scott M Baker, http://www.smbaker.com/
# Raspberry Pi Driver for the Sparkfun RGB Encoder (COM-10982 or COM-10984)
# Acknowledgements:
# py-gaugette by Guy Carpenter, https://github.com/guyc/py-gaugette
import math
import threading
import time
import RPi.GPIO as IO
# pin numbers used when "main" is run (demo mode)
# GPIO
# 17 - encoder A via 10k resistor
# 27 - encoder B via 10k resistor
#
# 22 - encoder pin 3 (switch) via 10k resistor
#
# 14 - encoder pin 1 (red) via 270 ohm resistor
# 15 - encoder pin 2 (green) via 150 ohm resistor
# 18 - encoder pin 4 (blue) via 150 ohm resistor
#
# Power
# GND - encoder C
# 3.3V - encoder pin 5 (led/switch common)
ENCODER_PIN_A = 17
ENCODER_PIN_B = 27
ENCODER_PIN_SW = 22
ENCODER_PIN_R = 14
ENCODER_PIN_G = 15
ENCODER_PIN_BLU = 18
class BasicEncoder:
# Just the encoder, no switch, no LEDs
def __init__(self, a_pin, b_pin):
self.a_pin = a_pin
self.b_pin = b_pin
IO.setmode(IO.BCM)
IO.setup(self.a_pin, IO.IN, pull_up_down=IO.PUD_UP)
IO.setup(self.b_pin, IO.IN, pull_up_down=IO.PUD_UP)
self.last_delta = 0
self.r_seq = self.rotation_sequence()
self.steps_per_cycle = 4 # 4 steps between detents
self.remainder = 0
def rotation_sequence(self):
a_state = IO.input(self.a_pin)
b_state = IO.input(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
def get_cycles(self):
self.remainder += self.get_delta()
cycles = self.remainder // self.steps_per_cycle
self.remainder %= self.steps_per_cycle # remainder always remains positive
return cycles
def get_switchstate(self):
# BasicEncoder doesn't have a switch
return 0
class SwitchEncoder(BasicEncoder):
# Encoder with a switch
def __init__(self, a_pin, b_pin, sw_pin):
BasicEncoder.__init__(self, a_pin, b_pin)
self.sw_pin = sw_pin
IO.setup(self.sw_pin, IO.IN, pull_up_down=IO.PUD_DOWN)
def get_switchstate(self):
return IO.input(self.sw_pin)
class RGBEncoder(SwitchEncoder):
# Encoder with a switch and LEDs
COLOR_NAMES = ["red", "green", "blue"]
def __init__(self, a_pin, b_pin, sw_pin, r_pin, g_pin, blu_pin):
SwitchEncoder.__init__(self, a_pin, b_pin, sw_pin)
self.color_pins = [r_pin, g_pin, blu_pin]
IO.setup(self.color_pins[0], IO.OUT)
IO.setup(self.color_pins[1], IO.OUT)
IO.setup(self.color_pins[2], IO.OUT)
self.color_servos=[]
for i in range(0,3):
servo = IO.PWM(self.color_pins[i], 50)
servo.start(100)
self.color_servos.append(servo)
def get_switchstate(self):
return IO.input(self.sw_pin)
def set_color(self, color, v):
self.color_servos[color].ChangeDutyCycle(100-v)
class EncoderWorker(threading.Thread):
def __init__(self, encoder):
threading.Thread.__init__(self)
self.lock = threading.Lock()
self.stopping = False
self.encoder = encoder
self.daemon = True
self.delta = 0
self.delay = 0.001
self.lastSwitchState = False
self.upEvent = False
self.downEvent = False
def run(self):
self.lastSwitchState = self.encoder.get_switchstate()
while not self.stopping:
delta = self.encoder.get_cycles()
with self.lock:
self.delta += delta
self.switchstate = self.encoder.get_switchstate()
if (not self.lastSwitchState) and (self.switchstate):
self.upEvent = True
if (self.lastSwitchState) and (not self.switchstate):
self.downEvent = True
self.lastSwitchState = self.switchstate
time.sleep(self.delay)
# get_delta, get_upEvent, and get_downEvent return events that occurred on
# the encoder. As a side effect, the corresponding event will be reset.
def get_delta(self):
with self.lock:
delta = self.delta
self.delta = 0
return delta
def get_upEvent(self):
with self.lock:
delta = self.upEvent
self.upEvent = False
return delta
def get_downEvent(self):
with self.lock:
delta = self.downEvent
self.downEvent = False
return delta
def set_color(self, color, v):
self.encoder.set_color(color, v)
def switch_demo():
value = 0
encoder = EncoderWorker(SwitchEncoder(ENCODER_PIN_A, ENCODER_PIN_B, ENCODER_PIN_SW))
encoder.start()
while 1:
delta = encoder.get_delta()
if delta!=0:
value = value + delta
print "value", value
if encoder.get_upEvent():
print "up!"
if encoder.get_downEvent():
print "down!"
def rgb_demo():
value = 0
toggle = False
colors = [0,0,0]
index = 0
encoder = EncoderWorker(RGBEncoder(ENCODER_PIN_A,
ENCODER_PIN_B,
ENCODER_PIN_SW,
ENCODER_PIN_R,
ENCODER_PIN_G,
ENCODER_PIN_BLU))
encoder.start()
while 1:
delta = encoder.get_delta()
if delta!=0:
colors[index] = min(100, max(0, colors[index] + delta))
print "color", RGBEncoder.COLOR_NAMES[index], "value", colors[index]
encoder.set_color(index, colors[index])
if encoder.get_upEvent():
print "up!"
index = (index + 1) % 3
if encoder.get_downEvent():
print "down!"
if __name__ == "__main__":
rgb_demo()