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 : print ('\n out0_pin() out1_pin() enc.value() enc2.value() enc4.value() |' , out0_pin (), out1_pin (), '|' , self .enc .value (), self .enc2 .value (), self .enc4 .value ())
40
53
41
54
def tearDown (self ):
42
55
self .enc .deinit ()
56
+ try :
57
+ self .enc2 .deinit ()
58
+ except :
59
+ pass
60
+ try :
61
+ self .enc4 .deinit ()
62
+ except :
63
+ pass
43
64
44
65
def rotate (self , pulses ):
45
66
for _ in range (abs (pulses )):
46
67
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 ))
68
+ if pulses > 0 :
69
+ if self .pulses % 2 :
70
+ out0_pin (not out0_pin ())
71
+ else :
72
+ out1_pin (not out1_pin ())
73
+ else :
74
+ if self .pulses % 2 :
75
+ out1_pin (not out1_pin ())
76
+ else :
77
+ out0_pin (not out0_pin ())
78
+ if PRINT : print ('out0_pin() out1_pin() enc.value() enc2.value() enc4.value() pulses self.pulses |' , out0_pin (), out1_pin (), '|' , self .enc .value (), self .enc2 .value (), self .enc4 .value (), '|' , pulses , self .pulses )
51
79
52
- def assertPosition (self , value ):
80
+ def assertPosition (self , value , value2 = None , value4 = None ):
53
81
self .assertEqual (self .enc .value (), value )
82
+ if not value2 is None :
83
+ self .assertEqual (self .enc2 .value (), value2 )
84
+ if not value4 is None :
85
+ self .assertEqual (self .enc4 .value (), value4 )
86
+ pass
54
87
88
+ @unittest .skipIf (sys .platform == "mimxrt" , "cannot read back the pin" )
55
89
def test_connections (self ):
56
90
# Test the hardware connections are correct. If this test fails, all tests will fail.
57
91
for ch , outp , inp in ((0 , out0_pin , in0_pin ), (1 , out1_pin , in1_pin )):
@@ -64,35 +98,47 @@ def test_connections(self):
64
98
def test_basics (self ):
65
99
self .assertPosition (0 )
66
100
self .rotate (100 )
67
- self .assertPosition (100 // 4 )
101
+ self .assertPosition (100 // 4 , 100 // 2 , 100 )
68
102
self .rotate (- 100 )
69
103
self .assertPosition (0 )
70
104
71
105
def test_partial (self ):
72
106
# With phase=1 (default), need 4x pulses to count a rotation
73
107
self .assertPosition (0 )
74
108
self .rotate (1 )
75
- self .assertPosition (0 )
109
+ self .assertPosition (1 , 1 , 1 )
76
110
self .rotate (1 )
77
- self .assertPosition (0 )
111
+ self .assertPosition (1 , 1 , 2 )
78
112
self .rotate (1 )
79
- self .assertPosition (1 ) # only 3 pulses to count first rotation?
113
+ self .assertPosition (1 , 2 , 3 )
80
114
self .rotate (1 )
81
- self .assertPosition (1 )
115
+ self .assertPosition (1 , 2 , 4 ) # +4
82
116
self .rotate (1 )
83
- self .assertPosition (1 )
117
+ self .assertPosition (2 , 3 , 5 )
84
118
self .rotate (1 )
85
- self .assertPosition (1 )
119
+ self .assertPosition (2 , 3 , 6 )
86
120
self .rotate (1 )
87
- self .assertPosition (2 ) # 4 for next rotation
121
+ self .assertPosition (2 , 4 , 7 )
122
+ self .rotate (1 )
123
+ self .assertPosition (2 , 4 , 8 ) # +4
88
124
self .rotate (- 1 )
89
- self .assertPosition (1 )
90
- self .rotate (- 4 )
91
- self .assertPosition (0 )
125
+ self .assertPosition (2 , 4 , 7 )
126
+ self .rotate (- 3 )
127
+ self .assertPosition (1 , 2 , 4 ) # -4
92
128
self .rotate (- 4 )
93
- self .assertPosition (- 1 )
129
+ self .assertPosition (0 , 0 , 0 ) # -4
130
+ self .rotate (- 1 )
131
+ self .assertPosition (0 , 0 , - 1 )
132
+ self .rotate (- 1 )
133
+ self .assertPosition (0 , - 1 , - 2 )
134
+ self .rotate (- 1 )
135
+ self .assertPosition (0 , - 1 , - 3 )
136
+ self .rotate (- 1 )
137
+ self .assertPosition (- 1 , - 2 , - 4 ) # -4
138
+ self .rotate (- 1 )
139
+ self .assertPosition (- 1 , - 2 , - 5 )
94
140
self .rotate (- 3 )
95
- self .assertPosition (- 1 )
141
+ self .assertPosition (- 2 , - 4 , - 8 ) # -4
96
142
97
143
98
144
if __name__ == "__main__" :
0 commit comments