|
| 1 | +# Test machine.Encoder implementation |
| 2 | +# |
| 3 | +# IMPORTANT: This test requires hardware connections: |
| 4 | +# - out0_pin and in0_pin must be wired together. |
| 5 | +# - out1_pin and in1_pin must be wired together. |
| 6 | + |
| 7 | +try: |
| 8 | + from machine import Encoder |
| 9 | +except ImportError: |
| 10 | + print("SKIP") |
| 11 | + raise SystemExit |
| 12 | + |
| 13 | +import sys |
| 14 | +from machine import Pin |
| 15 | + |
| 16 | +if "esp32" in sys.platform: |
| 17 | + id = 0 |
| 18 | + out0_pin = 4 |
| 19 | + in0_pin = 5 |
| 20 | + out1_pin = 12 |
| 21 | + in1_pin = 13 |
| 22 | +else: |
| 23 | + print("Please add support for this test on this platform.") |
| 24 | + raise SystemExit |
| 25 | + |
| 26 | +import unittest |
| 27 | + |
| 28 | +out0_pin = Pin(out0_pin, mode=Pin.OUT) |
| 29 | +in0_pin = Pin(in0_pin, mode=Pin.IN) |
| 30 | +out1_pin = Pin(out1_pin, mode=Pin.OUT) |
| 31 | +in1_pin = Pin(in1_pin, mode=Pin.IN) |
| 32 | + |
| 33 | + |
| 34 | +class TestEncoder(unittest.TestCase): |
| 35 | + def setUp(self): |
| 36 | + out0_pin(0) |
| 37 | + out1_pin(0) |
| 38 | + self.enc = Encoder(id, in0_pin, in1_pin) |
| 39 | + self.pulses = 0 # track the expected encoder position in software |
| 40 | + |
| 41 | + def tearDown(self): |
| 42 | + self.enc.deinit() |
| 43 | + |
| 44 | + def rotate(self, pulses): |
| 45 | + for _ in range(abs(pulses)): |
| 46 | + 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)) |
| 51 | + |
| 52 | + def assertPosition(self, value): |
| 53 | + self.assertEqual(self.enc.value(), value) |
| 54 | + |
| 55 | + def test_connections(self): |
| 56 | + # Test the hardware connections are correct. If this test fails, all tests will fail. |
| 57 | + for ch, outp, inp in ((0, out0_pin, in0_pin), (1, out1_pin, in1_pin)): |
| 58 | + print("Testing channel ", ch) |
| 59 | + outp(1) |
| 60 | + self.assertEqual(1, inp()) |
| 61 | + outp(0) |
| 62 | + self.assertEqual(0, inp()) |
| 63 | + |
| 64 | + def test_basics(self): |
| 65 | + self.assertPosition(0) |
| 66 | + self.rotate(100) |
| 67 | + self.assertPosition(100 // 4) |
| 68 | + self.rotate(-100) |
| 69 | + self.assertPosition(0) |
| 70 | + |
| 71 | + def test_partial(self): |
| 72 | + # With phase=1 (default), need 4x pulses to count a rotation |
| 73 | + self.assertPosition(0) |
| 74 | + self.rotate(1) |
| 75 | + self.assertPosition(0) |
| 76 | + self.rotate(1) |
| 77 | + self.assertPosition(0) |
| 78 | + self.rotate(1) |
| 79 | + self.assertPosition(1) # only 3 pulses to count first rotation? |
| 80 | + self.rotate(1) |
| 81 | + self.assertPosition(1) |
| 82 | + self.rotate(1) |
| 83 | + self.assertPosition(1) |
| 84 | + self.rotate(1) |
| 85 | + self.assertPosition(1) |
| 86 | + self.rotate(1) |
| 87 | + self.assertPosition(2) # 4 for next rotation |
| 88 | + self.rotate(-1) |
| 89 | + self.assertPosition(1) |
| 90 | + self.rotate(-4) |
| 91 | + self.assertPosition(0) |
| 92 | + self.rotate(-4) |
| 93 | + self.assertPosition(-1) |
| 94 | + self.rotate(-3) |
| 95 | + self.assertPosition(-1) |
| 96 | + |
| 97 | + |
| 98 | +if __name__ == "__main__": |
| 99 | + unittest.main() |
0 commit comments