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.2KB

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