]> git.gir.st - tmk_keyboard.git/blob - converter/ibm4704_usb/matrix.c
Add Initial files of 4704_usb
[tmk_keyboard.git] / converter / ibm4704_usb / matrix.c
1 /*
2 Copyright 2014 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 "action.h"
23 #include "print.h"
24 #include "debug.h"
25 #include "util.h"
26 #include "ibm4704.h"
27 #include "matrix.h"
28
29
30 static void matrix_make(uint8_t code);
31 static void matrix_break(uint8_t code);
32 static void matrix_clear(void);
33
34
35 /*
36 * Matrix Array usage:
37 * IBM 4704 scan codes are assigned into 128(16x8)-cell matrix.
38 *
39 * 8bit wide
40 * +---------+
41 * 0| |
42 * :| XX | 00-7F
43 * f| |
44 * +---------+
45 *
46 * Exceptions:
47 */
48 static uint8_t matrix[MATRIX_ROWS];
49
50 // scan code bits 7654 3210
51 // R:row/C:column -RRR RCCC
52 #define ROW(code) ((code>>3)&0x0f)
53 #define COL(code) (code&0x07)
54
55
56 inline
57 uint8_t matrix_rows(void)
58 {
59 return MATRIX_ROWS;
60 }
61
62 inline
63 uint8_t matrix_cols(void)
64 {
65 return MATRIX_COLS;
66 }
67
68 static void enable_break(void)
69 {
70 uint8_t ret;
71 for (uint8_t code = 0; code < 0x80; code++) {
72 while (ibm4704_send(0x80|code) != 0) {
73 print("z");
74 }
75 _delay_ms(1);
76 ret = ibm4704_recv();
77 if (ret!=0xFF) {
78 xprintf("%0X: %0X ", code, ret);
79 }
80 }
81 _delay_us(100);
82 print("End\n");
83 while (ibm4704_send(0xFF) != 0) {
84 print("Z");
85 } // End
86 }
87
88 void matrix_init(void)
89 {
90 debug_enable = true;
91
92 ibm4704_init();
93 matrix_clear();
94
95 xprintf("------\n");
96 enable_break();
97 //while (ibm4704_send(0x8C) != 0) ;
98 //while (ibm4704_send(0xFF) != 0) ;
99 }
100
101 /*
102 * IBM 4704 Scan Code
103 */
104 uint8_t matrix_scan(void)
105 {
106 uint8_t code = ibm4704_recv();
107 if (code==0xFF) {
108 // Not receivd
109 return 0;
110 } else if ((code&0x78)==0x78) {
111 // 0xFF-F8 and 0x7F-78 is not scan code
112 dprintf("Error: %0X\n", code);
113 matrix_clear();
114 return 0;
115 } else if (code&0x80) {
116 matrix_make(code);
117 } else {
118 matrix_break(code);
119 }
120 return 1;
121 }
122
123 inline
124 bool matrix_has_ghost(void)
125 {
126 return false;
127 }
128
129 inline
130 bool matrix_is_on(uint8_t row, uint8_t col)
131 {
132 return (matrix[row] & (1<<col));
133 }
134
135 inline
136 uint8_t matrix_get_row(uint8_t row)
137 {
138 return matrix[row];
139 }
140
141 void matrix_print(void)
142 {
143 print("\nr/c 01234567\n");
144 for (uint8_t row = 0; row < matrix_rows(); row++) {
145 // TODO: use new function
146 phex(row); print(": ");
147 pbin_reverse(matrix_get_row(row));
148 print("\n");
149 }
150 }
151
152 uint8_t matrix_key_count(void)
153 {
154 uint8_t count = 0;
155 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
156 count += bitpop(matrix[i]);
157 }
158 return count;
159 }
160
161
162
163 inline
164 static void matrix_make(uint8_t code)
165 {
166 matrix[ROW(code)] |= 1<<COL(code);
167 }
168
169 inline
170 static void matrix_break(uint8_t code)
171 {
172 matrix[ROW(code)] &= ~(1<<COL(code));
173 }
174
175 inline
176 static void matrix_clear(void)
177 {
178 for (uint8_t i=0; i < MATRIX_ROWS; i++) matrix[i] = 0x00;
179 }
Imprint / Impressum