2 NeXT non-ADB Keyboard USB Converter
4 Copyright 2013, Benjamin Gould (bgould@github.com)
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
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/
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.
19 Redistribution and use in source and binary forms, with or without
20 modification, are permitted provided that the following conditions are met:
22 * Redistributions of source code must retain the above copyright
23 notice, this list of conditions and the following disclaimer.
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
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.
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.
51 #include <util/delay.h>
60 static void matrix_make(uint8_t code);
61 static void matrix_break(uint8_t code);
63 static uint8_t matrix[MATRIX_ROWS];
64 #define ROW(code) ((code>>3)&0xF)
65 #define COL(code) (code&0x07)
67 static bool is_modified = false;
69 /* number of matrix rows */
71 uint8_t matrix_rows(void)
76 /* number of matrix columns */
78 uint8_t matrix_cols(void)
83 #ifndef NEXT_KBD_LED1_ON
84 #define NEXT_KBD_LED1_ON
87 #ifndef NEXT_KBD_LED1_OFF
88 #define NEXT_KBD_LED1_OFF
91 #define NEXT_KBD_PWR_READ (NEXT_KBD_PWR_PIN&(1<<NEXT_KBD_PWR_BIT))
93 static bool power_state = false;
95 /* intialize matrix for scanning. should be called once. */
96 void matrix_init(void)
102 // I've found that the matrix likes a little while for things to
103 // settle down before it gets started. Not sure why :)
106 dprintf("[ Intializing NeXT keyboard ]\n");
107 NEXT_KBD_LED1_DDR |= (1<<NEXT_KBD_LED1_BIT); // LED pin to output
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
113 power_state = NEXT_KBD_PWR_READ ? false : true;
114 dprintf("Initial power button state: %b\n", power_state);
118 // initialize matrix state: all keys off
119 for (uint8_t i=0; i < MATRIX_ROWS; i++) matrix[i] = 0x00;
121 #ifdef NEXT_KBD_INIT_FLASH_LEDS
122 dprintf("flashing LEDs:");
123 // flash the LEDs after initialization
125 for (uint8_t i = 0; i <= 6; i++)
127 leds_on = leds_on ? false : true;
128 dprintf(" %b", leds_on);
129 next_kbd_set_leds(leds_on, leds_on);
135 dprintf("[ NeXT keyboard initialized ]\n");
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) \
152 if (NEXT_KBD_PRESSED_##key(resp) > 0) \
155 matrix_break(code); \
158 #define NEXT_KBD_PWR_KEYCODE 0x58
160 /* scan all key states on matrix */
161 uint8_t matrix_scan(void)
165 //next_kbd_set_leds(false, false);
170 if (!NEXT_KBD_PWR_READ) {
171 matrix_make(NEXT_KBD_PWR_KEYCODE);
175 dprintf("Power state 1\n");
179 matrix_break(NEXT_KBD_PWR_KEYCODE);
183 dprintf("Power state 0\n");
188 uint32_t resp = (next_kbd_recv());
190 if (!resp || resp == NEXT_KBD_KMBUS_IDLE)
197 #ifdef NEXT_KBD_SHIFT_FLASH_LEDS
199 NEXT_KBD_PRESSED_SHIFT_LEFT(resp) ? true : false,
200 NEXT_KBD_PRESSED_SHIFT_RGHT(resp) ? true : false
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", \
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) \
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));
230 /* whether modified from previous scan. used after matrix_scan. */
231 bool matrix_is_modified()
236 /* whether a switch is on */
238 bool matrix_is_on(uint8_t row, uint8_t col)
240 return (matrix[row] & (1<<col));
243 /* matrix state on row */
245 uint8_t matrix_get_row(uint8_t row)
250 /* print matrix for debug */
251 void matrix_print(void)
256 static void matrix_make(uint8_t code)
258 if (!matrix_is_on(ROW(code), COL(code))) {
259 matrix[ROW(code)] |= 1<<COL(code);
265 static void matrix_break(uint8_t code)
267 if (matrix_is_on(ROW(code), COL(code))) {
268 matrix[ROW(code)] &= ~(1<<COL(code));