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.

107 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. // Port D not used on this keyboard
  33. // all columns are pulled-up
  34. PORTA = 0xFF;
  35. PORTC |= (0b111111<<2);
  36. //PORTD |= (1<<PIND7);
  37. // Port D not used on this keyboard
  38. // initialize matrix state: all keys off
  39. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  40. matrix[row] = 0x00;
  41. matrix_debouncing[row] = 0x00;
  42. }
  43. matrix_init_quantum(); // missing from original port by Luiz
  44. }
  45. void matrix_set_row_status(uint8_t row) {
  46. DDRB = (1 << row);
  47. PORTB = ~(1 << row);
  48. }
  49. uint8_t bit_reverse(uint8_t x) {
  50. x = ((x >> 1) & 0x55) | ((x << 1) & 0xaa);
  51. x = ((x >> 2) & 0x33) | ((x << 2) & 0xcc);
  52. x = ((x >> 4) & 0x0f) | ((x << 4) & 0xf0);
  53. return x;
  54. }
  55. uint8_t matrix_scan(void) {
  56. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  57. matrix_set_row_status(row);
  58. _delay_us(5);
  59. matrix_row_t cols = (
  60. // cols 0..7, PORTA 0 -> 7
  61. (~PINA) & 0xFF
  62. ) | (
  63. // cols 8..13, PORTC 7 -> 0
  64. bit_reverse((~PINC) & 0xFF) << 8
  65. );
  66. if (matrix_debouncing[row] != cols) {
  67. matrix_debouncing[row] = cols;
  68. debouncing = DEBOUNCE;
  69. }
  70. }
  71. if (debouncing) {
  72. if (--debouncing) {
  73. _delay_ms(1);
  74. } else {
  75. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  76. matrix[i] = matrix_debouncing[i];
  77. }
  78. }
  79. }
  80. matrix_scan_quantum(); // also missing in original PS2AVRGB implementation
  81. //matrix_scan_user();
  82. return 1;
  83. }
  84. inline matrix_row_t matrix_get_row(uint8_t row) {
  85. return matrix[row];
  86. }
  87. void matrix_print(void) {
  88. }