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 #ifndef NEXT_KBD_LED1_ON
70 #define NEXT_KBD_LED1_ON
73 #ifndef NEXT_KBD_LED1_OFF
74 #define NEXT_KBD_LED1_OFF
77 #define NEXT_KBD_PWR_READ (NEXT_KBD_PWR_PIN&(1<<NEXT_KBD_PWR_BIT))
79 static bool power_state = false;
81 /* intialize matrix for scanning. should be called once. */
82 void matrix_init(void)
88 // I've found that the matrix likes a little while for things to
89 // settle down before it gets started. Not sure why :)
92 dprintf("[ Intializing NeXT keyboard ]\n");
93 NEXT_KBD_LED1_DDR |= (1<<NEXT_KBD_LED1_BIT); // LED pin to output
96 NEXT_KBD_PWR_DDR &= ~(1<<NEXT_KBD_PWR_BIT); // Power Button pin to input
97 NEXT_KBD_PWR_PIN |= (1<<NEXT_KBD_PWR_BIT); // KBD_PWR pull up
99 power_state = NEXT_KBD_PWR_READ ? false : true;
100 dprintf("Initial power button state: %b\n", power_state);
104 // initialize matrix state: all keys off
105 for (uint8_t i=0; i < MATRIX_ROWS; i++) matrix[i] = 0x00;
107 #ifdef NEXT_KBD_INIT_FLASH_LEDS
108 dprintf("flashing LEDs:");
109 // flash the LEDs after initialization
111 for (uint8_t i = 0; i <= 6; i++)
113 leds_on = leds_on ? false : true;
114 dprintf(" %b", leds_on);
115 next_kbd_set_leds(leds_on, leds_on);
121 dprintf("[ NeXT keyboard initialized ]\n");
126 #define NEXT_KBD_KEYCODE(response) (uint8_t)((response&0xFF)>>1)
127 #define NEXT_KBD_PRESSED_KEYCODE(response) (uint8_t)(((response)&0xF00)==0x400)
128 #define NEXT_KBD_PRESSED(response, mask) (uint8_t)(((response)&mask)>0)
129 #define NEXT_KBD_PRESSED_CONTROL(response) NEXT_KBD_PRESSED(response,0x01000)
130 #define NEXT_KBD_PRESSED_SHIFT_LEFT(response) NEXT_KBD_PRESSED(response,0x02000)
131 #define NEXT_KBD_PRESSED_SHIFT_RGHT(response) NEXT_KBD_PRESSED(response,0x04000)
132 #define NEXT_KBD_PRESSED_CMD_LEFT(response) NEXT_KBD_PRESSED(response,0x08000)
133 #define NEXT_KBD_PRESSED_CMD_RGHT(response) NEXT_KBD_PRESSED(response,0x10000)
134 #define NEXT_KBD_PRESSED_ALT_LEFT(response) NEXT_KBD_PRESSED(response,0x20000)
135 #define NEXT_KBD_PRESSED_ALT_RGHT(response) NEXT_KBD_PRESSED(response,0x40000)
136 #define NEXT_KBD_MAKE_OR_BREAK(key, code) \
138 if (NEXT_KBD_PRESSED_##key(resp) > 0) \
141 matrix_break(code); \
144 #define NEXT_KBD_PWR_KEYCODE 0x58
146 /* scan all key states on matrix */
147 uint8_t matrix_scan(void)
151 //next_kbd_set_leds(false, false);
156 if (!NEXT_KBD_PWR_READ) {
157 matrix_make(NEXT_KBD_PWR_KEYCODE);
161 dprintf("Power state 1\n");
165 matrix_break(NEXT_KBD_PWR_KEYCODE);
169 dprintf("Power state 0\n");
174 uint32_t resp = (next_kbd_recv());
176 if (!resp || resp == NEXT_KBD_KMBUS_IDLE)
183 #ifdef NEXT_KBD_SHIFT_FLASH_LEDS
185 NEXT_KBD_PRESSED_SHIFT_LEFT(resp) ? true : false,
186 NEXT_KBD_PRESSED_SHIFT_RGHT(resp) ? true : false
190 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", \
192 NEXT_KBD_KEYCODE(resp), \
193 NEXT_KBD_PRESSED_KEYCODE(resp), \
194 NEXT_KBD_PRESSED_CONTROL(resp), \
195 NEXT_KBD_PRESSED_SHIFT_LEFT(resp), \
196 NEXT_KBD_PRESSED_SHIFT_RGHT(resp), \
197 NEXT_KBD_PRESSED_CMD_LEFT(resp), \
198 NEXT_KBD_PRESSED_CMD_RGHT(resp), \
199 NEXT_KBD_PRESSED_ALT_LEFT(resp), \
200 NEXT_KBD_PRESSED_ALT_RGHT(resp) \
203 // Modifier keys don't return keycode; have to check the upper bits
204 NEXT_KBD_MAKE_OR_BREAK(ALT_RGHT, 0x51);
205 NEXT_KBD_MAKE_OR_BREAK(ALT_LEFT, 0x52);
206 NEXT_KBD_MAKE_OR_BREAK(CMD_RGHT, 0x53);
207 NEXT_KBD_MAKE_OR_BREAK(CMD_LEFT, 0x54);
208 NEXT_KBD_MAKE_OR_BREAK(SHIFT_RGHT, 0x55);
209 NEXT_KBD_MAKE_OR_BREAK(SHIFT_LEFT, 0x56);
210 NEXT_KBD_MAKE_OR_BREAK(CONTROL, 0x57);
211 NEXT_KBD_MAKE_OR_BREAK(KEYCODE, NEXT_KBD_KEYCODE(resp));
216 /* matrix state on row */
218 uint8_t matrix_get_row(uint8_t row)
224 static void matrix_make(uint8_t code)
226 if (!matrix_is_on(ROW(code), COL(code))) {
227 matrix[ROW(code)] |= 1<<COL(code);
233 static void matrix_break(uint8_t code)
235 if (matrix_is_on(ROW(code), COL(code))) {
236 matrix[ROW(code)] &= ~(1<<COL(code));