summaryrefslogtreecommitdiff
path: root/quantum/encoder/tests/encoder_tests_split_left_eq_right.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'quantum/encoder/tests/encoder_tests_split_left_eq_right.cpp')
-rw-r--r--quantum/encoder/tests/encoder_tests_split_left_eq_right.cpp135
1 files changed, 135 insertions, 0 deletions
diff --git a/quantum/encoder/tests/encoder_tests_split_left_eq_right.cpp b/quantum/encoder/tests/encoder_tests_split_left_eq_right.cpp
new file mode 100644
index 0000000000..916e47b185
--- /dev/null
+++ b/quantum/encoder/tests/encoder_tests_split_left_eq_right.cpp
@@ -0,0 +1,135 @@
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
23extern "C" {
24#include "encoder.h"
25#include "encoder/tests/mock_split.h"
26}
27
28struct update {
29 int8_t index;
30 bool clockwise;
31};
32
33uint8_t updates_array_idx = 0;
34update updates[32];
35
36bool isLeftHand;
37
38bool 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
49bool setAndRead(pin_t pin, bool val) {
50 setPin(pin, val);
51 return encoder_read();
52}
53
54class EncoderSplitTestLeftEqRight : 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
65TEST_F(EncoderSplitTestLeftEqRight, TestInitLeft) {
66 isLeftHand = true;
67 encoder_init();
68 EXPECT_EQ(pinIsInputHigh[0], true);
69 EXPECT_EQ(pinIsInputHigh[1], true);
70 EXPECT_EQ(pinIsInputHigh[2], true);
71 EXPECT_EQ(pinIsInputHigh[3], true);
72 EXPECT_EQ(pinIsInputHigh[4], false);
73 EXPECT_EQ(pinIsInputHigh[5], false);
74 EXPECT_EQ(pinIsInputHigh[6], false);
75 EXPECT_EQ(pinIsInputHigh[7], false);
76 EXPECT_EQ(updates_array_idx, 0); // no updates received
77}
78
79TEST_F(EncoderSplitTestLeftEqRight, TestInitRight) {
80 isLeftHand = false;
81 encoder_init();
82 EXPECT_EQ(pinIsInputHigh[0], false);
83 EXPECT_EQ(pinIsInputHigh[1], false);
84 EXPECT_EQ(pinIsInputHigh[2], false);
85 EXPECT_EQ(pinIsInputHigh[3], false);
86 EXPECT_EQ(pinIsInputHigh[4], true);
87 EXPECT_EQ(pinIsInputHigh[5], true);
88 EXPECT_EQ(pinIsInputHigh[6], true);
89 EXPECT_EQ(pinIsInputHigh[7], true);
90 EXPECT_EQ(updates_array_idx, 0); // no updates received
91}
92
93TEST_F(EncoderSplitTestLeftEqRight, TestOneClockwiseLeft) {
94 isLeftHand = true;
95 encoder_init();
96 // send 4 pulses. with resolution 4, that's one step and we should get 1 update.
97 setAndRead(0, false);
98 setAndRead(1, false);
99 setAndRead(0, true);
100 setAndRead(1, true);
101
102 EXPECT_EQ(updates_array_idx, 1); // one update received
103 EXPECT_EQ(updates[0].index, 0);
104 EXPECT_EQ(updates[0].clockwise, true);
105}
106
107TEST_F(EncoderSplitTestLeftEqRight, TestOneClockwiseRightSent) {
108 isLeftHand = false;
109 encoder_init();
110 // send 4 pulses. with resolution 4, that's one step and we should get 1 update.
111 setAndRead(6, false);
112 setAndRead(7, false);
113 setAndRead(6, true);
114 setAndRead(7, true);
115
116 uint8_t slave_state[32] = {0};
117 encoder_state_raw(slave_state);
118
119 EXPECT_EQ(slave_state[0], 0);
120 EXPECT_EQ(slave_state[1], 0xFF);
121}
122
123TEST_F(EncoderSplitTestLeftEqRight, TestMultipleEncodersRightReceived) {
124 isLeftHand = true;
125 encoder_init();
126
127 uint8_t slave_state[32] = {1, 0xFF}; // First right encoder is CCW, Second right encoder CW
128 encoder_update_raw(slave_state);
129
130 EXPECT_EQ(updates_array_idx, 2); // two updates received, one for each changed item on the right side
131 EXPECT_EQ(updates[0].index, 2);
132 EXPECT_EQ(updates[0].clockwise, false);
133 EXPECT_EQ(updates[1].index, 3);
134 EXPECT_EQ(updates[1].clockwise, true);
135}