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 jaren geleden
13 jaren geleden
13 jaren geleden
13 jaren geleden
13 jaren geleden
13 jaren geleden
13 jaren geleden
13 jaren geleden
13 jaren geleden
13 jaren geleden
13 jaren geleden
13 jaren geleden
13 jaren geleden
13 jaren geleden
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. return MATRIX_ROWS;
  34. }
  35. inline
  36. int matrix_cols(void) {
  37. return MATRIX_COLS;
  38. }
  39. // this must be called once before matrix_scan.
  40. void matrix_init(void)
  41. {
  42. // row & col output(PB0-6)
  43. DDRB = 0xFF;
  44. PORTB = KEY_SELELCT(0, 0);
  45. // KEY & VALID input with pullup(PE6,7)
  46. DDRE = 0x3F;
  47. PORTE = 0xC0;
  48. // initialize matrix state: all keys off
  49. for (int i=0; i < MATRIX_ROWS; i++) _matrix0[i] = 0xFF;
  50. for (int i=0; i < MATRIX_ROWS; i++) _matrix1[i] = 0xFF;
  51. matrix = _matrix0;
  52. matrix_prev = _matrix1;
  53. }
  54. int matrix_scan(void)
  55. {
  56. uint8_t *tmp;
  57. tmp = matrix_prev;
  58. matrix_prev = matrix;
  59. matrix = tmp;
  60. for (int row = 0; row < MATRIX_ROWS; row++) {
  61. for (int col = 0; col < MATRIX_COLS; col++) {
  62. KEY_SELELCT(row, col);
  63. _delay_us(50); // from logic analyzer chart
  64. KEY_ENABLE;
  65. _delay_us(10); // from logic analyzer chart
  66. if (KEY_ON) {
  67. matrix[row] &= ~(1<<col);
  68. } else {
  69. matrix[row] |= (1<<col);
  70. }
  71. KEY_UNABLE;
  72. _delay_us(150); // from logic analyzer chart
  73. }
  74. }
  75. return 1;
  76. }
  77. bool matrix_is_modified(void) {
  78. for (int i = 0; i < MATRIX_ROWS; i++) {
  79. if (matrix[i] != matrix_prev[i])
  80. return true;
  81. }
  82. return false;
  83. }
  84. inline
  85. bool matrix_has_ghost(void) {
  86. return false;
  87. }
  88. inline
  89. uint16_t matrix_get_row(int row) {
  90. return matrix[row];
  91. }
  92. void matrix_print(void) {
  93. print("\nr/c 01234567\n");
  94. for (int row = 0; row < matrix_rows(); row++) {
  95. phex(row); print(": ");
  96. pbin_reverse(matrix_get_row(row));
  97. if (matrix_has_ghost_in_row(row)) {
  98. print(" <ghost");
  99. }
  100. print("\n");
  101. }
  102. }
  103. int matrix_key_count(void) {
  104. int count = 0;
  105. for (int i = 0; i < MATRIX_ROWS; i++) {
  106. count += bit_pop(~matrix[i]);
  107. }
  108. return count;
  109. }
  110. inline
  111. static bool matrix_has_ghost_in_row(int row) {
  112. return false;
  113. }
  114. static int bit_pop(uint8_t bits) {
  115. int c;
  116. for (c = 0; bits; c++)
  117. bits &= bits -1;
  118. return c;
  119. }