logo

qmk_firmware

custom branch of QMK firmware git clone https://anongit.hacktivis.me/git/qmk_firmware.git

debounce_test_common.cpp (8988B)


  1. /* Copyright 2021 Simon Arlott
  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 "debounce_test_common.h"
  18. #include <algorithm>
  19. #include <iomanip>
  20. #include <sstream>
  21. extern "C" {
  22. #include "debounce.h"
  23. #include "timer.h"
  24. void simulate_async_tick(uint32_t t);
  25. void reset_access_counter(void);
  26. uint32_t current_access_counter(void);
  27. uint32_t timer_read_internal(void);
  28. void set_time(uint32_t t);
  29. void advance_time(uint32_t ms);
  30. }
  31. void DebounceTest::addEvents(std::initializer_list<DebounceTestEvent> events) {
  32. events_.insert(events_.end(), events.begin(), events.end());
  33. }
  34. void DebounceTest::runEvents() {
  35. /* Run the test multiple times, from 1kHz to 10kHz scan rate */
  36. for (extra_iterations_ = 0; extra_iterations_ < 10; extra_iterations_++) {
  37. if (time_jumps_) {
  38. /* Don't advance time smoothly, jump to the next event (some tests require this) */
  39. auto_advance_time_ = false;
  40. runEventsInternal();
  41. } else {
  42. /* Run the test with both smooth and irregular time; it must produce the same result */
  43. auto_advance_time_ = true;
  44. runEventsInternal();
  45. auto_advance_time_ = false;
  46. runEventsInternal();
  47. }
  48. }
  49. }
  50. void DebounceTest::runEventsInternal() {
  51. fast_timer_t previous = 0;
  52. bool first = true;
  53. /* Initialise keyboard with start time (offset to avoid testing at 0) and all keys UP */
  54. debounce_init(MATRIX_ROWS);
  55. set_time(time_offset_);
  56. simulate_async_tick(async_time_jumps_);
  57. std::fill(std::begin(input_matrix_), std::end(input_matrix_), 0);
  58. std::fill(std::begin(output_matrix_), std::end(output_matrix_), 0);
  59. for (auto &event : events_) {
  60. if (!auto_advance_time_) {
  61. /* Jump to the next event */
  62. set_time(time_offset_ + event.time_);
  63. } else if (!first && event.time_ == previous + 1) {
  64. /* This event immediately follows the previous one, don't make extra debounce() calls */
  65. advance_time(1);
  66. } else {
  67. /* Fast forward to the time for this event, calling debounce() with no changes */
  68. ASSERT_LT((time_offset_ + event.time_) - timer_read_internal(), 60000) << "Test tries to advance more than 1 minute of time";
  69. while (timer_read_internal() != time_offset_ + event.time_) {
  70. runDebounce(false);
  71. checkCookedMatrix(false, "debounce() modified cooked matrix");
  72. advance_time(1);
  73. }
  74. }
  75. first = false;
  76. previous = event.time_;
  77. /* Prepare input matrix */
  78. for (auto &input : event.inputs_) {
  79. matrixUpdate(input_matrix_, "input", input);
  80. }
  81. /* Call debounce */
  82. runDebounce(!event.inputs_.empty());
  83. /* Prepare output matrix */
  84. for (auto &output : event.outputs_) {
  85. matrixUpdate(output_matrix_, "output", output);
  86. }
  87. /* Check output matrix has expected change events */
  88. for (auto &output : event.outputs_) {
  89. EXPECT_EQ(!!(cooked_matrix_[output.row_] & (1U << output.col_)), directionValue(output.direction_)) << "Missing event at " << strTime() << " expected key " << output.row_ << "," << output.col_ << " " << directionLabel(output.direction_) << "\ninput_matrix: changed=" << !event.inputs_.empty() << "\n" << strMatrix(input_matrix_) << "\nexpected_matrix:\n" << strMatrix(output_matrix_) << "\nactual_matrix:\n" << strMatrix(cooked_matrix_);
  90. }
  91. /* Check output matrix has no other changes */
  92. checkCookedMatrix(!event.inputs_.empty(), "debounce() cooked matrix does not match expected output matrix");
  93. /* Perform some extra iterations of the matrix scan with no changes */
  94. for (int i = 0; i < extra_iterations_; i++) {
  95. runDebounce(false);
  96. checkCookedMatrix(false, "debounce() modified cooked matrix");
  97. }
  98. }
  99. /* Check that no further changes happen for 1 minute */
  100. for (int i = 0; i < 60000; i++) {
  101. runDebounce(false);
  102. checkCookedMatrix(false, "debounce() modified cooked matrix");
  103. advance_time(1);
  104. }
  105. debounce_free();
  106. }
  107. void DebounceTest::runDebounce(bool changed) {
  108. std::copy(std::begin(input_matrix_), std::end(input_matrix_), std::begin(raw_matrix_));
  109. std::copy(std::begin(output_matrix_), std::end(output_matrix_), std::begin(cooked_matrix_));
  110. reset_access_counter();
  111. bool cooked_changed = debounce(raw_matrix_, cooked_matrix_, MATRIX_ROWS, changed);
  112. if (!std::equal(std::begin(input_matrix_), std::end(input_matrix_), std::begin(raw_matrix_))) {
  113. FAIL() << "Fatal error: debounce() modified raw matrix at " << strTime() << "\ninput_matrix: changed=" << changed << "\n" << strMatrix(input_matrix_) << "\nraw_matrix:\n" << strMatrix(raw_matrix_);
  114. }
  115. if (std::equal(std::begin(output_matrix_), std::end(output_matrix_), std::begin(cooked_matrix_)) == cooked_changed) {
  116. FAIL() << "Fatal error: debounce() reported a wrong cooked matrix change result at " << strTime() << "\noutput_matrix: cooked_changed=" << cooked_changed << "\n" << strMatrix(output_matrix_) << "\ncooked_matrix:\n" << strMatrix(cooked_matrix_);
  117. }
  118. if (current_access_counter() > 1) {
  119. FAIL() << "Fatal error: debounce() read the timer multiple times, which is not allowed, at " << strTime() << "\ntimer: access_count=" << current_access_counter() << "\noutput_matrix: cooked_changed=" << cooked_changed << "\n" << strMatrix(output_matrix_) << "\ncooked_matrix:\n" << strMatrix(cooked_matrix_);
  120. }
  121. }
  122. void DebounceTest::checkCookedMatrix(bool changed, const std::string &error_message) {
  123. if (!std::equal(std::begin(output_matrix_), std::end(output_matrix_), std::begin(cooked_matrix_))) {
  124. FAIL() << "Unexpected event: " << error_message << " at " << strTime() << "\ninput_matrix: changed=" << changed << "\n" << strMatrix(input_matrix_) << "\nexpected_matrix:\n" << strMatrix(output_matrix_) << "\nactual_matrix:\n" << strMatrix(cooked_matrix_);
  125. }
  126. }
  127. std::string DebounceTest::strTime() {
  128. std::stringstream text;
  129. text << "time " << (timer_read_internal() - time_offset_) << " (extra_iterations=" << extra_iterations_ << ", auto_advance_time=" << auto_advance_time_ << ")";
  130. return text.str();
  131. }
  132. std::string DebounceTest::strMatrix(matrix_row_t matrix[]) {
  133. std::stringstream text;
  134. text << "\t" << std::setw(3) << "";
  135. for (int col = 0; col < MATRIX_COLS; col++) {
  136. text << " " << std::setw(2) << col;
  137. }
  138. text << "\n";
  139. for (int row = 0; row < MATRIX_ROWS; row++) {
  140. text << "\t" << std::setw(2) << row << ":";
  141. for (int col = 0; col < MATRIX_COLS; col++) {
  142. text << ((matrix[row] & (1U << col)) ? " XX" : " __");
  143. }
  144. text << "\n";
  145. }
  146. return text.str();
  147. }
  148. bool DebounceTest::directionValue(Direction direction) {
  149. switch (direction) {
  150. case DOWN:
  151. return true;
  152. case UP:
  153. return false;
  154. }
  155. }
  156. std::string DebounceTest::directionLabel(Direction direction) {
  157. switch (direction) {
  158. case DOWN:
  159. return "DOWN";
  160. case UP:
  161. return "UP";
  162. }
  163. }
  164. /* Modify a matrix and verify that events always specify a change */
  165. void DebounceTest::matrixUpdate(matrix_row_t matrix[], const std::string &name, const MatrixTestEvent &event) {
  166. ASSERT_NE(!!(matrix[event.row_] & (1U << event.col_)), directionValue(event.direction_)) << "Test " << name << " at " << strTime() << " sets key " << event.row_ << "," << event.col_ << " " << directionLabel(event.direction_) << " but it is already " << directionLabel(event.direction_) << "\n" << name << "_matrix:\n" << strMatrix(matrix);
  167. switch (event.direction_) {
  168. case DOWN:
  169. matrix[event.row_] |= (1U << event.col_);
  170. break;
  171. case UP:
  172. matrix[event.row_] &= ~(1U << event.col_);
  173. break;
  174. }
  175. }
  176. DebounceTestEvent::DebounceTestEvent(fast_timer_t time, std::initializer_list<MatrixTestEvent> inputs, std::initializer_list<MatrixTestEvent> outputs) : time_(time), inputs_(inputs), outputs_(outputs) {}
  177. MatrixTestEvent::MatrixTestEvent(int row, int col, Direction direction) : row_(row), col_(col), direction_(direction) {}