Add encoder abstraction. (#21548)
This commit is contained in:
parent
2eb9ff8efd
commit
9d9cdaaa2d
50 changed files with 863 additions and 653 deletions
6
quantum/encoder/tests/config_encoder_common.h
Normal file
6
quantum/encoder/tests/config_encoder_common.h
Normal file
|
@ -0,0 +1,6 @@
|
|||
// Copyright 2023 Nick Brassel (@tzarc)
|
||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||
#pragma once
|
||||
|
||||
// Override the one in quantum/util because it doesn't like working on x64 builds.
|
||||
#define ARRAY_SIZE(array) (sizeof((array)) / sizeof((array)[0]))
|
|
@ -1,6 +1,7 @@
|
|||
// Copyright 2022 Nick Brassel (@tzarc)
|
||||
// Copyright 2022-2023 Nick Brassel (@tzarc)
|
||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||
#pragma once
|
||||
#include "config_encoder_common.h"
|
||||
|
||||
#define MATRIX_ROWS 1
|
||||
#define MATRIX_COLS 1
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
// Copyright 2022 Nick Brassel (@tzarc)
|
||||
// Copyright 2022-2023 Nick Brassel (@tzarc)
|
||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||
#pragma once
|
||||
#include "config_encoder_common.h"
|
||||
|
||||
#define MATRIX_ROWS 1
|
||||
#define MATRIX_COLS 1
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
// Copyright 2022 Nick Brassel (@tzarc)
|
||||
// Copyright 2022-2023 Nick Brassel (@tzarc)
|
||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||
#pragma once
|
||||
#include "config_encoder_common.h"
|
||||
|
||||
#define MATRIX_ROWS 1
|
||||
#define MATRIX_COLS 1
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
// Copyright 2022 Nick Brassel (@tzarc)
|
||||
// Copyright 2022-2023 Nick Brassel (@tzarc)
|
||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||
#pragma once
|
||||
#include "config_encoder_common.h"
|
||||
|
||||
#define MATRIX_ROWS 1
|
||||
#define MATRIX_COLS 1
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
// Copyright 2022 Nick Brassel (@tzarc)
|
||||
// Copyright 2022-2023 Nick Brassel (@tzarc)
|
||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||
#pragma once
|
||||
#include "config_encoder_common.h"
|
||||
|
||||
#define MATRIX_ROWS 1
|
||||
#define MATRIX_COLS 1
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
// Copyright 2022 Nick Brassel (@tzarc)
|
||||
// Copyright 2022-2023 Nick Brassel (@tzarc)
|
||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||
#pragma once
|
||||
#include "config_encoder_common.h"
|
||||
|
||||
#define MATRIX_ROWS 1
|
||||
#define MATRIX_COLS 1
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
// Copyright 2022 Nick Brassel (@tzarc)
|
||||
// Copyright 2022-2023 Nick Brassel (@tzarc)
|
||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||
#pragma once
|
||||
#include "config_encoder_common.h"
|
||||
|
||||
#define MATRIX_ROWS 1
|
||||
#define MATRIX_COLS 1
|
||||
|
|
|
@ -41,7 +41,7 @@ bool encoder_update_kb(uint8_t index, bool clockwise) {
|
|||
|
||||
bool setAndRead(pin_t pin, bool val) {
|
||||
setPin(pin, val);
|
||||
return encoder_read();
|
||||
return encoder_task();
|
||||
}
|
||||
|
||||
class EncoderTest : public ::testing::Test {};
|
||||
|
|
|
@ -33,22 +33,29 @@ struct update {
|
|||
uint8_t updates_array_idx = 0;
|
||||
update updates[32];
|
||||
|
||||
bool isMaster;
|
||||
bool isLeftHand;
|
||||
|
||||
extern "C" {
|
||||
bool is_keyboard_master(void) {
|
||||
return isMaster;
|
||||
}
|
||||
|
||||
bool encoder_update_kb(uint8_t index, bool clockwise) {
|
||||
if (!isLeftHand) {
|
||||
if (!is_keyboard_master()) {
|
||||
// this method has no effect on slave half
|
||||
printf("ignoring update on right hand (%d,%s)\n", index, clockwise ? "CW" : "CC");
|
||||
printf("ignoring update on slave (%d,%s)\n", index, clockwise ? "CW" : "CC");
|
||||
return true;
|
||||
}
|
||||
updates[updates_array_idx % 32] = {index, clockwise};
|
||||
updates_array_idx++;
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
bool setAndRead(pin_t pin, bool val) {
|
||||
setPin(pin, val);
|
||||
return encoder_read();
|
||||
return encoder_task();
|
||||
}
|
||||
|
||||
class EncoderSplitTestLeftEqRight : public ::testing::Test {
|
||||
|
@ -63,6 +70,7 @@ class EncoderSplitTestLeftEqRight : public ::testing::Test {
|
|||
};
|
||||
|
||||
TEST_F(EncoderSplitTestLeftEqRight, TestInitLeft) {
|
||||
isMaster = true;
|
||||
isLeftHand = true;
|
||||
encoder_init();
|
||||
EXPECT_EQ(pinIsInputHigh[0], true);
|
||||
|
@ -77,6 +85,7 @@ TEST_F(EncoderSplitTestLeftEqRight, TestInitLeft) {
|
|||
}
|
||||
|
||||
TEST_F(EncoderSplitTestLeftEqRight, TestInitRight) {
|
||||
isMaster = true;
|
||||
isLeftHand = false;
|
||||
encoder_init();
|
||||
EXPECT_EQ(pinIsInputHigh[0], false);
|
||||
|
@ -90,7 +99,8 @@ TEST_F(EncoderSplitTestLeftEqRight, TestInitRight) {
|
|||
EXPECT_EQ(updates_array_idx, 0); // no updates received
|
||||
}
|
||||
|
||||
TEST_F(EncoderSplitTestLeftEqRight, TestOneClockwiseLeft) {
|
||||
TEST_F(EncoderSplitTestLeftEqRight, TestOneClockwiseLeftMaster) {
|
||||
isMaster = true;
|
||||
isLeftHand = true;
|
||||
encoder_init();
|
||||
// send 4 pulses. with resolution 4, that's one step and we should get 1 update.
|
||||
|
@ -102,9 +112,19 @@ TEST_F(EncoderSplitTestLeftEqRight, TestOneClockwiseLeft) {
|
|||
EXPECT_EQ(updates_array_idx, 1); // one update received
|
||||
EXPECT_EQ(updates[0].index, 0);
|
||||
EXPECT_EQ(updates[0].clockwise, true);
|
||||
|
||||
int events_queued = 0;
|
||||
encoder_events_t events;
|
||||
encoder_retrieve_events(&events);
|
||||
while (events.tail != events.head) {
|
||||
events.tail = (events.tail + 1) % MAX_QUEUED_ENCODER_EVENTS;
|
||||
++events_queued;
|
||||
}
|
||||
EXPECT_EQ(events_queued, 0); // No events should be queued on master
|
||||
}
|
||||
|
||||
TEST_F(EncoderSplitTestLeftEqRight, TestOneClockwiseRightSent) {
|
||||
TEST_F(EncoderSplitTestLeftEqRight, TestOneClockwiseRightMaster) {
|
||||
isMaster = true;
|
||||
isLeftHand = false;
|
||||
encoder_init();
|
||||
// send 4 pulses. with resolution 4, that's one step and we should get 1 update.
|
||||
|
@ -113,23 +133,60 @@ TEST_F(EncoderSplitTestLeftEqRight, TestOneClockwiseRightSent) {
|
|||
setAndRead(6, true);
|
||||
setAndRead(7, true);
|
||||
|
||||
uint8_t slave_state[32] = {0};
|
||||
encoder_state_raw(slave_state);
|
||||
EXPECT_EQ(updates_array_idx, 1); // one update received
|
||||
EXPECT_EQ(updates[0].index, 3);
|
||||
EXPECT_EQ(updates[0].clockwise, true);
|
||||
|
||||
EXPECT_EQ(slave_state[0], 0);
|
||||
EXPECT_EQ(slave_state[1], 0xFF);
|
||||
int events_queued = 0;
|
||||
encoder_events_t events;
|
||||
encoder_retrieve_events(&events);
|
||||
while (events.tail != events.head) {
|
||||
events.tail = (events.tail + 1) % MAX_QUEUED_ENCODER_EVENTS;
|
||||
++events_queued;
|
||||
}
|
||||
EXPECT_EQ(events_queued, 0); // No events should be queued on master
|
||||
}
|
||||
|
||||
TEST_F(EncoderSplitTestLeftEqRight, TestMultipleEncodersRightReceived) {
|
||||
TEST_F(EncoderSplitTestLeftEqRight, TestOneClockwiseLeftSlave) {
|
||||
isMaster = false;
|
||||
isLeftHand = true;
|
||||
encoder_init();
|
||||
// send 4 pulses. with resolution 4, that's one step and we should get 1 update.
|
||||
setAndRead(0, false);
|
||||
setAndRead(1, false);
|
||||
setAndRead(0, true);
|
||||
setAndRead(1, true);
|
||||
|
||||
uint8_t slave_state[32] = {1, 0xFF}; // First right encoder is CCW, Second right encoder CW
|
||||
encoder_update_raw(slave_state);
|
||||
EXPECT_EQ(updates_array_idx, 0); // no updates received
|
||||
|
||||
EXPECT_EQ(updates_array_idx, 2); // two updates received, one for each changed item on the right side
|
||||
EXPECT_EQ(updates[0].index, 2);
|
||||
EXPECT_EQ(updates[0].clockwise, false);
|
||||
EXPECT_EQ(updates[1].index, 3);
|
||||
EXPECT_EQ(updates[1].clockwise, true);
|
||||
int events_queued = 0;
|
||||
encoder_events_t events;
|
||||
encoder_retrieve_events(&events);
|
||||
while (events.tail != events.head) {
|
||||
events.tail = (events.tail + 1) % MAX_QUEUED_ENCODER_EVENTS;
|
||||
++events_queued;
|
||||
}
|
||||
EXPECT_EQ(events_queued, 1); // One event should be queued on slave
|
||||
}
|
||||
|
||||
TEST_F(EncoderSplitTestLeftEqRight, TestOneClockwiseRightSlave) {
|
||||
isMaster = false;
|
||||
isLeftHand = false;
|
||||
encoder_init();
|
||||
// send 4 pulses. with resolution 4, that's one step and we should get 1 update.
|
||||
setAndRead(6, false);
|
||||
setAndRead(7, false);
|
||||
setAndRead(6, true);
|
||||
setAndRead(7, true);
|
||||
|
||||
EXPECT_EQ(updates_array_idx, 0); // no updates received
|
||||
|
||||
int events_queued = 0;
|
||||
encoder_events_t events;
|
||||
encoder_retrieve_events(&events);
|
||||
while (events.tail != events.head) {
|
||||
events.tail = (events.tail + 1) % MAX_QUEUED_ENCODER_EVENTS;
|
||||
++events_queued;
|
||||
}
|
||||
EXPECT_EQ(events_queued, 1); // One event should be queued on slave
|
||||
}
|
||||
|
|
|
@ -33,22 +33,29 @@ struct update {
|
|||
uint8_t updates_array_idx = 0;
|
||||
update updates[32];
|
||||
|
||||
bool isMaster;
|
||||
bool isLeftHand;
|
||||
|
||||
extern "C" {
|
||||
bool is_keyboard_master(void) {
|
||||
return isMaster;
|
||||
}
|
||||
|
||||
bool encoder_update_kb(uint8_t index, bool clockwise) {
|
||||
if (!isLeftHand) {
|
||||
if (!is_keyboard_master()) {
|
||||
// this method has no effect on slave half
|
||||
printf("ignoring update on right hand (%d,%s)\n", index, clockwise ? "CW" : "CC");
|
||||
printf("ignoring update on slave (%d,%s)\n", index, clockwise ? "CW" : "CC");
|
||||
return true;
|
||||
}
|
||||
updates[updates_array_idx % 32] = {index, clockwise};
|
||||
updates_array_idx++;
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
bool setAndRead(pin_t pin, bool val) {
|
||||
setPin(pin, val);
|
||||
return encoder_read();
|
||||
return encoder_task();
|
||||
}
|
||||
|
||||
class EncoderSplitTestLeftGreaterThanRight : public ::testing::Test {
|
||||
|
@ -94,7 +101,8 @@ TEST_F(EncoderSplitTestLeftGreaterThanRight, TestInitRight) {
|
|||
EXPECT_EQ(updates_array_idx, 0); // no updates received
|
||||
}
|
||||
|
||||
TEST_F(EncoderSplitTestLeftGreaterThanRight, TestOneClockwiseLeft) {
|
||||
TEST_F(EncoderSplitTestLeftGreaterThanRight, TestOneClockwiseLeftMaster) {
|
||||
isMaster = true;
|
||||
isLeftHand = true;
|
||||
encoder_init();
|
||||
// send 4 pulses. with resolution 4, that's one step and we should get 1 update.
|
||||
|
@ -106,9 +114,19 @@ TEST_F(EncoderSplitTestLeftGreaterThanRight, TestOneClockwiseLeft) {
|
|||
EXPECT_EQ(updates_array_idx, 1); // one update received
|
||||
EXPECT_EQ(updates[0].index, 0);
|
||||
EXPECT_EQ(updates[0].clockwise, true);
|
||||
|
||||
int events_queued = 0;
|
||||
encoder_events_t events;
|
||||
encoder_retrieve_events(&events);
|
||||
while (events.tail != events.head) {
|
||||
events.tail = (events.tail + 1) % MAX_QUEUED_ENCODER_EVENTS;
|
||||
++events_queued;
|
||||
}
|
||||
EXPECT_EQ(events_queued, 0); // No events should be queued on master
|
||||
}
|
||||
|
||||
TEST_F(EncoderSplitTestLeftGreaterThanRight, TestOneClockwiseRightSent) {
|
||||
TEST_F(EncoderSplitTestLeftGreaterThanRight, TestOneClockwiseRightMaster) {
|
||||
isMaster = true;
|
||||
isLeftHand = false;
|
||||
encoder_init();
|
||||
// send 4 pulses. with resolution 4, that's one step and we should get 1 update.
|
||||
|
@ -117,23 +135,60 @@ TEST_F(EncoderSplitTestLeftGreaterThanRight, TestOneClockwiseRightSent) {
|
|||
setAndRead(6, true);
|
||||
setAndRead(7, true);
|
||||
|
||||
uint8_t slave_state[32] = {0};
|
||||
encoder_state_raw(slave_state);
|
||||
EXPECT_EQ(updates_array_idx, 1); // one update received
|
||||
EXPECT_EQ(updates[0].index, 3);
|
||||
EXPECT_EQ(updates[0].clockwise, true);
|
||||
|
||||
EXPECT_EQ(slave_state[0], 0xFF);
|
||||
EXPECT_EQ(slave_state[1], 0);
|
||||
int events_queued = 0;
|
||||
encoder_events_t events;
|
||||
encoder_retrieve_events(&events);
|
||||
while (events.tail != events.head) {
|
||||
events.tail = (events.tail + 1) % MAX_QUEUED_ENCODER_EVENTS;
|
||||
++events_queued;
|
||||
}
|
||||
EXPECT_EQ(events_queued, 0); // No events should be queued on master
|
||||
}
|
||||
|
||||
TEST_F(EncoderSplitTestLeftGreaterThanRight, TestMultipleEncodersRightReceived) {
|
||||
TEST_F(EncoderSplitTestLeftGreaterThanRight, TestOneClockwiseLeftSlave) {
|
||||
isMaster = false;
|
||||
isLeftHand = true;
|
||||
encoder_init();
|
||||
// send 4 pulses. with resolution 4, that's one step and we should get 1 update.
|
||||
setAndRead(0, false);
|
||||
setAndRead(1, false);
|
||||
setAndRead(0, true);
|
||||
setAndRead(1, true);
|
||||
|
||||
uint8_t slave_state[32] = {1, 0xFF}; // First right encoder is CCW, Second right encoder no change, third right encoder CW
|
||||
encoder_update_raw(slave_state);
|
||||
EXPECT_EQ(updates_array_idx, 0); // no updates received
|
||||
|
||||
EXPECT_EQ(updates_array_idx, 2); // two updates received, one for each changed item on the right side
|
||||
EXPECT_EQ(updates[0].index, 3);
|
||||
EXPECT_EQ(updates[0].clockwise, false);
|
||||
EXPECT_EQ(updates[1].index, 4);
|
||||
EXPECT_EQ(updates[1].clockwise, true);
|
||||
int events_queued = 0;
|
||||
encoder_events_t events;
|
||||
encoder_retrieve_events(&events);
|
||||
while (events.tail != events.head) {
|
||||
events.tail = (events.tail + 1) % MAX_QUEUED_ENCODER_EVENTS;
|
||||
++events_queued;
|
||||
}
|
||||
EXPECT_EQ(events_queued, 1); // One event should be queued on slave
|
||||
}
|
||||
|
||||
TEST_F(EncoderSplitTestLeftGreaterThanRight, TestOneClockwiseRightSlave) {
|
||||
isMaster = false;
|
||||
isLeftHand = false;
|
||||
encoder_init();
|
||||
// send 4 pulses. with resolution 4, that's one step and we should get 1 update.
|
||||
setAndRead(6, false);
|
||||
setAndRead(7, false);
|
||||
setAndRead(6, true);
|
||||
setAndRead(7, true);
|
||||
|
||||
EXPECT_EQ(updates_array_idx, 0); // no updates received
|
||||
|
||||
int events_queued = 0;
|
||||
encoder_events_t events;
|
||||
encoder_retrieve_events(&events);
|
||||
while (events.tail != events.head) {
|
||||
events.tail = (events.tail + 1) % MAX_QUEUED_ENCODER_EVENTS;
|
||||
++events_queued;
|
||||
}
|
||||
EXPECT_EQ(events_queued, 1); // One event should be queued on slave
|
||||
}
|
||||
|
|
|
@ -33,22 +33,29 @@ struct update {
|
|||
uint8_t updates_array_idx = 0;
|
||||
update updates[32];
|
||||
|
||||
bool isMaster;
|
||||
bool isLeftHand;
|
||||
|
||||
extern "C" {
|
||||
bool is_keyboard_master(void) {
|
||||
return isMaster;
|
||||
}
|
||||
|
||||
bool encoder_update_kb(uint8_t index, bool clockwise) {
|
||||
if (!isLeftHand) {
|
||||
if (!is_keyboard_master()) {
|
||||
// this method has no effect on slave half
|
||||
printf("ignoring update on right hand (%d,%s)\n", index, clockwise ? "CW" : "CC");
|
||||
printf("ignoring update on slave (%d,%s)\n", index, clockwise ? "CW" : "CC");
|
||||
return true;
|
||||
}
|
||||
updates[updates_array_idx % 32] = {index, clockwise};
|
||||
updates_array_idx++;
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
bool setAndRead(pin_t pin, bool val) {
|
||||
setPin(pin, val);
|
||||
return encoder_read();
|
||||
return encoder_task();
|
||||
}
|
||||
|
||||
class EncoderSplitTestLeftLessThanRight : public ::testing::Test {
|
||||
|
@ -94,7 +101,8 @@ TEST_F(EncoderSplitTestLeftLessThanRight, TestInitRight) {
|
|||
EXPECT_EQ(updates_array_idx, 0); // no updates received
|
||||
}
|
||||
|
||||
TEST_F(EncoderSplitTestLeftLessThanRight, TestOneClockwiseLeft) {
|
||||
TEST_F(EncoderSplitTestLeftLessThanRight, TestOneClockwiseLeftMaster) {
|
||||
isMaster = true;
|
||||
isLeftHand = true;
|
||||
encoder_init();
|
||||
// send 4 pulses. with resolution 4, that's one step and we should get 1 update.
|
||||
|
@ -106,9 +114,19 @@ TEST_F(EncoderSplitTestLeftLessThanRight, TestOneClockwiseLeft) {
|
|||
EXPECT_EQ(updates_array_idx, 1); // one update received
|
||||
EXPECT_EQ(updates[0].index, 0);
|
||||
EXPECT_EQ(updates[0].clockwise, true);
|
||||
|
||||
int events_queued = 0;
|
||||
encoder_events_t events;
|
||||
encoder_retrieve_events(&events);
|
||||
while (events.tail != events.head) {
|
||||
events.tail = (events.tail + 1) % MAX_QUEUED_ENCODER_EVENTS;
|
||||
++events_queued;
|
||||
}
|
||||
EXPECT_EQ(events_queued, 0); // No events should be queued on master
|
||||
}
|
||||
|
||||
TEST_F(EncoderSplitTestLeftLessThanRight, TestOneClockwiseRightSent) {
|
||||
TEST_F(EncoderSplitTestLeftLessThanRight, TestOneClockwiseRightMaster) {
|
||||
isMaster = true;
|
||||
isLeftHand = false;
|
||||
encoder_init();
|
||||
// send 4 pulses. with resolution 4, that's one step and we should get 1 update.
|
||||
|
@ -117,23 +135,60 @@ TEST_F(EncoderSplitTestLeftLessThanRight, TestOneClockwiseRightSent) {
|
|||
setAndRead(6, true);
|
||||
setAndRead(7, true);
|
||||
|
||||
uint8_t slave_state[32] = {0};
|
||||
encoder_state_raw(slave_state);
|
||||
EXPECT_EQ(updates_array_idx, 1); // one update received
|
||||
EXPECT_EQ(updates[0].index, 3);
|
||||
EXPECT_EQ(updates[0].clockwise, true);
|
||||
|
||||
EXPECT_EQ(slave_state[0], 0);
|
||||
EXPECT_EQ(slave_state[1], 0xFF);
|
||||
int events_queued = 0;
|
||||
encoder_events_t events;
|
||||
encoder_retrieve_events(&events);
|
||||
while (events.tail != events.head) {
|
||||
events.tail = (events.tail + 1) % MAX_QUEUED_ENCODER_EVENTS;
|
||||
++events_queued;
|
||||
}
|
||||
EXPECT_EQ(events_queued, 0); // No events should be queued on master
|
||||
}
|
||||
|
||||
TEST_F(EncoderSplitTestLeftLessThanRight, TestMultipleEncodersRightReceived) {
|
||||
TEST_F(EncoderSplitTestLeftLessThanRight, TestOneClockwiseLeftSlave) {
|
||||
isMaster = false;
|
||||
isLeftHand = true;
|
||||
encoder_init();
|
||||
// send 4 pulses. with resolution 4, that's one step and we should get 1 update.
|
||||
setAndRead(0, false);
|
||||
setAndRead(1, false);
|
||||
setAndRead(0, true);
|
||||
setAndRead(1, true);
|
||||
|
||||
uint8_t slave_state[32] = {1, 0, 0xFF}; // First right encoder is CCW, Second right encoder no change, third right encoder CW
|
||||
encoder_update_raw(slave_state);
|
||||
EXPECT_EQ(updates_array_idx, 0); // no updates received
|
||||
|
||||
EXPECT_EQ(updates_array_idx, 2); // two updates received, one for each changed item on the right side
|
||||
EXPECT_EQ(updates[0].index, 2);
|
||||
EXPECT_EQ(updates[0].clockwise, false);
|
||||
EXPECT_EQ(updates[1].index, 4);
|
||||
EXPECT_EQ(updates[1].clockwise, true);
|
||||
int events_queued = 0;
|
||||
encoder_events_t events;
|
||||
encoder_retrieve_events(&events);
|
||||
while (events.tail != events.head) {
|
||||
events.tail = (events.tail + 1) % MAX_QUEUED_ENCODER_EVENTS;
|
||||
++events_queued;
|
||||
}
|
||||
EXPECT_EQ(events_queued, 1); // One event should be queued on slave
|
||||
}
|
||||
|
||||
TEST_F(EncoderSplitTestLeftLessThanRight, TestOneClockwiseRightSlave) {
|
||||
isMaster = false;
|
||||
isLeftHand = false;
|
||||
encoder_init();
|
||||
// send 4 pulses. with resolution 4, that's one step and we should get 1 update.
|
||||
setAndRead(6, false);
|
||||
setAndRead(7, false);
|
||||
setAndRead(6, true);
|
||||
setAndRead(7, true);
|
||||
|
||||
EXPECT_EQ(updates_array_idx, 0); // no updates received
|
||||
|
||||
int events_queued = 0;
|
||||
encoder_events_t events;
|
||||
encoder_retrieve_events(&events);
|
||||
while (events.tail != events.head) {
|
||||
events.tail = (events.tail + 1) % MAX_QUEUED_ENCODER_EVENTS;
|
||||
++events_queued;
|
||||
}
|
||||
EXPECT_EQ(events_queued, 1); // One event should be queued on slave
|
||||
}
|
||||
|
|
|
@ -33,22 +33,29 @@ struct update {
|
|||
uint8_t updates_array_idx = 0;
|
||||
update updates[32];
|
||||
|
||||
bool isMaster;
|
||||
bool isLeftHand;
|
||||
|
||||
extern "C" {
|
||||
bool is_keyboard_master(void) {
|
||||
return isMaster;
|
||||
}
|
||||
|
||||
bool encoder_update_kb(uint8_t index, bool clockwise) {
|
||||
if (!isLeftHand) {
|
||||
if (!is_keyboard_master()) {
|
||||
// this method has no effect on slave half
|
||||
printf("ignoring update on right hand (%d,%s)\n", index, clockwise ? "CW" : "CC");
|
||||
printf("ignoring update on slave (%d,%s)\n", index, clockwise ? "CW" : "CC");
|
||||
return true;
|
||||
}
|
||||
updates[updates_array_idx % 32] = {index, clockwise};
|
||||
updates_array_idx++;
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
bool setAndRead(pin_t pin, bool val) {
|
||||
setPin(pin, val);
|
||||
return encoder_read();
|
||||
return encoder_task();
|
||||
}
|
||||
|
||||
class EncoderSplitTestNoLeft : public ::testing::Test {
|
||||
|
@ -82,19 +89,8 @@ TEST_F(EncoderSplitTestNoLeft, TestInitRight) {
|
|||
EXPECT_EQ(updates_array_idx, 0); // no updates received
|
||||
}
|
||||
|
||||
TEST_F(EncoderSplitTestNoLeft, TestOneClockwiseLeft) {
|
||||
isLeftHand = true;
|
||||
encoder_init();
|
||||
// send 4 pulses. with resolution 4, that's one step and we should get 1 update.
|
||||
setAndRead(0, false);
|
||||
setAndRead(1, false);
|
||||
setAndRead(0, true);
|
||||
setAndRead(1, true);
|
||||
|
||||
EXPECT_EQ(updates_array_idx, 0); // no updates received
|
||||
}
|
||||
|
||||
TEST_F(EncoderSplitTestNoLeft, TestOneClockwiseRightSent) {
|
||||
TEST_F(EncoderSplitTestNoLeft, TestOneClockwiseLeftMaster) {
|
||||
isMaster = true;
|
||||
isLeftHand = false;
|
||||
encoder_init();
|
||||
// send 4 pulses. with resolution 4, that's one step and we should get 1 update.
|
||||
|
@ -103,23 +99,38 @@ TEST_F(EncoderSplitTestNoLeft, TestOneClockwiseRightSent) {
|
|||
setAndRead(2, true);
|
||||
setAndRead(3, true);
|
||||
|
||||
uint8_t slave_state[32] = {0};
|
||||
encoder_state_raw(slave_state);
|
||||
EXPECT_EQ(updates_array_idx, 1); // one update received
|
||||
EXPECT_EQ(updates[0].index, 1);
|
||||
EXPECT_EQ(updates[0].clockwise, true);
|
||||
|
||||
EXPECT_EQ(slave_state[0], 0);
|
||||
EXPECT_EQ(slave_state[1], 0xFF);
|
||||
int events_queued = 0;
|
||||
encoder_events_t events;
|
||||
encoder_retrieve_events(&events);
|
||||
while (events.tail != events.head) {
|
||||
events.tail = (events.tail + 1) % MAX_QUEUED_ENCODER_EVENTS;
|
||||
++events_queued;
|
||||
}
|
||||
EXPECT_EQ(events_queued, 0); // No events should be queued on master
|
||||
}
|
||||
|
||||
TEST_F(EncoderSplitTestNoLeft, TestMultipleEncodersRightReceived) {
|
||||
isLeftHand = true;
|
||||
TEST_F(EncoderSplitTestNoLeft, TestOneClockwiseRightSlave) {
|
||||
isMaster = false;
|
||||
isLeftHand = false;
|
||||
encoder_init();
|
||||
// send 4 pulses. with resolution 4, that's one step and we should get 1 update.
|
||||
setAndRead(2, false);
|
||||
setAndRead(3, false);
|
||||
setAndRead(2, true);
|
||||
setAndRead(3, true);
|
||||
|
||||
uint8_t slave_state[32] = {1, 0xFF}; // First right encoder is CCW, Second right encoder no change, third right encoder CW
|
||||
encoder_update_raw(slave_state);
|
||||
EXPECT_EQ(updates_array_idx, 0); // no updates received
|
||||
|
||||
EXPECT_EQ(updates_array_idx, 2); // two updates received, one for each changed item on the right side
|
||||
EXPECT_EQ(updates[0].index, 0);
|
||||
EXPECT_EQ(updates[0].clockwise, false);
|
||||
EXPECT_EQ(updates[1].index, 1);
|
||||
EXPECT_EQ(updates[1].clockwise, true);
|
||||
int events_queued = 0;
|
||||
encoder_events_t events;
|
||||
encoder_retrieve_events(&events);
|
||||
while (events.tail != events.head) {
|
||||
events.tail = (events.tail + 1) % MAX_QUEUED_ENCODER_EVENTS;
|
||||
++events_queued;
|
||||
}
|
||||
EXPECT_EQ(events_queued, 1); // One event should be queued on slave
|
||||
}
|
||||
|
|
|
@ -33,22 +33,29 @@ struct update {
|
|||
uint8_t updates_array_idx = 0;
|
||||
update updates[32];
|
||||
|
||||
bool isMaster;
|
||||
bool isLeftHand;
|
||||
|
||||
extern "C" {
|
||||
bool is_keyboard_master(void) {
|
||||
return isMaster;
|
||||
}
|
||||
|
||||
bool encoder_update_kb(uint8_t index, bool clockwise) {
|
||||
if (!isLeftHand) {
|
||||
if (!is_keyboard_master()) {
|
||||
// this method has no effect on slave half
|
||||
printf("ignoring update on right hand (%d,%s)\n", index, clockwise ? "CW" : "CC");
|
||||
printf("ignoring update on slave (%d,%s)\n", index, clockwise ? "CW" : "CC");
|
||||
return true;
|
||||
}
|
||||
updates[updates_array_idx % 32] = {index, clockwise};
|
||||
updates_array_idx++;
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
bool setAndRead(pin_t pin, bool val) {
|
||||
setPin(pin, val);
|
||||
return encoder_read();
|
||||
return encoder_task();
|
||||
}
|
||||
|
||||
class EncoderSplitTestNoRight : public ::testing::Test {
|
||||
|
@ -82,37 +89,48 @@ TEST_F(EncoderSplitTestNoRight, TestInitRight) {
|
|||
EXPECT_EQ(updates_array_idx, 0); // no updates received
|
||||
}
|
||||
|
||||
TEST_F(EncoderSplitTestNoRight, TestOneClockwiseLeft) {
|
||||
TEST_F(EncoderSplitTestNoRight, TestOneClockwiseLeftMaster) {
|
||||
isMaster = true;
|
||||
isLeftHand = true;
|
||||
encoder_init();
|
||||
// send 4 pulses. with resolution 4, that's one step and we should get 1 update.
|
||||
setAndRead(0, false);
|
||||
setAndRead(1, false);
|
||||
setAndRead(0, true);
|
||||
setAndRead(1, true);
|
||||
setAndRead(2, false);
|
||||
setAndRead(3, false);
|
||||
setAndRead(2, true);
|
||||
setAndRead(3, true);
|
||||
|
||||
EXPECT_EQ(updates_array_idx, 1); // one updates received
|
||||
EXPECT_EQ(updates[0].index, 0);
|
||||
EXPECT_EQ(updates_array_idx, 1); // one update received
|
||||
EXPECT_EQ(updates[0].index, 1);
|
||||
EXPECT_EQ(updates[0].clockwise, true);
|
||||
|
||||
int events_queued = 0;
|
||||
encoder_events_t events;
|
||||
encoder_retrieve_events(&events);
|
||||
while (events.tail != events.head) {
|
||||
events.tail = (events.tail + 1) % MAX_QUEUED_ENCODER_EVENTS;
|
||||
++events_queued;
|
||||
}
|
||||
EXPECT_EQ(events_queued, 0); // No events should be queued on master
|
||||
}
|
||||
|
||||
TEST_F(EncoderSplitTestNoRight, TestOneClockwiseRightSent) {
|
||||
isLeftHand = false;
|
||||
encoder_init();
|
||||
|
||||
uint8_t slave_state[32] = {0xAA, 0xAA};
|
||||
encoder_state_raw(slave_state);
|
||||
|
||||
EXPECT_EQ(slave_state[0], 0xAA);
|
||||
EXPECT_EQ(slave_state[1], 0xAA);
|
||||
}
|
||||
|
||||
TEST_F(EncoderSplitTestNoRight, TestMultipleEncodersRightReceived) {
|
||||
TEST_F(EncoderSplitTestNoRight, TestOneClockwiseRightSlave) {
|
||||
isMaster = false;
|
||||
isLeftHand = true;
|
||||
encoder_init();
|
||||
// send 4 pulses. with resolution 4, that's one step and we should get 1 update.
|
||||
setAndRead(2, false);
|
||||
setAndRead(3, false);
|
||||
setAndRead(2, true);
|
||||
setAndRead(3, true);
|
||||
|
||||
uint8_t slave_state[32] = {1, 0xFF}; // These values would trigger updates if there were encoders on the other side
|
||||
encoder_update_raw(slave_state);
|
||||
EXPECT_EQ(updates_array_idx, 0); // no updates received
|
||||
|
||||
EXPECT_EQ(updates_array_idx, 0); // no updates received -- no right-hand encoders
|
||||
int events_queued = 0;
|
||||
encoder_events_t events;
|
||||
encoder_retrieve_events(&events);
|
||||
while (events.tail != events.head) {
|
||||
events.tail = (events.tail + 1) % MAX_QUEUED_ENCODER_EVENTS;
|
||||
++events_queued;
|
||||
}
|
||||
EXPECT_EQ(events_queued, 1); // One event should be queued on slave
|
||||
}
|
||||
|
|
|
@ -50,7 +50,7 @@ bool encoder_update_kb(uint8_t index, bool clockwise) {
|
|||
|
||||
bool setAndRead(pin_t pin, bool val) {
|
||||
setPin(pin, val);
|
||||
return encoder_read();
|
||||
return encoder_task();
|
||||
}
|
||||
|
||||
class EncoderSplitTestRole : public ::testing::Test {
|
||||
|
@ -87,9 +87,6 @@ TEST_F(EncoderSplitTestRole, TestPrimaryRight) {
|
|||
setAndRead(6, true);
|
||||
setAndRead(7, true);
|
||||
|
||||
uint8_t slave_state[32] = {0};
|
||||
encoder_state_raw(slave_state);
|
||||
|
||||
EXPECT_EQ(num_updates, 1); // one update received
|
||||
}
|
||||
|
||||
|
@ -116,8 +113,5 @@ TEST_F(EncoderSplitTestRole, TestNotPrimaryRight) {
|
|||
setAndRead(6, true);
|
||||
setAndRead(7, true);
|
||||
|
||||
uint8_t slave_state[32] = {0};
|
||||
encoder_state_raw(slave_state);
|
||||
|
||||
EXPECT_EQ(num_updates, 0); // zero updates received
|
||||
}
|
||||
|
|
|
@ -36,7 +36,3 @@ bool setPin(pin_t pin, bool val) {
|
|||
}
|
||||
|
||||
void last_encoder_activity_trigger(void) {}
|
||||
|
||||
__attribute__((weak)) bool is_keyboard_master(void) {
|
||||
return true;
|
||||
}
|
||||
|
|
|
@ -22,9 +22,6 @@
|
|||
#define SPLIT_KEYBOARD
|
||||
typedef uint8_t pin_t;
|
||||
|
||||
void encoder_state_raw(uint8_t* slave_state);
|
||||
void encoder_update_raw(uint8_t* slave_state);
|
||||
|
||||
extern bool pins[];
|
||||
extern bool pinIsInputHigh[];
|
||||
|
||||
|
|
|
@ -3,6 +3,7 @@ encoder_CONFIG := $(QUANTUM_PATH)/encoder/tests/config_mock.h
|
|||
|
||||
encoder_SRC := \
|
||||
platforms/test/timer.c \
|
||||
drivers/encoder/encoder_quadrature.c \
|
||||
$(QUANTUM_PATH)/encoder/tests/mock.c \
|
||||
$(QUANTUM_PATH)/encoder/tests/encoder_tests.cpp \
|
||||
$(QUANTUM_PATH)/encoder.c
|
||||
|
@ -13,6 +14,7 @@ encoder_split_left_eq_right_CONFIG := $(QUANTUM_PATH)/encoder/tests/config_mock_
|
|||
|
||||
encoder_split_left_eq_right_SRC := \
|
||||
platforms/test/timer.c \
|
||||
drivers/encoder/encoder_quadrature.c \
|
||||
$(QUANTUM_PATH)/encoder/tests/mock_split.c \
|
||||
$(QUANTUM_PATH)/encoder/tests/encoder_tests_split_left_eq_right.cpp \
|
||||
$(QUANTUM_PATH)/encoder.c
|
||||
|
@ -23,6 +25,7 @@ encoder_split_left_gt_right_CONFIG := $(QUANTUM_PATH)/encoder/tests/config_mock_
|
|||
|
||||
encoder_split_left_gt_right_SRC := \
|
||||
platforms/test/timer.c \
|
||||
drivers/encoder/encoder_quadrature.c \
|
||||
$(QUANTUM_PATH)/encoder/tests/mock_split.c \
|
||||
$(QUANTUM_PATH)/encoder/tests/encoder_tests_split_left_gt_right.cpp \
|
||||
$(QUANTUM_PATH)/encoder.c
|
||||
|
@ -33,6 +36,7 @@ encoder_split_left_lt_right_CONFIG := $(QUANTUM_PATH)/encoder/tests/config_mock_
|
|||
|
||||
encoder_split_left_lt_right_SRC := \
|
||||
platforms/test/timer.c \
|
||||
drivers/encoder/encoder_quadrature.c \
|
||||
$(QUANTUM_PATH)/encoder/tests/mock_split.c \
|
||||
$(QUANTUM_PATH)/encoder/tests/encoder_tests_split_left_lt_right.cpp \
|
||||
$(QUANTUM_PATH)/encoder.c
|
||||
|
@ -43,6 +47,7 @@ encoder_split_no_left_CONFIG := $(QUANTUM_PATH)/encoder/tests/config_mock_split_
|
|||
|
||||
encoder_split_no_left_SRC := \
|
||||
platforms/test/timer.c \
|
||||
drivers/encoder/encoder_quadrature.c \
|
||||
$(QUANTUM_PATH)/encoder/tests/mock_split.c \
|
||||
$(QUANTUM_PATH)/encoder/tests/encoder_tests_split_no_left.cpp \
|
||||
$(QUANTUM_PATH)/encoder.c
|
||||
|
@ -53,6 +58,7 @@ encoder_split_no_right_CONFIG := $(QUANTUM_PATH)/encoder/tests/config_mock_split
|
|||
|
||||
encoder_split_no_right_SRC := \
|
||||
platforms/test/timer.c \
|
||||
drivers/encoder/encoder_quadrature.c \
|
||||
$(QUANTUM_PATH)/encoder/tests/mock_split.c \
|
||||
$(QUANTUM_PATH)/encoder/tests/encoder_tests_split_no_right.cpp \
|
||||
$(QUANTUM_PATH)/encoder.c
|
||||
|
@ -63,6 +69,7 @@ encoder_split_role_CONFIG := $(QUANTUM_PATH)/encoder/tests/config_mock_split_rol
|
|||
|
||||
encoder_split_role_SRC := \
|
||||
platforms/test/timer.c \
|
||||
drivers/encoder/encoder_quadrature.c \
|
||||
$(QUANTUM_PATH)/encoder/tests/mock_split.c \
|
||||
$(QUANTUM_PATH)/encoder/tests/encoder_tests_split_role.cpp \
|
||||
$(QUANTUM_PATH)/encoder.c
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue