-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathrotary_test.py
executable file
·76 lines (62 loc) · 2.44 KB
/
rotary_test.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
# Sample code for both the RotaryEncoder class and the Switch class.
# The common pin for the encoder should be wired to ground.
# The sw_pin should be shorted to ground by the switch.
# Output looks like this:
#
# A B STATE SEQ DELTA SWITCH
# 1 1 3 2 1 0
# 0 1 2 3 1 0
# 0 0 0 0 1 0
# 1 0 1 1 1 0
# 1 1 3 2 1 0
# 0 1 2 3 1 0
import gaugette.rotary_encoder
import gaugette.switch
import math
A_PIN = 7
B_PIN = 8
SW_PIN = 9
encoder = gaugette.rotary_encoder.RotaryEncoder(A_PIN, B_PIN)
switch = gaugette.switch.Switch(SW_PIN)
last_state = None
last_switch_state = None
last_delta = 0
last_sequence = encoder.rotation_sequence()
last_heading = 0
# NOTE: the library includes individual calls to get
# the rotation_state, rotation_sequence and delta values.
# However this demo only reads the rotation_state and locally
# derives the rotation_sequence and delta. This ensures that
# the derived values are based on the same two input bits A and B.
# If we used the library calls, there is a very real chance that
# the inputs would change while we were sampling, giving us
# inconsistent values in the output table.
while True:
state = encoder.rotation_state()
switch_state = switch.get_state()
if (state != last_state or switch_state != last_switch_state):
last_switch_state = switch_state
last_state = state
# print a heading every 20 lines
if last_heading % 20 == 0:
print "A B STATE SEQ DELTA SWITCH"
last_heading += 1
# extract individual signal bits for A and B
a_state = state & 0x01
b_state = (state & 0x02) >> 1
# compute sequence number:
# This is the same as the value returned by encoder.rotation_sequence()
sequence = (a_state ^ b_state) | b_state << 1
# compute delta:
# This is the same as the value returned by encoder.get_delta()
delta = (sequence - last_sequence) % 4
if delta == 3:
delta = -1
elif delta==2:
# this is an attempt to make sense out of a missed step:
# assume that we have moved two steps in the same direction
# that we were previously moving.
delta = int(math.copysign(delta, last_delta))
last_delta = delta
last_sequence = sequence
print '%1d %1d %3d %4d %4d %4d' % (a_state, b_state, state, sequence, delta, switch_state)