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.

130 lines
2.9 KiB

  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. #ifndef DEBOUNCE
  18. # define DEBOUNCE 5
  19. #endif
  20. static uint8_t debouncing = DEBOUNCE;
  21. static matrix_row_t matrix[MATRIX_ROWS];
  22. static matrix_row_t matrix_debouncing[MATRIX_ROWS];
  23. void matrix_set_row_status(uint8_t row);
  24. uint8_t bit_reverse(uint8_t x);
  25. __attribute__ ((weak))
  26. void matrix_init_kb(void) {
  27. matrix_init_user();
  28. }
  29. __attribute__ ((weak))
  30. void matrix_scan_kb(void) {
  31. matrix_scan_user();
  32. }
  33. __attribute__ ((weak))
  34. void matrix_init_user(void) {
  35. }
  36. __attribute__ ((weak))
  37. void matrix_scan_user(void) {
  38. }
  39. void matrix_init(void) {
  40. // all outputs for rows high
  41. DDRB = 0xFF;
  42. PORTB = 0xFF;
  43. // all inputs for columns
  44. DDRA = 0x00;
  45. DDRC &= ~(0x111111<<2);
  46. DDRD &= ~(1<<PIND7);
  47. // all columns are pulled-up
  48. PORTA = 0xFF;
  49. PORTC |= (0b111111<<2);
  50. PORTD |= (1<<PIND7);
  51. // initialize matrix state: all keys off
  52. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  53. matrix[row] = 0x00;
  54. matrix_debouncing[row] = 0x00;
  55. }
  56. matrix_init_quantum();
  57. }
  58. uint8_t matrix_scan(void) {
  59. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  60. matrix_set_row_status(row);
  61. _delay_us(5);
  62. matrix_row_t cols = (
  63. // cols 0..7, PORTA 0 -> 7
  64. (~PINA) & 0xFF
  65. ) | (
  66. // cols 8..13, PORTC 7 -> 0
  67. bit_reverse((~PINC) & 0xFF) << 8
  68. ) | (
  69. // col 14, PORTD 7
  70. ((~PIND) & (1 << PIND7)) << 7
  71. );
  72. if (matrix_debouncing[row] != cols) {
  73. matrix_debouncing[row] = cols;
  74. debouncing = DEBOUNCE;
  75. }
  76. }
  77. if (debouncing) {
  78. if (--debouncing) {
  79. _delay_ms(1);
  80. } else {
  81. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  82. matrix[i] = matrix_debouncing[i];
  83. }
  84. }
  85. }
  86. matrix_scan_quantum();
  87. return 1;
  88. }
  89. // declarations
  90. void matrix_set_row_status(uint8_t row) {
  91. DDRB = (1 << row);
  92. PORTB = ~(1 << row);
  93. }
  94. uint8_t bit_reverse(uint8_t x) {
  95. x = ((x >> 1) & 0x55) | ((x << 1) & 0xaa);
  96. x = ((x >> 2) & 0x33) | ((x << 2) & 0xcc);
  97. x = ((x >> 4) & 0x0f) | ((x << 4) & 0xf0);
  98. return x;
  99. }
  100. inline matrix_row_t matrix_get_row(uint8_t row) {
  101. return matrix[row];
  102. }
  103. void matrix_print(void) {
  104. }