summaryrefslogtreecommitdiff
path: root/quantum/encoder/tests/encoder_tests_split_left_eq_right.cpp
diff options
context:
space:
mode:
authorNick Brassel <nick@tzarc.org>2024-02-18 21:17:15 +1100
committerGitHub <noreply@github.com>2024-02-18 21:17:15 +1100
commit9d9cdaaa2d035787b0b50c26f2975695fdbc16f4 (patch)
tree1a9f5d16ffc0e3bd27bc14791c25405a79ccd069 /quantum/encoder/tests/encoder_tests_split_left_eq_right.cpp
parent2eb9ff8efd1df2c98724481c71c8ab8a5b62e31e (diff)
Add encoder abstraction. (#21548)
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.cpp91
1 files changed, 74 insertions, 17 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
index 916e47b185..7d6b3e30e6 100644
--- a/quantum/encoder/tests/encoder_tests_split_left_eq_right.cpp
+++ b/quantum/encoder/tests/encoder_tests_split_left_eq_right.cpp
@@ -33,22 +33,29 @@ struct update {
33uint8_t updates_array_idx = 0; 33uint8_t updates_array_idx = 0;
34update updates[32]; 34update updates[32];
35 35
36bool isMaster;
36bool isLeftHand; 37bool isLeftHand;
37 38
39extern "C" {
40bool is_keyboard_master(void) {
41 return isMaster;
42}
43
38bool encoder_update_kb(uint8_t index, bool clockwise) { 44bool encoder_update_kb(uint8_t index, bool clockwise) {
39 if (!isLeftHand) { 45 if (!is_keyboard_master()) {
40 // this method has no effect on slave half 46 // this method has no effect on slave half
41 printf("ignoring update on right hand (%d,%s)\n", index, clockwise ? "CW" : "CC"); 47 printf("ignoring update on slave (%d,%s)\n", index, clockwise ? "CW" : "CC");
42 return true; 48 return true;
43 } 49 }
44 updates[updates_array_idx % 32] = {index, clockwise}; 50 updates[updates_array_idx % 32] = {index, clockwise};
45 updates_array_idx++; 51 updates_array_idx++;
46 return true; 52 return true;
47} 53}
54};
48 55
49bool setAndRead(pin_t pin, bool val) { 56bool setAndRead(pin_t pin, bool val) {
50 setPin(pin, val); 57 setPin(pin, val);
51 return encoder_read(); 58 return encoder_task();
52} 59}
53 60
54class EncoderSplitTestLeftEqRight : public ::testing::Test { 61class EncoderSplitTestLeftEqRight : public ::testing::Test {
@@ -63,6 +70,7 @@ class EncoderSplitTestLeftEqRight : public ::testing::Test {
63}; 70};
64 71
65TEST_F(EncoderSplitTestLeftEqRight, TestInitLeft) { 72TEST_F(EncoderSplitTestLeftEqRight, TestInitLeft) {
73 isMaster = true;
66 isLeftHand = true; 74 isLeftHand = true;
67 encoder_init(); 75 encoder_init();
68 EXPECT_EQ(pinIsInputHigh[0], true); 76 EXPECT_EQ(pinIsInputHigh[0], true);
@@ -77,6 +85,7 @@ TEST_F(EncoderSplitTestLeftEqRight, TestInitLeft) {
77} 85}
78 86
79TEST_F(EncoderSplitTestLeftEqRight, TestInitRight) { 87TEST_F(EncoderSplitTestLeftEqRight, TestInitRight) {
88 isMaster = true;
80 isLeftHand = false; 89 isLeftHand = false;
81 encoder_init(); 90 encoder_init();
82 EXPECT_EQ(pinIsInputHigh[0], false); 91 EXPECT_EQ(pinIsInputHigh[0], false);
@@ -90,7 +99,8 @@ TEST_F(EncoderSplitTestLeftEqRight, TestInitRight) {
90 EXPECT_EQ(updates_array_idx, 0); // no updates received 99 EXPECT_EQ(updates_array_idx, 0); // no updates received
91} 100}
92 101
93TEST_F(EncoderSplitTestLeftEqRight, TestOneClockwiseLeft) { 102TEST_F(EncoderSplitTestLeftEqRight, TestOneClockwiseLeftMaster) {
103 isMaster = true;
94 isLeftHand = true; 104 isLeftHand = true;
95 encoder_init(); 105 encoder_init();
96 // send 4 pulses. with resolution 4, that's one step and we should get 1 update. 106 // send 4 pulses. with resolution 4, that's one step and we should get 1 update.
@@ -102,9 +112,19 @@ TEST_F(EncoderSplitTestLeftEqRight, TestOneClockwiseLeft) {
102 EXPECT_EQ(updates_array_idx, 1); // one update received 112 EXPECT_EQ(updates_array_idx, 1); // one update received
103 EXPECT_EQ(updates[0].index, 0); 113 EXPECT_EQ(updates[0].index, 0);
104 EXPECT_EQ(updates[0].clockwise, true); 114 EXPECT_EQ(updates[0].clockwise, true);
115
116 int events_queued = 0;
117 encoder_events_t events;
118 encoder_retrieve_events(&events);
119 while (events.tail != events.head) {
120 events.tail = (events.tail + 1) % MAX_QUEUED_ENCODER_EVENTS;
121 ++events_queued;
122 }
123 EXPECT_EQ(events_queued, 0); // No events should be queued on master
105} 124}
106 125
107TEST_F(EncoderSplitTestLeftEqRight, TestOneClockwiseRightSent) { 126TEST_F(EncoderSplitTestLeftEqRight, TestOneClockwiseRightMaster) {
127 isMaster = true;
108 isLeftHand = false; 128 isLeftHand = false;
109 encoder_init(); 129 encoder_init();
110 // send 4 pulses. with resolution 4, that's one step and we should get 1 update. 130 // send 4 pulses. with resolution 4, that's one step and we should get 1 update.
@@ -113,23 +133,60 @@ TEST_F(EncoderSplitTestLeftEqRight, TestOneClockwiseRightSent) {
113 setAndRead(6, true); 133 setAndRead(6, true);
114 setAndRead(7, true); 134 setAndRead(7, true);
115 135
116 uint8_t slave_state[32] = {0}; 136 EXPECT_EQ(updates_array_idx, 1); // one update received
117 encoder_state_raw(slave_state); 137 EXPECT_EQ(updates[0].index, 3);
138 EXPECT_EQ(updates[0].clockwise, true);
118 139
119 EXPECT_EQ(slave_state[0], 0); 140 int events_queued = 0;
120 EXPECT_EQ(slave_state[1], 0xFF); 141 encoder_events_t events;
142 encoder_retrieve_events(&events);
143 while (events.tail != events.head) {
144 events.tail = (events.tail + 1) % MAX_QUEUED_ENCODER_EVENTS;
145 ++events_queued;
146 }
147 EXPECT_EQ(events_queued, 0); // No events should be queued on master
121} 148}
122 149
123TEST_F(EncoderSplitTestLeftEqRight, TestMultipleEncodersRightReceived) { 150TEST_F(EncoderSplitTestLeftEqRight, TestOneClockwiseLeftSlave) {
151 isMaster = false;
124 isLeftHand = true; 152 isLeftHand = true;
125 encoder_init(); 153 encoder_init();
154 // send 4 pulses. with resolution 4, that's one step and we should get 1 update.
155 setAndRead(0, false);
156 setAndRead(1, false);
157 setAndRead(0, true);
158 setAndRead(1, true);
126 159
127 uint8_t slave_state[32] = {1, 0xFF}; // First right encoder is CCW, Second right encoder CW 160 EXPECT_EQ(updates_array_idx, 0); // no updates received
128 encoder_update_raw(slave_state);
129 161
130 EXPECT_EQ(updates_array_idx, 2); // two updates received, one for each changed item on the right side 162 int events_queued = 0;
131 EXPECT_EQ(updates[0].index, 2); 163 encoder_events_t events;
132 EXPECT_EQ(updates[0].clockwise, false); 164 encoder_retrieve_events(&events);
133 EXPECT_EQ(updates[1].index, 3); 165 while (events.tail != events.head) {
134 EXPECT_EQ(updates[1].clockwise, true); 166 events.tail = (events.tail + 1) % MAX_QUEUED_ENCODER_EVENTS;
167 ++events_queued;
168 }
169 EXPECT_EQ(events_queued, 1); // One event should be queued on slave
170}
171
172TEST_F(EncoderSplitTestLeftEqRight, TestOneClockwiseRightSlave) {
173 isMaster = false;
174 isLeftHand = false;
175 encoder_init();
176 // send 4 pulses. with resolution 4, that's one step and we should get 1 update.
177 setAndRead(6, false);
178 setAndRead(7, false);
179 setAndRead(6, true);
180 setAndRead(7, true);
181
182 EXPECT_EQ(updates_array_idx, 0); // no updates received
183
184 int events_queued = 0;
185 encoder_events_t events;
186 encoder_retrieve_events(&events);
187 while (events.tail != events.head) {
188 events.tail = (events.tail + 1) % MAX_QUEUED_ENCODER_EVENTS;
189 ++events_queued;
190 }
191 EXPECT_EQ(events_queued, 1); // One event should be queued on slave
135} 192}