logo

qmk_firmware

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

matrix.c (4102B)


  1. /* Copyright 2023 ArthurCyy <https://github.com/ArthurCyy>
  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 "wait.h"
  18. #include "common/shift_register.h"
  19. static uint8_t read_rows(void);
  20. static void init_cols(void);
  21. static void select_col(uint8_t col);
  22. static void unselect_col(uint8_t col);
  23. static void unselect_cols(void);
  24. static const pin_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;
  25. static const pin_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS;
  26. #ifdef DIP_SWITCH_PINS
  27. # define NUMBER_OF_DIP_SWITCHES (sizeof(dip_switch_pad) / sizeof(pin_t))
  28. static pin_t dip_switch_pad[] = DIP_SWITCH_PINS;
  29. #endif
  30. void matrix_init_custom(void) {
  31. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  32. gpio_set_pin_input_low(row_pins[row]);
  33. }
  34. shift_init();
  35. init_cols();
  36. }
  37. bool matrix_scan_custom(matrix_row_t current_matrix[]) {
  38. bool changed = false;
  39. for (uint8_t col = 0; col < MATRIX_COLS; col++) {
  40. select_col(col);
  41. waitInputPinDelay();
  42. uint8_t rows = read_rows();
  43. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  44. bool prev_bit = ((uint32_t)(current_matrix[row]) & (matrix_row_t)(1UL << col)) ? 1 : 0;
  45. bool curr_bit = ((uint32_t)rows & (uint32_t)(1UL << row)) ? 1 : 0;
  46. if (prev_bit != curr_bit) {
  47. current_matrix[row] = (uint32_t)(current_matrix[row]) ^ (uint32_t)(1UL << col);
  48. changed = true;
  49. }
  50. }
  51. unselect_col(col);
  52. }
  53. return changed;
  54. }
  55. void matrix_power_up(void) {
  56. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  57. palDisableLineEvent(row_pins[row]);
  58. gpio_set_pin_input_low(row_pins[row]);
  59. }
  60. init_cols();
  61. #ifdef DIP_SWITCH_PINS
  62. for (uint8_t i = 1; i < NUMBER_OF_DIP_SWITCHES; i++) {
  63. gpio_set_pin_input_high(dip_switch_pad[i]);
  64. }
  65. #endif
  66. }
  67. void matrix_power_down(void) {
  68. unselect_cols();
  69. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  70. gpio_set_pin_input_low(row_pins[row]);
  71. palEnableLineEvent(row_pins[row], PAL_EVENT_MODE_RISING_EDGE);
  72. }
  73. #ifdef DIP_SWITCH_PINS
  74. for (uint8_t i = 1; i < NUMBER_OF_DIP_SWITCHES; i++) {
  75. gpio_set_pin_input_low(dip_switch_pad[i]);
  76. }
  77. #endif
  78. }
  79. static uint8_t read_rows(void) {
  80. uint8_t row_value = 0;
  81. for(uint8_t row = 0; row < MATRIX_ROWS; row++) {
  82. row_value |= (gpio_read_pin(row_pins[row]) << row);
  83. }
  84. return row_value;
  85. }
  86. static void init_cols(void) {
  87. shift_writeAll(0);
  88. for(uint8_t col = 0; col < MATRIX_COLS; col++) {
  89. if(col_pins[col] < H0) {
  90. gpio_set_pin_output(col_pins[col]);
  91. gpio_write_pin_low(col_pins[col]);
  92. }
  93. }
  94. }
  95. static void select_col(uint8_t col) {
  96. if(col_pins[col] < H0){
  97. gpio_write_pin_high(col_pins[col]);
  98. waitInputPinDelay();
  99. waitInputPinDelay();
  100. waitInputPinDelay();
  101. waitInputPinDelay();
  102. waitInputPinDelay();
  103. waitInputPinDelay();
  104. }else{
  105. shift_writePin(col_pins[col], 1);
  106. }
  107. }
  108. static void unselect_col(uint8_t col) {
  109. if(col_pins[col] < H0){
  110. gpio_write_pin_low(col_pins[col]);
  111. }else{
  112. shift_writePin(col_pins[col], 0);
  113. }
  114. }
  115. static void unselect_cols(void) {
  116. shift_writeAll(1);
  117. for(uint8_t col = 0; col < MATRIX_COLS; col++) {
  118. if(col_pins[col] < H0) {
  119. gpio_set_pin_output(col_pins[col]);
  120. gpio_write_pin_high(col_pins[col]);
  121. }
  122. }
  123. }