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 5.0KB

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
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. inline
  38. uint8_t matrix_rows(void)
  39. {
  40. return MATRIX_ROWS;
  41. }
  42. inline
  43. uint8_t matrix_cols(void)
  44. {
  45. return MATRIX_COLS;
  46. }
  47. void matrix_init(void)
  48. {
  49. #ifdef DEBUG
  50. debug_enable = true;
  51. debug_keyboard = true;
  52. #endif
  53. KEY_INIT();
  54. // initialize matrix state: all keys off
  55. for (uint8_t i=0; i < MATRIX_ROWS; i++) _matrix0[i] = 0x00;
  56. for (uint8_t i=0; i < MATRIX_ROWS; i++) _matrix1[i] = 0x00;
  57. matrix = _matrix0;
  58. matrix_prev = _matrix1;
  59. }
  60. uint8_t matrix_scan(void)
  61. {
  62. uint8_t *tmp;
  63. tmp = matrix_prev;
  64. matrix_prev = matrix;
  65. matrix = tmp;
  66. // power on
  67. if (!KEY_POWER_STATE()) KEY_POWER_ON();
  68. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  69. for (uint8_t col = 0; col < MATRIX_COLS; col++) {
  70. KEY_SELECT(row, col);
  71. _delay_us(5);
  72. // Not sure this is needed. This just emulates HHKB controller's behaviour.
  73. if (matrix_prev[row] & (1<<col)) {
  74. KEY_PREV_ON();
  75. }
  76. _delay_us(10);
  77. // NOTE: KEY_STATE is valid only in 20us after KEY_ENABLE.
  78. // If V-USB interrupts in this section we could lose 40us or so
  79. // and would read invalid value from KEY_STATE.
  80. uint8_t last = TIMER_RAW;
  81. KEY_ENABLE();
  82. // Wait for KEY_STATE outputs its value.
  83. // 1us was ok on one HHKB, but not worked on another.
  84. // no wait doesn't work on Teensy++ with pro(1us works)
  85. // no wait does work on tmk PCB(8MHz) with pro2
  86. // 1us wait does work on both of above
  87. // 1us wait doesn't work on tmk(16MHz)
  88. // 5us wait does work on tmk(16MHz)
  89. // 5us wait does work on tmk(16MHz/2)
  90. // 5us wait does work on tmk(8MHz)
  91. // 10us wait does work on Teensy++ with pro
  92. // 10us wait does work on 328p+iwrap with pro
  93. // 10us wait doesn't work on tmk PCB(8MHz) with pro2(very lagged scan)
  94. _delay_us(5);
  95. if (KEY_STATE()) {
  96. matrix[row] &= ~(1<<col);
  97. } else {
  98. matrix[row] |= (1<<col);
  99. }
  100. // Ignore if this code region execution time elapses more than 20us.
  101. // MEMO: 20[us] * (TIMER_RAW_FREQ / 1000000)[count per us]
  102. // MEMO: then change above using this rule: a/(b/c) = a*1/(b/c) = a*(c/b)
  103. if (TIMER_DIFF_RAW(TIMER_RAW, last) > 20/(1000000/TIMER_RAW_FREQ)) {
  104. matrix[row] = matrix_prev[row];
  105. }
  106. _delay_us(5);
  107. KEY_PREV_OFF();
  108. KEY_UNABLE();
  109. // NOTE: KEY_STATE keep its state in 20us after KEY_ENABLE.
  110. // This takes 25us or more to make sure KEY_STATE returns to idle state.
  111. _delay_us(75);
  112. }
  113. if (matrix[row] ^ matrix_prev[row]) matrix_last_modified = timer_read32();
  114. }
  115. // power off
  116. if (KEY_POWER_STATE() &&
  117. (USB_DeviceState == DEVICE_STATE_Suspended ||
  118. USB_DeviceState == DEVICE_STATE_Unattached ) &&
  119. timer_elapsed32(matrix_last_modified) > MATRIX_POWER_SAVE) {
  120. KEY_POWER_OFF();
  121. suspend_power_down();
  122. }
  123. return 1;
  124. }
  125. bool matrix_is_modified(void)
  126. {
  127. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  128. if (matrix[i] != matrix_prev[i])
  129. return true;
  130. }
  131. return false;
  132. }
  133. inline
  134. bool matrix_has_ghost(void)
  135. {
  136. return false;
  137. }
  138. inline
  139. bool matrix_is_on(uint8_t row, uint8_t col)
  140. {
  141. return (matrix[row] & (1<<col));
  142. }
  143. inline
  144. matrix_row_t matrix_get_row(uint8_t row)
  145. {
  146. return matrix[row];
  147. }
  148. void matrix_print(void)
  149. {
  150. print("\nr/c 01234567\n");
  151. for (uint8_t row = 0; row < matrix_rows(); row++) {
  152. xprintf("%02X: %08b\n", row, bitrev(matrix_get_row(row)));
  153. }
  154. }
  155. void matrix_power_up(void) {
  156. KEY_POWER_ON();
  157. }
  158. void matrix_power_down(void) {
  159. KEY_POWER_OFF();
  160. }