keyboard stuff
at master 317 lines 9.4 kB view raw
1/* Copyright 2021 Simon Arlott 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 19#include "debounce_test_common.h" 20 21TEST_F(DebounceTest, OneKeyShort1) { 22 addEvents({ 23 /* Time, Inputs, Outputs */ 24 {0, {{0, 1, DOWN}}, {{0, 1, DOWN}}}, 25 {1, {{0, 1, UP}}, {}}, 26 27 {5, {}, {{0, 1, UP}}}, 28 /* Press key again after 1ms delay (debounce has not yet finished) */ 29 {6, {{0, 1, DOWN}}, {}}, 30 {10, {}, {{0, 1, DOWN}}}, /* 5ms after UP at time 5 */ 31 }); 32 runEvents(); 33} 34 35TEST_F(DebounceTest, OneKeyShort2) { 36 addEvents({ 37 /* Time, Inputs, Outputs */ 38 {0, {{0, 1, DOWN}}, {{0, 1, DOWN}}}, 39 {1, {{0, 1, UP}}, {}}, 40 41 {5, {}, {{0, 1, UP}}}, 42 /* Press key again after 2ms delay (debounce has not yet finished) */ 43 {7, {{0, 1, DOWN}}, {}}, 44 {10, {}, {{0, 1, DOWN}}}, /* 5ms after UP at time 5 */ 45 }); 46 runEvents(); 47} 48 49TEST_F(DebounceTest, OneKeyShort3) { 50 addEvents({ 51 /* Time, Inputs, Outputs */ 52 {0, {{0, 1, DOWN}}, {{0, 1, DOWN}}}, 53 {1, {{0, 1, UP}}, {}}, 54 55 {5, {}, {{0, 1, UP}}}, 56 /* Press key again after 3ms delay (debounce has not yet finished) */ 57 {8, {{0, 1, DOWN}}, {}}, 58 {10, {}, {{0, 1, DOWN}}}, /* 5ms after UP at time 5 */ 59 }); 60 runEvents(); 61} 62 63TEST_F(DebounceTest, OneKeyShort4) { 64 addEvents({ 65 /* Time, Inputs, Outputs */ 66 {0, {{0, 1, DOWN}}, {{0, 1, DOWN}}}, 67 {1, {{0, 1, UP}}, {}}, 68 69 {5, {}, {{0, 1, UP}}}, 70 /* Press key again after 4ms delay (debounce has not yet finished) */ 71 {9, {{0, 1, DOWN}}, {}}, 72 {10, {}, {{0, 1, DOWN}}}, /* 5ms after UP at time 5 */ 73 }); 74 runEvents(); 75} 76 77TEST_F(DebounceTest, OneKeyShort5) { 78 addEvents({ 79 /* Time, Inputs, Outputs */ 80 {0, {{0, 1, DOWN}}, {{0, 1, DOWN}}}, 81 {1, {{0, 1, UP}}, {}}, 82 83 {5, {}, {{0, 1, UP}}}, 84 /* Press key again after 5ms delay (debounce has finished) */ 85 {10, {{0, 1, DOWN}}, {{0, 1, DOWN}}}, 86 }); 87 runEvents(); 88} 89 90TEST_F(DebounceTest, OneKeyShort6) { 91 addEvents({ 92 /* Time, Inputs, Outputs */ 93 {0, {{0, 1, DOWN}}, {{0, 1, DOWN}}}, 94 {1, {{0, 1, UP}}, {}}, 95 96 {5, {}, {{0, 1, UP}}}, 97 /* Press key after after 6ms delay (debounce has finished) */ 98 {11, {{0, 1, DOWN}}, {{0, 1, DOWN}}}, 99 }); 100 runEvents(); 101} 102 103TEST_F(DebounceTest, OneKeyBouncing1) { 104 addEvents({ 105 /* Time, Inputs, Outputs */ 106 {0, {{0, 1, DOWN}}, {{0, 1, DOWN}}}, 107 {1, {{0, 1, UP}}, {}}, 108 {2, {{0, 1, DOWN}}, {}}, 109 {3, {{0, 1, UP}}, {}}, 110 {4, {{0, 1, DOWN}}, {}}, 111 {5, {{0, 1, UP}}, {{0, 1, UP}}}, 112 /* Press key again after 1ms delay (debounce has not yet finished) */ 113 {6, {{0, 1, DOWN}}, {}}, 114 {10, {}, {{0, 1, DOWN}}}, /* 5ms after UP at time 5 */ 115 }); 116 runEvents(); 117} 118 119TEST_F(DebounceTest, OneKeyBouncing2) { 120 addEvents({ 121 /* Time, Inputs, Outputs */ 122 {0, {{0, 1, DOWN}}, {{0, 1, DOWN}}}, 123 /* Change twice in the same time period */ 124 {1, {{0, 1, UP}}, {}}, 125 {1, {{0, 1, DOWN}}, {}}, 126 /* Change three times in the same time period */ 127 {2, {{0, 1, UP}}, {}}, 128 {2, {{0, 1, DOWN}}, {}}, 129 {2, {{0, 1, UP}}, {}}, 130 /* Change three times in the same time period */ 131 {3, {{0, 1, DOWN}}, {}}, 132 {3, {{0, 1, UP}}, {}}, 133 {3, {{0, 1, DOWN}}, {}}, 134 /* Change twice in the same time period */ 135 {4, {{0, 1, UP}}, {}}, 136 {4, {{0, 1, DOWN}}, {}}, 137 {5, {{0, 1, UP}}, {{0, 1, UP}}}, 138 /* Press key again after 1ms delay (debounce has not yet finished) */ 139 {6, {{0, 1, DOWN}}, {}}, 140 {10, {}, {{0, 1, DOWN}}}, /* 5ms after UP at time 5 */ 141 }); 142 runEvents(); 143} 144 145TEST_F(DebounceTest, OneKeyLong) { 146 addEvents({ 147 /* Time, Inputs, Outputs */ 148 {0, {{0, 1, DOWN}}, {{0, 1, DOWN}}}, 149 150 {25, {{0, 1, UP}}, {{0, 1, UP}}}, 151 152 {50, {{0, 1, DOWN}}, {{0, 1, DOWN}}}, 153 }); 154 runEvents(); 155} 156 157TEST_F(DebounceTest, TwoRowsShort) { 158 addEvents({ 159 /* Time, Inputs, Outputs */ 160 {0, {{0, 1, DOWN}}, {{0, 1, DOWN}}}, 161 {1, {{0, 1, UP}}, {}}, 162 {2, {{2, 0, DOWN}}, {{2, 0, DOWN}}}, 163 {3, {{2, 0, UP}}, {}}, 164 165 {5, {}, {{0, 1, UP}}}, 166 /* Press key again after 1ms delay (debounce has not yet finished) */ 167 {6, {{0, 1, DOWN}}, {}}, 168 {7, {}, {{2, 0, UP}}}, 169 170 /* Press key again after 1ms delay (debounce has not yet finished) */ 171 {9, {{2, 0, DOWN}}, {}}, 172 {10, {}, {{0, 1, DOWN}}}, /* 5ms after UP at time 5 */ 173 174 {12, {}, {{2, 0, DOWN}}}, /* 5ms after UP at time 7 */ 175 }); 176 runEvents(); 177} 178 179TEST_F(DebounceTest, TwoKeysOverlap) { 180 addEvents({ 181 /* Time, Inputs, Outputs */ 182 {0, {{0, 1, DOWN}}, {{0, 1, DOWN}}}, 183 {1, {{0, 1, UP}}, {}}, 184 /* Press a second key during the first debounce */ 185 {2, {{0, 2, DOWN}}, {}}, 186 187 /* Key registers as soon as debounce finishes, 5ms after time 0 */ 188 {5, {}, {{0, 1, UP}, {0, 2, DOWN}}}, 189 {6, {{0, 1, DOWN}}, {}}, 190 191 /* Key registers as soon as debounce finishes, 5ms after time 5 */ 192 {10, {}, {{0, 1, DOWN}}}, 193 /* Release both keys */ 194 {11, {{0, 1, UP}}, {}}, 195 {12, {{0, 2, UP}}, {}}, 196 197 /* Keys register as soon as debounce finishes, 5ms after time 10 */ 198 {15, {}, {{0, 1, UP}, {0, 2, UP}}}, 199 }); 200 runEvents(); 201} 202 203TEST_F(DebounceTest, TwoKeysSimultaneous1) { 204 addEvents({ 205 /* Time, Inputs, Outputs */ 206 {0, {{0, 1, DOWN}, {0, 2, DOWN}}, {{0, 1, DOWN}, {0, 2, DOWN}}}, 207 {20, {{0, 1, UP}}, {{0, 1, UP}}}, 208 {21, {{0, 2, UP}}, {}}, 209 210 /* Key registers as soon as debounce finishes, 5ms after time 20 */ 211 {25, {}, {{0, 2, UP}}}, 212 }); 213 runEvents(); 214} 215 216TEST_F(DebounceTest, TwoKeysSimultaneous2) { 217 addEvents({ 218 /* Time, Inputs, Outputs */ 219 {0, {{0, 1, DOWN}, {0, 2, DOWN}}, {{0, 1, DOWN}, {0, 2, DOWN}}}, 220 {20, {{0, 1, UP}, {0, 2, UP}}, {{0, 1, UP}, {0, 2, UP}}}, 221 }); 222 runEvents(); 223} 224 225TEST_F(DebounceTest, OneKeyDelayedScan1) { 226 addEvents({ 227 /* Time, Inputs, Outputs */ 228 {0, {{0, 1, DOWN}}, {{0, 1, DOWN}}}, 229 230 /* Processing is very late but the change will now be accepted */ 231 {300, {{0, 1, UP}}, {{0, 1, UP}}}, 232 }); 233 time_jumps_ = true; 234 runEvents(); 235} 236 237TEST_F(DebounceTest, OneKeyDelayedScan2) { 238 addEvents({ 239 /* Time, Inputs, Outputs */ 240 {0, {{0, 1, DOWN}}, {{0, 1, DOWN}}}, 241 242 /* Processing is very late but the change will now be accepted even with a 1 scan delay */ 243 {300, {}, {}}, 244 {300, {{0, 1, UP}}, {{0, 1, UP}}}, 245 }); 246 time_jumps_ = true; 247 runEvents(); 248} 249 250TEST_F(DebounceTest, OneKeyDelayedScan3) { 251 addEvents({ 252 /* Time, Inputs, Outputs */ 253 {0, {{0, 1, DOWN}}, {{0, 1, DOWN}}}, 254 255 /* Processing is very late but the change will now be accepted even with a 1ms delay */ 256 {300, {}, {}}, 257 {301, {{0, 1, UP}}, {{0, 1, UP}}}, 258 }); 259 time_jumps_ = true; 260 runEvents(); 261} 262 263TEST_F(DebounceTest, OneKeyDelayedScan4) { 264 addEvents({ 265 /* Time, Inputs, Outputs */ 266 {0, {{0, 1, DOWN}}, {{0, 1, DOWN}}}, 267 268 /* Processing is a bit late but the change will now be accepted */ 269 {50, {{0, 1, UP}}, {{0, 1, UP}}}, 270 }); 271 time_jumps_ = true; 272 runEvents(); 273} 274 275TEST_F(DebounceTest, OneKeyDelayedScan5) { 276 addEvents({ 277 /* Time, Inputs, Outputs */ 278 {0, {{0, 1, DOWN}}, {{0, 1, DOWN}}}, 279 280 /* Processing is very late but the change will now be accepted even with a 1 scan delay */ 281 {50, {}, {}}, 282 {50, {{0, 1, UP}}, {{0, 1, UP}}}, 283 }); 284 time_jumps_ = true; 285 runEvents(); 286} 287 288TEST_F(DebounceTest, OneKeyDelayedScan6) { 289 addEvents({ 290 /* Time, Inputs, Outputs */ 291 {0, {{0, 1, DOWN}}, {{0, 1, DOWN}}}, 292 293 /* Processing is very late but the change will now be accepted even with a 1ms delay */ 294 {50, {}, {}}, 295 {51, {{0, 1, UP}}, {{0, 1, UP}}}, 296 }); 297 time_jumps_ = true; 298 runEvents(); 299} 300 301TEST_F(DebounceTest, AsyncTickOneKeyShort1) { 302 addEvents({ 303 /* Time, Inputs, Outputs */ 304 {0, {{0, 1, DOWN}}, {{0, 1, DOWN}}}, 305 {1, {{0, 1, UP}}, {}}, 306 307 {5, {}, {{0, 1, UP}}}, 308 /* Press key again after 1ms delay (debounce has not yet finished) */ 309 {6, {{0, 1, DOWN}}, {}}, 310 {10, {}, {{0, 1, DOWN}}}, /* 5ms after UP at time 5 */ 311 }); 312 /* 313 * Debounce implementations should never read the timer more than once per invocation 314 */ 315 async_time_jumps_ = DEBOUNCE; 316 runEvents(); 317}