Keyboard firmwares for Atmel AVR and Cortex-M
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.

matrix.c 4.6KB

13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. #include <avr/wdt.h>
  27. #include "suspend.h"
  28. #include "lufa.h"
  29. // matrix power saving
  30. #define MATRIX_POWER_SAVE 10000
  31. static uint32_t matrix_last_modified = 0;
  32. // matrix state buffer(1:on, 0:off)
  33. static matrix_row_t *matrix;
  34. static matrix_row_t *matrix_prev;
  35. static matrix_row_t _matrix0[MATRIX_ROWS];
  36. static matrix_row_t _matrix1[MATRIX_ROWS];
  37. void matrix_init(void)
  38. {
  39. #ifdef DEBUG
  40. debug_enable = true;
  41. debug_keyboard = true;
  42. #endif
  43. KEY_INIT();
  44. // initialize matrix state: all keys off
  45. for (uint8_t i=0; i < MATRIX_ROWS; i++) _matrix0[i] = 0x00;
  46. for (uint8_t i=0; i < MATRIX_ROWS; i++) _matrix1[i] = 0x00;
  47. matrix = _matrix0;
  48. matrix_prev = _matrix1;
  49. }
  50. uint8_t matrix_scan(void)
  51. {
  52. uint8_t *tmp;
  53. tmp = matrix_prev;
  54. matrix_prev = matrix;
  55. matrix = tmp;
  56. // power on
  57. if (!KEY_POWER_STATE()) KEY_POWER_ON();
  58. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  59. for (uint8_t col = 0; col < MATRIX_COLS; col++) {
  60. KEY_SELECT(row, col);
  61. _delay_us(5);
  62. // Not sure this is needed. This just emulates HHKB controller's behaviour.
  63. if (matrix_prev[row] & (1<<col)) {
  64. KEY_PREV_ON();
  65. }
  66. _delay_us(10);
  67. // NOTE: KEY_STATE is valid only in 20us after KEY_ENABLE.
  68. // If V-USB interrupts in this section we could lose 40us or so
  69. // and would read invalid value from KEY_STATE.
  70. uint8_t last = TIMER_RAW;
  71. KEY_ENABLE();
  72. // Wait for KEY_STATE outputs its value.
  73. // 1us was ok on one HHKB, but not worked on another.
  74. // no wait doesn't work on Teensy++ with pro(1us works)
  75. // no wait does work on tmk PCB(8MHz) with pro2
  76. // 1us wait does work on both of above
  77. // 1us wait doesn't work on tmk(16MHz)
  78. // 5us wait does work on tmk(16MHz)
  79. // 5us wait does work on tmk(16MHz/2)
  80. // 5us wait does work on tmk(8MHz)
  81. // 10us wait does work on Teensy++ with pro
  82. // 10us wait does work on 328p+iwrap with pro
  83. // 10us wait doesn't work on tmk PCB(8MHz) with pro2(very lagged scan)
  84. _delay_us(5);
  85. if (KEY_STATE()) {
  86. matrix[row] &= ~(1<<col);
  87. } else {
  88. matrix[row] |= (1<<col);
  89. }
  90. // Ignore if this code region execution time elapses more than 20us.
  91. // MEMO: 20[us] * (TIMER_RAW_FREQ / 1000000)[count per us]
  92. // MEMO: then change above using this rule: a/(b/c) = a*1/(b/c) = a*(c/b)
  93. if (TIMER_DIFF_RAW(TIMER_RAW, last) > 20/(1000000/TIMER_RAW_FREQ)) {
  94. matrix[row] = matrix_prev[row];
  95. }
  96. _delay_us(5);
  97. KEY_PREV_OFF();
  98. KEY_UNABLE();
  99. // NOTE: KEY_STATE keep its state in 20us after KEY_ENABLE.
  100. // This takes 25us or more to make sure KEY_STATE returns to idle state.
  101. #ifdef HHKB_JP
  102. // Looks like JP needs faster scan due to its twice larger matrix
  103. // or it can drop keys in fast key typing
  104. _delay_us(30);
  105. #else
  106. _delay_us(75);
  107. #endif
  108. }
  109. if (matrix[row] ^ matrix_prev[row]) matrix_last_modified = timer_read32();
  110. }
  111. // power off
  112. if (KEY_POWER_STATE() &&
  113. (USB_DeviceState == DEVICE_STATE_Suspended ||
  114. USB_DeviceState == DEVICE_STATE_Unattached ) &&
  115. timer_elapsed32(matrix_last_modified) > MATRIX_POWER_SAVE) {
  116. KEY_POWER_OFF();
  117. suspend_power_down();
  118. }
  119. return 1;
  120. }
  121. inline
  122. matrix_row_t matrix_get_row(uint8_t row)
  123. {
  124. return matrix[row];
  125. }
  126. void matrix_power_up(void) {
  127. KEY_POWER_ON();
  128. }
  129. void matrix_power_down(void) {
  130. KEY_POWER_OFF();
  131. }