summaryrefslogtreecommitdiff
path: root/keyboards/miiiw/blackio83/matrix.c
blob: 841ff3c16e69b6266238088411edd3da4f6fd9f1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/* Copyright 2023 ArthurCyy <https://github.com/ArthurCyy>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "quantum.h"
#include "matrix.h"
#include "common/shift_register.h"

static uint8_t read_rows(void);
static void    init_cols(void);
static void    select_col(uint8_t col);
static void    unselect_col(uint8_t col);
static void    unselect_cols(void);

static const pin_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;
static const pin_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS;

#ifdef DIP_SWITCH_PINS
#    define NUMBER_OF_DIP_SWITCHES (sizeof(dip_switch_pad) / sizeof(pin_t))
static pin_t dip_switch_pad[] = DIP_SWITCH_PINS;
#endif

void matrix_init_custom(void) {
    for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
        setPinInputLow(row_pins[row]);
    }
    
    shift_init();
    init_cols();
}

bool matrix_scan_custom(matrix_row_t current_matrix[]) {
    bool changed = false;

    for (uint8_t col = 0; col < MATRIX_COLS; col++) {
        select_col(col);
        waitInputPinDelay();
        uint8_t rows = read_rows();
        for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
            bool prev_bit = ((uint32_t)(current_matrix[row]) & (matrix_row_t)(1UL << col)) ? 1 : 0;
            bool curr_bit = ((uint32_t)rows & (uint32_t)(1UL << row)) ? 1 : 0;
            if (prev_bit != curr_bit) {
                current_matrix[row] = (uint32_t)(current_matrix[row]) ^ (uint32_t)(1UL << col);
                changed = true;
            }
        }
        unselect_col(col);
    }

    return changed;
}

void matrix_power_up(void) {
    for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
        palDisableLineEvent(row_pins[row]);
        setPinInputLow(row_pins[row]);
    }
    init_cols();
#ifdef DIP_SWITCH_PINS
    for (uint8_t i = 1; i < NUMBER_OF_DIP_SWITCHES; i++) {
        setPinInputHigh(dip_switch_pad[i]);
    }
#endif
}

void matrix_power_down(void) {
    unselect_cols();
    for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
        setPinInputLow(row_pins[row]);
        palEnableLineEvent(row_pins[row], PAL_EVENT_MODE_RISING_EDGE);
    }
#ifdef DIP_SWITCH_PINS
    for (uint8_t i = 1; i < NUMBER_OF_DIP_SWITCHES; i++) {
        setPinInputLow(dip_switch_pad[i]);
    }
#endif
}

static uint8_t read_rows(void) {
    uint8_t row_value = 0;
    for(uint8_t row = 0; row < MATRIX_ROWS; row++) {
        row_value |= (readPin(row_pins[row]) << row);
    }
    return row_value;
}

static void init_cols(void) {
    shift_writeAll(0);
    for(uint8_t col = 0; col < MATRIX_COLS; col++) {
        if(col_pins[col] < H0) {
            setPinOutput(col_pins[col]);
            writePinLow(col_pins[col]);
        }
    }
}

static void select_col(uint8_t col) {
    if(col_pins[col] < H0){
        writePinHigh(col_pins[col]);
        waitInputPinDelay();
        waitInputPinDelay();
        waitInputPinDelay();
        waitInputPinDelay();
        waitInputPinDelay();
        waitInputPinDelay();
    }else{
        shift_writePin(col_pins[col], 1);
    }
}

static void unselect_col(uint8_t col) {
    if(col_pins[col] < H0){
        writePinLow(col_pins[col]);
    }else{
        shift_writePin(col_pins[col], 0);
    }
}

static void unselect_cols(void) {
    shift_writeAll(1);
    for(uint8_t col = 0; col < MATRIX_COLS; col++) {
        if(col_pins[col] < H0) {
            setPinOutput(col_pins[col]);
            writePinHigh(col_pins[col]);
        }
    }
}