matrix.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /* Copyright 2022 @ 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. #ifndef PIN_USED_74HC595
  19. # define PIN_USED_74HC595 8
  20. #endif
  21. #ifndef PIN_START_74HC595
  22. # define PIN_START_74HC595 8
  23. #endif
  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. writePinHigh(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 readPin(pin);
  51. } else {
  52. return 1;
  53. }
  54. }
  55. void small_delay(volatile uint8_t timeout) {
  56. while (timeout--);
  57. }
  58. static void shiftOut(uint16_t dataOut) {
  59. ATOMIC_BLOCK_FORCEON {
  60. for (uint8_t i = 0; i < PIN_USED_74HC595; i++) {
  61. if (dataOut & 0x1) {
  62. writePinHigh(DATA_PIN_74HC595);
  63. } else {
  64. writePinLow(DATA_PIN_74HC595);
  65. }
  66. dataOut = dataOut >> 1;
  67. writePinHigh(CLOCK_PIN_74HC595);
  68. small_delay(2);
  69. writePinLow(CLOCK_PIN_74HC595);
  70. }
  71. writePinHigh(LATCH_PIN_74HC595);
  72. small_delay(2);
  73. writePinLow(LATCH_PIN_74HC595);
  74. }
  75. }
  76. static void shiftOut_single(uint8_t data) {
  77. ATOMIC_BLOCK_FORCEON {
  78. if (data & 0x1) {
  79. writePinHigh(DATA_PIN_74HC595);
  80. } else {
  81. writePinLow(DATA_PIN_74HC595);
  82. }
  83. writePinHigh(CLOCK_PIN_74HC595);
  84. small_delay(2);
  85. writePinLow(CLOCK_PIN_74HC595);
  86. writePinHigh(LATCH_PIN_74HC595);
  87. small_delay(2);
  88. writePinLow(LATCH_PIN_74HC595);
  89. }
  90. }
  91. static bool select_col(uint8_t col) {
  92. pin_t pin = col_pins[col];
  93. if (pin != NO_PIN) {
  94. setPinOutput_writeLow(pin);
  95. return true;
  96. } else {
  97. if (col == PIN_START_74HC595) {
  98. shiftOut_single(0x00);
  99. }
  100. return true;
  101. }
  102. return false;
  103. }
  104. static void unselect_col(uint8_t col) {
  105. pin_t pin = col_pins[col];
  106. if (pin != NO_PIN) {
  107. #ifdef MATRIX_UNSELECT_DRIVE_HIGH
  108. setPinOutput_writeHigh(pin);
  109. #else
  110. setPinInputHigh_atomic(pin);
  111. #endif
  112. } else {
  113. shiftOut_single(0x01);
  114. }
  115. }
  116. static void unselect_cols(void) {
  117. // unselect column pins
  118. for (uint8_t x = 0; x < MATRIX_COLS; x++) {
  119. pin_t pin = col_pins[x];
  120. if (pin != NO_PIN) {
  121. #ifdef MATRIX_UNSELECT_DRIVE_HIGH
  122. setPinOutput_writeHigh(pin);
  123. #else
  124. setPinInputHigh_atomic(pin);
  125. #endif
  126. }
  127. if (x == PIN_START_74HC595)
  128. // unselect Shift Register
  129. shiftOut(0xFFFF);
  130. }
  131. }
  132. static void matrix_init_pins(void) {
  133. setPinOutput(DATA_PIN_74HC595);
  134. setPinOutput(CLOCK_PIN_74HC595);
  135. setPinOutput(LATCH_PIN_74HC595);
  136. #ifdef MATRIX_UNSELECT_DRIVE_HIGH
  137. for (uint8_t x = 0; x < MATRIX_COLS; x++) {
  138. if (col_pins[x] != NO_PIN) {
  139. setPinOutput(col_pins[x]);
  140. }
  141. }
  142. #endif
  143. unselect_cols();
  144. for (uint8_t x = 0; x < MATRIX_ROWS; x++) {
  145. if (row_pins[x] != NO_PIN) {
  146. setPinInputHigh_atomic(row_pins[x]);
  147. }
  148. }
  149. }
  150. static void matrix_read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col, matrix_row_t row_shifter) {
  151. bool key_pressed = false;
  152. // Select col
  153. if (!select_col(current_col)) { // select col
  154. return; // skip NO_PIN col
  155. }
  156. matrix_output_select_delay();
  157. // For each row...
  158. for (uint8_t row_index = 0; row_index < ROWS_PER_HAND; row_index++) {
  159. // Check row pin state
  160. if (readMatrixPin(row_pins[row_index]) == 0) {
  161. // Pin LO, set col bit
  162. current_matrix[row_index] |= row_shifter;
  163. key_pressed = true;
  164. } else {
  165. // Pin HI, clear col bit
  166. current_matrix[row_index] &= ~row_shifter;
  167. }
  168. }
  169. // Unselect col
  170. unselect_col(current_col);
  171. matrix_output_unselect_delay(current_col, key_pressed); // wait for all Row signals to go HIGH
  172. }
  173. void matrix_init_custom(void) {
  174. // initialize key pins
  175. matrix_init_pins();
  176. }
  177. bool matrix_scan_custom(matrix_row_t current_matrix[]) {
  178. matrix_row_t curr_matrix[MATRIX_ROWS] = {0};
  179. // Set col, read rows
  180. matrix_row_t row_shifter = MATRIX_ROW_SHIFTER;
  181. for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++, row_shifter <<= 1) {
  182. matrix_read_rows_on_col(curr_matrix, current_col, row_shifter);
  183. }
  184. bool changed = memcmp(current_matrix, curr_matrix, sizeof(curr_matrix)) != 0;
  185. if (changed) memcpy(current_matrix, curr_matrix, sizeof(curr_matrix));
  186. return changed;
  187. }