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.

149 lines
4.3 KiB

  1. /*
  2. Copyright 2014 Ralf Schmitt <ralf@bunkertor.net>
  3. Copyright 2016 Daniel Svensson <dsvensson@gmail.com>
  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 <stdint.h>
  16. #include <stdbool.h>
  17. #include <avr/io.h>
  18. #include <util/delay.h>
  19. #include "wait.h"
  20. #include "print.h"
  21. #include "debug.h"
  22. #include "util.h"
  23. #include "matrix.h"
  24. #include "debounce.h"
  25. static matrix_row_t matrix[MATRIX_ROWS];
  26. static matrix_row_t matrix_debouncing[MATRIX_ROWS];
  27. static matrix_row_t read_cols(void);
  28. static void select_row(uint8_t col);
  29. // user-defined overridable functions
  30. __attribute__((weak)) void matrix_init_kb(void) { matrix_init_user(); }
  31. __attribute__((weak)) void matrix_scan_kb(void) { matrix_scan_user(); }
  32. __attribute__((weak)) void matrix_init_user(void) {}
  33. __attribute__((weak)) void matrix_scan_user(void) {}
  34. // helper functions
  35. inline uint8_t matrix_rows(void)
  36. {
  37. return MATRIX_ROWS;
  38. }
  39. inline uint8_t matrix_cols(void)
  40. {
  41. return MATRIX_COLS;
  42. }
  43. void matrix_init(void)
  44. {
  45. /* Column output pins */
  46. DDRD |= 0b01111011;
  47. /* Row input pins */
  48. DDRC &= ~0b10000000;
  49. DDRB &= ~0b01111111;
  50. PORTC |= 0b10000000;
  51. PORTB |= 0b01111111;
  52. for (uint8_t i=0; i < matrix_rows(); i++) {
  53. matrix[i] = 0;
  54. matrix_debouncing[i] = 0;
  55. }
  56. matrix_init_quantum();
  57. }
  58. uint8_t matrix_scan(void)
  59. {
  60. bool changed = false;
  61. for (uint8_t col = 0; col < MATRIX_COLS; col++) {
  62. select_row(col);
  63. wait_us(30);
  64. matrix_row_t rows = read_cols();
  65. for (uint8_t row = 0; row < matrix_rows(); row++) {
  66. bool prev_bit = matrix_debouncing[row] & ((matrix_row_t)1<<col);
  67. bool curr_bit = rows & (1<<row);
  68. if ((changed |= prev_bit != curr_bit)) {
  69. matrix_debouncing[row] ^= (matrix_row_t) 1 << col;
  70. }
  71. }
  72. }
  73. debounce(matrix_debouncing, matrix, matrix_rows(), changed);
  74. matrix_scan_quantum();
  75. return (uint8_t)changed;
  76. }
  77. inline
  78. matrix_row_t matrix_get_row(uint8_t row)
  79. {
  80. return matrix[row];
  81. }
  82. void matrix_print(void)
  83. {
  84. print("\nr/c 0123456789ABCDEF\n");
  85. for (uint8_t row = 0; row < matrix_rows(); row++) {
  86. print_hex8(row); print(": ");
  87. print_bin_reverse16(matrix_get_row(row));
  88. print("\n");
  89. }
  90. }
  91. static matrix_row_t read_cols(void)
  92. {
  93. return
  94. (PINB&(1<<5) ? 0 : ((matrix_row_t)1<<0)) |
  95. (PINC&(1<<7) ? 0 : ((matrix_row_t)1<<1)) |
  96. (PINB&(1<<4) ? 0 : ((matrix_row_t)1<<2)) |
  97. (PINB&(1<<6) ? 0 : ((matrix_row_t)1<<3)) |
  98. (PINB&(1<<1) ? 0 : ((matrix_row_t)1<<4)) |
  99. (PINB&(1<<2) ? 0 : ((matrix_row_t)1<<5)) |
  100. (PINB&(1<<3) ? 0 : ((matrix_row_t)1<<6)) |
  101. (PINB&(1<<0) ? 0 : ((matrix_row_t)1<<7));
  102. }
  103. static void select_row(uint8_t col)
  104. {
  105. switch (col) {
  106. case 0: PORTD = (PORTD & ~0b01111011) | 0b00011011; break;
  107. case 1: PORTD = (PORTD & ~0b01111011) | 0b01000011; break;
  108. case 2: PORTD = (PORTD & ~0b01111011) | 0b01100000; break;
  109. case 3: PORTD = (PORTD & ~0b01111011) | 0b01111001; break;
  110. case 4: PORTD = (PORTD & ~0b01111011) | 0b01100010; break;
  111. case 5: PORTD = (PORTD & ~0b01111011) | 0b01101010; break;
  112. case 6: PORTD = (PORTD & ~0b01111011) | 0b01110001; break;
  113. case 7: PORTD = (PORTD & ~0b01111011) | 0b01101001; break;
  114. case 8: PORTD = (PORTD & ~0b01111011) | 0b01100001; break;
  115. case 9: PORTD = (PORTD & ~0b01111011) | 0b01111000; break;
  116. case 10: PORTD = (PORTD & ~0b01111011) | 0b00100011; break;
  117. case 11: PORTD = (PORTD & ~0b01111011) | 0b00101011; break;
  118. case 12: PORTD = (PORTD & ~0b01111011) | 0b00110011; break;
  119. case 13: PORTD = (PORTD & ~0b01111011) | 0b01110000; break;
  120. case 14: PORTD = (PORTD & ~0b01111011) | 0b00010011; break;
  121. case 15: PORTD = (PORTD & ~0b01111011) | 0b01101000; break;
  122. case 16: PORTD = (PORTD & ~0b01111011) | 0b00001011; break;
  123. case 17: PORTD = (PORTD & ~0b01111011) | 0b00111011; break;
  124. }
  125. }