diff options
| author | Nick Brassel <nick@tzarc.org> | 2024-02-18 21:17:15 +1100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-02-18 21:17:15 +1100 |
| commit | 9d9cdaaa2d035787b0b50c26f2975695fdbc16f4 (patch) | |
| tree | 1a9f5d16ffc0e3bd27bc14791c25405a79ccd069 /quantum/encoder/tests/encoder_tests_split_no_left.cpp | |
| parent | 2eb9ff8efd1df2c98724481c71c8ab8a5b62e31e (diff) | |
Add encoder abstraction. (#21548)
Diffstat (limited to 'quantum/encoder/tests/encoder_tests_split_no_left.cpp')
| -rw-r--r-- | quantum/encoder/tests/encoder_tests_split_no_left.cpp | 69 |
1 files changed, 40 insertions, 29 deletions
diff --git a/quantum/encoder/tests/encoder_tests_split_no_left.cpp b/quantum/encoder/tests/encoder_tests_split_no_left.cpp index b6b2d7e2d1..980e4074ff 100644 --- a/quantum/encoder/tests/encoder_tests_split_no_left.cpp +++ b/quantum/encoder/tests/encoder_tests_split_no_left.cpp | |||
| @@ -33,22 +33,29 @@ struct update { | |||
| 33 | uint8_t updates_array_idx = 0; | 33 | uint8_t updates_array_idx = 0; |
| 34 | update updates[32]; | 34 | update updates[32]; |
| 35 | 35 | ||
| 36 | bool isMaster; | ||
| 36 | bool isLeftHand; | 37 | bool isLeftHand; |
| 37 | 38 | ||
| 39 | extern "C" { | ||
| 40 | bool is_keyboard_master(void) { | ||
| 41 | return isMaster; | ||
| 42 | } | ||
| 43 | |||
| 38 | bool encoder_update_kb(uint8_t index, bool clockwise) { | 44 | bool 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 | ||
| 49 | bool setAndRead(pin_t pin, bool val) { | 56 | bool 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 | ||
| 54 | class EncoderSplitTestNoLeft : public ::testing::Test { | 61 | class EncoderSplitTestNoLeft : public ::testing::Test { |
| @@ -82,19 +89,8 @@ TEST_F(EncoderSplitTestNoLeft, 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 | ||
| 85 | TEST_F(EncoderSplitTestNoLeft, TestOneClockwiseLeft) { | 92 | TEST_F(EncoderSplitTestNoLeft, TestOneClockwiseLeftMaster) { |
| 86 | isLeftHand = true; | 93 | isMaster = 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 | |||
| 97 | TEST_F(EncoderSplitTestNoLeft, TestOneClockwiseRightSent) { | ||
| 98 | isLeftHand = false; | 94 | isLeftHand = false; |
| 99 | encoder_init(); | 95 | encoder_init(); |
| 100 | // 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. |
| @@ -103,23 +99,38 @@ TEST_F(EncoderSplitTestNoLeft, TestOneClockwiseRightSent) { | |||
| 103 | setAndRead(2, true); | 99 | setAndRead(2, true); |
| 104 | setAndRead(3, true); | 100 | setAndRead(3, true); |
| 105 | 101 | ||
| 106 | uint8_t slave_state[32] = {0}; | 102 | EXPECT_EQ(updates_array_idx, 1); // one update received |
| 107 | encoder_state_raw(slave_state); | 103 | EXPECT_EQ(updates[0].index, 1); |
| 104 | EXPECT_EQ(updates[0].clockwise, true); | ||
| 108 | 105 | ||
| 109 | EXPECT_EQ(slave_state[0], 0); | 106 | int events_queued = 0; |
| 110 | EXPECT_EQ(slave_state[1], 0xFF); | 107 | encoder_events_t events; |
| 108 | encoder_retrieve_events(&events); | ||
| 109 | while (events.tail != events.head) { | ||
| 110 | events.tail = (events.tail + 1) % MAX_QUEUED_ENCODER_EVENTS; | ||
| 111 | ++events_queued; | ||
| 112 | } | ||
| 113 | EXPECT_EQ(events_queued, 0); // No events should be queued on master | ||
| 111 | } | 114 | } |
| 112 | 115 | ||
| 113 | TEST_F(EncoderSplitTestNoLeft, TestMultipleEncodersRightReceived) { | 116 | TEST_F(EncoderSplitTestNoLeft, TestOneClockwiseRightSlave) { |
| 114 | isLeftHand = true; | 117 | isMaster = false; |
| 118 | isLeftHand = false; | ||
| 115 | 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); | ||
| 116 | 125 | ||
| 117 | uint8_t slave_state[32] = {1, 0xFF}; // First right encoder is CCW, Second right encoder no change, third right encoder CW | 126 | EXPECT_EQ(updates_array_idx, 0); // no updates received |
| 118 | encoder_update_raw(slave_state); | ||
| 119 | 127 | ||
| 120 | EXPECT_EQ(updates_array_idx, 2); // two updates received, one for each changed item on the right side | 128 | int events_queued = 0; |
| 121 | EXPECT_EQ(updates[0].index, 0); | 129 | encoder_events_t events; |
| 122 | EXPECT_EQ(updates[0].clockwise, false); | 130 | encoder_retrieve_events(&events); |
| 123 | EXPECT_EQ(updates[1].index, 1); | 131 | while (events.tail != events.head) { |
| 124 | EXPECT_EQ(updates[1].clockwise, true); | 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 | ||
| 125 | } | 136 | } |
