]> git.donarmstrong.com Git - tmk_firmware.git/blob - converter/news_usb/matrix.c
Merge branch 'keymap2'
[tmk_firmware.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 static bool is_modified = false;
51
52
53 inline
54 uint8_t matrix_rows(void)
55 {
56     return MATRIX_ROWS;
57 }
58
59 inline
60 uint8_t matrix_cols(void)
61 {
62     return MATRIX_COLS;
63 }
64
65 void matrix_init(void)
66 {
67     print_enable = true;
68     debug_enable = true;
69     debug_matrix = true;
70     news_init();
71
72     // initialize matrix state: all keys off
73     for (uint8_t i=0; i < MATRIX_ROWS; i++) matrix[i] = 0x00;
74
75     return;
76 }
77
78 uint8_t matrix_scan(void)
79 {
80     is_modified = false;
81
82     uint8_t code;
83     code = news_recv();
84     if (code == 0) {
85         return 0;
86     }
87
88     phex(code); print(" ");
89     if (code&0x80) {
90         // break code
91         if (matrix_is_on(ROW(code), COL(code))) {
92             matrix[ROW(code)] &= ~(1<<COL(code));
93             is_modified = true;
94         }
95     } else {
96         // make code
97         if (!matrix_is_on(ROW(code), COL(code))) {
98             matrix[ROW(code)] |=  (1<<COL(code));
99             is_modified = true;
100         }
101     }
102     return code;
103 }
104
105 bool matrix_is_modified(void)
106 {
107     return is_modified;
108 }
109
110 inline
111 bool matrix_has_ghost(void)
112 {
113     return false;
114 }
115
116 inline
117 bool matrix_is_on(uint8_t row, uint8_t col)
118 {
119     return (matrix[row] & (1<<col));
120 }
121
122 inline
123 uint8_t matrix_get_row(uint8_t row)
124 {
125     return matrix[row];
126 }
127
128 void matrix_print(void)
129 {
130     print("\nr/c 01234567\n");
131     for (uint8_t row = 0; row < matrix_rows(); row++) {
132         phex(row); print(": ");
133         pbin_reverse(matrix_get_row(row));
134         print("\n");
135     }
136 }
137
138 uint8_t matrix_key_count(void)
139 {
140     uint8_t count = 0;
141     for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
142         count += bitpop(matrix[i]);
143     }
144     return count;
145 }