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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 "matrix_skel.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. // KEY_PREV: (on: 1/ off: 0)
  21. // PE6,PE7(KEY, KEY_PREV)
  22. #define COL_ENABLE (1<<6)
  23. #define KEY_SELELCT(ROW, COL) (PORTB = (PORTB&(1<<7))|COL_ENABLE|(((COL)&0x07)<<3)|((ROW)&0x07))
  24. #define KEY_ENABLE (PORTB &= ~COL_ENABLE)
  25. #define KEY_UNABLE (PORTB |= COL_ENABLE)
  26. #define KEY_STATE (PINE&(1<<6))
  27. #define KEY_PREV_ON (PORTE |= (1<<7))
  28. #define KEY_PREV_OFF (PORTE &= ~(1<<7))
  29. // matrix state buffer
  30. static uint8_t *matrix;
  31. static uint8_t *matrix_prev;
  32. static uint8_t _matrix0[MATRIX_ROWS];
  33. static uint8_t _matrix1[MATRIX_ROWS];
  34. inline
  35. int matrix_rows(void)
  36. {
  37. return MATRIX_ROWS;
  38. }
  39. inline
  40. int matrix_cols(void)
  41. {
  42. return MATRIX_COLS;
  43. }
  44. // this must be called once before matrix_scan.
  45. void matrix_init(void)
  46. {
  47. // row & col output(PB0-6)
  48. DDRB = 0xFF;
  49. KEY_SELELCT(0, 0);
  50. // KEY: input with pullup(PE6)
  51. // KEY_PREV: output(PE7)
  52. DDRE = 0xBF;
  53. PORTE = 0x40;
  54. // initialize matrix state: all keys off
  55. for (int i=0; i < MATRIX_ROWS; i++) _matrix0[i] = 0x00;
  56. for (int i=0; i < MATRIX_ROWS; i++) _matrix1[i] = 0x00;
  57. matrix = _matrix0;
  58. matrix_prev = _matrix1;
  59. }
  60. int matrix_scan(void)
  61. {
  62. uint8_t *tmp;
  63. tmp = matrix_prev;
  64. matrix_prev = matrix;
  65. matrix = tmp;
  66. for (int row = 0; row < MATRIX_ROWS; row++) {
  67. for (int col = 0; col < MATRIX_COLS; col++) {
  68. KEY_SELELCT(row, col);
  69. _delay_us(40); // from logic analyzer chart
  70. if (matrix_prev[row] & (1<<col)) {
  71. KEY_PREV_ON;
  72. }
  73. _delay_us(7); // from logic analyzer chart
  74. KEY_ENABLE;
  75. _delay_us(10); // from logic analyzer chart
  76. if (KEY_STATE) {
  77. matrix[row] &= ~(1<<col);
  78. } else {
  79. matrix[row] |= (1<<col);
  80. }
  81. KEY_PREV_OFF;
  82. KEY_UNABLE;
  83. _delay_us(150); // from logic analyzer chart
  84. }
  85. }
  86. return 1;
  87. }
  88. bool matrix_is_modified(void)
  89. {
  90. for (int i = 0; i < MATRIX_ROWS; i++) {
  91. if (matrix[i] != matrix_prev[i])
  92. return true;
  93. }
  94. return false;
  95. }
  96. inline
  97. bool matrix_has_ghost(void)
  98. {
  99. return false;
  100. }
  101. inline
  102. bool matrix_is_on(int row, int col)
  103. {
  104. return (matrix[row] & (1<<col));
  105. }
  106. inline
  107. uint16_t matrix_get_row(int row)
  108. {
  109. return matrix[row];
  110. }
  111. void matrix_print(void)
  112. {
  113. print("\nr/c 01234567\n");
  114. for (int row = 0; row < matrix_rows(); row++) {
  115. phex(row); print(": ");
  116. pbin_reverse(matrix_get_row(row));
  117. print("\n");
  118. }
  119. }
  120. int matrix_key_count(void)
  121. {
  122. int count = 0;
  123. for (int i = 0; i < MATRIX_ROWS; i++) {
  124. count += bitpop(matrix[i]);
  125. }
  126. return count;
  127. }