diff options
Diffstat (limited to 'keyboards/switchplate/southpaw_65/matrix.c')
| -rw-r--r-- | keyboards/switchplate/southpaw_65/matrix.c | 95 |
1 files changed, 95 insertions, 0 deletions
diff --git a/keyboards/switchplate/southpaw_65/matrix.c b/keyboards/switchplate/southpaw_65/matrix.c new file mode 100644 index 000000000..e701d274f --- /dev/null +++ b/keyboards/switchplate/southpaw_65/matrix.c | |||
| @@ -0,0 +1,95 @@ | |||
| 1 | /* Copyright 2019 | ||
| 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 <stdint.h> | ||
| 17 | #include <stdbool.h> | ||
| 18 | #include <avr/io.h> | ||
| 19 | #include <string.h> | ||
| 20 | #include "matrix.h" | ||
| 21 | #include "pca9555.h" | ||
| 22 | #include "quantum.h" | ||
| 23 | |||
| 24 | #include "debug.h" | ||
| 25 | |||
| 26 | // PCA9555 slave addresses | ||
| 27 | #define IC1 0x20 | ||
| 28 | #define IC2 0x21 | ||
| 29 | |||
| 30 | //_____Utility funcs___________________________________________________________ | ||
| 31 | |||
| 32 | static void init_pins(void) { | ||
| 33 | // init all rows - IC1 port0 input | ||
| 34 | pca9555_set_config(IC1, PCA9555_PORT0, ALL_INPUT); // same as initial state | ||
| 35 | |||
| 36 | // init all cols high - IC2 all input | ||
| 37 | pca9555_set_config(IC1, PCA9555_PORT1, ALL_INPUT); // same as initial state | ||
| 38 | pca9555_set_config(IC2, PCA9555_PORT0, ALL_INPUT); // same as initial state | ||
| 39 | pca9555_set_config(IC2, PCA9555_PORT1, ALL_INPUT); // same as initial state | ||
| 40 | |||
| 41 | pca9555_set_output(IC1, PCA9555_PORT0, ALL_LOW); | ||
| 42 | } | ||
| 43 | |||
| 44 | static void select_row(uint8_t row) { | ||
| 45 | // For the Southpaw Ext 65% pins 1-6 are used for the rows | ||
| 46 | uint8_t pin = row; | ||
| 47 | uint8_t mask = 2 << pin; | ||
| 48 | |||
| 49 | pca9555_set_config(IC1, PCA9555_PORT0, ALL_INPUT & (~mask)); | ||
| 50 | } | ||
| 51 | |||
| 52 | static uint32_t read_cols(void) { | ||
| 53 | //Read column inputs. Pins 13-31 are used. Split across both ICs but they are sequential | ||
| 54 | uint32_t state_1 = pca9555_readPins(IC1, PCA9555_PORT1); | ||
| 55 | uint32_t state_2 = pca9555_readPins(IC2, PCA9555_PORT0); | ||
| 56 | uint32_t state_3 = pca9555_readPins(IC2, PCA9555_PORT1); | ||
| 57 | |||
| 58 | uint32_t state = (((state_3 & 0b01111111) << 12) | (state_2 << 4) | ((state_1 & 0b11110000) >> 4)); | ||
| 59 | return ~state; | ||
| 60 | } | ||
| 61 | |||
| 62 | static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) { | ||
| 63 | // Store last value of row prior to reading | ||
| 64 | matrix_row_t last_row_value = current_matrix[current_row]; | ||
| 65 | |||
| 66 | // Clear data in matrix row | ||
| 67 | current_matrix[current_row] = 0; | ||
| 68 | |||
| 69 | // Select row and wait for row selection to stabilize | ||
| 70 | select_row(current_row); | ||
| 71 | // Skip the wait_us(30); as i2c is slow enough to debounce the io changes | ||
| 72 | |||
| 73 | current_matrix[current_row] = read_cols(); | ||
| 74 | |||
| 75 | // No need to Unselect row as the next `select_row` will blank everything | ||
| 76 | |||
| 77 | return (last_row_value != current_matrix[current_row]); | ||
| 78 | } | ||
| 79 | |||
| 80 | //_____CUSTOM MATRIX IMPLEMENTATION____________________________________________________ | ||
| 81 | |||
| 82 | void matrix_init_custom(void) { | ||
| 83 | pca9555_init(IC1); | ||
| 84 | pca9555_init(IC2); | ||
| 85 | |||
| 86 | init_pins(); | ||
| 87 | } | ||
| 88 | |||
| 89 | bool matrix_scan_custom(matrix_row_t current_matrix[]) { | ||
| 90 | bool changed = false; | ||
| 91 | for (uint8_t current_row = 0; current_row < MATRIX_ROWS; current_row++) { | ||
| 92 | changed |= read_cols_on_row(current_matrix, current_row); | ||
| 93 | } | ||
| 94 | return changed; | ||
| 95 | } | ||
