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.

92 lines
2.2 KiB

  1. #include "matrix.h"
  2. #include "gpio.h"
  3. static uint8_t read_rows(void) {
  4. return (readPin(C7) ? 0 : 1) |
  5. (readPin(B1) ? 0 : 2) |
  6. (readPin(B2) ? 0 : 4) |
  7. (readPin(C6) ? 0 : 8) |
  8. (readPin(B4) ? 0 : 16) |
  9. (readPin(B5) ? 0 : 32);
  10. }
  11. static void select_col(uint8_t col) {
  12. writePinLow(D3);
  13. writePin(D4, (col & 1));
  14. writePin(D5, (col & 2));
  15. writePin(D6, (col & 4));
  16. writePin(D7, (col & 8));
  17. }
  18. static void unselect_cols(void) {
  19. writePinHigh(D3);
  20. }
  21. void matrix_init_custom(void) {
  22. /* 74HC154 col pin configuration
  23. * pin: D3 D7 D6 D5 D4
  24. * row: off 0 x x x x
  25. * 0 1 0 0 0 0
  26. * 1 1 0 0 0 1
  27. * 2 1 0 0 1 0
  28. * 3 1 0 0 1 1
  29. * 4 1 0 1 0 0
  30. * 5 1 0 1 0 1
  31. * 6 1 0 1 1 0
  32. * 7 1 0 1 1 1
  33. * 8 1 1 0 0 0
  34. * 9 1 1 0 0 1
  35. * 10 1 1 0 1 0
  36. * 11 1 1 0 1 1
  37. * 12 1 1 1 0 0
  38. * 13 1 1 1 0 1
  39. * 14 1 1 1 1 0
  40. * 15 1 1 1 1 1
  41. */
  42. setPinOutput(D3);
  43. writePinHigh(D3);
  44. setPinOutput(D4);
  45. setPinOutput(D5);
  46. setPinOutput(D6);
  47. setPinOutput(D7);
  48. /* Row pin configuration
  49. *
  50. * row: 0 1 2 3 4 5
  51. * pin: C7 B1 B2 C6 B4 B5
  52. *
  53. */
  54. setPinInputHigh(C7);
  55. setPinInputHigh(B1);
  56. setPinInputHigh(B2);
  57. setPinInputHigh(C6);
  58. setPinInputHigh(B4);
  59. setPinInputHigh(B5);
  60. }
  61. bool matrix_scan_custom(matrix_row_t current_matrix[]) {
  62. bool changed = false;
  63. for (uint8_t col = 0; col < MATRIX_COLS; col++) {
  64. select_col(col);
  65. matrix_io_delay();
  66. uint8_t rows = read_rows();
  67. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  68. bool prev_bit = current_matrix[row] & ((matrix_row_t)1 << col);
  69. bool curr_bit = rows & (1 << row);
  70. if (prev_bit != curr_bit) {
  71. current_matrix[row] ^= ((matrix_row_t)1 << col);
  72. changed = true;
  73. }
  74. }
  75. unselect_cols();
  76. }
  77. return changed;
  78. }