diff options
| author | Nick Brassel <nick@tzarc.org> | 2022-03-08 16:58:05 +1100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-03-08 16:58:05 +1100 |
| commit | 2f6751e48a37699cfd999e0afd8731ca3962611c (patch) | |
| tree | c64605c10a8f8f758f46be209d79210bbf15c167 /quantum/encoder/tests/encoder_tests_split_no_left.cpp | |
| parent | 2218690d0b718dc68de4c5d093f1ec8e55d82d00 (diff) | |
Asymmetric encoders, encoder tests. (#16068)
Diffstat (limited to 'quantum/encoder/tests/encoder_tests_split_no_left.cpp')
| -rw-r--r-- | quantum/encoder/tests/encoder_tests_split_no_left.cpp | 125 |
1 files changed, 125 insertions, 0 deletions
diff --git a/quantum/encoder/tests/encoder_tests_split_no_left.cpp b/quantum/encoder/tests/encoder_tests_split_no_left.cpp new file mode 100644 index 0000000000..b6b2d7e2d1 --- /dev/null +++ b/quantum/encoder/tests/encoder_tests_split_no_left.cpp | |||
| @@ -0,0 +1,125 @@ | |||
| 1 | /* Copyright 2021 Balz Guenat | ||
| 2 | * | ||
| 3 | * This program is free software: you can redistribute it and/or modify | ||
| 4 | * it under the terms of the GNU General Public License as published by | ||
| 5 | * the Free Software Foundation, either version 2 of the License, or | ||
| 6 | * (at your option) any later version. | ||
| 7 | * | ||
| 8 | * This program is distributed in the hope that it will be useful, | ||
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 11 | * GNU General Public License for more details. | ||
| 12 | * | ||
| 13 | * You should have received a copy of the GNU General Public License | ||
| 14 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| 15 | */ | ||
| 16 | |||
| 17 | #include "gtest/gtest.h" | ||
| 18 | #include "gmock/gmock.h" | ||
| 19 | #include <vector> | ||
| 20 | #include <algorithm> | ||
| 21 | #include <stdio.h> | ||
| 22 | |||
| 23 | extern "C" { | ||
| 24 | #include "encoder.h" | ||
| 25 | #include "encoder/tests/mock_split.h" | ||
| 26 | } | ||
| 27 | |||
| 28 | struct update { | ||
| 29 | int8_t index; | ||
| 30 | bool clockwise; | ||
| 31 | }; | ||
| 32 | |||
| 33 | uint8_t updates_array_idx = 0; | ||
| 34 | update updates[32]; | ||
| 35 | |||
| 36 | bool isLeftHand; | ||
| 37 | |||
| 38 | bool encoder_update_kb(uint8_t index, bool clockwise) { | ||
| 39 | if (!isLeftHand) { | ||
| 40 | // this method has no effect on slave half | ||
| 41 | printf("ignoring update on right hand (%d,%s)\n", index, clockwise ? "CW" : "CC"); | ||
| 42 | return true; | ||
| 43 | } | ||
| 44 | updates[updates_array_idx % 32] = {index, clockwise}; | ||
| 45 | updates_array_idx++; | ||
| 46 | return true; | ||
| 47 | } | ||
| 48 | |||
| 49 | bool setAndRead(pin_t pin, bool val) { | ||
| 50 | setPin(pin, val); | ||
| 51 | return encoder_read(); | ||
| 52 | } | ||
| 53 | |||
| 54 | class EncoderSplitTestNoLeft : public ::testing::Test { | ||
| 55 | protected: | ||
| 56 | void SetUp() override { | ||
| 57 | updates_array_idx = 0; | ||
| 58 | for (int i = 0; i < 32; i++) { | ||
| 59 | pinIsInputHigh[i] = 0; | ||
| 60 | pins[i] = 0; | ||
| 61 | } | ||
| 62 | } | ||
| 63 | }; | ||
| 64 | |||
| 65 | TEST_F(EncoderSplitTestNoLeft, TestInitLeft) { | ||
| 66 | isLeftHand = true; | ||
| 67 | encoder_init(); | ||
| 68 | EXPECT_EQ(pinIsInputHigh[0], false); | ||
| 69 | EXPECT_EQ(pinIsInputHigh[1], false); | ||
| 70 | EXPECT_EQ(pinIsInputHigh[2], false); | ||
| 71 | EXPECT_EQ(pinIsInputHigh[3], false); | ||
| 72 | EXPECT_EQ(updates_array_idx, 0); // no updates received | ||
| 73 | } | ||
| 74 | |||
| 75 | TEST_F(EncoderSplitTestNoLeft, TestInitRight) { | ||
| 76 | isLeftHand = false; | ||
| 77 | encoder_init(); | ||
| 78 | EXPECT_EQ(pinIsInputHigh[0], true); | ||
| 79 | EXPECT_EQ(pinIsInputHigh[1], true); | ||
| 80 | EXPECT_EQ(pinIsInputHigh[2], true); | ||
| 81 | EXPECT_EQ(pinIsInputHigh[3], true); | ||
| 82 | EXPECT_EQ(updates_array_idx, 0); // no updates received | ||
| 83 | } | ||
| 84 | |||
| 85 | TEST_F(EncoderSplitTestNoLeft, TestOneClockwiseLeft) { | ||
| 86 | isLeftHand = true; | ||
| 87 | encoder_init(); | ||
| 88 | // send 4 pulses. with resolution 4, that's one step and we should get 1 update. | ||
| 89 | setAndRead(0, false); | ||
| 90 | setAndRead(1, false); | ||
| 91 | setAndRead(0, true); | ||
| 92 | setAndRead(1, true); | ||
| 93 | |||
| 94 | EXPECT_EQ(updates_array_idx, 0); // no updates received | ||
| 95 | } | ||
| 96 | |||
| 97 | TEST_F(EncoderSplitTestNoLeft, TestOneClockwiseRightSent) { | ||
| 98 | isLeftHand = false; | ||
| 99 | encoder_init(); | ||
| 100 | // send 4 pulses. with resolution 4, that's one step and we should get 1 update. | ||
| 101 | setAndRead(2, false); | ||
| 102 | setAndRead(3, false); | ||
| 103 | setAndRead(2, true); | ||
| 104 | setAndRead(3, true); | ||
| 105 | |||
| 106 | uint8_t slave_state[32] = {0}; | ||
| 107 | encoder_state_raw(slave_state); | ||
| 108 | |||
| 109 | EXPECT_EQ(slave_state[0], 0); | ||
| 110 | EXPECT_EQ(slave_state[1], 0xFF); | ||
| 111 | } | ||
| 112 | |||
| 113 | TEST_F(EncoderSplitTestNoLeft, TestMultipleEncodersRightReceived) { | ||
| 114 | isLeftHand = true; | ||
| 115 | encoder_init(); | ||
| 116 | |||
| 117 | uint8_t slave_state[32] = {1, 0xFF}; // First right encoder is CCW, Second right encoder no change, third right encoder CW | ||
| 118 | encoder_update_raw(slave_state); | ||
| 119 | |||
| 120 | EXPECT_EQ(updates_array_idx, 2); // two updates received, one for each changed item on the right side | ||
| 121 | EXPECT_EQ(updates[0].index, 0); | ||
| 122 | EXPECT_EQ(updates[0].clockwise, false); | ||
| 123 | EXPECT_EQ(updates[1].index, 1); | ||
| 124 | EXPECT_EQ(updates[1].clockwise, true); | ||
| 125 | } | ||
