Fix matrix.c to use new default impl.
[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 #ifndef NEXT_KBD_LED1_ON
70 #define NEXT_KBD_LED1_ON
71 #endif
72
73 #ifndef NEXT_KBD_LED1_OFF
74 #define NEXT_KBD_LED1_OFF
75 #endif
76
77 #define NEXT_KBD_PWR_READ (NEXT_KBD_PWR_PIN&(1<<NEXT_KBD_PWR_BIT))
78
79 static bool power_state = false;
80
81 /* intialize matrix for scanning. should be called once. */
82 void matrix_init(void)
83 {
84 #ifdef DEBUG_ON_INIT
85 debug_enable = true;
86 #endif
87
88 // I've found that the matrix likes a little while for things to
89 // settle down before it gets started. Not sure why :)
90 _delay_ms(250);
91
92 dprintf("[ Intializing NeXT keyboard ]\n");
93 NEXT_KBD_LED1_DDR |= (1<<NEXT_KBD_LED1_BIT); // LED pin to output
94 NEXT_KBD_LED1_ON;
95
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
98
99 power_state = NEXT_KBD_PWR_READ ? false : true;
100 dprintf("Initial power button state: %b\n", power_state);
101
102 next_kbd_init();
103
104 // initialize matrix state: all keys off
105 for (uint8_t i=0; i < MATRIX_ROWS; i++) matrix[i] = 0x00;
106
107 #ifdef NEXT_KBD_INIT_FLASH_LEDS
108 dprintf("flashing LEDs:");
109 // flash the LEDs after initialization
110 bool leds_on = true;
111 for (uint8_t i = 0; i <= 6; i++)
112 {
113 leds_on = leds_on ? false : true;
114 dprintf(" %b", leds_on);
115 next_kbd_set_leds(leds_on, leds_on);
116 _delay_ms(250);
117 }
118 dprintf("\n");
119 #endif
120
121 dprintf("[ NeXT keyboard initialized ]\n");
122
123 return;
124 }
125
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) \
137 do { \
138 if (NEXT_KBD_PRESSED_##key(resp) > 0) \
139 matrix_make(code); \
140 else \
141 matrix_break(code); \
142 } while (0);
143
144 #define NEXT_KBD_PWR_KEYCODE 0x58
145
146 /* scan all key states on matrix */
147 uint8_t matrix_scan(void)
148 {
149 _delay_ms(5);
150
151 //next_kbd_set_leds(false, false);
152 NEXT_KBD_LED1_OFF;
153
154 is_modified = false;
155
156 if (!NEXT_KBD_PWR_READ) {
157 matrix_make(NEXT_KBD_PWR_KEYCODE);
158 power_state = 1;
159 if (is_modified)
160 {
161 dprintf("Power state 1\n");
162
163 }
164 } else {
165 matrix_break(NEXT_KBD_PWR_KEYCODE);
166 power_state = 0;
167 if (is_modified)
168 {
169 dprintf("Power state 0\n");
170
171 }
172 }
173
174 uint32_t resp = (next_kbd_recv());
175
176 if (!resp || resp == NEXT_KBD_KMBUS_IDLE)
177 {
178 return 0;
179 }
180
181 NEXT_KBD_LED1_ON;
182
183 #ifdef NEXT_KBD_SHIFT_FLASH_LEDS
184 next_kbd_set_leds(
185 NEXT_KBD_PRESSED_SHIFT_LEFT(resp) ? true : false,
186 NEXT_KBD_PRESSED_SHIFT_RGHT(resp) ? true : false
187 );
188 #endif
189
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", \
191 resp, \
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) \
201 );
202
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));
212
213 return 1;
214 }
215
216 /* matrix state on row */
217 inline
218 uint8_t matrix_get_row(uint8_t row)
219 {
220 return matrix[row];
221 }
222
223 inline
224 static void matrix_make(uint8_t code)
225 {
226 if (!matrix_is_on(ROW(code), COL(code))) {
227 matrix[ROW(code)] |= 1<<COL(code);
228 is_modified = true;
229 }
230 }
231
232 inline
233 static void matrix_break(uint8_t code)
234 {
235 if (matrix_is_on(ROW(code), COL(code))) {
236 matrix[ROW(code)] &= ~(1<<COL(code));
237 is_modified = true;
238 }
239 }
Imprint / Impressum