matrix.c (3303B)
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 "matrix.h" 21 #include "tca6424.h" 22 #include "abelx.h" 23 24 static const uint16_t col_pins[MATRIX_COLS] = CUSTOM_MATRIX_COL_PINS; 25 26 void matrix_init_custom(void) 27 { 28 tca6424_init(); 29 // set port0 30 tca6424_write_config(TCA6424_PORT0, 0); 31 // set port1 32 tca6424_write_config(TCA6424_PORT1, 0); 33 // set port2 34 tca6424_write_config(TCA6424_PORT2, 0xF5); 35 36 // clear output 37 tca6424_write_port(TCA6424_PORT0, 0); 38 tca6424_write_port(TCA6424_PORT1, 0); 39 tca6424_write_port(TCA6424_PORT2, 0); 40 } 41 42 43 static uint8_t row_mask[] = {ROW1_MASK,ROW2_MASK,ROW3_MASK,ROW4_MASK,ROW5_MASK,ROW6_MASK}; 44 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}; 45 46 bool matrix_scan_custom(matrix_row_t current_matrix[]) 47 { 48 bool changed = false; 49 uint8_t p0_data = tca6424_read_port(TCA6424_PORT0); 50 51 for (int col = 0; col < MATRIX_COLS; col++) { 52 // Select col and wait for col selecton to stabilize 53 switch(col) { 54 case 0: 55 set_pin(col_pins[col]); 56 break; 57 case 1 ... 8: 58 tca6424_write_port(TCA6424_PORT1, col_mask[col]); 59 break; 60 default: 61 tca6424_write_port(TCA6424_PORT0, col_mask[col]|(p0_data&0x01)); 62 break; 63 } 64 matrix_io_delay(); 65 66 // read row port for all rows 67 uint8_t row_value = tca6424_read_port(ROW_PORT); 68 for (uint8_t row = 0; row < MATRIX_ROWS; row++) { 69 uint8_t tmp = row; 70 // Store last value of row prior to reading 71 matrix_row_t last_row_value = current_matrix[tmp]; 72 73 // Check row pin state 74 if (row_value & row_mask[row]) { 75 // Pin HI, set col bit 76 current_matrix[tmp] |= (1 << col); 77 } else { 78 // Pin LOW, clear col bit 79 current_matrix[tmp] &= ~(1 << col); 80 } 81 82 // Determine if the matrix changed state 83 if ((last_row_value != current_matrix[tmp]) && !(changed)) { 84 changed = true; 85 } 86 } 87 // Unselect col 88 switch(col) { 89 case 0: 90 clear_pin(col_pins[col]); 91 break; 92 case 8: 93 tca6424_write_port(TCA6424_PORT1, 0); 94 break; 95 case 15: 96 tca6424_write_port(TCA6424_PORT0, p0_data&0x01); 97 break; 98 default: 99 break; 100 } 101 } 102 103 return changed; 104 }