keyboard stuff
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 EncoderSplitTestNoRight : 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(EncoderSplitTestNoRight, 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(updates_array_idx, 0); // no updates received
80}
81
82TEST_F(EncoderSplitTestNoRight, TestInitRight) {
83 isLeftHand = false;
84 encoder_init();
85 EXPECT_EQ(pinIsInputHigh[0], false);
86 EXPECT_EQ(pinIsInputHigh[1], false);
87 EXPECT_EQ(pinIsInputHigh[2], false);
88 EXPECT_EQ(pinIsInputHigh[3], false);
89 EXPECT_EQ(updates_array_idx, 0); // no updates received
90}
91
92TEST_F(EncoderSplitTestNoRight, TestOneClockwiseLeftMaster) {
93 isMaster = true;
94 isLeftHand = true;
95 encoder_init();
96 // send 4 pulses. with resolution 4, that's one step and we should get 1 update.
97 setAndRead(2, false);
98 setAndRead(3, false);
99 setAndRead(2, true);
100 setAndRead(3, true);
101
102 EXPECT_EQ(updates_array_idx, 1); // one update received
103 EXPECT_EQ(updates[0].index, 1);
104 EXPECT_EQ(updates[0].clockwise, true);
105
106 int events_queued = 0;
107 encoder_events_t events;
108 encoder_retrieve_events(&events);
109 while (events.tail != events.head) {
110 events.tail = (events.tail + 1) % MAX_QUEUED_ENCODER_EVENTS;
111 ++events_queued;
112 }
113 EXPECT_EQ(events_queued, 0); // No events should be queued on master
114}
115
116TEST_F(EncoderSplitTestNoRight, TestOneClockwiseRightSlave) {
117 isMaster = false;
118 isLeftHand = true;
119 encoder_init();
120 // send 4 pulses. with resolution 4, that's one step and we should get 1 update.
121 setAndRead(2, false);
122 setAndRead(3, false);
123 setAndRead(2, true);
124 setAndRead(3, true);
125
126 EXPECT_EQ(updates_array_idx, 0); // no updates received
127
128 int events_queued = 0;
129 encoder_events_t events;
130 encoder_retrieve_events(&events);
131 while (events.tail != events.head) {
132 events.tail = (events.tail + 1) % MAX_QUEUED_ENCODER_EVENTS;
133 ++events_queued;
134 }
135 EXPECT_EQ(events_queued, 1); // One event should be queued on slave
136}