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.

197 lines
5.2 KiB

  1. /* Copyright 2021 @ Keychron (https://www.keychron.com)
  2. *
  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. *
  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. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include "matrix.h"
  17. #include "quantum.h"
  18. // Pin connected to DS of 74HC595
  19. #define DATA_PIN A7
  20. // Pin connected to SH_CP of 74HC595
  21. #define CLOCK_PIN B1
  22. // Pin connected to ST_CP of 74HC595
  23. #define LATCH_PIN B0
  24. #ifdef MATRIX_ROW_PINS
  25. static pin_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;
  26. #endif // MATRIX_ROW_PINS
  27. #ifdef MATRIX_COL_PINS
  28. static pin_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS;
  29. #endif // MATRIX_COL_PINS
  30. #define ROWS_PER_HAND (MATRIX_ROWS)
  31. static inline void setPinOutput_writeLow(pin_t pin) {
  32. ATOMIC_BLOCK_FORCEON {
  33. setPinOutput(pin);
  34. writePinLow(pin);
  35. }
  36. }
  37. static inline void setPinOutput_writeHigh(pin_t pin) {
  38. ATOMIC_BLOCK_FORCEON {
  39. setPinOutput(pin);
  40. gpio_write_pin_high(pin);
  41. }
  42. }
  43. static inline void setPinInputHigh_atomic(pin_t pin) {
  44. ATOMIC_BLOCK_FORCEON {
  45. setPinInputHigh(pin);
  46. }
  47. }
  48. static inline uint8_t readMatrixPin(pin_t pin) {
  49. if (pin != NO_PIN) {
  50. return gpio_read_pin(pin);
  51. } else {
  52. return 1;
  53. }
  54. }
  55. static void shiftOut(uint8_t dataOut) {
  56. for (uint8_t i = 0; i < 8; i++) {
  57. if (dataOut & 0x1) {
  58. setPinOutput_writeHigh(DATA_PIN);
  59. } else {
  60. setPinOutput_writeLow(DATA_PIN);
  61. }
  62. dataOut = dataOut >> 1;
  63. setPinOutput_writeHigh(CLOCK_PIN);
  64. setPinOutput_writeLow(CLOCK_PIN);
  65. }
  66. setPinOutput_writeHigh(LATCH_PIN);
  67. setPinOutput_writeLow(LATCH_PIN);
  68. }
  69. static void shiftout_single(uint8_t data) {
  70. if (data & 0x1) {
  71. setPinOutput_writeHigh(DATA_PIN);
  72. } else {
  73. setPinOutput_writeLow(DATA_PIN);
  74. }
  75. setPinOutput_writeHigh(CLOCK_PIN);
  76. setPinOutput_writeLow(CLOCK_PIN);
  77. setPinOutput_writeHigh(LATCH_PIN);
  78. setPinOutput_writeLow(LATCH_PIN);
  79. }
  80. static bool select_col(uint8_t col) {
  81. pin_t pin = col_pins[col];
  82. if (pin != NO_PIN) {
  83. setPinOutput_writeLow(pin);
  84. return true;
  85. } else {
  86. if (col == 8) {
  87. shiftout_single(0x00);
  88. } else {
  89. shiftout_single(0x01);
  90. }
  91. return true;
  92. }
  93. return false;
  94. }
  95. static void unselect_col(uint8_t col) {
  96. pin_t pin = col_pins[col];
  97. if (pin != NO_PIN) {
  98. #ifdef MATRIX_UNSELECT_DRIVE_HIGH
  99. setPinOutput_writeHigh(pin);
  100. #else
  101. setPinInputHigh_atomic(pin);
  102. #endif
  103. } else {
  104. if (col == (MATRIX_COLS - 1))
  105. shiftout_single(0x01);
  106. }
  107. }
  108. static void unselect_cols(void) {
  109. // unselect column pins
  110. for (uint8_t x = 0; x < MATRIX_COLS; x++) {
  111. pin_t pin = col_pins[x];
  112. if (pin != NO_PIN) {
  113. #ifdef MATRIX_UNSELECT_DRIVE_HIGH
  114. setPinOutput_writeHigh(pin);
  115. #else
  116. setPinInputHigh_atomic(pin);
  117. #endif
  118. }
  119. if (x == (MATRIX_COLS - 1))
  120. // unselect Shift Register
  121. shiftOut(0xFF);
  122. }
  123. }
  124. static void matrix_init_pins(void) {
  125. unselect_cols();
  126. for (uint8_t x = 0; x < MATRIX_ROWS; x++) {
  127. if (row_pins[x] != NO_PIN) {
  128. setPinInputHigh_atomic(row_pins[x]);
  129. }
  130. }
  131. }
  132. static void matrix_read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col, matrix_row_t row_shifter) {
  133. bool key_pressed = false;
  134. // Select col
  135. if (!select_col(current_col)) { // select col
  136. return; // skip NO_PIN col
  137. }
  138. matrix_output_select_delay();
  139. // For each row...
  140. for (uint8_t row_index = 0; row_index < ROWS_PER_HAND; row_index++) {
  141. // Check row pin state
  142. if (readMatrixPin(row_pins[row_index]) == 0) {
  143. // Pin LO, set col bit
  144. current_matrix[row_index] |= row_shifter;
  145. key_pressed = true;
  146. } else {
  147. // Pin HI, clear col bit
  148. current_matrix[row_index] &= ~row_shifter;
  149. }
  150. }
  151. // Unselect col
  152. unselect_col(current_col);
  153. matrix_output_unselect_delay(current_col, key_pressed); // wait for all Row signals to go HIGH
  154. }
  155. void matrix_init_custom(void) {
  156. // initialize key pins
  157. matrix_init_pins();
  158. }
  159. bool matrix_scan_custom(matrix_row_t current_matrix[]) {
  160. matrix_row_t curr_matrix[MATRIX_ROWS] = {0};
  161. // Set col, read rows
  162. matrix_row_t row_shifter = MATRIX_ROW_SHIFTER;
  163. for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++, row_shifter <<= 1) {
  164. matrix_read_rows_on_col(curr_matrix, current_col, row_shifter);
  165. }
  166. bool changed = memcmp(current_matrix, curr_matrix, sizeof(curr_matrix)) != 0;
  167. if (changed) memcpy(current_matrix, curr_matrix, sizeof(curr_matrix));
  168. return changed;
  169. }