summaryrefslogtreecommitdiff
path: root/quantum/encoder/tests/encoder_tests_split_no_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_no_right.cpp
parent2eb9ff8efd1df2c98724481c71c8ab8a5b62e31e (diff)
Add encoder abstraction. (#21548)
Diffstat (limited to 'quantum/encoder/tests/encoder_tests_split_no_right.cpp')
-rw-r--r--quantum/encoder/tests/encoder_tests_split_no_right.cpp66
1 files changed, 42 insertions, 24 deletions
diff --git a/quantum/encoder/tests/encoder_tests_split_no_right.cpp b/quantum/encoder/tests/encoder_tests_split_no_right.cpp
index fa0a7c18a8..d39659853b 100644
--- a/quantum/encoder/tests/encoder_tests_split_no_right.cpp
+++ b/quantum/encoder/tests/encoder_tests_split_no_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 EncoderSplitTestNoRight : public ::testing::Test { 61class EncoderSplitTestNoRight : public ::testing::Test {
@@ -82,37 +89,48 @@ TEST_F(EncoderSplitTestNoRight, TestInitRight) {
82 EXPECT_EQ(updates_array_idx, 0); // no updates received 89 EXPECT_EQ(updates_array_idx, 0); // no updates received
83} 90}
84 91
85TEST_F(EncoderSplitTestNoRight, TestOneClockwiseLeft) { 92TEST_F(EncoderSplitTestNoRight, TestOneClockwiseLeftMaster) {
93 isMaster = true;
86 isLeftHand = true; 94 isLeftHand = true;
87 encoder_init(); 95 encoder_init();
88 // send 4 pulses. with resolution 4, that's one step and we should get 1 update. 96 // send 4 pulses. with resolution 4, that's one step and we should get 1 update.
89 setAndRead(0, false); 97 setAndRead(2, false);
90 setAndRead(1, false); 98 setAndRead(3, false);
91 setAndRead(0, true); 99 setAndRead(2, true);
92 setAndRead(1, true); 100 setAndRead(3, true);
93 101
94 EXPECT_EQ(updates_array_idx, 1); // one updates received 102 EXPECT_EQ(updates_array_idx, 1); // one update received
95 EXPECT_EQ(updates[0].index, 0); 103 EXPECT_EQ(updates[0].index, 1);
96 EXPECT_EQ(updates[0].clockwise, true); 104 EXPECT_EQ(updates[0].clockwise, true);
97}
98 105
99TEST_F(EncoderSplitTestNoRight, TestOneClockwiseRightSent) { 106 int events_queued = 0;
100 isLeftHand = false; 107 encoder_events_t events;
101 encoder_init(); 108 encoder_retrieve_events(&events);
102 109 while (events.tail != events.head) {
103 uint8_t slave_state[32] = {0xAA, 0xAA}; 110 events.tail = (events.tail + 1) % MAX_QUEUED_ENCODER_EVENTS;
104 encoder_state_raw(slave_state); 111 ++events_queued;
105 112 }
106 EXPECT_EQ(slave_state[0], 0xAA); 113 EXPECT_EQ(events_queued, 0); // No events should be queued on master
107 EXPECT_EQ(slave_state[1], 0xAA);
108} 114}
109 115
110TEST_F(EncoderSplitTestNoRight, TestMultipleEncodersRightReceived) { 116TEST_F(EncoderSplitTestNoRight, TestOneClockwiseRightSlave) {
117 isMaster = false;
111 isLeftHand = true; 118 isLeftHand = true;
112 encoder_init(); 119 encoder_init();
120 // send 4 pulses. with resolution 4, that's one step and we should get 1 update.
121 setAndRead(2, false);
122 setAndRead(3, false);
123 setAndRead(2, true);
124 setAndRead(3, true);
113 125
114 uint8_t slave_state[32] = {1, 0xFF}; // These values would trigger updates if there were encoders on the other side 126 EXPECT_EQ(updates_array_idx, 0); // no updates received
115 encoder_update_raw(slave_state);
116 127
117 EXPECT_EQ(updates_array_idx, 0); // no updates received -- no right-hand encoders 128 int events_queued = 0;
129 encoder_events_t events;
130 encoder_retrieve_events(&events);
131 while (events.tail != events.head) {
132 events.tail = (events.tail + 1) % MAX_QUEUED_ENCODER_EVENTS;
133 ++events_queued;
134 }
135 EXPECT_EQ(events_queued, 1); // One event should be queued on slave
118} 136}