encoder_tests_split_no_right.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. #include "gtest/gtest.h"
  17. #include "gmock/gmock.h"
  18. #include <vector>
  19. #include <algorithm>
  20. #include <stdio.h>
  21. extern "C" {
  22. #include "encoder.h"
  23. #include "encoder/tests/mock_split.h"
  24. }
  25. struct update {
  26. uint8_t index;
  27. bool clockwise;
  28. };
  29. uint8_t updates_array_idx = 0;
  30. update updates[32];
  31. bool isMaster;
  32. bool isLeftHand;
  33. extern "C" {
  34. bool is_keyboard_master(void) {
  35. return isMaster;
  36. }
  37. bool is_keyboard_left(void) {
  38. return isLeftHand;
  39. }
  40. bool encoder_update_kb(uint8_t index, bool clockwise) {
  41. if (!is_keyboard_master()) {
  42. // this method has no effect on slave half
  43. printf("ignoring update on slave (%d,%s)\n", index, clockwise ? "CW" : "CC");
  44. return true;
  45. }
  46. updates[updates_array_idx % 32] = {index, clockwise};
  47. updates_array_idx++;
  48. return true;
  49. }
  50. };
  51. bool setAndRead(pin_t pin, bool val) {
  52. setPin(pin, val);
  53. return encoder_task();
  54. }
  55. class EncoderSplitTestNoRight : public ::testing::Test {
  56. protected:
  57. void SetUp() override {
  58. updates_array_idx = 0;
  59. for (int i = 0; i < 32; i++) {
  60. pinIsInputHigh[i] = 0;
  61. pins[i] = 0;
  62. }
  63. }
  64. };
  65. TEST_F(EncoderSplitTestNoRight, TestInitLeft) {
  66. isLeftHand = true;
  67. encoder_init();
  68. EXPECT_EQ(pinIsInputHigh[0], true);
  69. EXPECT_EQ(pinIsInputHigh[1], true);
  70. EXPECT_EQ(pinIsInputHigh[2], true);
  71. EXPECT_EQ(pinIsInputHigh[3], true);
  72. EXPECT_EQ(updates_array_idx, 0); // no updates received
  73. }
  74. TEST_F(EncoderSplitTestNoRight, TestInitRight) {
  75. isLeftHand = false;
  76. encoder_init();
  77. EXPECT_EQ(pinIsInputHigh[0], false);
  78. EXPECT_EQ(pinIsInputHigh[1], false);
  79. EXPECT_EQ(pinIsInputHigh[2], false);
  80. EXPECT_EQ(pinIsInputHigh[3], false);
  81. EXPECT_EQ(updates_array_idx, 0); // no updates received
  82. }
  83. TEST_F(EncoderSplitTestNoRight, TestOneClockwiseLeftMaster) {
  84. isMaster = true;
  85. isLeftHand = true;
  86. encoder_init();
  87. // send 4 pulses. with resolution 4, that's one step and we should get 1 update.
  88. setAndRead(2, false);
  89. setAndRead(3, false);
  90. setAndRead(2, true);
  91. setAndRead(3, true);
  92. EXPECT_EQ(updates_array_idx, 1); // one update received
  93. EXPECT_EQ(updates[0].index, 1);
  94. EXPECT_EQ(updates[0].clockwise, true);
  95. int events_queued = 0;
  96. encoder_events_t events;
  97. encoder_retrieve_events(&events);
  98. while (events.tail != events.head) {
  99. events.tail = (events.tail + 1) % MAX_QUEUED_ENCODER_EVENTS;
  100. ++events_queued;
  101. }
  102. EXPECT_EQ(events_queued, 0); // No events should be queued on master
  103. }
  104. TEST_F(EncoderSplitTestNoRight, TestOneClockwiseRightSlave) {
  105. isMaster = false;
  106. isLeftHand = true;
  107. encoder_init();
  108. // send 4 pulses. with resolution 4, that's one step and we should get 1 update.
  109. setAndRead(2, false);
  110. setAndRead(3, false);
  111. setAndRead(2, true);
  112. setAndRead(3, true);
  113. EXPECT_EQ(updates_array_idx, 0); // no updates received
  114. int events_queued = 0;
  115. encoder_events_t events;
  116. encoder_retrieve_events(&events);
  117. while (events.tail != events.head) {
  118. events.tail = (events.tail + 1) % MAX_QUEUED_ENCODER_EVENTS;
  119. ++events_queued;
  120. }
  121. EXPECT_EQ(events_queued, 1); // One event should be queued on slave
  122. }