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.

135 lines
4.0 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_split.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 isLeftHand;
  32. bool encoder_update_kb(uint8_t index, bool clockwise) {
  33. if (!isLeftHand) {
  34. // this method has no effect on slave half
  35. printf("ignoring update on right hand (%d,%s)\n", index, clockwise ? "CW" : "CC");
  36. return true;
  37. }
  38. updates[updates_array_idx % 32] = {index, clockwise};
  39. updates_array_idx++;
  40. return true;
  41. }
  42. bool setAndRead(pin_t pin, bool val) {
  43. setPin(pin, val);
  44. return encoder_read();
  45. }
  46. class EncoderSplitTestLeftEqRight : public ::testing::Test {
  47. protected:
  48. void SetUp() override {
  49. updates_array_idx = 0;
  50. for (int i = 0; i < 32; i++) {
  51. pinIsInputHigh[i] = 0;
  52. pins[i] = 0;
  53. }
  54. }
  55. };
  56. TEST_F(EncoderSplitTestLeftEqRight, TestInitLeft) {
  57. isLeftHand = true;
  58. encoder_init();
  59. EXPECT_EQ(pinIsInputHigh[0], true);
  60. EXPECT_EQ(pinIsInputHigh[1], true);
  61. EXPECT_EQ(pinIsInputHigh[2], true);
  62. EXPECT_EQ(pinIsInputHigh[3], true);
  63. EXPECT_EQ(pinIsInputHigh[4], false);
  64. EXPECT_EQ(pinIsInputHigh[5], false);
  65. EXPECT_EQ(pinIsInputHigh[6], false);
  66. EXPECT_EQ(pinIsInputHigh[7], false);
  67. EXPECT_EQ(updates_array_idx, 0); // no updates received
  68. }
  69. TEST_F(EncoderSplitTestLeftEqRight, TestInitRight) {
  70. isLeftHand = false;
  71. encoder_init();
  72. EXPECT_EQ(pinIsInputHigh[0], false);
  73. EXPECT_EQ(pinIsInputHigh[1], false);
  74. EXPECT_EQ(pinIsInputHigh[2], false);
  75. EXPECT_EQ(pinIsInputHigh[3], false);
  76. EXPECT_EQ(pinIsInputHigh[4], true);
  77. EXPECT_EQ(pinIsInputHigh[5], true);
  78. EXPECT_EQ(pinIsInputHigh[6], true);
  79. EXPECT_EQ(pinIsInputHigh[7], true);
  80. EXPECT_EQ(updates_array_idx, 0); // no updates received
  81. }
  82. TEST_F(EncoderSplitTestLeftEqRight, TestOneClockwiseLeft) {
  83. isLeftHand = true;
  84. encoder_init();
  85. // send 4 pulses. with resolution 4, that's one step and we should get 1 update.
  86. setAndRead(0, false);
  87. setAndRead(1, false);
  88. setAndRead(0, true);
  89. setAndRead(1, true);
  90. EXPECT_EQ(updates_array_idx, 1); // one update received
  91. EXPECT_EQ(updates[0].index, 0);
  92. EXPECT_EQ(updates[0].clockwise, true);
  93. }
  94. TEST_F(EncoderSplitTestLeftEqRight, TestOneClockwiseRightSent) {
  95. isLeftHand = false;
  96. encoder_init();
  97. // send 4 pulses. with resolution 4, that's one step and we should get 1 update.
  98. setAndRead(6, false);
  99. setAndRead(7, false);
  100. setAndRead(6, true);
  101. setAndRead(7, true);
  102. uint8_t slave_state[32] = {0};
  103. encoder_state_raw(slave_state);
  104. EXPECT_EQ(slave_state[0], 0);
  105. EXPECT_EQ(slave_state[1], 0xFF);
  106. }
  107. TEST_F(EncoderSplitTestLeftEqRight, TestMultipleEncodersRightReceived) {
  108. isLeftHand = true;
  109. encoder_init();
  110. uint8_t slave_state[32] = {1, 0xFF}; // First right encoder is CCW, Second right encoder CW
  111. encoder_update_raw(slave_state);
  112. EXPECT_EQ(updates_array_idx, 2); // two updates received, one for each changed item on the right side
  113. EXPECT_EQ(updates[0].index, 2);
  114. EXPECT_EQ(updates[0].clockwise, false);
  115. EXPECT_EQ(updates[1].index, 3);
  116. EXPECT_EQ(updates[1].clockwise, true);
  117. }