diff options
Diffstat (limited to 'keyboards/matrix/abelx/matrix.c')
| -rw-r--r-- | keyboards/matrix/abelx/matrix.c | 108 |
1 files changed, 108 insertions, 0 deletions
diff --git a/keyboards/matrix/abelx/matrix.c b/keyboards/matrix/abelx/matrix.c new file mode 100644 index 000000000..56129bd0b --- /dev/null +++ b/keyboards/matrix/abelx/matrix.c | |||
| @@ -0,0 +1,108 @@ | |||
| 1 | /** | ||
| 2 | * matrix.c | ||
| 3 | * | ||
| 4 | * Copyright 2020 astro <yuleiz@gmail.com> | ||
| 5 | * | ||
| 6 | * This program is free software: you can redistribute it and/or modify | ||
| 7 | * it under the terms of the GNU General Public License as published by | ||
| 8 | * the Free Software Foundation, either version 2 of the License, or | ||
| 9 | * (at your option) any later version. | ||
| 10 | * | ||
| 11 | * This program is distributed in the hope that it will be useful, | ||
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 14 | * GNU General Public License for more details. | ||
| 15 | * | ||
| 16 | * You should have received a copy of the GNU General Public License | ||
| 17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| 18 | */ | ||
| 19 | |||
| 20 | #include <stdint.h> | ||
| 21 | #include <stdbool.h> | ||
| 22 | #include <string.h> | ||
| 23 | #include "quantum.h" | ||
| 24 | #include "matrix.h" | ||
| 25 | #include "tca6424.h" | ||
| 26 | #include "abelx.h" | ||
| 27 | |||
| 28 | static const uint16_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS; | ||
| 29 | |||
| 30 | void matrix_init_custom(void) | ||
| 31 | { | ||
| 32 | tca6424_init(); | ||
| 33 | // set port0 | ||
| 34 | tca6424_write_config(TCA6424_PORT0, 0); | ||
| 35 | // set port1 | ||
| 36 | tca6424_write_config(TCA6424_PORT1, 0); | ||
| 37 | // set port2 | ||
| 38 | tca6424_write_config(TCA6424_PORT2, 0xF5); | ||
| 39 | |||
| 40 | // clear output | ||
| 41 | tca6424_write_port(TCA6424_PORT0, 0); | ||
| 42 | tca6424_write_port(TCA6424_PORT1, 0); | ||
| 43 | tca6424_write_port(TCA6424_PORT2, 0); | ||
| 44 | } | ||
| 45 | |||
| 46 | |||
| 47 | static uint8_t row_mask[] = {ROW1_MASK,ROW2_MASK,ROW3_MASK,ROW4_MASK,ROW5_MASK,ROW6_MASK}; | ||
| 48 | static uint8_t col_mask[] = {COL1_MASK, COL2_MASK, COL3_MASK, COL4_MASK, COL5_MASK, COL6_MASK, COL7_MASK, COL8_MASK, COL9_MASK, COL10_MASK, COL11_MASK, COL12_MASK, COL13_MASK, COL14_MASK, COL15_MASK, COL16_MASK}; | ||
| 49 | |||
| 50 | bool matrix_scan_custom(matrix_row_t current_matrix[]) | ||
| 51 | { | ||
| 52 | bool changed = false; | ||
| 53 | uint8_t p0_data = tca6424_read_port(TCA6424_PORT0); | ||
| 54 | |||
| 55 | for (int col = 0; col < MATRIX_COLS; col++) { | ||
| 56 | // Select col and wait for col selecton to stabilize | ||
| 57 | switch(col) { | ||
| 58 | case 0: | ||
| 59 | set_pin(col_pins[col]); | ||
| 60 | break; | ||
| 61 | case 1 ... 8: | ||
| 62 | tca6424_write_port(TCA6424_PORT1, col_mask[col]); | ||
| 63 | break; | ||
| 64 | default: | ||
| 65 | tca6424_write_port(TCA6424_PORT0, col_mask[col]|(p0_data&0x01)); | ||
| 66 | break; | ||
| 67 | } | ||
| 68 | matrix_io_delay(); | ||
| 69 | |||
| 70 | // read row port for all rows | ||
| 71 | uint8_t row_value = tca6424_read_port(ROW_PORT); | ||
| 72 | for (uint8_t row = 0; row < MATRIX_ROWS; row++) { | ||
| 73 | uint8_t tmp = row; | ||
| 74 | // Store last value of row prior to reading | ||
| 75 | matrix_row_t last_row_value = current_matrix[tmp]; | ||
| 76 | |||
| 77 | // Check row pin state | ||
| 78 | if (row_value & row_mask[row]) { | ||
| 79 | // Pin HI, set col bit | ||
| 80 | current_matrix[tmp] |= (1 << col); | ||
| 81 | } else { | ||
| 82 | // Pin LOW, clear col bit | ||
| 83 | current_matrix[tmp] &= ~(1 << col); | ||
| 84 | } | ||
| 85 | |||
| 86 | // Determine if the matrix changed state | ||
| 87 | if ((last_row_value != current_matrix[tmp]) && !(changed)) { | ||
| 88 | changed = true; | ||
| 89 | } | ||
| 90 | } | ||
| 91 | // Unselect col | ||
| 92 | switch(col) { | ||
| 93 | case 0: | ||
| 94 | clear_pin(col_pins[col]); | ||
| 95 | break; | ||
| 96 | case 8: | ||
| 97 | tca6424_write_port(TCA6424_PORT1, 0); | ||
| 98 | break; | ||
| 99 | case 15: | ||
| 100 | tca6424_write_port(TCA6424_PORT0, p0_data&0x01); | ||
| 101 | break; | ||
| 102 | default: | ||
| 103 | break; | ||
| 104 | } | ||
| 105 | } | ||
| 106 | |||
| 107 | return changed; | ||
| 108 | } | ||
