matrix.c 2.8 KB

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