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.

121 lines
3.6 KiB

  1. /*
  2. Copyright 2012-2020 Jun Wako, Jack Humbert, Yiancar-Designs
  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. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. #include "matrix.h"
  15. #include "wait.h"
  16. #include "i2c_master.h"
  17. static const pin_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;
  18. static const pin_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS;
  19. static void unselect_rows(void) {
  20. for(uint8_t x = 0; x < MATRIX_ROWS; x++) {
  21. setPinInputHigh(row_pins[x]);
  22. }
  23. }
  24. static void select_row(uint8_t row) {
  25. setPinOutput(row_pins[row]);
  26. writePinLow(row_pins[row]);
  27. }
  28. static void unselect_row(uint8_t row) {
  29. setPinInputHigh(row_pins[row]);
  30. }
  31. static void init_pins(void) {
  32. unselect_rows();
  33. // Set I/O
  34. uint8_t send_data = 0x1F;
  35. i2c_write_register((PORT_EXPANDER_ADDRESS << 1), 0x00, &send_data, 1, 20);
  36. // Set Pull-up
  37. i2c_write_register((PORT_EXPANDER_ADDRESS << 1), 0x06, &send_data, 1, 20);
  38. for (uint8_t x = 0; x < MATRIX_COLS; x++) {
  39. if ( x < 10 ) {
  40. setPinInputHigh(col_pins[x]);
  41. }
  42. }
  43. }
  44. void matrix_init_custom(void) {
  45. // TODO: initialize hardware here
  46. // Initialize I2C
  47. i2c_init();
  48. // initialize key pins
  49. init_pins();
  50. wait_ms(50);
  51. }
  52. static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) {
  53. // Store last value of row prior to reading
  54. matrix_row_t last_row_value = current_matrix[current_row];
  55. // Clear data in matrix row
  56. current_matrix[current_row] = 0;
  57. // Select row and wait for row selecton to stabilize
  58. select_row(current_row);
  59. matrix_io_delay();
  60. uint8_t port_expander_col_buffer;
  61. i2c_read_register((PORT_EXPANDER_ADDRESS << 1), 0x09, &port_expander_col_buffer, 1, 20);
  62. // For each col...
  63. for(uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
  64. uint8_t pin_state;
  65. // Select the col pin to read (active low)
  66. switch (col_index) {
  67. case 10 :
  68. pin_state = port_expander_col_buffer & (1 << 0);
  69. break;
  70. case 11 :
  71. pin_state = port_expander_col_buffer & (1 << 1);
  72. break;
  73. case 12 :
  74. pin_state = port_expander_col_buffer & (1 << 2);
  75. break;
  76. case 13 :
  77. pin_state = port_expander_col_buffer & (1 << 3);
  78. break;
  79. case 14 :
  80. pin_state = port_expander_col_buffer & (1 << 4);
  81. break;
  82. default :
  83. pin_state = readPin(col_pins[col_index]);
  84. }
  85. // Populate the matrix row with the state of the col pin
  86. current_matrix[current_row] |= pin_state ? 0 : (MATRIX_ROW_SHIFTER << col_index);
  87. }
  88. // Unselect row
  89. unselect_row(current_row);
  90. return (last_row_value != current_matrix[current_row]);
  91. }
  92. bool matrix_scan_custom(matrix_row_t current_matrix[]) {
  93. bool matrix_has_changed = false;
  94. // Set row, read cols
  95. for (uint8_t current_row = 0; current_row < MATRIX_ROWS; current_row++) {
  96. matrix_has_changed |= read_cols_on_row(current_matrix, current_row);
  97. }
  98. return matrix_has_changed;
  99. }