keyboard stuff
at master 192 lines 5.8 kB view raw
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 23extern "C" { 24#include "encoder.h" 25#include "encoder/tests/mock_split.h" 26} 27 28struct update { 29 int8_t index; 30 bool clockwise; 31}; 32 33uint8_t updates_array_idx = 0; 34update updates[32]; 35 36bool isMaster; 37bool isLeftHand; 38 39extern "C" { 40bool is_keyboard_master(void) { 41 return isMaster; 42} 43 44bool 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 56bool setAndRead(pin_t pin, bool val) { 57 setPin(pin, val); 58 return encoder_task(); 59} 60 61class EncoderSplitTestLeftEqRight : 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 72TEST_F(EncoderSplitTestLeftEqRight, TestInitLeft) { 73 isMaster = true; 74 isLeftHand = true; 75 encoder_init(); 76 EXPECT_EQ(pinIsInputHigh[0], true); 77 EXPECT_EQ(pinIsInputHigh[1], true); 78 EXPECT_EQ(pinIsInputHigh[2], true); 79 EXPECT_EQ(pinIsInputHigh[3], true); 80 EXPECT_EQ(pinIsInputHigh[4], false); 81 EXPECT_EQ(pinIsInputHigh[5], false); 82 EXPECT_EQ(pinIsInputHigh[6], false); 83 EXPECT_EQ(pinIsInputHigh[7], false); 84 EXPECT_EQ(updates_array_idx, 0); // no updates received 85} 86 87TEST_F(EncoderSplitTestLeftEqRight, TestInitRight) { 88 isMaster = true; 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(updates_array_idx, 0); // no updates received 100} 101 102TEST_F(EncoderSplitTestLeftEqRight, TestOneClockwiseLeftMaster) { 103 isMaster = true; 104 isLeftHand = true; 105 encoder_init(); 106 // send 4 pulses. with resolution 4, that's one step and we should get 1 update. 107 setAndRead(0, false); 108 setAndRead(1, false); 109 setAndRead(0, true); 110 setAndRead(1, true); 111 112 EXPECT_EQ(updates_array_idx, 1); // one update received 113 EXPECT_EQ(updates[0].index, 0); 114 EXPECT_EQ(updates[0].clockwise, true); 115 116 int events_queued = 0; 117 encoder_events_t events; 118 encoder_retrieve_events(&events); 119 while (events.tail != events.head) { 120 events.tail = (events.tail + 1) % MAX_QUEUED_ENCODER_EVENTS; 121 ++events_queued; 122 } 123 EXPECT_EQ(events_queued, 0); // No events should be queued on master 124} 125 126TEST_F(EncoderSplitTestLeftEqRight, TestOneClockwiseRightMaster) { 127 isMaster = true; 128 isLeftHand = false; 129 encoder_init(); 130 // send 4 pulses. with resolution 4, that's one step and we should get 1 update. 131 setAndRead(6, false); 132 setAndRead(7, false); 133 setAndRead(6, true); 134 setAndRead(7, true); 135 136 EXPECT_EQ(updates_array_idx, 1); // one update received 137 EXPECT_EQ(updates[0].index, 3); 138 EXPECT_EQ(updates[0].clockwise, true); 139 140 int events_queued = 0; 141 encoder_events_t events; 142 encoder_retrieve_events(&events); 143 while (events.tail != events.head) { 144 events.tail = (events.tail + 1) % MAX_QUEUED_ENCODER_EVENTS; 145 ++events_queued; 146 } 147 EXPECT_EQ(events_queued, 0); // No events should be queued on master 148} 149 150TEST_F(EncoderSplitTestLeftEqRight, TestOneClockwiseLeftSlave) { 151 isMaster = false; 152 isLeftHand = true; 153 encoder_init(); 154 // send 4 pulses. with resolution 4, that's one step and we should get 1 update. 155 setAndRead(0, false); 156 setAndRead(1, false); 157 setAndRead(0, true); 158 setAndRead(1, true); 159 160 EXPECT_EQ(updates_array_idx, 0); // no updates received 161 162 int events_queued = 0; 163 encoder_events_t events; 164 encoder_retrieve_events(&events); 165 while (events.tail != events.head) { 166 events.tail = (events.tail + 1) % MAX_QUEUED_ENCODER_EVENTS; 167 ++events_queued; 168 } 169 EXPECT_EQ(events_queued, 1); // One event should be queued on slave 170} 171 172TEST_F(EncoderSplitTestLeftEqRight, TestOneClockwiseRightSlave) { 173 isMaster = false; 174 isLeftHand = false; 175 encoder_init(); 176 // send 4 pulses. with resolution 4, that's one step and we should get 1 update. 177 setAndRead(6, false); 178 setAndRead(7, false); 179 setAndRead(6, true); 180 setAndRead(7, true); 181 182 EXPECT_EQ(updates_array_idx, 0); // no updates received 183 184 int events_queued = 0; 185 encoder_events_t events; 186 encoder_retrieve_events(&events); 187 while (events.tail != events.head) { 188 events.tail = (events.tail + 1) % MAX_QUEUED_ENCODER_EVENTS; 189 ++events_queued; 190 } 191 EXPECT_EQ(events_queued, 1); // One event should be queued on slave 192}