qmk_firmware

QMK firmware for my keyboards (Corne, Sweep Ferris) and trackball (Ploopy Adept)
Log | Files | Refs | Submodules | LICENSE

encoder_tests.cpp (3865B)


      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.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 encoder_update_kb(uint8_t index, bool clockwise) {
     37     updates[updates_array_idx % 32] = {index, clockwise};
     38     updates_array_idx++;
     39     return true;
     40 }
     41 
     42 bool setAndRead(pin_t pin, bool val) {
     43     setPin(pin, val);
     44     return encoder_task();
     45 }
     46 
     47 class EncoderTest : public ::testing::Test {};
     48 
     49 TEST_F(EncoderTest, TestInit) {
     50     updates_array_idx = 0;
     51     encoder_init();
     52     EXPECT_EQ(pinIsInputHigh[0], true);
     53     EXPECT_EQ(pinIsInputHigh[1], true);
     54     EXPECT_EQ(updates_array_idx, 0);
     55 }
     56 
     57 TEST_F(EncoderTest, TestOneClockwise) {
     58     updates_array_idx = 0;
     59     encoder_init();
     60     // send 4 pulses. with resolution 4, that's one step and we should get 1 update.
     61     setAndRead(0, false);
     62     setAndRead(1, false);
     63     setAndRead(0, true);
     64     setAndRead(1, true);
     65 
     66     EXPECT_EQ(updates_array_idx, 1);
     67     EXPECT_EQ(updates[0].index, 0);
     68     EXPECT_EQ(updates[0].clockwise, true);
     69 }
     70 
     71 TEST_F(EncoderTest, TestOneCounterClockwise) {
     72     updates_array_idx = 0;
     73     encoder_init();
     74     setAndRead(1, false);
     75     setAndRead(0, false);
     76     setAndRead(1, true);
     77     setAndRead(0, true);
     78 
     79     EXPECT_EQ(updates_array_idx, 1);
     80     EXPECT_EQ(updates[0].index, 0);
     81     EXPECT_EQ(updates[0].clockwise, false);
     82 }
     83 
     84 TEST_F(EncoderTest, TestTwoClockwiseOneCC) {
     85     updates_array_idx = 0;
     86     encoder_init();
     87     setAndRead(0, false);
     88     setAndRead(1, false);
     89     setAndRead(0, true);
     90     setAndRead(1, true);
     91     setAndRead(0, false);
     92     setAndRead(1, false);
     93     setAndRead(0, true);
     94     setAndRead(1, true);
     95     setAndRead(1, false);
     96     setAndRead(0, false);
     97     setAndRead(1, true);
     98     setAndRead(0, true);
     99 
    100     EXPECT_EQ(updates_array_idx, 3);
    101     EXPECT_EQ(updates[0].index, 0);
    102     EXPECT_EQ(updates[0].clockwise, true);
    103     EXPECT_EQ(updates[1].index, 0);
    104     EXPECT_EQ(updates[1].clockwise, true);
    105     EXPECT_EQ(updates[2].index, 0);
    106     EXPECT_EQ(updates[2].clockwise, false);
    107 }
    108 
    109 TEST_F(EncoderTest, TestNoEarly) {
    110     updates_array_idx = 0;
    111     encoder_init();
    112     // send 3 pulses. with resolution 4, that's not enough for a step.
    113     setAndRead(0, false);
    114     setAndRead(1, false);
    115     setAndRead(0, true);
    116     EXPECT_EQ(updates_array_idx, 0);
    117     // now send last pulse
    118     setAndRead(1, true);
    119     EXPECT_EQ(updates_array_idx, 1);
    120     EXPECT_EQ(updates[0].index, 0);
    121     EXPECT_EQ(updates[0].clockwise, true);
    122 }
    123 
    124 TEST_F(EncoderTest, TestHalfway) {
    125     updates_array_idx = 0;
    126     encoder_init();
    127     // go halfway
    128     setAndRead(0, false);
    129     setAndRead(1, false);
    130     EXPECT_EQ(updates_array_idx, 0);
    131     // back off
    132     setAndRead(1, true);
    133     setAndRead(0, true);
    134     EXPECT_EQ(updates_array_idx, 0);
    135     // go all the way
    136     setAndRead(0, false);
    137     setAndRead(1, false);
    138     setAndRead(0, true);
    139     setAndRead(1, true);
    140     // should result in 1 update
    141     EXPECT_EQ(updates_array_idx, 1);
    142     EXPECT_EQ(updates[0].index, 0);
    143     EXPECT_EQ(updates[0].clockwise, true);
    144 }