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

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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. * scan matrix
  3. */
  4. #include <avr/io.h>
  5. #include <util/delay.h>
  6. #include "keymap.h"
  7. #include "matrix.h"
  8. #include "print.h"
  9. // matrix is active low. (key on: 0/key off: 1)
  10. //
  11. // HHKB has no ghost and no bounce.
  12. // row: HC4051 select input channel(0-8)
  13. // PB0, PB1, PB2(A, B, C)
  14. // col: LS145 select low output line(0-8)
  15. // PB3, PB4, PB5, PB6(A, B, C, D)
  16. // use D as ENABLE: (enable: 0/unenable: 1)
  17. // key: KEY: (on: 0/ off:1)
  18. // UNKNOWN: unknown whether input or output
  19. // PE6,PE7(KEY, UNKNOWN)
  20. #define COL_ENABLE (1<<6)
  21. #define KEY_SELELCT(ROW, COL) (PORTB = COL_ENABLE|(((COL)&0x07)<<3)|((ROW)&0x07))
  22. #define KEY_ENABLE (PORTB &= ~COL_ENABLE)
  23. #define KEY_UNABLE (PORTB |= COL_ENABLE)
  24. #define KEY_ON ((PINE&(1<<6)) ? false : true)
  25. // matrix state buffer
  26. uint8_t *matrix;
  27. uint8_t *matrix_prev;
  28. static uint8_t _matrix0[MATRIX_ROWS];
  29. static uint8_t _matrix1[MATRIX_ROWS];
  30. static bool matrix_has_ghost_in_row(int row);
  31. static int bit_pop(uint8_t bits);
  32. inline
  33. int matrix_rows(void) {
  34. return MATRIX_ROWS;
  35. }
  36. inline
  37. int matrix_cols(void) {
  38. return MATRIX_COLS;
  39. }
  40. // this must be called once before matrix_scan.
  41. void matrix_init(void)
  42. {
  43. // row & col output(PB0-6)
  44. DDRB = 0xFF;
  45. PORTB = KEY_SELELCT(0, 0);
  46. // KEY & VALID input with pullup(PE6,7)
  47. DDRE = 0x3F;
  48. PORTE = 0xC0;
  49. // initialize matrix state: all keys off
  50. for (int i=0; i < MATRIX_ROWS; i++) _matrix0[i] = 0xFF;
  51. for (int i=0; i < MATRIX_ROWS; i++) _matrix1[i] = 0xFF;
  52. matrix = _matrix0;
  53. matrix_prev = _matrix1;
  54. }
  55. int matrix_scan(void)
  56. {
  57. uint8_t *tmp;
  58. tmp = matrix_prev;
  59. matrix_prev = matrix;
  60. matrix = tmp;
  61. for (int row = 0; row < MATRIX_ROWS; row++) {
  62. for (int col = 0; col < MATRIX_COLS; col++) {
  63. KEY_SELELCT(row, col);
  64. _delay_us(50); // from logic analyzer chart
  65. KEY_ENABLE;
  66. _delay_us(10); // from logic analyzer chart
  67. if (KEY_ON) {
  68. matrix[row] &= ~(1<<col);
  69. } else {
  70. matrix[row] |= (1<<col);
  71. }
  72. KEY_UNABLE;
  73. _delay_us(150); // from logic analyzer chart
  74. }
  75. }
  76. return 1;
  77. }
  78. bool matrix_is_modified(void) {
  79. for (int i = 0; i < MATRIX_ROWS; i++) {
  80. if (matrix[i] != matrix_prev[i])
  81. return true;
  82. }
  83. return false;
  84. }
  85. inline
  86. bool matrix_has_ghost(void) {
  87. return false;
  88. }
  89. inline
  90. uint16_t matrix_get_row(int row) {
  91. return matrix[row];
  92. }
  93. void matrix_print(void) {
  94. print("\nr/c 01234567\n");
  95. for (int row = 0; row < matrix_rows(); row++) {
  96. phex(row); print(": ");
  97. pbin_reverse(matrix_get_row(row));
  98. if (matrix_has_ghost_in_row(row)) {
  99. print(" <ghost");
  100. }
  101. print("\n");
  102. }
  103. }
  104. int matrix_key_count(void) {
  105. int count = 0;
  106. for (int i = 0; i < MATRIX_ROWS; i++) {
  107. count += bit_pop(~matrix[i]);
  108. }
  109. return count;
  110. }
  111. inline
  112. static bool matrix_has_ghost_in_row(int row) {
  113. return false;
  114. }
  115. static int bit_pop(uint8_t bits) {
  116. int c;
  117. for (c = 0; bits; c++)
  118. bits &= bits -1;
  119. return c;
  120. }