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

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