summaryrefslogtreecommitdiff
path: root/keyboards/annepro2/matrix.c
blob: a1585e4ddf1f8594bd6ff567c949e2a2e0dbc0db (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
/*
 * Copyright (c) 2018 Charlie Waters
 *
 * 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 <stdint.h>
#include <stdbool.h>
#include <string.h>
#include <hal.h>
#include "timer.h"
#include "wait.h"
#include "print.h"
#include "matrix.h"
#include "annepro2.h"

pin_t row_list[MATRIX_ROWS] = MATRIX_ROW_PINS;
pin_t col_list[MATRIX_COLS] = MATRIX_COL_PINS;

bool matrix_scan_custom(matrix_row_t current_matrix[]) {
    bool matrix_has_changed = false;
    // cache of input ports for columns
    static uint16_t port_cache[4];
    // scan each row
    for (int row = 0; row < MATRIX_ROWS; row++) {
        palClearLine(row_list[row]);
        __NOP();
        __NOP();
        __NOP();
        __NOP();
        // read i/o ports
        port_cache[0] = palReadPort(IOPORTA);
        port_cache[1] = palReadPort(IOPORTB);
        port_cache[2] = palReadPort(IOPORTC);
        port_cache[3] = palReadPort(IOPORTD);
        palSetLine(row_list[row]);

        // get columns from ports
        matrix_row_t data = 0;
        for (int col = 0; col < MATRIX_COLS; ++col) {
            pin_t line = col_list[col];
            uint16_t port = port_cache[HT32_PAL_IDX(PAL_PORT(line))];
            data |= (((port & (1 << PAL_PAD(line))) ? 0 : 1) << col);
        }

        if (current_matrix[row] != data) {
            current_matrix[row] = data;
            matrix_has_changed  = true;
        }
    }
    return matrix_has_changed;
}