matrix.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. Copyright 2017 Luiz Ribeiro <luizribeiro@gmail.com>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. #include <avr/io.h>
  15. #include <util/delay.h>
  16. #include "matrix.h"
  17. #include "backlight.h"
  18. #ifndef DEBOUNCE
  19. #define DEBOUNCE 5
  20. #endif
  21. static uint8_t debouncing = DEBOUNCE;
  22. static matrix_row_t matrix[MATRIX_ROWS];
  23. static matrix_row_t matrix_debouncing[MATRIX_ROWS];
  24. __attribute__ ((weak))
  25. void matrix_init_user(void) {}
  26. __attribute__ ((weak))
  27. void matrix_scan_user(void) {}
  28. __attribute__ ((weak))
  29. void matrix_init_kb(void) {
  30. matrix_init_user();
  31. }
  32. __attribute__ ((weak))
  33. void matrix_scan_kb(void) {
  34. matrix_scan_user();
  35. }
  36. void matrix_init(void) {
  37. // all outputs for rows high
  38. DDRB = 0xFF;
  39. PORTB = 0xFF;
  40. // all inputs for columns
  41. DDRA = 0x00;
  42. DDRC &= ~(0x111111<<2);
  43. DDRD &= ~(1<<PIND7);
  44. // all columns are pulled-up
  45. PORTA = 0xFF;
  46. PORTC |= (0b111111<<2);
  47. PORTD |= (1<<PIND7);
  48. // initialize matrix state: all keys off
  49. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  50. matrix[row] = 0x00;
  51. matrix_debouncing[row] = 0x00;
  52. }
  53. matrix_init_quantum();
  54. }
  55. void matrix_set_row_status(uint8_t row) {
  56. DDRB = (1 << row);
  57. PORTB = ~(1 << row);
  58. }
  59. uint8_t bit_reverse(uint8_t x) {
  60. x = ((x >> 1) & 0x55) | ((x << 1) & 0xaa);
  61. x = ((x >> 2) & 0x33) | ((x << 2) & 0xcc);
  62. x = ((x >> 4) & 0x0f) | ((x << 4) & 0xf0);
  63. return x;
  64. }
  65. uint8_t matrix_scan(void) {
  66. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  67. matrix_set_row_status(row);
  68. _delay_us(5);
  69. matrix_row_t cols = (
  70. // cols 0..7, PORTA 0 -> 7
  71. (~PINA) & 0xFF
  72. ) | (
  73. // cols 8..13, PORTC 7 -> 0
  74. bit_reverse((~PINC) & 0xFF) << 8
  75. ) | (
  76. // col 14, PORTD 7
  77. ((~PIND) & (1 << PIND7)) << 7
  78. );
  79. if (matrix_debouncing[row] != cols) {
  80. matrix_debouncing[row] = cols;
  81. debouncing = DEBOUNCE;
  82. }
  83. }
  84. if (debouncing) {
  85. if (--debouncing) {
  86. _delay_ms(1);
  87. } else {
  88. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  89. matrix[i] = matrix_debouncing[i];
  90. }
  91. }
  92. }
  93. matrix_scan_quantum();
  94. return 1;
  95. }
  96. inline matrix_row_t matrix_get_row(uint8_t row) {
  97. return matrix[row];
  98. }
  99. void matrix_print(void) {
  100. }