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.

108 lines
2.8 KiB

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