You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

144 lines
3.8 KiB

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