]> git.gir.st - tmk_keyboard.git/blob - converter/news_usb/matrix.c
Merge branch 'matrix-fix'
[tmk_keyboard.git] / converter / news_usb / matrix.c
1 /*
2 Copyright 2012 Jun Wako <wakojun@gmail.com>
3
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 2 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 #include <stdint.h>
19 #include <stdbool.h>
20 #include <avr/io.h>
21 #include <util/delay.h>
22 #include "print.h"
23 #include "util.h"
24 #include "news.h"
25 #include "matrix.h"
26 #include "debug.h"
27
28
29 /*
30 * Matrix Array usage:
31 *
32 * ROW: 16
33 * COL:8
34 *
35 * 8bit wide
36 * +---------+
37 * 0|00 ... 07|
38 * 1|08 ... 0F|
39 * :| ... |
40 * :| ... |
41 * E|70 ... 77|
42 * F|78 ... 7F|
43 * +---------+
44 *
45 */
46 static uint8_t matrix[MATRIX_ROWS];
47 #define ROW(code) ((code>>3)&0xF)
48 #define COL(code) (code&0x07)
49
50
51 void matrix_init(void)
52 {
53 news_init();
54
55 // initialize matrix state: all keys off
56 for (uint8_t i=0; i < MATRIX_ROWS; i++) matrix[i] = 0x00;
57
58 return;
59 }
60
61 uint8_t matrix_scan(void)
62 {
63 uint8_t code;
64 code = news_recv();
65 if (code == 0) {
66 return 0;
67 }
68
69 phex(code); print(" ");
70 if (code&0x80) {
71 // break code
72 if (matrix_is_on(ROW(code), COL(code))) {
73 matrix[ROW(code)] &= ~(1<<COL(code));
74 }
75 } else {
76 // make code
77 if (!matrix_is_on(ROW(code), COL(code))) {
78 matrix[ROW(code)] |= (1<<COL(code));
79 }
80 }
81 return code;
82 }
83
84 inline
85 uint8_t matrix_get_row(uint8_t row)
86 {
87 return matrix[row];
88 }
Imprint / Impressum