]> git.friedersdorff.com Git - max/tmk_keyboard.git/blob - converter/sun_usb/matrix.c
c1e98257d9876efaf082e251c9a24511e080fdc5
[max/tmk_keyboard.git] / converter / sun_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 "serial.h"
25 #include "matrix.h"
26 #include "debug.h"
27
28
29 /*
30  * Matrix Array usage:
31  *
32  * ROW: 16(4bits)
33  * COL:  8(3bits)
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  * Command From System To Keyboard
46  * 0x01 Reset
47  * 0x02 Bell On
48  * 0x03 Bell Off
49  * 0x0A Click On
50  * 0x0B Click Off
51  * 0x0E LED
52  * 0x0F Layout
53  *
54  * Command From Keyboard To System
55  * 0x7F Idle
56  * 0xFE Layout Response
57  * 0xFF Reset Response(followed by 0x04)
58  *
59  * bit: 3       2       1       0
60  * LED: CapsLk  ScrLk   Compose NumLk
61  *      
62  * Connector
63  *   8Pin mini DIN
64  *      8  7  6
65  *     5    4  3
66  *       2   1
67  *     receptacle
68  *
69  * 1: GND
70  * 2: GND
71  * 3: 5V
72  * 4: RX/TX(Mouse)
73  * 5: RX
74  * 6: TX
75  * 7: GND
76  * 8: 5V
77  */
78 static uint8_t matrix[MATRIX_ROWS];
79 #define ROW(code)      ((code>>3)&0xF)
80 #define COL(code)      (code&0x07)
81
82 static bool is_modified = false;
83
84
85 inline
86 uint8_t matrix_rows(void)
87 {
88     return MATRIX_ROWS;
89 }
90
91 inline
92 uint8_t matrix_cols(void)
93 {
94     return MATRIX_COLS;
95 }
96
97 void matrix_init(void)
98 {
99     print_enable = true;
100     debug_enable = true;
101
102     serial_init();
103
104     // initialize matrix state: all keys off
105     for (uint8_t i=0; i < MATRIX_ROWS; i++) matrix[i] = 0x00;
106
107     return;
108 }
109
110 uint8_t matrix_scan(void)
111 {
112     is_modified = false;
113
114     uint8_t code;
115     code = serial_recv();
116     if (code == 0) {
117         return 0;
118     }
119
120     phex(code); print("(");
121     code = ~code;
122     phex(code); print(")");
123 return 0;
124     if (code&0x80) {
125         // break code
126         if (matrix_is_on(ROW(code), COL(code))) {
127             matrix[ROW(code)] &= ~(1<<COL(code));
128             is_modified = true;
129         }
130     } else {
131         // make code
132         if (!matrix_is_on(ROW(code), COL(code))) {
133             matrix[ROW(code)] |=  (1<<COL(code));
134             is_modified = true;
135         }
136     }
137     return code;
138 }
139
140 bool matrix_is_modified(void)
141 {
142     return is_modified;
143 }
144
145 inline
146 bool matrix_has_ghost(void)
147 {
148     return false;
149 }
150
151 inline
152 bool matrix_is_on(uint8_t row, uint8_t col)
153 {
154     return (matrix[row] & (1<<col));
155 }
156
157 inline
158 uint8_t matrix_get_row(uint8_t row)
159 {
160     return matrix[row];
161 }
162
163 void matrix_print(void)
164 {
165     print("\nr/c 01234567\n");
166     for (uint8_t row = 0; row < matrix_rows(); row++) {
167         phex(row); print(": ");
168         pbin_reverse(matrix_get_row(row));
169         print("\n");
170     }
171 }
172
173 uint8_t matrix_key_count(void)
174 {
175     uint8_t count = 0;
176     for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
177         count += bitpop(matrix[i]);
178     }
179     return count;
180 }