summaryrefslogtreecommitdiff
path: root/keyboards/al1/matrix.c
blob: 1407cbc089aedb0a274a0900ba41ce2907173c66 (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
#include "matrix.h"

#include "gpio.h"

static uint8_t read_rows(void) {
    return (readPin(C7) ? 0 : 1) |
           (readPin(B1) ? 0 : 2) |
           (readPin(B2) ? 0 : 4) |
           (readPin(C6) ? 0 : 8) |
           (readPin(B4) ? 0 : 16) |
           (readPin(B5) ? 0 : 32);
}

static void select_col(uint8_t col) {
    writePinLow(D3);

    writePin(D4, (col & 1));
    writePin(D5, (col & 2));
    writePin(D6, (col & 4));
    writePin(D7, (col & 8));
}

static void  unselect_cols(void) {
    writePinHigh(D3);
}

void matrix_init_custom(void) {
    /* 74HC154 col pin configuration
     * pin:     D3  D7  D6  D5  D4
     * row: off  0   x   x   x   x
     *      0    1   0   0   0   0
     *      1    1   0   0   0   1
     *      2    1   0   0   1   0
     *      3    1   0   0   1   1
     *      4    1   0   1   0   0
     *      5    1   0   1   0   1
     *      6    1   0   1   1   0
     *      7    1   0   1   1   1
     *      8    1   1   0   0   0
     *      9    1   1   0   0   1
     *      10   1   1   0   1   0
     *      11   1   1   0   1   1
     *      12   1   1   1   0   0
     *      13   1   1   1   0   1
     *      14   1   1   1   1   0
     *      15   1   1   1   1   1
     */
    setPinOutput(D3);
    writePinHigh(D3);

    setPinOutput(D4);
    setPinOutput(D5);
    setPinOutput(D6);
    setPinOutput(D7);


    /* Row pin configuration
     *
     * row:  0  1  2  3  4  5
     * pin: C7 B1 B2 C6 B4 B5
     *
     */
    setPinInputHigh(C7);
    setPinInputHigh(B1);
    setPinInputHigh(B2);
    setPinInputHigh(C6);
    setPinInputHigh(B4);
    setPinInputHigh(B5);
}

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

    for (uint8_t col = 0; col < MATRIX_COLS; col++) {
        select_col(col);
        matrix_io_delay();
        uint8_t rows = read_rows();

        for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
            bool prev_bit = current_matrix[row] & ((matrix_row_t)1 << col);
            bool curr_bit = rows & (1 << row);

            if (prev_bit != curr_bit) {
                current_matrix[row] ^= ((matrix_row_t)1 << col);
                changed = true;
            }
        }
        unselect_cols();
    }

    return changed;
}