logo

qmk_firmware

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

matrix.c (3648B)


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