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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. 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. inline
  104. static bool matrix_has_ghost_in_row(int row) {
  105. return false;
  106. }