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.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

matrix.c 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*
  2. Copyright 2011 Jun Wako <[email protected]>
  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. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. /*
  15. * scan matrix
  16. */
  17. #include <stdint.h>
  18. #include <stdbool.h>
  19. #include <util/delay.h>
  20. #include "print.h"
  21. #include "debug.h"
  22. #include "util.h"
  23. #include "timer.h"
  24. #include "matrix.h"
  25. #include "hhkb_avr.h"
  26. // matrix state buffer(1:on, 0:off)
  27. static matrix_row_t *matrix;
  28. static matrix_row_t *matrix_prev;
  29. static matrix_row_t _matrix0[MATRIX_ROWS];
  30. static matrix_row_t _matrix1[MATRIX_ROWS];
  31. inline
  32. uint8_t matrix_rows(void)
  33. {
  34. return MATRIX_ROWS;
  35. }
  36. inline
  37. uint8_t matrix_cols(void)
  38. {
  39. return MATRIX_COLS;
  40. }
  41. void matrix_init(void)
  42. {
  43. #ifdef DEBUG
  44. debug_enable = true;
  45. debug_keyboard = true;
  46. #endif
  47. KEY_INIT();
  48. // initialize matrix state: all keys off
  49. for (uint8_t i=0; i < MATRIX_ROWS; i++) _matrix0[i] = 0x00;
  50. for (uint8_t i=0; i < MATRIX_ROWS; i++) _matrix1[i] = 0x00;
  51. matrix = _matrix0;
  52. matrix_prev = _matrix1;
  53. }
  54. uint8_t matrix_scan(void)
  55. {
  56. uint8_t *tmp;
  57. tmp = matrix_prev;
  58. matrix_prev = matrix;
  59. matrix = tmp;
  60. KEY_POWER_ON();
  61. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  62. for (uint8_t col = 0; col < MATRIX_COLS; col++) {
  63. KEY_SELECT(row, col);
  64. _delay_us(5);
  65. // Not sure this is needed. This just emulates HHKB controller's behaviour.
  66. if (matrix_prev[row] & (1<<col)) {
  67. KEY_PREV_ON();
  68. }
  69. _delay_us(10);
  70. // NOTE: KEY_STATE is valid only in 20us after KEY_ENABLE.
  71. // If V-USB interrupts in this section we could lose 40us or so
  72. // and would read invalid value from KEY_STATE.
  73. uint8_t last = TIMER_RAW;
  74. KEY_ENABLE();
  75. // Wait for KEY_STATE outputs its value.
  76. // 1us was ok on one HHKB, but not worked on another.
  77. // no wait doesn't work on Teensy++ with pro(1us works)
  78. // no wait does work on tmk PCB(8MHz) with pro2
  79. // 1us wait does work on both of above
  80. // 1us wait doesn't work on tmk(16MHz)
  81. // 5us wait does work on tmk(16MHz)
  82. // 5us wait does work on tmk(16MHz/2)
  83. // 5us wait does work on tmk(8MHz)
  84. // 10us wait does work on Teensy++ with pro
  85. // 10us wait does work on 328p+iwrap with pro
  86. // 10us wait doesn't work on tmk PCB(8MHz) with pro2(very lagged scan)
  87. _delay_us(5);
  88. if (KEY_STATE()) {
  89. matrix[row] &= ~(1<<col);
  90. } else {
  91. matrix[row] |= (1<<col);
  92. }
  93. // Ignore if this code region execution time elapses more than 20us.
  94. // MEMO: 20[us] * (TIMER_RAW_FREQ / 1000000)[count per us]
  95. // MEMO: then change above using this rule: a/(b/c) = a*1/(b/c) = a*(c/b)
  96. if (TIMER_DIFF_RAW(TIMER_RAW, last) > 20/(1000000/TIMER_RAW_FREQ)) {
  97. matrix[row] = matrix_prev[row];
  98. }
  99. _delay_us(5);
  100. KEY_PREV_OFF();
  101. KEY_UNABLE();
  102. // NOTE: KEY_STATE keep its state in 20us after KEY_ENABLE.
  103. // This takes 25us or more to make sure KEY_STATE returns to idle state.
  104. _delay_us(75);
  105. }
  106. }
  107. KEY_POWER_OFF();
  108. return 1;
  109. }
  110. bool matrix_is_modified(void)
  111. {
  112. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  113. if (matrix[i] != matrix_prev[i])
  114. return true;
  115. }
  116. return false;
  117. }
  118. inline
  119. bool matrix_has_ghost(void)
  120. {
  121. return false;
  122. }
  123. inline
  124. bool matrix_is_on(uint8_t row, uint8_t col)
  125. {
  126. return (matrix[row] & (1<<col));
  127. }
  128. inline
  129. matrix_row_t matrix_get_row(uint8_t row)
  130. {
  131. return matrix[row];
  132. }
  133. void matrix_print(void)
  134. {
  135. print("\nr/c 01234567\n");
  136. for (uint8_t row = 0; row < matrix_rows(); row++) {
  137. xprintf("%02X: %08b\n", row, bitrev(matrix_get_row(row)));
  138. }
  139. }