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.

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