summaryrefslogtreecommitdiffstats
path: root/converter/usb_usb/matrix.c
blob: 3cdb0860d5ecd1594a2f8923fc48ff13866cbdcf (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
/*
Copyright 2011 Jun Wako <wakojun@gmail.com>

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 "usb_hid.h"
#include "keycode.h"
#include "util.h"
#include "print.h"
#include "debug.h"
#include "matrix.h"

/* KEY CODE to Matrix
 *
 * HID keycode(1 byte):
 * Higher 5 bits indicates ROW and lower 3 bits COL.
 *
 *  7 6 5 4 3 2 1 0
 * +---------------+
 * |   ROW   | COL |
 * +---------------+
 *
 * Matrix space(32 * 8):
 *      01234567
 *   0 +--------+
 *   : |        |
 *   : |        |
 *  31 +--------+
 */
#define ROW_MASK 0xF8
#define COL_MASK 0x07
#define CODE(row, col)  (((row) << 3) | (col))
#define ROW(code)       (((code) & ROW_MASK) >> 3)
#define COL(code)       ((code) & COL_MASK)
#define ROW_BITS(code)  (1 << COL(code))


uint8_t matrix_rows(void) { return MATRIX_ROWS; }
uint8_t matrix_cols(void) { return MATRIX_COLS; }
void matrix_init(void) {}
bool matrix_has_ghost(void) { return false; }

static bool matrix_is_mod =false;

uint8_t matrix_scan(void) {
    static uint16_t last_time_stamp = 0;

    if (last_time_stamp != usb_hid_time_stamp) {
        last_time_stamp = usb_hid_time_stamp;
        matrix_is_mod = true;
    } else {
        matrix_is_mod = false;
    }
    return 1;
}

bool matrix_is_modified(void) {

    return matrix_is_mod;
}

bool matrix_is_on(uint8_t row, uint8_t col) {
    uint8_t code = CODE(row, col);

    if (IS_MOD(code)) {
        if (usb_hid_keyboard_report.mods & ROW_BITS(code)) {
            return true;
        }
    }
    for (uint8_t i = 0; i < REPORT_KEYS; i++) {
        if (usb_hid_keyboard_report.keys[i] == code) {
            return true;
        }
    }
    return false;
}

uint8_t matrix_get_row(uint8_t row) {
    uint8_t row_bits = 0;

    if (IS_MOD(CODE(row, 0)) && usb_hid_keyboard_report.mods) {
        row_bits |= usb_hid_keyboard_report.mods;
    }

    for (uint8_t i = 0; i < REPORT_KEYS; i++) {
        if (IS_ANY(usb_hid_keyboard_report.keys[i])) {
            if (row == ROW(usb_hid_keyboard_report.keys[i])) {
                row_bits |= ROW_BITS(usb_hid_keyboard_report.keys[i]);
            }
        }
    }
    return row_bits;
}

uint8_t matrix_key_count(void) {
    uint8_t count = 0;

    count += bitpop(usb_hid_keyboard_report.mods);
    for (uint8_t i = 0; i < REPORT_KEYS; i++) {
        if (IS_ANY(usb_hid_keyboard_report.keys[i])) {
            count++;
        }
    }
    return count;
}

void matrix_print(void) {
    print("\nr/c 01234567\n");
    for (uint8_t row = 0; row < matrix_rows(); row++) {
        phex(row); print(": ");
        pbin_reverse(matrix_get_row(row));
#ifdef MATRIX_HAS_GHOST
        if (matrix_has_ghost_in_row(row)) {
            print(" <ghost");
        }
#endif
        print("\n");
    }
}