summaryrefslogtreecommitdiff
path: root/quantum/encoder/tests/encoder_tests_split_left_gt_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_gt_right.cpp
parent2eb9ff8efd1df2c98724481c71c8ab8a5b62e31e (diff)
Add encoder abstraction. (#21548)
Diffstat (limited to 'quantum/encoder/tests/encoder_tests_split_left_gt_right.cpp')
-rw-r--r--quantum/encoder/tests/encoder_tests_split_left_gt_right.cpp89
1 files changed, 72 insertions, 17 deletions
diff --git a/quantum/encoder/tests/encoder_tests_split_left_gt_right.cpp b/quantum/encoder/tests/encoder_tests_split_left_gt_right.cpp
index 7b64bb2981..2beb4e3972 100644
--- a/quantum/encoder/tests/encoder_tests_split_left_gt_right.cpp
+++ b/quantum/encoder/tests/encoder_tests_split_left_gt_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 EncoderSplitTestLeftGreaterThanRight : public ::testing::Test { 61class EncoderSplitTestLeftGreaterThanRight : public ::testing::Test {
@@ -94,7 +101,8 @@ TEST_F(EncoderSplitTestLeftGreaterThanRight, TestInitRight) {
94 EXPECT_EQ(updates_array_idx, 0); // no updates received 101 EXPECT_EQ(updates_array_idx, 0); // no updates received
95} 102}
96 103
97TEST_F(EncoderSplitTestLeftGreaterThanRight, TestOneClockwiseLeft) { 104TEST_F(EncoderSplitTestLeftGreaterThanRight, TestOneClockwiseLeftMaster) {
105 isMaster = true;
98 isLeftHand = true; 106 isLeftHand = true;
99 encoder_init(); 107 encoder_init();
100 // send 4 pulses. with resolution 4, that's one step and we should get 1 update. 108 // send 4 pulses. with resolution 4, that's one step and we should get 1 update.
@@ -106,9 +114,19 @@ TEST_F(EncoderSplitTestLeftGreaterThanRight, TestOneClockwiseLeft) {
106 EXPECT_EQ(updates_array_idx, 1); // one update received 114 EXPECT_EQ(updates_array_idx, 1); // one update received
107 EXPECT_EQ(updates[0].index, 0); 115 EXPECT_EQ(updates[0].index, 0);
108 EXPECT_EQ(updates[0].clockwise, true); 116 EXPECT_EQ(updates[0].clockwise, true);
117
118 int events_queued = 0;
119 encoder_events_t events;
120 encoder_retrieve_events(&events);
121 while (events.tail != events.head) {
122 events.tail = (events.tail + 1) % MAX_QUEUED_ENCODER_EVENTS;
123 ++events_queued;
124 }
125 EXPECT_EQ(events_queued, 0); // No events should be queued on master
109} 126}
110 127
111TEST_F(EncoderSplitTestLeftGreaterThanRight, TestOneClockwiseRightSent) { 128TEST_F(EncoderSplitTestLeftGreaterThanRight, TestOneClockwiseRightMaster) {
129 isMaster = true;
112 isLeftHand = false; 130 isLeftHand = false;
113 encoder_init(); 131 encoder_init();
114 // send 4 pulses. with resolution 4, that's one step and we should get 1 update. 132 // send 4 pulses. with resolution 4, that's one step and we should get 1 update.
@@ -117,23 +135,60 @@ TEST_F(EncoderSplitTestLeftGreaterThanRight, TestOneClockwiseRightSent) {
117 setAndRead(6, true); 135 setAndRead(6, true);
118 setAndRead(7, true); 136 setAndRead(7, true);
119 137
120 uint8_t slave_state[32] = {0}; 138 EXPECT_EQ(updates_array_idx, 1); // one update received
121 encoder_state_raw(slave_state); 139 EXPECT_EQ(updates[0].index, 3);
140 EXPECT_EQ(updates[0].clockwise, true);
122 141
123 EXPECT_EQ(slave_state[0], 0xFF); 142 int events_queued = 0;
124 EXPECT_EQ(slave_state[1], 0); 143 encoder_events_t events;
144 encoder_retrieve_events(&events);
145 while (events.tail != events.head) {
146 events.tail = (events.tail + 1) % MAX_QUEUED_ENCODER_EVENTS;
147 ++events_queued;
148 }
149 EXPECT_EQ(events_queued, 0); // No events should be queued on master
125} 150}
126 151
127TEST_F(EncoderSplitTestLeftGreaterThanRight, TestMultipleEncodersRightReceived) { 152TEST_F(EncoderSplitTestLeftGreaterThanRight, TestOneClockwiseLeftSlave) {
153 isMaster = false;
128 isLeftHand = true; 154 isLeftHand = true;
129 encoder_init(); 155 encoder_init();
156 // send 4 pulses. with resolution 4, that's one step and we should get 1 update.
157 setAndRead(0, false);
158 setAndRead(1, false);
159 setAndRead(0, true);
160 setAndRead(1, true);
130 161
131 uint8_t slave_state[32] = {1, 0xFF}; // First right encoder is CCW, Second right encoder no change, third right encoder CW 162 EXPECT_EQ(updates_array_idx, 0); // no updates received
132 encoder_update_raw(slave_state);
133 163
134 EXPECT_EQ(updates_array_idx, 2); // two updates received, one for each changed item on the right side 164 int events_queued = 0;
135 EXPECT_EQ(updates[0].index, 3); 165 encoder_events_t events;
136 EXPECT_EQ(updates[0].clockwise, false); 166 encoder_retrieve_events(&events);
137 EXPECT_EQ(updates[1].index, 4); 167 while (events.tail != events.head) {
138 EXPECT_EQ(updates[1].clockwise, true); 168 events.tail = (events.tail + 1) % MAX_QUEUED_ENCODER_EVENTS;
169 ++events_queued;
170 }
171 EXPECT_EQ(events_queued, 1); // One event should be queued on slave
172}
173
174TEST_F(EncoderSplitTestLeftGreaterThanRight, TestOneClockwiseRightSlave) {
175 isMaster = false;
176 isLeftHand = false;
177 encoder_init();
178 // send 4 pulses. with resolution 4, that's one step and we should get 1 update.
179 setAndRead(6, false);
180 setAndRead(7, false);
181 setAndRead(6, true);
182 setAndRead(7, true);
183
184 EXPECT_EQ(updates_array_idx, 0); // no updates received
185
186 int events_queued = 0;
187 encoder_events_t events;
188 encoder_retrieve_events(&events);
189 while (events.tail != events.head) {
190 events.tail = (events.tail + 1) % MAX_QUEUED_ENCODER_EVENTS;
191 ++events_queued;
192 }
193 EXPECT_EQ(events_queued, 1); // One event should be queued on slave
139} 194}