matrix.c 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. #include "matrix.h"
  2. #include "quantum.h"
  3. static matrix_row_t read_row(uint8_t row) {
  4. matrix_io_delay(); // without this wait read unstable value.
  5. // keypad and program buttons
  6. if (row == 12) {
  7. return ~(readPin(B4) | (readPin(B5) << 1) | 0b11111100);
  8. }
  9. return ~(readPin(B6) | readPin(B2) << 1 | readPin(B3) << 2 | readPin(B1) << 3 | readPin(F7) << 4 | readPin(F6) << 5 | readPin(F5) << 6 | readPin(F4) << 7);
  10. }
  11. static void unselect_rows(void) {
  12. // set A,B,C,G to 0
  13. PORTD &= 0xF0;
  14. }
  15. static void select_rows(uint8_t row) {
  16. // set A,B,C,G to row value
  17. PORTD |= (0x0F & row);
  18. }
  19. void matrix_init_custom(void) {
  20. // output low (multiplexers)
  21. setPinOutput(D0);
  22. setPinOutput(D1);
  23. setPinOutput(D2);
  24. setPinOutput(D3);
  25. // input with pullup (matrix)
  26. setPinInputHigh(B6);
  27. setPinInputHigh(B2);
  28. setPinInputHigh(B3);
  29. setPinInputHigh(B1);
  30. setPinInputHigh(F7);
  31. setPinInputHigh(F6);
  32. setPinInputHigh(F5);
  33. setPinInputHigh(F4);
  34. // input with pullup (program and keypad buttons)
  35. setPinInputHigh(B4);
  36. setPinInputHigh(B5);
  37. // initialize row and col
  38. unselect_rows();
  39. }
  40. bool matrix_scan_custom(matrix_row_t current_matrix[]) {
  41. bool matrix_has_changed = false;
  42. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  43. select_rows(i);
  44. matrix_row_t row = read_row(i);
  45. unselect_rows();
  46. bool row_has_changed = current_matrix[i] != row;
  47. matrix_has_changed |= row_has_changed;
  48. current_matrix[i] = row;
  49. }
  50. return matrix_has_changed;
  51. }