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.

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