simplyKeeb-60K

3.5 KBC
matrix.c
3.5 KB82 lines • cpp
1#include QMK_KEYBOARD_H
2#include "matrix.h"
3#include "pca9555.h"
4#include "i2c_master.h"
5
6static uint8_t matrix_row_pins_mcu[MATRIX_ROWS_PER_SIDE] = MATRIX_ROW_PINS_L;
7static uint8_t matrix_col_pins_mcu[MATRIX_COLS_PER_SIDE] = MATRIX_COL_PINS_L;
8static uint8_t matrix_row_pins_pca[MATRIX_ROWS_PER_SIDE] = MATRIX_ROW_PINS_R;
9static uint8_t matrix_col_pins_pca[MATRIX_COLS_PER_SIDE] = MATRIX_COL_PINS_R;
10
11matrix_row_t read_row_left(matrix_row_t last_row_value, matrix_row_t current_row_value, uint8_t current_row) {
12    pin_t current_row_pin = matrix_row_pins_mcu[current_row];
13    writePinLow(current_row_pin);
14    matrix_io_delay(); // wait for pin stabilization
15
16    for (uint8_t current_col = 0; current_col < MATRIX_COLS_PER_SIDE; current_col++) {
17        uint8_t pin_state = readPin(matrix_col_pins_mcu[current_col]);
18        if(!pin_state) { // If LOW is read, the key for the current column and row combination was pressed
19            current_row_value = current_row_value | (MATRIX_ROW_SHIFTER << current_col);
20        }
21    }
22
23    writePinHigh(current_row_pin);
24    return current_row_value;
25}
26
27matrix_row_t read_row_right(matrix_row_t last_row_value, matrix_row_t current_row_value, uint8_t current_row) {
28    pin_t current_row_pin = matrix_row_pins_pca[current_row];
29    pca9555_set_output(RIGHT_HALF, PCA9555_PORT1, ALL_HIGH & (~current_row_pin));
30
31    for (uint8_t current_col = 0; current_col < MATRIX_COLS_PER_SIDE; current_col++) {
32        uint8_t pin_state = 0;
33        pca9555_readPins(RIGHT_HALF, PCA9555_PORT0, &pin_state);
34        
35        if(!(pin_state & matrix_col_pins_pca[current_col])) { // If LOW is read, the key for the current column and row combination was pressed
36            current_row_value = current_row_value | (MATRIX_ROW_SHIFTER << (current_col + MATRIX_COLS_PER_SIDE)); // offset by the number of left side cols (MATRIX_COLS_PER_SIDE)
37        }
38    }
39
40    return current_row_value;
41}
42
43void matrix_init_custom(void) {
44    pca9555_init(RIGHT_HALF);
45
46    // Init PCA9555 Pins
47    pca9555_set_config(RIGHT_HALF, PCA9555_PORT0, ALL_INPUT); // Set all Port 0 Pins to input (columns)
48    pca9555_set_config(RIGHT_HALF, PCA9555_PORT1, ALL_OUTPUT); // Set all Port 1 pins to output (rows + leds)    
49    pca9555_set_output(RIGHT_HALF, PCA9555_PORT1, ALL_HIGH); // All HIGH per  default
50
51    // Init MCU Pins
52    uint8_t matrix_row_pins_mcu[MATRIX_ROWS_PER_SIDE] = MATRIX_ROW_PINS_L;
53    uint8_t matrix_col_pins_mcu[MATRIX_COLS_PER_SIDE] = MATRIX_COL_PINS_L;
54
55    for (int current_col = 0; current_col < MATRIX_COLS_PER_SIDE; current_col++) {
56        pin_t pin = matrix_col_pins_mcu[current_col];
57        setPinInputHigh(pin); // column pins are HIGH per default. If a column pin is LOW during the read_row, the key on this column and row was pressed.
58    }
59
60    for (int current_row = 0; current_row < MATRIX_ROWS_PER_SIDE; current_row++) {
61        pin_t pin = matrix_row_pins_mcu[current_row];
62        setPinOutput(pin);
63        writePinHigh(pin);
64    }
65}
66
67bool matrix_scan_custom(matrix_row_t current_matrix[]) {
68    bool changed = false;
69
70    for (uint8_t current_row = 0; current_row < MATRIX_ROWS; current_row++) {
71        matrix_row_t last_row_value = current_matrix[current_row];
72        matrix_row_t current_row_value = 0;
73
74        current_row_value = read_row_left(last_row_value, current_row_value, current_row);
75        current_row_value = read_row_right(last_row_value, current_row_value, current_row);
76
77        current_matrix[current_row] = current_row_value;
78        changed = changed || current_row_value != last_row_value;
79    }
80
81    return changed;
82}