Keyboard firmwares for Atmel AVR and Cortex-M
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

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