524d57847603cdecc60873ede7425553696bf83b
[tmk_keyboard.git] / converter / next_usb / matrix.c
1 /*
2 NeXT non-ADB Keyboard USB Converter
3
4 Copyright 2013, Benjamin Gould (bgould@github.com)
5
6 Based on:
7 TMK firmware code Copyright 2011,2012 Jun WAKO <wakojun@gmail.com>
8 Arduino code by "Ladyada" Limor Fried (http://ladyada.net/, http://adafruit.com/), released under BSD license
9
10 Timing reference thanks to http://m0115.web.fc2.com/ (dead link), http://cfile7.uf.tistory.com/image/14448E464F410BF22380BB
11 Pinouts thanks to http://www.68k.org/~degs/nextkeyboard.html
12 Keycodes from http://ftp.netbsd.org/pub/NetBSD/NetBSD-release-6/src/sys/arch/next68k/dev/
13
14 This software is licensed with a Modified BSD License.
15 All of this is supposed to be Free Software, Open Source, DFSG-free,
16 GPL-compatible, and OK to use in both free and proprietary applications.
17 Additions and corrections to this file are welcome.
18
19 Redistribution and use in source and binary forms, with or without
20 modification, are permitted provided that the following conditions are met:
21
22 * Redistributions of source code must retain the above copyright
23 notice, this list of conditions and the following disclaimer.
24
25 * Redistributions in binary form must reproduce the above copyright
26 notice, this list of conditions and the following disclaimer in
27 the documentation and/or other materials provided with the
28 distribution.
29
30 * Neither the name of the copyright holders nor the names of
31 contributors may be used to endorse or promote products derived
32 from this software without specific prior written permission.
33
34 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
35 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
36 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
37 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
38 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
39 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
40 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
41 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
42 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
43 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
44 POSSIBILITY OF SUCH DAMAGE.
45
46 */
47
48 #include <stdint.h>
49 #include <stdbool.h>
50 #include <avr/io.h>
51 #include <util/delay.h>
52 #include "print.h"
53 #include "util.h"
54 #include "serial.h"
55 #include "matrix.h"
56 #include "debug.h"
57 #include "matrix.h"
58 #include "next_kbd.h"
59
60 static void matrix_make(uint8_t code);
61 static void matrix_break(uint8_t code);
62
63 static uint8_t matrix[MATRIX_ROWS];
64 #define ROW(code) ((code>>3)&0xF)
65 #define COL(code) (code&0x07)
66
67 static bool is_modified = false;
68
69 /* number of matrix rows */
70 inline
71 uint8_t matrix_rows(void)
72 {
73 return MATRIX_ROWS;
74 }
75
76 /* number of matrix columns */
77 inline
78 uint8_t matrix_cols(void)
79 {
80 return MATRIX_COLS;
81 }
82
83 #ifndef NEXT_KBD_LED1_ON
84 #define NEXT_KBD_LED1_ON
85 #endif
86
87 #ifndef NEXT_KBD_LED1_OFF
88 #define NEXT_KBD_LED1_OFF
89 #endif
90
91 #define NEXT_KBD_PWR_READ (NEXT_KBD_PWR_PIN&(1<<NEXT_KBD_PWR_BIT))
92
93 static bool power_state = false;
94
95 /* intialize matrix for scanning. should be called once. */
96 void matrix_init(void)
97 {
98 #ifdef DEBUG_ON_INIT
99 debug_enable = true;
100 #endif
101
102 // I've found that the matrix likes a little while for things to
103 // settle down before it gets started. Not sure why :)
104 _delay_ms(250);
105
106 dprintf("[ Intializing NeXT keyboard ]\n");
107 NEXT_KBD_LED1_DDR |= (1<<NEXT_KBD_LED1_BIT); // LED pin to output
108 NEXT_KBD_LED1_ON;
109
110 NEXT_KBD_PWR_DDR &= ~(1<<NEXT_KBD_PWR_BIT); // Power Button pin to input
111 NEXT_KBD_PWR_PIN |= (1<<NEXT_KBD_PWR_BIT); // KBD_PWR pull up
112
113 power_state = NEXT_KBD_PWR_READ ? false : true;
114 dprintf("Initial power button state: %b\n", power_state);
115
116 next_kbd_init();
117
118 // initialize matrix state: all keys off
119 for (uint8_t i=0; i < MATRIX_ROWS; i++) matrix[i] = 0x00;
120
121 #ifdef NEXT_KBD_INIT_FLASH_LEDS
122 dprintf("flashing LEDs:");
123 // flash the LEDs after initialization
124 bool leds_on = true;
125 for (uint8_t i = 0; i <= 6; i++)
126 {
127 leds_on = leds_on ? false : true;
128 dprintf(" %b", leds_on);
129 next_kbd_set_leds(leds_on, leds_on);
130 _delay_ms(250);
131 }
132 dprintf("\n");
133 #endif
134
135 dprintf("[ NeXT keyboard initialized ]\n");
136
137 return;
138 }
139
140 #define NEXT_KBD_KEYCODE(response) (uint8_t)((response&0xFF)>>1)
141 #define NEXT_KBD_PRESSED_KEYCODE(response) (uint8_t)(((response)&0xF00)==0x400)
142 #define NEXT_KBD_PRESSED(response, mask) (uint8_t)(((response)&mask)>0)
143 #define NEXT_KBD_PRESSED_CONTROL(response) NEXT_KBD_PRESSED(response,0x01000)
144 #define NEXT_KBD_PRESSED_SHIFT_LEFT(response) NEXT_KBD_PRESSED(response,0x02000)
145 #define NEXT_KBD_PRESSED_SHIFT_RGHT(response) NEXT_KBD_PRESSED(response,0x04000)
146 #define NEXT_KBD_PRESSED_CMD_LEFT(response) NEXT_KBD_PRESSED(response,0x08000)
147 #define NEXT_KBD_PRESSED_CMD_RGHT(response) NEXT_KBD_PRESSED(response,0x10000)
148 #define NEXT_KBD_PRESSED_ALT_LEFT(response) NEXT_KBD_PRESSED(response,0x20000)
149 #define NEXT_KBD_PRESSED_ALT_RGHT(response) NEXT_KBD_PRESSED(response,0x40000)
150 #define NEXT_KBD_MAKE_OR_BREAK(key, code) \
151 do { \
152 if (NEXT_KBD_PRESSED_##key(resp) > 0) \
153 matrix_make(code); \
154 else \
155 matrix_break(code); \
156 } while (0);
157
158 #define NEXT_KBD_PWR_KEYCODE 0x58
159
160 /* scan all key states on matrix */
161 uint8_t matrix_scan(void)
162 {
163 _delay_ms(5);
164
165 //next_kbd_set_leds(false, false);
166 NEXT_KBD_LED1_OFF;
167
168 is_modified = false;
169
170 if (!NEXT_KBD_PWR_READ) {
171 matrix_make(NEXT_KBD_PWR_KEYCODE);
172 power_state = 1;
173 if (is_modified)
174 {
175 dprintf("Power state 1\n");
176
177 }
178 } else {
179 matrix_break(NEXT_KBD_PWR_KEYCODE);
180 power_state = 0;
181 if (is_modified)
182 {
183 dprintf("Power state 0\n");
184
185 }
186 }
187
188 uint32_t resp = (next_kbd_recv());
189
190 if (!resp || resp == NEXT_KBD_KMBUS_IDLE)
191 {
192 return 0;
193 }
194
195 NEXT_KBD_LED1_ON;
196
197 #ifdef NEXT_KBD_SHIFT_FLASH_LEDS
198 next_kbd_set_leds(
199 NEXT_KBD_PRESSED_SHIFT_LEFT(resp) ? true : false,
200 NEXT_KBD_PRESSED_SHIFT_RGHT(resp) ? true : false
201 );
202 #endif
203
204 dprintf("[ r=%04lX keycode=%02X pressed=%X CTRL=%X SHIFT_LEFT=%X SHIFT_RGHT=%X CMD_LEFT=%X CMD_RGHT=%X ALT_LEFT=%X ALT_RGHT=%X ]\n", \
205 resp, \
206 NEXT_KBD_KEYCODE(resp), \
207 NEXT_KBD_PRESSED_KEYCODE(resp), \
208 NEXT_KBD_PRESSED_CONTROL(resp), \
209 NEXT_KBD_PRESSED_SHIFT_LEFT(resp), \
210 NEXT_KBD_PRESSED_SHIFT_RGHT(resp), \
211 NEXT_KBD_PRESSED_CMD_LEFT(resp), \
212 NEXT_KBD_PRESSED_CMD_RGHT(resp), \
213 NEXT_KBD_PRESSED_ALT_LEFT(resp), \
214 NEXT_KBD_PRESSED_ALT_RGHT(resp) \
215 );
216
217 // Modifier keys don't return keycode; have to check the upper bits
218 NEXT_KBD_MAKE_OR_BREAK(ALT_RGHT, 0x51);
219 NEXT_KBD_MAKE_OR_BREAK(ALT_LEFT, 0x52);
220 NEXT_KBD_MAKE_OR_BREAK(CMD_RGHT, 0x53);
221 NEXT_KBD_MAKE_OR_BREAK(CMD_LEFT, 0x54);
222 NEXT_KBD_MAKE_OR_BREAK(SHIFT_RGHT, 0x55);
223 NEXT_KBD_MAKE_OR_BREAK(SHIFT_LEFT, 0x56);
224 NEXT_KBD_MAKE_OR_BREAK(CONTROL, 0x57);
225 NEXT_KBD_MAKE_OR_BREAK(KEYCODE, NEXT_KBD_KEYCODE(resp));
226
227 return 1;
228 }
229
230 /* whether modified from previous scan. used after matrix_scan. */
231 bool matrix_is_modified()
232 {
233 return is_modified;
234 }
235
236 /* whether a switch is on */
237 inline
238 bool matrix_is_on(uint8_t row, uint8_t col)
239 {
240 return (matrix[row] & (1<<col));
241 }
242
243 /* matrix state on row */
244 inline
245 uint8_t matrix_get_row(uint8_t row)
246 {
247 return matrix[row];
248 }
249
250 /* print matrix for debug */
251 void matrix_print(void)
252 {
253 }
254
255 inline
256 static void matrix_make(uint8_t code)
257 {
258 if (!matrix_is_on(ROW(code), COL(code))) {
259 matrix[ROW(code)] |= 1<<COL(code);
260 is_modified = true;
261 }
262 }
263
264 inline
265 static void matrix_break(uint8_t code)
266 {
267 if (matrix_is_on(ROW(code), COL(code))) {
268 matrix[ROW(code)] &= ~(1<<COL(code));
269 is_modified = true;
270 }
271 }
Imprint / Impressum