summaryrefslogtreecommitdiff
path: root/keyboards/kinesis/nguyenvietyen/matrix.c
blob: 3e078946e09faf46af622d17caeb4395bb32e781 (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
#include "matrix.h"

#include "quantum.h"

static matrix_row_t read_row(uint8_t row) {
    matrix_io_delay();  // without this wait read unstable value.

    // keypad and program buttons
    if (row == 12) {
        return ~(readPin(B4) | (readPin(B5) << 1) | 0b11111100);
    }

    return ~(readPin(B6) | readPin(B2) << 1 | readPin(B3) << 2 | readPin(B1) << 3 | readPin(F7) << 4 | readPin(F6) << 5 | readPin(F5) << 6 | readPin(F4) << 7);
}

static void unselect_rows(void) {
    // set A,B,C,G to 0
    PORTD &= 0xF0;
}

static void select_rows(uint8_t row) {
    // set A,B,C,G to row value
    PORTD |= (0x0F & row);
}

void matrix_init_custom(void) {
    // output low (multiplexers)
    setPinOutput(D0);
    setPinOutput(D1);
    setPinOutput(D2);
    setPinOutput(D3);

    // input with pullup (matrix)
    setPinInputHigh(B6);
    setPinInputHigh(B2);
    setPinInputHigh(B3);
    setPinInputHigh(B1);
    setPinInputHigh(F7);
    setPinInputHigh(F6);
    setPinInputHigh(F5);
    setPinInputHigh(F4);

    // input with pullup (program and keypad buttons)
    setPinInputHigh(B4);
    setPinInputHigh(B5);

    // initialize row and col
    unselect_rows();
}

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

    for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
        select_rows(i);
        matrix_row_t row = read_row(i);
        unselect_rows();
        bool row_has_changed = current_matrix[i] != row;
        matrix_has_changed |= row_has_changed;
        current_matrix[i] = row;
    }

    return matrix_has_changed;
}