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.

194 lines
5.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_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 isMaster;
  32. bool isLeftHand;
  33. extern "C" {
  34. bool is_keyboard_master(void) {
  35. return isMaster;
  36. }
  37. bool encoder_update_kb(uint8_t index, bool clockwise) {
  38. if (!is_keyboard_master()) {
  39. // this method has no effect on slave half
  40. printf("ignoring update on slave (%d,%s)\n", index, clockwise ? "CW" : "CC");
  41. return true;
  42. }
  43. updates[updates_array_idx % 32] = {index, clockwise};
  44. updates_array_idx++;
  45. return true;
  46. }
  47. };
  48. bool setAndRead(pin_t pin, bool val) {
  49. setPin(pin, val);
  50. return encoder_task();
  51. }
  52. class EncoderSplitTestLeftGreaterThanRight : public ::testing::Test {
  53. protected:
  54. void SetUp() override {
  55. updates_array_idx = 0;
  56. for (int i = 0; i < 32; i++) {
  57. pinIsInputHigh[i] = 0;
  58. pins[i] = 0;
  59. }
  60. }
  61. };
  62. TEST_F(EncoderSplitTestLeftGreaterThanRight, TestInitLeft) {
  63. isLeftHand = true;
  64. encoder_init();
  65. EXPECT_EQ(pinIsInputHigh[0], true);
  66. EXPECT_EQ(pinIsInputHigh[1], true);
  67. EXPECT_EQ(pinIsInputHigh[2], true);
  68. EXPECT_EQ(pinIsInputHigh[3], true);
  69. EXPECT_EQ(pinIsInputHigh[4], true);
  70. EXPECT_EQ(pinIsInputHigh[5], true);
  71. EXPECT_EQ(pinIsInputHigh[6], false);
  72. EXPECT_EQ(pinIsInputHigh[7], false);
  73. EXPECT_EQ(pinIsInputHigh[8], false);
  74. EXPECT_EQ(pinIsInputHigh[9], false);
  75. EXPECT_EQ(updates_array_idx, 0); // no updates received
  76. }
  77. TEST_F(EncoderSplitTestLeftGreaterThanRight, TestInitRight) {
  78. isLeftHand = false;
  79. encoder_init();
  80. EXPECT_EQ(pinIsInputHigh[0], false);
  81. EXPECT_EQ(pinIsInputHigh[1], false);
  82. EXPECT_EQ(pinIsInputHigh[2], false);
  83. EXPECT_EQ(pinIsInputHigh[3], false);
  84. EXPECT_EQ(pinIsInputHigh[4], false);
  85. EXPECT_EQ(pinIsInputHigh[5], false);
  86. EXPECT_EQ(pinIsInputHigh[6], true);
  87. EXPECT_EQ(pinIsInputHigh[7], true);
  88. EXPECT_EQ(pinIsInputHigh[8], true);
  89. EXPECT_EQ(pinIsInputHigh[9], true);
  90. EXPECT_EQ(updates_array_idx, 0); // no updates received
  91. }
  92. TEST_F(EncoderSplitTestLeftGreaterThanRight, 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(0, false);
  98. setAndRead(1, false);
  99. setAndRead(0, true);
  100. setAndRead(1, true);
  101. EXPECT_EQ(updates_array_idx, 1); // one update received
  102. EXPECT_EQ(updates[0].index, 0);
  103. EXPECT_EQ(updates[0].clockwise, true);
  104. int events_queued = 0;
  105. encoder_events_t events;
  106. encoder_retrieve_events(&events);
  107. while (events.tail != events.head) {
  108. events.tail = (events.tail + 1) % MAX_QUEUED_ENCODER_EVENTS;
  109. ++events_queued;
  110. }
  111. EXPECT_EQ(events_queued, 0); // No events should be queued on master
  112. }
  113. TEST_F(EncoderSplitTestLeftGreaterThanRight, TestOneClockwiseRightMaster) {
  114. isMaster = true;
  115. isLeftHand = false;
  116. encoder_init();
  117. // send 4 pulses. with resolution 4, that's one step and we should get 1 update.
  118. setAndRead(6, false);
  119. setAndRead(7, false);
  120. setAndRead(6, true);
  121. setAndRead(7, true);
  122. EXPECT_EQ(updates_array_idx, 1); // one update received
  123. EXPECT_EQ(updates[0].index, 3);
  124. EXPECT_EQ(updates[0].clockwise, true);
  125. int events_queued = 0;
  126. encoder_events_t events;
  127. encoder_retrieve_events(&events);
  128. while (events.tail != events.head) {
  129. events.tail = (events.tail + 1) % MAX_QUEUED_ENCODER_EVENTS;
  130. ++events_queued;
  131. }
  132. EXPECT_EQ(events_queued, 0); // No events should be queued on master
  133. }
  134. TEST_F(EncoderSplitTestLeftGreaterThanRight, TestOneClockwiseLeftSlave) {
  135. isMaster = false;
  136. isLeftHand = true;
  137. encoder_init();
  138. // send 4 pulses. with resolution 4, that's one step and we should get 1 update.
  139. setAndRead(0, false);
  140. setAndRead(1, false);
  141. setAndRead(0, true);
  142. setAndRead(1, true);
  143. EXPECT_EQ(updates_array_idx, 0); // no updates received
  144. int events_queued = 0;
  145. encoder_events_t events;
  146. encoder_retrieve_events(&events);
  147. while (events.tail != events.head) {
  148. events.tail = (events.tail + 1) % MAX_QUEUED_ENCODER_EVENTS;
  149. ++events_queued;
  150. }
  151. EXPECT_EQ(events_queued, 1); // One event should be queued on slave
  152. }
  153. TEST_F(EncoderSplitTestLeftGreaterThanRight, TestOneClockwiseRightSlave) {
  154. isMaster = false;
  155. isLeftHand = false;
  156. encoder_init();
  157. // send 4 pulses. with resolution 4, that's one step and we should get 1 update.
  158. setAndRead(6, false);
  159. setAndRead(7, false);
  160. setAndRead(6, true);
  161. setAndRead(7, true);
  162. EXPECT_EQ(updates_array_idx, 0); // no updates received
  163. int events_queued = 0;
  164. encoder_events_t events;
  165. encoder_retrieve_events(&events);
  166. while (events.tail != events.head) {
  167. events.tail = (events.tail + 1) % MAX_QUEUED_ENCODER_EVENTS;
  168. ++events_queued;
  169. }
  170. EXPECT_EQ(events_queued, 1); // One event should be queued on slave
  171. }