encoder_tests_split_left_lt_right.cpp 5.9 KB

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