logo

qmk_firmware

custom branch of QMK firmware git clone https://anongit.hacktivis.me/git/qmk_firmware.git

matrix.c (3347B)


  1. /*
  2. Copyright 2017 Gabriel Young <gabeplaysdrums@live.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 <util/delay.h>
  15. #include "matrix.h"
  16. static matrix_row_t scan_col(void) {
  17. // Each of the 8 columns is read off pins as below
  18. // 7 6 5 4 3 2 1 0
  19. // ,--,--,--,--,--,--,--,--,
  20. // |B0|B3|B2|B1|B6|B4|B5|C7|
  21. // `--`--`--`--`--`--`--`--`
  22. return (
  23. (PINC&(1<<2) ? 0 : ((matrix_row_t)1<<0)) |
  24. (PIND&(1<<0) ? 0 : ((matrix_row_t)1<<1)) |
  25. (PIND&(1<<1) ? 0 : ((matrix_row_t)1<<2)) |
  26. (PINC&(1<<7) ? 0 : ((matrix_row_t)1<<3)) |
  27. (PIND&(1<<5) ? 0 : ((matrix_row_t)1<<4)) |
  28. (PIND&(1<<4) ? 0 : ((matrix_row_t)1<<5)) |
  29. (PIND&(1<<2) ? 0 : ((matrix_row_t)1<<6)) |
  30. (PIND&(1<<6) ? 0 : ((matrix_row_t)1<<7))
  31. );
  32. }
  33. static void select_row(uint8_t row) {
  34. switch (row) {
  35. case 0: PORTB = (PORTB & ~0b01111110) | 0b00111010; break;
  36. case 1: PORTB = (PORTB & ~0b01111110) | 0b01011000; break;
  37. case 2: PORTB = (PORTB & ~0b01111110) | 0b01110000; break;
  38. case 3: PORTB = (PORTB & ~0b01111110) | 0b01101110; break;
  39. case 4: PORTB = (PORTB & ~0b01111110) | 0b01101100; break;
  40. case 5: PORTB = (PORTB & ~0b01111110) | 0b01101010; break;
  41. case 6: PORTB = (PORTB & ~0b01111110) | 0b01101000; break;
  42. case 7: PORTB = (PORTB & ~0b01111110) | 0b01100100; break;
  43. case 8: PORTB = (PORTB & ~0b01111110) | 0b01100000; break;
  44. case 9: PORTB = (PORTB & ~0b01111110) | 0b01100010; break;
  45. case 10: PORTB = (PORTB & ~0b01111110) | 0b00011010; break;
  46. case 11: PORTB = (PORTB & ~0b01111110) | 0b00011000; break;
  47. case 12: PORTB = (PORTB & ~0b01111110) | 0b00111100; break;
  48. case 13: PORTB = (PORTB & ~0b01111110) | 0b01100110; break;
  49. case 14: PORTB = (PORTB & ~0b01111110) | 0b00111000; break;
  50. case 15: PORTB = (PORTB & ~0b01111110) | 0b01110010; break;
  51. case 16: PORTB = (PORTB & ~0b01111110) | 0b00011110; break;
  52. case 17: PORTB = (PORTB & ~0b01111110) | 0b00111110; break;
  53. }
  54. }
  55. void matrix_init_custom(void) {
  56. /* Column output pins */
  57. DDRB |= 0b01111110;
  58. /* Row input pins */
  59. DDRC &= ~0b10000100;
  60. DDRD &= ~0b01110111;
  61. PORTC |= 0b10000100;
  62. PORTD |= 0b01110111;
  63. }
  64. // matrix is 18 uint8_t.
  65. // we select the row (one of 18), then read the column
  66. bool matrix_scan_custom(matrix_row_t current_matrix[]) {
  67. bool has_changed = false;
  68. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  69. matrix_row_t orig = current_matrix[row];
  70. select_row(row);
  71. _delay_us(3);
  72. current_matrix[row] = scan_col();
  73. has_changed |= (orig != current_matrix[row]);
  74. }
  75. return has_changed;
  76. }