summaryrefslogtreecommitdiff
path: root/quantum/encoder/tests/encoder_tests_split_no_left.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'quantum/encoder/tests/encoder_tests_split_no_left.cpp')
-rw-r--r--quantum/encoder/tests/encoder_tests_split_no_left.cpp125
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
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 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
65TEST_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
75TEST_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
85TEST_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
97TEST_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
113TEST_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}