matrix.c 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 "matrix.h"
  16. #include "quantum.h"
  17. matrix_row_t read_rows(void) {
  18. return
  19. (PINB & (1 << 1) ? 0 : ((matrix_row_t)1 << 0)) |
  20. (PINC & (1 << 2) ? 0 : ((matrix_row_t)1 << 1)) |
  21. (PINB & (1 << 6) ? 0 : ((matrix_row_t)1 << 2)) |
  22. (PINB & (1 << 4) ? 0 : ((matrix_row_t)1 << 3)) |
  23. (PINB & (1 << 3) ? 0 : ((matrix_row_t)1 << 4)) |
  24. (PINB & (1 << 2) ? 0 : ((matrix_row_t)1 << 5)) |
  25. (PINB & (1 << 0) ? 0 : ((matrix_row_t)1 << 6)) |
  26. (PINB & (1 << 5) ? 0 : ((matrix_row_t)1 << 7));
  27. }
  28. void select_col(uint8_t col) {
  29. switch (col) {
  30. case 0: PORTD = (PORTD & ~0b01111110) | 0b01100010; break;
  31. case 1: PORTD = (PORTD & ~0b01111110) | 0b01101000; break;
  32. case 2: PORTD = (PORTD & ~0b01111110) | 0b01101100; break;
  33. case 3: PORTD = (PORTD & ~0b01111110) | 0b01110000; break;
  34. case 4: PORTD = (PORTD & ~0b01111110) | 0b01111000; break;
  35. case 5: PORTD = (PORTD & ~0b01111110) | 0b01100000; break;
  36. case 6: PORTD = (PORTD & ~0b01111110) | 0b01110100; break;
  37. case 7: PORTD = (PORTD & ~0b01111110) | 0b01100100; break;
  38. case 8: PORTD = (PORTD & ~0b01111110) | 0b01111100; break;
  39. case 9: PORTD = (PORTD & ~0b01111110) | 0b01101010; break;
  40. case 10: PORTD = (PORTD & ~0b01111110) | 0b00110110; break;
  41. case 11: PORTD = (PORTD & ~0b01111110) | 0b00010110; break;
  42. case 12: PORTD = (PORTD & ~0b01111110) | 0b01001110; break;
  43. case 13: PORTD = (PORTD & ~0b01111110) | 0b00111110; break;
  44. case 14: PORTD = (PORTD & ~0b01111110) | 0b00011110; break;
  45. case 15: PORTD = (PORTD & ~0b01111110) | 0b01000110; break;
  46. case 16: PORTD = (PORTD & ~0b01111110) | 0b00100110; break;
  47. case 17: PORTD = (PORTD & ~0b01111110) | 0b00101110; break;
  48. }
  49. }
  50. void matrix_init_custom(void) {
  51. /* Column output pins */
  52. setPinOutput(D1);
  53. setPinOutput(D2);
  54. setPinOutput(D3);
  55. setPinOutput(D4);
  56. setPinOutput(D5);
  57. setPinOutput(D6);
  58. /* Row input pins */
  59. writePinHigh(B0);
  60. writePinHigh(B1);
  61. writePinHigh(B2);
  62. writePinHigh(B3);
  63. writePinHigh(B4);
  64. writePinHigh(B5);
  65. writePinHigh(B6);
  66. writePinHigh(C2);
  67. }
  68. bool matrix_scan_custom(matrix_row_t current_matrix[]) {
  69. bool changed = false;
  70. for (uint8_t col = 0; col < MATRIX_COLS; col++) {
  71. select_col(col);
  72. matrix_io_delay();
  73. matrix_row_t rows = read_rows();
  74. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  75. bool prev_bit = current_matrix[row] & ((matrix_row_t)1 << col);
  76. bool curr_bit = rows & (1 << row);
  77. if (prev_bit != curr_bit) {
  78. current_matrix[row] ^= (matrix_row_t)1 << col;
  79. changed = true;
  80. }
  81. }
  82. }
  83. return changed;
  84. }