encoder_tests_split_left_lt_right.cpp (5945B)
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 updates_array_idx = 0; 34 update updates[32]; 35 36 bool isMaster; 37 bool isLeftHand; 38 39 extern "C" { 40 bool is_keyboard_master(void) { 41 return isMaster; 42 } 43 44 bool encoder_update_kb(uint8_t index, bool clockwise) { 45 if (!is_keyboard_master()) { 46 // this method has no effect on slave half 47 printf("ignoring update on slave (%d,%s)\n", index, clockwise ? "CW" : "CC"); 48 return true; 49 } 50 updates[updates_array_idx % 32] = {index, clockwise}; 51 updates_array_idx++; 52 return true; 53 } 54 }; 55 56 bool setAndRead(pin_t pin, bool val) { 57 setPin(pin, val); 58 return encoder_task(); 59 } 60 61 class EncoderSplitTestLeftLessThanRight : public ::testing::Test { 62 protected: 63 void SetUp() override { 64 updates_array_idx = 0; 65 for (int i = 0; i < 32; i++) { 66 pinIsInputHigh[i] = 0; 67 pins[i] = 0; 68 } 69 } 70 }; 71 72 TEST_F(EncoderSplitTestLeftLessThanRight, TestInitLeft) { 73 isLeftHand = true; 74 encoder_init(); 75 EXPECT_EQ(pinIsInputHigh[0], true); 76 EXPECT_EQ(pinIsInputHigh[1], true); 77 EXPECT_EQ(pinIsInputHigh[2], true); 78 EXPECT_EQ(pinIsInputHigh[3], true); 79 EXPECT_EQ(pinIsInputHigh[4], false); 80 EXPECT_EQ(pinIsInputHigh[5], false); 81 EXPECT_EQ(pinIsInputHigh[6], false); 82 EXPECT_EQ(pinIsInputHigh[7], false); 83 EXPECT_EQ(pinIsInputHigh[8], false); 84 EXPECT_EQ(pinIsInputHigh[9], false); 85 EXPECT_EQ(updates_array_idx, 0); // no updates received 86 } 87 88 TEST_F(EncoderSplitTestLeftLessThanRight, TestInitRight) { 89 isLeftHand = false; 90 encoder_init(); 91 EXPECT_EQ(pinIsInputHigh[0], false); 92 EXPECT_EQ(pinIsInputHigh[1], false); 93 EXPECT_EQ(pinIsInputHigh[2], false); 94 EXPECT_EQ(pinIsInputHigh[3], false); 95 EXPECT_EQ(pinIsInputHigh[4], true); 96 EXPECT_EQ(pinIsInputHigh[5], true); 97 EXPECT_EQ(pinIsInputHigh[6], true); 98 EXPECT_EQ(pinIsInputHigh[7], true); 99 EXPECT_EQ(pinIsInputHigh[8], true); 100 EXPECT_EQ(pinIsInputHigh[9], true); 101 EXPECT_EQ(updates_array_idx, 0); // no updates received 102 } 103 104 TEST_F(EncoderSplitTestLeftLessThanRight, TestOneClockwiseLeftMaster) { 105 isMaster = true; 106 isLeftHand = true; 107 encoder_init(); 108 // send 4 pulses. with resolution 4, that's one step and we should get 1 update. 109 setAndRead(0, false); 110 setAndRead(1, false); 111 setAndRead(0, true); 112 setAndRead(1, true); 113 114 EXPECT_EQ(updates_array_idx, 1); // one update received 115 EXPECT_EQ(updates[0].index, 0); 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 126 } 127 128 TEST_F(EncoderSplitTestLeftLessThanRight, TestOneClockwiseRightMaster) { 129 isMaster = true; 130 isLeftHand = false; 131 encoder_init(); 132 // send 4 pulses. with resolution 4, that's one step and we should get 1 update. 133 setAndRead(6, false); 134 setAndRead(7, false); 135 setAndRead(6, true); 136 setAndRead(7, true); 137 138 EXPECT_EQ(updates_array_idx, 1); // one update received 139 EXPECT_EQ(updates[0].index, 3); 140 EXPECT_EQ(updates[0].clockwise, true); 141 142 int events_queued = 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 150 } 151 152 TEST_F(EncoderSplitTestLeftLessThanRight, TestOneClockwiseLeftSlave) { 153 isMaster = false; 154 isLeftHand = true; 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); 161 162 EXPECT_EQ(updates_array_idx, 0); // no updates received 163 164 int events_queued = 0; 165 encoder_events_t events; 166 encoder_retrieve_events(&events); 167 while (events.tail != events.head) { 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 174 TEST_F(EncoderSplitTestLeftLessThanRight, 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 194 }