13
13
import sys
14
14
from machine import Pin
15
15
16
+ PRINT = False
17
+ PIN_INIT_VALUE = 1
18
+
16
19
if "esp32" in sys .platform :
17
20
id = 0
18
21
out0_pin = 4
19
22
in0_pin = 5
20
23
out1_pin = 12
21
24
in1_pin = 13
25
+ elif sys .platform == "mimxrt" :
26
+ if "Teensy" in sys .implementation ._machine :
27
+ id = 0
28
+ out0_pin = "D2"
29
+ in0_pin = "D3"
30
+ out1_pin = "D5"
31
+ in1_pin = "D4"
22
32
else :
23
33
print ("Please add support for this test on this platform." )
24
34
raise SystemExit
33
43
34
44
class TestEncoder (unittest .TestCase ):
35
45
def setUp (self ):
36
- out0_pin (0 )
37
- out1_pin (0 )
38
- self .enc = Encoder (id , in0_pin , in1_pin )
46
+ out0_pin (PIN_INIT_VALUE )
47
+ out1_pin (PIN_INIT_VALUE )
48
+ self .enc = Encoder (id , in0_pin , in1_pin , phases = 1 )
49
+ self .enc2 = Encoder (id + 1 , in0_pin , in1_pin , phases = 2 )
50
+ self .enc4 = Encoder (id + 2 , in0_pin , in1_pin , phases = 4 )
39
51
self .pulses = 0 # track the expected encoder position in software
52
+ if PRINT :
53
+ print (
54
+ "\n out0_pin() out1_pin() enc.value() enc2.value() enc4.value() |" ,
55
+ out0_pin (),
56
+ out1_pin (),
57
+ "|" ,
58
+ self .enc .value (),
59
+ self .enc2 .value (),
60
+ self .enc4 .value (),
61
+ )
40
62
41
63
def tearDown (self ):
42
64
self .enc .deinit ()
65
+ try :
66
+ self .enc2 .deinit ()
67
+ except :
68
+ pass
69
+ try :
70
+ self .enc4 .deinit ()
71
+ except :
72
+ pass
43
73
44
74
def rotate (self , pulses ):
45
75
for _ in range (abs (pulses )):
46
76
self .pulses += 1 if (pulses > 0 ) else - 1
47
- q = self .pulses % 4
48
- # Only one pin should change state each "step" so output won't glitch
49
- out0_pin (q in (1 , 2 ))
50
- out1_pin (q in (2 , 3 ))
77
+ if pulses > 0 :
78
+ if self .pulses % 2 :
79
+ out0_pin (not out0_pin ())
80
+ else :
81
+ out1_pin (not out1_pin ())
82
+ else :
83
+ if self .pulses % 2 :
84
+ out1_pin (not out1_pin ())
85
+ else :
86
+ out0_pin (not out0_pin ())
87
+ if PRINT :
88
+ print (
89
+ "out0_pin() out1_pin() enc.value() enc2.value() enc4.value() pulses self.pulses |" ,
90
+ out0_pin (),
91
+ out1_pin (),
92
+ "|" ,
93
+ self .enc .value (),
94
+ self .enc2 .value (),
95
+ self .enc4 .value (),
96
+ "|" ,
97
+ pulses ,
98
+ self .pulses ,
99
+ )
51
100
52
- def assertPosition (self , value ):
101
+ def assertPosition (self , value , value2 = None , value4 = None ):
53
102
self .assertEqual (self .enc .value (), value )
103
+ if not value2 is None :
104
+ self .assertEqual (self .enc2 .value (), value2 )
105
+ if not value4 is None :
106
+ self .assertEqual (self .enc4 .value (), value4 )
107
+ pass
54
108
109
+ @unittest .skipIf (sys .platform == "mimxrt" , "cannot read back the pin" )
55
110
def test_connections (self ):
56
111
# Test the hardware connections are correct. If this test fails, all tests will fail.
57
112
for ch , outp , inp in ((0 , out0_pin , in0_pin ), (1 , out1_pin , in1_pin )):
@@ -64,35 +119,47 @@ def test_connections(self):
64
119
def test_basics (self ):
65
120
self .assertPosition (0 )
66
121
self .rotate (100 )
67
- self .assertPosition (100 // 4 )
122
+ self .assertPosition (100 // 4 , 100 // 2 , 100 )
68
123
self .rotate (- 100 )
69
124
self .assertPosition (0 )
70
125
71
126
def test_partial (self ):
72
127
# With phase=1 (default), need 4x pulses to count a rotation
73
128
self .assertPosition (0 )
74
129
self .rotate (1 )
75
- self .assertPosition (0 )
130
+ self .assertPosition (1 , 1 , 1 )
76
131
self .rotate (1 )
77
- self .assertPosition (0 )
132
+ self .assertPosition (1 , 1 , 2 )
78
133
self .rotate (1 )
79
- self .assertPosition (1 ) # only 3 pulses to count first rotation?
134
+ self .assertPosition (1 , 2 , 3 )
80
135
self .rotate (1 )
81
- self .assertPosition (1 )
136
+ self .assertPosition (1 , 2 , 4 ) # +4
82
137
self .rotate (1 )
83
- self .assertPosition (1 )
138
+ self .assertPosition (2 , 3 , 5 )
84
139
self .rotate (1 )
85
- self .assertPosition (1 )
140
+ self .assertPosition (2 , 3 , 6 )
86
141
self .rotate (1 )
87
- self .assertPosition (2 ) # 4 for next rotation
142
+ self .assertPosition (2 , 4 , 7 )
143
+ self .rotate (1 )
144
+ self .assertPosition (2 , 4 , 8 ) # +4
88
145
self .rotate (- 1 )
89
- self .assertPosition (1 )
90
- self .rotate (- 4 )
91
- self .assertPosition (0 )
146
+ self .assertPosition (2 , 4 , 7 )
147
+ self .rotate (- 3 )
148
+ self .assertPosition (1 , 2 , 4 ) # -4
92
149
self .rotate (- 4 )
93
- self .assertPosition (- 1 )
150
+ self .assertPosition (0 , 0 , 0 ) # -4
151
+ self .rotate (- 1 )
152
+ self .assertPosition (0 , 0 , - 1 )
153
+ self .rotate (- 1 )
154
+ self .assertPosition (0 , - 1 , - 2 )
155
+ self .rotate (- 1 )
156
+ self .assertPosition (0 , - 1 , - 3 )
157
+ self .rotate (- 1 )
158
+ self .assertPosition (- 1 , - 2 , - 4 ) # -4
159
+ self .rotate (- 1 )
160
+ self .assertPosition (- 1 , - 2 , - 5 )
94
161
self .rotate (- 3 )
95
- self .assertPosition (- 1 )
162
+ self .assertPosition (- 2 , - 4 , - 8 ) # -4
96
163
97
164
98
165
if __name__ == "__main__" :
0 commit comments