diff options
| -rw-r--r-- | quantum/encoder.c | 14 | ||||
| -rw-r--r-- | quantum/encoder/tests/config_mock_split_role.h | 26 | ||||
| -rw-r--r-- | quantum/encoder/tests/encoder_tests_split_role.cpp | 122 | ||||
| -rw-r--r-- | quantum/encoder/tests/mock.c | 4 | ||||
| -rw-r--r-- | quantum/encoder/tests/mock_split.c | 4 | ||||
| -rw-r--r-- | quantum/encoder/tests/rules.mk | 10 | ||||
| -rw-r--r-- | quantum/encoder/tests/testlist.mk | 3 |
7 files changed, 180 insertions, 3 deletions
diff --git a/quantum/encoder.c b/quantum/encoder.c index 1393e34868..3aee340249 100644 --- a/quantum/encoder.c +++ b/quantum/encoder.c | |||
| @@ -80,6 +80,10 @@ __attribute__((weak)) bool encoder_update_kb(uint8_t index, bool clockwise) { | |||
| 80 | return encoder_update_user(index, clockwise); | 80 | return encoder_update_user(index, clockwise); |
| 81 | } | 81 | } |
| 82 | 82 | ||
| 83 | __attribute__((weak)) bool should_process_encoder(void) { | ||
| 84 | return is_keyboard_master(); | ||
| 85 | } | ||
| 86 | |||
| 83 | void encoder_init(void) { | 87 | void encoder_init(void) { |
| 84 | #ifdef SPLIT_KEYBOARD | 88 | #ifdef SPLIT_KEYBOARD |
| 85 | thisHand = isLeftHand ? 0 : NUM_ENCODERS_LEFT; | 89 | thisHand = isLeftHand ? 0 : NUM_ENCODERS_LEFT; |
| @@ -179,8 +183,11 @@ static bool encoder_update(uint8_t index, uint8_t state) { | |||
| 179 | 183 | ||
| 180 | encoder_value[index]++; | 184 | encoder_value[index]++; |
| 181 | changed = true; | 185 | changed = true; |
| 186 | #ifdef SPLIT_KEYBOARD | ||
| 187 | if (should_process_encoder()) | ||
| 188 | #endif // SPLIT_KEYBOARD | ||
| 182 | #ifdef ENCODER_MAP_ENABLE | 189 | #ifdef ENCODER_MAP_ENABLE |
| 183 | encoder_exec_mapping(index, ENCODER_COUNTER_CLOCKWISE); | 190 | encoder_exec_mapping(index, ENCODER_COUNTER_CLOCKWISE); |
| 184 | #else // ENCODER_MAP_ENABLE | 191 | #else // ENCODER_MAP_ENABLE |
| 185 | encoder_update_kb(index, ENCODER_COUNTER_CLOCKWISE); | 192 | encoder_update_kb(index, ENCODER_COUNTER_CLOCKWISE); |
| 186 | #endif // ENCODER_MAP_ENABLE | 193 | #endif // ENCODER_MAP_ENABLE |
| @@ -193,8 +200,11 @@ static bool encoder_update(uint8_t index, uint8_t state) { | |||
| 193 | #endif | 200 | #endif |
| 194 | encoder_value[index]--; | 201 | encoder_value[index]--; |
| 195 | changed = true; | 202 | changed = true; |
| 203 | #ifdef SPLIT_KEYBOARD | ||
| 204 | if (should_process_encoder()) | ||
| 205 | #endif // SPLIT_KEYBOARD | ||
| 196 | #ifdef ENCODER_MAP_ENABLE | 206 | #ifdef ENCODER_MAP_ENABLE |
| 197 | encoder_exec_mapping(index, ENCODER_CLOCKWISE); | 207 | encoder_exec_mapping(index, ENCODER_CLOCKWISE); |
| 198 | #else // ENCODER_MAP_ENABLE | 208 | #else // ENCODER_MAP_ENABLE |
| 199 | encoder_update_kb(index, ENCODER_CLOCKWISE); | 209 | encoder_update_kb(index, ENCODER_CLOCKWISE); |
| 200 | #endif // ENCODER_MAP_ENABLE | 210 | #endif // ENCODER_MAP_ENABLE |
diff --git a/quantum/encoder/tests/config_mock_split_role.h b/quantum/encoder/tests/config_mock_split_role.h new file mode 100644 index 0000000000..c80ac4d519 --- /dev/null +++ b/quantum/encoder/tests/config_mock_split_role.h | |||
| @@ -0,0 +1,26 @@ | |||
| 1 | // Copyright 2022 Nick Brassel (@tzarc) | ||
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later | ||
| 3 | #pragma once | ||
| 4 | |||
| 5 | #define MATRIX_ROWS 1 | ||
| 6 | #define MATRIX_COLS 1 | ||
| 7 | |||
| 8 | /* Here, "pins" from 0 to 31 are allowed. */ | ||
| 9 | #define ENCODERS_PAD_A \ | ||
| 10 | { 0, 2 } | ||
| 11 | #define ENCODERS_PAD_B \ | ||
| 12 | { 1, 3 } | ||
| 13 | #define ENCODERS_PAD_A_RIGHT \ | ||
| 14 | { 4, 6 } | ||
| 15 | #define ENCODERS_PAD_B_RIGHT \ | ||
| 16 | { 5, 7 } | ||
| 17 | |||
| 18 | #ifdef __cplusplus | ||
| 19 | extern "C" { | ||
| 20 | #endif | ||
| 21 | |||
| 22 | #include "mock_split.h" | ||
| 23 | |||
| 24 | #ifdef __cplusplus | ||
| 25 | }; | ||
| 26 | #endif | ||
diff --git a/quantum/encoder/tests/encoder_tests_split_role.cpp b/quantum/encoder/tests/encoder_tests_split_role.cpp new file mode 100644 index 0000000000..02264067f4 --- /dev/null +++ b/quantum/encoder/tests/encoder_tests_split_role.cpp | |||
| @@ -0,0 +1,122 @@ | |||
| 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 | |||
| 23 | extern "C" { | ||
| 24 | #include "encoder.h" | ||
| 25 | #include "encoder/tests/mock_split.h" | ||
| 26 | } | ||
| 27 | |||
| 28 | struct update { | ||
| 29 | int8_t index; | ||
| 30 | bool clockwise; | ||
| 31 | }; | ||
| 32 | |||
| 33 | uint8_t num_updates = 0; | ||
| 34 | |||
| 35 | bool isMaster; | ||
| 36 | bool isLeftHand; | ||
| 37 | |||
| 38 | bool is_keyboard_master(void) { | ||
| 39 | return isMaster; | ||
| 40 | } | ||
| 41 | |||
| 42 | bool encoder_update_kb(uint8_t index, bool clockwise) { | ||
| 43 | if (!isMaster) { | ||
| 44 | ADD_FAILURE() << "We shouldn't get here."; | ||
| 45 | } | ||
| 46 | num_updates++; | ||
| 47 | return true; | ||
| 48 | } | ||
| 49 | |||
| 50 | bool setAndRead(pin_t pin, bool val) { | ||
| 51 | setPin(pin, val); | ||
| 52 | return encoder_read(); | ||
| 53 | } | ||
| 54 | |||
| 55 | class EncoderSplitTestRole : public ::testing::Test { | ||
| 56 | protected: | ||
| 57 | void SetUp() override { | ||
| 58 | num_updates = 0; | ||
| 59 | for (int i = 0; i < 32; i++) { | ||
| 60 | pinIsInputHigh[i] = 0; | ||
| 61 | pins[i] = 0; | ||
| 62 | } | ||
| 63 | } | ||
| 64 | }; | ||
| 65 | |||
| 66 | TEST_F(EncoderSplitTestRole, TestPrimaryLeft) { | ||
| 67 | isMaster = true; | ||
| 68 | isLeftHand = true; | ||
| 69 | encoder_init(); | ||
| 70 | // send 4 pulses. with resolution 4, that's one step and we should get 1 update. | ||
| 71 | setAndRead(0, false); | ||
| 72 | setAndRead(1, false); | ||
| 73 | setAndRead(0, true); | ||
| 74 | setAndRead(1, true); | ||
| 75 | |||
| 76 | EXPECT_EQ(num_updates, 1); // one update received | ||
| 77 | } | ||
| 78 | |||
| 79 | TEST_F(EncoderSplitTestRole, TestPrimaryRight) { | ||
| 80 | isMaster = true; | ||
| 81 | isLeftHand = false; | ||
| 82 | encoder_init(); | ||
| 83 | // send 4 pulses. with resolution 4, that's one step and we should get 1 update. | ||
| 84 | setAndRead(6, false); | ||
| 85 | setAndRead(7, false); | ||
| 86 | setAndRead(6, true); | ||
| 87 | setAndRead(7, true); | ||
| 88 | |||
| 89 | uint8_t slave_state[32] = {0}; | ||
| 90 | encoder_state_raw(slave_state); | ||
| 91 | |||
| 92 | EXPECT_EQ(num_updates, 1); // one update received | ||
| 93 | } | ||
| 94 | |||
| 95 | TEST_F(EncoderSplitTestRole, TestNotPrimaryLeft) { | ||
| 96 | isMaster = false; | ||
| 97 | isLeftHand = true; | ||
| 98 | encoder_init(); | ||
| 99 | // send 4 pulses. with resolution 4, that's one step and we should get 1 update. | ||
| 100 | setAndRead(0, false); | ||
| 101 | setAndRead(1, false); | ||
| 102 | setAndRead(0, true); | ||
| 103 | setAndRead(1, true); | ||
| 104 | |||
| 105 | EXPECT_EQ(num_updates, 0); // zero updates received | ||
| 106 | } | ||
| 107 | |||
| 108 | TEST_F(EncoderSplitTestRole, TestNotPrimaryRight) { | ||
| 109 | isMaster = false; | ||
| 110 | isLeftHand = false; | ||
| 111 | encoder_init(); | ||
| 112 | // send 4 pulses. with resolution 4, that's one step and we should get 1 update. | ||
| 113 | setAndRead(6, false); | ||
| 114 | setAndRead(7, false); | ||
| 115 | setAndRead(6, true); | ||
| 116 | setAndRead(7, true); | ||
| 117 | |||
| 118 | uint8_t slave_state[32] = {0}; | ||
| 119 | encoder_state_raw(slave_state); | ||
| 120 | |||
| 121 | EXPECT_EQ(num_updates, 0); // zero updates received | ||
| 122 | } | ||
diff --git a/quantum/encoder/tests/mock.c b/quantum/encoder/tests/mock.c index 10a00cb8f2..61f2f8294d 100644 --- a/quantum/encoder/tests/mock.c +++ b/quantum/encoder/tests/mock.c | |||
| @@ -34,3 +34,7 @@ bool setPin(pin_t pin, bool val) { | |||
| 34 | pins[pin] = val; | 34 | pins[pin] = val; |
| 35 | return val; | 35 | return val; |
| 36 | } | 36 | } |
| 37 | |||
| 38 | __attribute__((weak)) bool is_keyboard_master(void) { | ||
| 39 | return true; | ||
| 40 | } | ||
diff --git a/quantum/encoder/tests/mock_split.c b/quantum/encoder/tests/mock_split.c index dd3c26d958..5cc6cd19e1 100644 --- a/quantum/encoder/tests/mock_split.c +++ b/quantum/encoder/tests/mock_split.c | |||
| @@ -36,3 +36,7 @@ bool setPin(pin_t pin, bool val) { | |||
| 36 | } | 36 | } |
| 37 | 37 | ||
| 38 | void last_encoder_activity_trigger(void) {} | 38 | void last_encoder_activity_trigger(void) {} |
| 39 | |||
| 40 | __attribute__((weak)) bool is_keyboard_master(void) { | ||
| 41 | return true; | ||
| 42 | } | ||
diff --git a/quantum/encoder/tests/rules.mk b/quantum/encoder/tests/rules.mk index 6a2611952c..d01c1c66ee 100644 --- a/quantum/encoder/tests/rules.mk +++ b/quantum/encoder/tests/rules.mk | |||
| @@ -56,3 +56,13 @@ encoder_split_no_right_SRC := \ | |||
| 56 | $(QUANTUM_PATH)/encoder/tests/mock_split.c \ | 56 | $(QUANTUM_PATH)/encoder/tests/mock_split.c \ |
| 57 | $(QUANTUM_PATH)/encoder/tests/encoder_tests_split_no_right.cpp \ | 57 | $(QUANTUM_PATH)/encoder/tests/encoder_tests_split_no_right.cpp \ |
| 58 | $(QUANTUM_PATH)/encoder.c | 58 | $(QUANTUM_PATH)/encoder.c |
| 59 | |||
| 60 | encoder_split_role_DEFS := -DENCODER_TESTS -DENCODER_ENABLE -DENCODER_MOCK_SPLIT | ||
| 61 | encoder_split_role_INC := $(QUANTUM_PATH)/split_common | ||
| 62 | encoder_split_role_CONFIG := $(QUANTUM_PATH)/encoder/tests/config_mock_split_role.h | ||
| 63 | |||
| 64 | encoder_split_role_SRC := \ | ||
| 65 | platforms/test/timer.c \ | ||
| 66 | $(QUANTUM_PATH)/encoder/tests/mock_split.c \ | ||
| 67 | $(QUANTUM_PATH)/encoder/tests/encoder_tests_split_role.cpp \ | ||
| 68 | $(QUANTUM_PATH)/encoder.c | ||
diff --git a/quantum/encoder/tests/testlist.mk b/quantum/encoder/tests/testlist.mk index 6b2fd84d96..a407f1fadd 100644 --- a/quantum/encoder/tests/testlist.mk +++ b/quantum/encoder/tests/testlist.mk | |||
| @@ -4,4 +4,5 @@ TEST_LIST += \ | |||
| 4 | encoder_split_left_gt_right \ | 4 | encoder_split_left_gt_right \ |
| 5 | encoder_split_left_lt_right \ | 5 | encoder_split_left_lt_right \ |
| 6 | encoder_split_no_left \ | 6 | encoder_split_no_left \ |
| 7 | encoder_split_no_right | 7 | encoder_split_no_right \ |
| 8 | encoder_split_role \ | ||
