aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortmk <nobody@nowhere>2010-08-22 23:58:37 +0900
committertmk <nobody@nowhere>2010-08-23 00:01:42 +0900
commit8cfb3712d58e4a88029dfd00111714e91939005e (patch)
tree4650383eac715a84419e678e0e990c99a7423907
parent20711b59e7b72d0a1875c3d38832fade6f56445d (diff)
downloadqmk_firmware-8cfb3712d58e4a88029dfd00111714e91939005e.tar.gz
qmk_firmware-8cfb3712d58e4a88029dfd00111714e91939005e.zip
matrix scan & default keymap
-rw-r--r--Makefile1
-rw-r--r--keymap.c27
-rw-r--r--keymap.h9
-rw-r--r--mykey.c186
-rw-r--r--print.c8
-rw-r--r--print.h1
-rw-r--r--usbkeycodes.h273
7 files changed, 467 insertions, 38 deletions
diff --git a/Makefile b/Makefile
index d54627ce1..c15cc2167 100644
--- a/Makefile
+++ b/Makefile
@@ -46,6 +46,7 @@ TARGET = mykey
46 46
47# List C source files here. (C dependencies are automatically generated.) 47# List C source files here. (C dependencies are automatically generated.)
48SRC = $(TARGET).c \ 48SRC = $(TARGET).c \
49 keymap.c \
49 usb_keyboard_debug.c \ 50 usb_keyboard_debug.c \
50 print.c 51 print.c
51 52
diff --git a/keymap.c b/keymap.c
new file mode 100644
index 000000000..73971ed86
--- /dev/null
+++ b/keymap.c
@@ -0,0 +1,27 @@
1#include "keymap.h"
2
3/*
4 * keymap for modified macway keyboard
5 */
6#define MATRIX_ROWS 9
7#define MATRIX_COLS 8
8
9const uint8_t Keymap[MATRIX_COLS][MATRIX_ROWS] = {
10 { KB_LALT, KB_1, KB_2, KB_3, KB_4, KB_7, KB_8, KB_9, KB_0 },
11 { KB_NO, KB_ESCAPE, KB_RALT, KB_NO, KB_5, KB_6, KB_EQUAL, KB_NO, KB_MINUS },
12 { KB_BSPACE, KB_TAB, KB_LGUI, KB_RSHIFT, KB_T, KB_Y, KB_RBRACKET, KB_NO, KB_LBRACKET },
13 { KB_NO, KB_Q, KB_W, KB_E, KB_R, KB_U, KB_I, KB_O, KB_P },
14 { KB_BSLASH, KB_A, KB_S, KB_D, KB_F, KB_J, KB_K, KB_L, KB_SCOLON },
15 { KB_NO, KB_LCTRL, KB_NO, KB_UP, KB_G, KB_H, KB_NO, KB_GRAVE, KB_QUOTE },
16 { KB_ENTER, KB_Z, KB_X, KB_C, KB_V, KB_M, KB_COMMA, KB_DOWN, KB_NO },
17 { KB_SPACE, KB_DOWN, KB_RIGHT, KB_LEFT, KB_B, KB_N, KB_LSHIFT, KB_NO, KB_SLASH }
18};
19
20uint8_t get_keycode(uint8_t row, uint8_t col)
21{
22 if (row >= MATRIX_ROWS)
23 return KB_NO;
24 if (col >= MATRIX_COLS)
25 return KB_NO;
26 return Keymap[col][row];
27}
diff --git a/keymap.h b/keymap.h
new file mode 100644
index 000000000..bf6e47240
--- /dev/null
+++ b/keymap.h
@@ -0,0 +1,9 @@
1#ifndef KEYMAP_H
2#define KEYMAP_H 1
3
4#include <stdint.h>
5#include "usbkeycodes.h"
6
7uint8_t get_keycode(uint8_t row, uint8_t col);
8
9#endif
diff --git a/mykey.c b/mykey.c
index f69503e5f..098bb4e51 100644
--- a/mykey.c
+++ b/mykey.c
@@ -27,6 +27,7 @@
27#include <util/delay.h> 27#include <util/delay.h>
28#include "usb_keyboard_debug.h" 28#include "usb_keyboard_debug.h"
29#include "print.h" 29#include "print.h"
30#include "keymap.h"
30 31
31#define LED_CONFIG (DDRD |= (1<<6)) 32#define LED_CONFIG (DDRD |= (1<<6))
32#define LED_ON (PORTD &= ~(1<<6)) 33#define LED_ON (PORTD &= ~(1<<6))
@@ -38,21 +39,127 @@ uint8_t number_keys[10]=
38 39
39uint16_t idle_count=0; 40uint16_t idle_count=0;
40 41
42
43
44//
45// scan matrix
46//
47uint8_t MAX_ROW = 9;
48
49// initialize ports for matrix
50void port_setup(void)
51{
52 // Column: input w/pullup
53 DDRB = 0x00;
54 PORTB = 0xFF;
55
56 // Row: Hi-Z(unselected)
57 // PD:0,1,2,3,6,7
58 // PC:6,7
59 // PF:7
60 DDRD = 0x00;
61 PORTD = 0x00;
62 DDRC = 0x00;
63 PORTC = 0x00;
64 DDRF = 0x00;
65 PORTF = 0x00;
66}
67
68// select a row of matrix for read
69void select_row(uint8_t row)
70{
71 switch (row) {
72 case 0:
73 DDRD = (1<<0);
74 PORTD = 0x00;
75 DDRC = 0x00;
76 PORTC = 0x00;
77 DDRF = 0x00;
78 PORTF = 0x00;
79 break;
80 case 1:
81 DDRD = (1<<1);
82 PORTD = 0x00;
83 DDRC = 0x00;
84 PORTC = 0x00;
85 DDRF = 0x00;
86 PORTF = 0x00;
87 break;
88 case 2:
89 DDRD = (1<<2);
90 PORTD = 0x00;
91 DDRC = 0x00;
92 PORTC = 0x00;
93 DDRF = 0x00;
94 PORTF = 0x00;
95 break;
96 case 3:
97 DDRD = (1<<3);
98 PORTD = 0x00;
99 DDRC = 0x00;
100 PORTC = 0x00;
101 DDRF = 0x00;
102 PORTF = 0x00;
103 break;
104 case 4:
105 DDRD = (1<<6);
106 PORTD = 0x00;
107 DDRC = 0x00;
108 PORTC = 0x00;
109 DDRF = 0x00;
110 PORTF = 0x00;
111 break;
112 case 5:
113 DDRD = (1<<7);
114 PORTD = 0x00;
115 DDRC = 0x00;
116 PORTC = 0x00;
117 DDRF = 0x00;
118 PORTF = 0x00;
119 break;
120 case 6:
121 DDRD = 0x00;
122 PORTD = 0x00;
123 DDRC = (1<<6);
124 PORTC = 0x00;
125 DDRF = 0x00;
126 PORTF = 0x00;
127 break;
128 case 7:
129 DDRD = 0x00;
130 PORTD = 0x00;
131 DDRC = (1<<7);
132 PORTC = 0x00;
133 DDRF = 0x00;
134 PORTF = 0x00;
135 break;
136 case 8:
137 DDRD = 0x00;
138 PORTD = 0x00;
139 DDRC = 0x00;
140 PORTC = 0x00;
141 DDRF = (1<<7);
142 PORTF = 0x00;
143 break;
144 }
145}
146
147uint8_t read_col(void)
148{
149 return PINB;
150}
151
41int main(void) 152int main(void)
42{ 153{
43 uint8_t b, d, mask, i, reset_idle; 154 uint8_t i, reset_idle;
44 uint8_t b_prev=0xFF, d_prev=0xFF; 155 uint8_t prev_state[MAX_ROW];
156 for (int i=0; i < MAX_ROW; i++) prev_state[i] = 0xFF;
45 157
46 // set for 16 MHz clock 158 // set for 16 MHz clock
47 CPU_PRESCALE(0); 159 CPU_PRESCALE(0);
48 160
49 // Configure all port B and port D pins as inputs with pullup resistors. 161 port_setup();
50 // See the "Using I/O Pins" page for details. 162
51 // http://www.pjrc.com/teensy/pins.html
52 DDRD = 0x00;
53 DDRB = 0x00;
54 PORTB = 0xFF;
55 PORTD = 0xFF;
56 163
57 // Initialize the USB, and then wait for the host to set configuration. 164 // Initialize the USB, and then wait for the host to set configuration.
58 // If the Teensy is powered without a PC connected to the USB port, 165 // If the Teensy is powered without a PC connected to the USB port,
@@ -76,32 +183,40 @@ int main(void)
76 print("All Port B or Port D pins are inputs with pullup resistors.\n"); 183 print("All Port B or Port D pins are inputs with pullup resistors.\n");
77 print("Any connection to ground on Port B or D pins will result in\n"); 184 print("Any connection to ground on Port B or D pins will result in\n");
78 print("keystrokes sent to the PC (and debug messages here).\n"); 185 print("keystrokes sent to the PC (and debug messages here).\n");
186
187 uint8_t col;
188 uint8_t code;
79 while (1) { 189 while (1) {
80 // read all port B and port D pins 190 reset_idle = 0;
81 b = PINB; 191
82 d = PIND; 192 for (uint8_t r=0; r < MAX_ROW; r++) {
83 // check if any pins are low, but were high previously 193 select_row(r);
84 mask = 1; 194
85 reset_idle = 0; 195 // without this read unstable value.
86 for (i=0; i<8; i++) { 196 _delay_us(30);
87 if (((b & mask) == 0) && (b_prev & mask) != 0) { 197
88 //usb_keyboard_press(KEY_B, KEY_SHIFT); 198 col = read_col();
89 //usb_keyboard_press(number_keys[i], 0); 199 if (col != prev_state[r]) {
90 print("Port B, bit "); 200 prev_state[r] = col;
91 phex(i); 201 phex(r);
92 print("\n"); 202 print(": ");
93 reset_idle = 1; 203 pbin(col);
94 } 204 print("\n");
95 if (((d & mask) == 0) && (d_prev & mask) != 0) { 205
96 //usb_keyboard_press(KEY_D, KEY_SHIFT); 206 for (int c = 0; c < 8; c++) {
97 //usb_keyboard_press(number_keys[i], 0); 207 if (col & 1<<c) continue;
98 print("Port D, bit "); 208 code = get_keycode(r, c);
99 phex(i); 209 phex(code);
100 print("\n"); 210 print("\n");
101 reset_idle = 1; 211 usb_keyboard_press(code, 0);
102 } 212 }
103 mask = mask << 1; 213
104 } 214 reset_idle = 1;
215 }
216 }
217
218
219
105 // if any keypresses were detected, reset the idle counter 220 // if any keypresses were detected, reset the idle counter
106 if (reset_idle) { 221 if (reset_idle) {
107 // variables shared with interrupt routines must be 222 // variables shared with interrupt routines must be
@@ -111,11 +226,10 @@ int main(void)
111 idle_count = 0; 226 idle_count = 0;
112 sei(); 227 sei();
113 } 228 }
229
114 // now the current pins will be the previous, and 230 // now the current pins will be the previous, and
115 // wait a short delay so we're not highly sensitive 231 // wait a short delay so we're not highly sensitive
116 // to mechanical "bounce". 232 // to mechanical "bounce".
117 b_prev = b;
118 d_prev = d;
119 _delay_ms(2); 233 _delay_ms(2);
120 } 234 }
121} 235}
diff --git a/print.c b/print.c
index e6aa1fe52..2f0617c64 100644
--- a/print.c
+++ b/print.c
@@ -58,5 +58,9 @@ void phex16(unsigned int i)
58} 58}
59 59
60 60
61 61void pbin(unsigned char c)
62 62{
63 for (int i=7; i>=0; i--) {
64 usb_debug_putchar((c & (1<<i)) ? '1' : '0');
65 }
66}
diff --git a/print.h b/print.h
index 1de618fba..d6bb0aad2 100644
--- a/print.h
+++ b/print.h
@@ -12,5 +12,6 @@
12void print_P(const char *s); 12void print_P(const char *s);
13void phex(unsigned char c); 13void phex(unsigned char c);
14void phex16(unsigned int i); 14void phex16(unsigned int i);
15void pbin(unsigned char c);
15 16
16#endif 17#endif
diff --git a/usbkeycodes.h b/usbkeycodes.h
new file mode 100644
index 000000000..ca2082743
--- /dev/null
+++ b/usbkeycodes.h
@@ -0,0 +1,273 @@
1/* Some modified from Keyboard Upgrade 0.3.0
2 * 2010/08/22
3 */
4/*
5 * Keyboard Upgrade -- Firmware for homebrew computer keyboard controllers.
6 * Copyright (C) 2009 Robert Homann
7 *
8 * Based on RUMP (http://mg8.org/rump/), Copyright (C) 2008 Chris Lee
9 *
10 * Based on c64key (http://symlink.dk/projects/c64key/),
11 * Copyright (C) 2006-2007 Mikkel Holm Olsen
12 *
13 * Based on HID-Test by Christian Starkjohann, Objective Development
14 *
15 * This file is part of the Keyboard Upgrade package.
16 *
17 * This program is free software; you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation; either version 2 of the License, or
20 * (at your option) any later version.
21 *
22 * This program is distributed in the hope that it will be useful, but
23 * WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26 *
27 * You should have received a copy of the GNU General Public License
28 * along with the Keyboard Upgrade package; if not, write to the
29 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
30 * MA 02110-1301 USA
31 */
32
33#ifndef USBKEYCODES_H
34#define USBKEYCODES_H
35
36/*
37 * The USB keycodes are enumerated here - the first part is simply
38 * an enumeration of the allowed scan-codes used for USB HID devices.
39 */
40/*
41 * see 10 Keyboard/Keypad Page(0x07)
42 * http://www.usb.org/developers/devclass_docs/Hut1_12.pdf
43 */
44enum keycodes {
45 KB_NO = 0,
46 KB_ROLL_OVER,
47 KB_POST_FAIL,
48 KB_UNDEFINED,
49 KB_A,
50 KB_B,
51 KB_C,
52 KB_D,
53 KB_E,
54 KB_F,
55 KB_G,
56 KB_H,
57 KB_I,
58 KB_J,
59 KB_K,
60 KB_L,
61 KB_M, /* 0x10 */
62 KB_N,
63 KB_O,
64 KB_P,
65 KB_Q,
66 KB_R,
67 KB_S,
68 KB_T,
69 KB_U,
70 KB_V,
71 KB_W,
72 KB_X,
73 KB_Y,
74 KB_Z,
75 KB_1,
76 KB_2,
77 KB_3, /* 0x20 */
78 KB_4,
79 KB_5,
80 KB_6,
81 KB_7,
82 KB_8,
83 KB_9,
84 KB_0,
85 KB_ENTER,
86 KB_ESCAPE,
87 KB_BSPACE,
88 KB_TAB,
89 KB_SPACE,
90 KB_MINUS,
91 KB_EQUAL,
92 KB_LBRACKET, /* [ */
93 KB_RBRACKET, /* ] */
94 KB_BSLASH, /* \ (and |) */
95 KB_NONUS_HASH, /* Non-US # and ~ */
96 KB_SCOLON, /* ; (and :) */
97 KB_QUOTE, /* ' and " */
98 KB_GRAVE, /* Grave accent and tilde */
99 KB_COMMA, /* , and < */
100 KB_DOT, /* . and > */
101 KB_SLASH, /* / and ? */
102 KB_CAPSLOCK,
103 KB_F1,
104 KB_F2,
105 KB_F3,
106 KB_F4,
107 KB_F5,
108 KB_F6,
109 KB_F7, /* 0x40 */
110 KB_F8,
111 KB_F9,
112 KB_F10,
113 KB_F11,
114 KB_F12,
115 KB_PSCREEN,
116 KB_SCKLOCK,
117 KB_BREAK,
118 KB_INSERT,
119 KB_HOME,
120 KB_PGUP,
121 KB_DELETE,
122 KB_END,
123 KB_PGDOWN,
124 KB_RIGHT,
125 KB_LEFT, /* 0x50 */
126 KB_DOWN,
127 KB_UP,
128 KB_NUMLOCK,
129 KP_SLASH,
130 KP_ASTERISK,
131 KP_MINUS,
132 KP_PLUS,
133 KP_ENTER,
134 KP_1,
135 KP_2,
136 KP_3,
137 KP_4,
138 KP_5,
139 KP_6,
140 KP_7,
141 KP_8, /* 0x60 */
142 KP_9,
143 KP_0,
144 KP_DOT,
145 KB_NONUS_BSLASH, /* Non-US \ and | */
146 KB_APPLICATION,
147 KB_POWER,
148 KP_EQUAL,
149 KB_F13,
150 KB_F14,
151 KB_F15,
152 KB_F16,
153 KB_F17,
154 KB_F18,
155 KB_F19,
156 KB_F20,
157 KB_F21, /* 0x70 */
158 KB_F22,
159 KB_F23,
160 KB_F24,
161 KB_EXECUTE,
162 KB_HELP,
163 KB_MENU,
164 KB_SELECT,
165 KB_STOP,
166 KB_AGAIN,
167 KB_UNDO,
168 KB_CUT,
169 KB_COPY,
170 KB_PASTE,
171 KB_FIND,
172 KB_MUTE,
173 KB_VOLUP, /* 0x80 */
174 KB_VOLDOWN,
175 KB_LOCKING_CAPS, /* locking Caps Lock */
176 KB_LOCKING_NUM, /* locking Num Lock */
177 KB_LOCKING_SCROLL, /* locking Scroll Lock */
178 KP_COMMA,
179 KP_EQUAL_AS400, /* equal sign on AS/400 */
180 KB_INT1,
181 KB_INT2,
182 KB_INT3,
183 KB_INT4,
184 KB_INT5,
185 KB_INT6,
186 KB_INT7,
187 KB_INT8,
188 KB_INT9,
189 KB_LANG1, /* 0x90 */
190 KB_LANG2,
191 KB_LANG3,
192 KB_LANG4,
193 KB_LANG5,
194 KB_LANG6,
195 KB_LANG7,
196 KB_LANG8,
197 KB_LANG9,
198 KB_ALT_ERASE,
199 KB_SYSREQ,
200 KB_CANCEL,
201 KB_CLEAR,
202 KB_PRIOR,
203 KB_RETURN,
204 KB_SEPARATOR,
205 KB_OUT,
206 KB_OPER,
207 KB_CLEAR_AGAIN,
208 KB_CRSEL,
209 KB_EXSEL,
210
211 KP_00 = 0xB0,
212 KP_000,
213 KB_THOUSANDS_SEPARATOR,
214 KB_DECIMAL_SEPARATOR,
215 CURRENCY_UNIT,
216 CURRENCY_SUB_UNIT,
217 KP_LPAREN,
218 KP_RPAREN,
219 KP_LCBRACKET, /* { */
220 KP_RCBRACKET, /* } */
221 KP_TAB,
222 KP_BSPACE,
223 KP_A,
224 KP_B,
225 KP_C,
226 KP_D,
227 KP_E,
228 KP_F,
229 KP_XOR,
230 KP_HAT,
231 KP_PERC,
232 KP_LT,
233 KP_GT,
234 KP_AND,
235 KP_LAZYAND,
236 KP_OR,
237 KP_LAZYOR,
238 KP_COLON,
239 KP_HASH,
240 KP_SPACE,
241 KP_ATMARK,
242 KP_EXCLAMATION,
243 KP_MEM_STORE,
244 KP_MEM_RECALL,
245 KP_MEM_CLEAR,
246 KP_MEM_ADD,
247 KP_MEM_SUB,
248 KP_MEM_MUL,
249 KP_MEM_DIV,
250 KP_PLUS_MINUS,
251 KP_CLEAR,
252 KP_CLEAR_ENTRY,
253 KP_BINARY,
254 KP_OCTAL,
255 KP_DECIMAL,
256 KP_HEXADECIMAL,
257
258 /*
259 * These are NOT standard USB HID - handled specially in decoding,
260 * so they will be mapped to the modifier byte in the USB report.
261 */
262 MOD_START = 0xE0,
263 KB_LCTRL = 0xE0, /* 0x01 */
264 KB_LSHIFT, /* 0x02 */
265 KB_LALT, /* 0x04 */
266 KB_LGUI, /* 0x08 */
267 KB_RCTRL, /* 0x10 */
268 KB_RSHIFT, /* 0x20 */
269 KB_RALT, /* 0x40 */
270 KB_RGUI, /* 0x80 */
271};
272
273#endif /* USBKEYCODES_H */