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.

192 lines
5.7 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 EncoderSplitTestLeftEqRight : 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(EncoderSplitTestLeftEqRight, TestInitLeft) {
  63. isMaster = true;
  64. isLeftHand = true;
  65. encoder_init();
  66. EXPECT_EQ(pinIsInputHigh[0], true);
  67. EXPECT_EQ(pinIsInputHigh[1], true);
  68. EXPECT_EQ(pinIsInputHigh[2], true);
  69. EXPECT_EQ(pinIsInputHigh[3], true);
  70. EXPECT_EQ(pinIsInputHigh[4], false);
  71. EXPECT_EQ(pinIsInputHigh[5], false);
  72. EXPECT_EQ(pinIsInputHigh[6], false);
  73. EXPECT_EQ(pinIsInputHigh[7], false);
  74. EXPECT_EQ(updates_array_idx, 0); // no updates received
  75. }
  76. TEST_F(EncoderSplitTestLeftEqRight, TestInitRight) {
  77. isMaster = true;
  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], true);
  85. EXPECT_EQ(pinIsInputHigh[5], true);
  86. EXPECT_EQ(pinIsInputHigh[6], true);
  87. EXPECT_EQ(pinIsInputHigh[7], true);
  88. EXPECT_EQ(updates_array_idx, 0); // no updates received
  89. }
  90. TEST_F(EncoderSplitTestLeftEqRight, TestOneClockwiseLeftMaster) {
  91. isMaster = true;
  92. isLeftHand = true;
  93. encoder_init();
  94. // send 4 pulses. with resolution 4, that's one step and we should get 1 update.
  95. setAndRead(0, false);
  96. setAndRead(1, false);
  97. setAndRead(0, true);
  98. setAndRead(1, true);
  99. EXPECT_EQ(updates_array_idx, 1); // one update received
  100. EXPECT_EQ(updates[0].index, 0);
  101. EXPECT_EQ(updates[0].clockwise, true);
  102. int events_queued = 0;
  103. encoder_events_t events;
  104. encoder_retrieve_events(&events);
  105. while (events.tail != events.head) {
  106. events.tail = (events.tail + 1) % MAX_QUEUED_ENCODER_EVENTS;
  107. ++events_queued;
  108. }
  109. EXPECT_EQ(events_queued, 0); // No events should be queued on master
  110. }
  111. TEST_F(EncoderSplitTestLeftEqRight, TestOneClockwiseRightMaster) {
  112. isMaster = true;
  113. isLeftHand = false;
  114. encoder_init();
  115. // send 4 pulses. with resolution 4, that's one step and we should get 1 update.
  116. setAndRead(6, false);
  117. setAndRead(7, false);
  118. setAndRead(6, true);
  119. setAndRead(7, true);
  120. EXPECT_EQ(updates_array_idx, 1); // one update received
  121. EXPECT_EQ(updates[0].index, 3);
  122. EXPECT_EQ(updates[0].clockwise, true);
  123. int events_queued = 0;
  124. encoder_events_t events;
  125. encoder_retrieve_events(&events);
  126. while (events.tail != events.head) {
  127. events.tail = (events.tail + 1) % MAX_QUEUED_ENCODER_EVENTS;
  128. ++events_queued;
  129. }
  130. EXPECT_EQ(events_queued, 0); // No events should be queued on master
  131. }
  132. TEST_F(EncoderSplitTestLeftEqRight, TestOneClockwiseLeftSlave) {
  133. isMaster = false;
  134. isLeftHand = true;
  135. encoder_init();
  136. // send 4 pulses. with resolution 4, that's one step and we should get 1 update.
  137. setAndRead(0, false);
  138. setAndRead(1, false);
  139. setAndRead(0, true);
  140. setAndRead(1, true);
  141. EXPECT_EQ(updates_array_idx, 0); // no updates received
  142. int events_queued = 0;
  143. encoder_events_t events;
  144. encoder_retrieve_events(&events);
  145. while (events.tail != events.head) {
  146. events.tail = (events.tail + 1) % MAX_QUEUED_ENCODER_EVENTS;
  147. ++events_queued;
  148. }
  149. EXPECT_EQ(events_queued, 1); // One event should be queued on slave
  150. }
  151. TEST_F(EncoderSplitTestLeftEqRight, TestOneClockwiseRightSlave) {
  152. isMaster = false;
  153. isLeftHand = false;
  154. encoder_init();
  155. // send 4 pulses. with resolution 4, that's one step and we should get 1 update.
  156. setAndRead(6, false);
  157. setAndRead(7, false);
  158. setAndRead(6, true);
  159. setAndRead(7, true);
  160. EXPECT_EQ(updates_array_idx, 0); // no updates received
  161. int events_queued = 0;
  162. encoder_events_t events;
  163. encoder_retrieve_events(&events);
  164. while (events.tail != events.head) {
  165. events.tail = (events.tail + 1) % MAX_QUEUED_ENCODER_EVENTS;
  166. ++events_queued;
  167. }
  168. EXPECT_EQ(events_queued, 1); // One event should be queued on slave
  169. }