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

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