encoder_tests_split_left_eq_right.cpp 5.7 KB

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