matrix scan & default keymap
parent
20711b59e7
commit
8cfb3712d5
1
Makefile
1
Makefile
|
@ -46,6 +46,7 @@ TARGET = mykey
|
|||
|
||||
# List C source files here. (C dependencies are automatically generated.)
|
||||
SRC = $(TARGET).c \
|
||||
keymap.c \
|
||||
usb_keyboard_debug.c \
|
||||
print.c
|
||||
|
||||
|
|
|
@ -0,0 +1,27 @@
|
|||
#include "keymap.h"
|
||||
|
||||
/*
|
||||
* keymap for modified macway keyboard
|
||||
*/
|
||||
#define MATRIX_ROWS 9
|
||||
#define MATRIX_COLS 8
|
||||
|
||||
const uint8_t Keymap[MATRIX_COLS][MATRIX_ROWS] = {
|
||||
{ KB_LALT, KB_1, KB_2, KB_3, KB_4, KB_7, KB_8, KB_9, KB_0 },
|
||||
{ KB_NO, KB_ESCAPE, KB_RALT, KB_NO, KB_5, KB_6, KB_EQUAL, KB_NO, KB_MINUS },
|
||||
{ KB_BSPACE, KB_TAB, KB_LGUI, KB_RSHIFT, KB_T, KB_Y, KB_RBRACKET, KB_NO, KB_LBRACKET },
|
||||
{ KB_NO, KB_Q, KB_W, KB_E, KB_R, KB_U, KB_I, KB_O, KB_P },
|
||||
{ KB_BSLASH, KB_A, KB_S, KB_D, KB_F, KB_J, KB_K, KB_L, KB_SCOLON },
|
||||
{ KB_NO, KB_LCTRL, KB_NO, KB_UP, KB_G, KB_H, KB_NO, KB_GRAVE, KB_QUOTE },
|
||||
{ KB_ENTER, KB_Z, KB_X, KB_C, KB_V, KB_M, KB_COMMA, KB_DOWN, KB_NO },
|
||||
{ KB_SPACE, KB_DOWN, KB_RIGHT, KB_LEFT, KB_B, KB_N, KB_LSHIFT, KB_NO, KB_SLASH }
|
||||
};
|
||||
|
||||
uint8_t get_keycode(uint8_t row, uint8_t col)
|
||||
{
|
||||
if (row >= MATRIX_ROWS)
|
||||
return KB_NO;
|
||||
if (col >= MATRIX_COLS)
|
||||
return KB_NO;
|
||||
return Keymap[col][row];
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
#ifndef KEYMAP_H
|
||||
#define KEYMAP_H 1
|
||||
|
||||
#include <stdint.h>
|
||||
#include "usbkeycodes.h"
|
||||
|
||||
uint8_t get_keycode(uint8_t row, uint8_t col);
|
||||
|
||||
#endif
|
186
mykey.c
186
mykey.c
|
@ -27,6 +27,7 @@
|
|||
#include <util/delay.h>
|
||||
#include "usb_keyboard_debug.h"
|
||||
#include "print.h"
|
||||
#include "keymap.h"
|
||||
|
||||
#define LED_CONFIG (DDRD |= (1<<6))
|
||||
#define LED_ON (PORTD &= ~(1<<6))
|
||||
|
@ -38,21 +39,127 @@ uint8_t number_keys[10]=
|
|||
|
||||
uint16_t idle_count=0;
|
||||
|
||||
|
||||
|
||||
//
|
||||
// scan matrix
|
||||
//
|
||||
uint8_t MAX_ROW = 9;
|
||||
|
||||
// initialize ports for matrix
|
||||
void port_setup(void)
|
||||
{
|
||||
// Column: input w/pullup
|
||||
DDRB = 0x00;
|
||||
PORTB = 0xFF;
|
||||
|
||||
// Row: Hi-Z(unselected)
|
||||
// PD:0,1,2,3,6,7
|
||||
// PC:6,7
|
||||
// PF:7
|
||||
DDRD = 0x00;
|
||||
PORTD = 0x00;
|
||||
DDRC = 0x00;
|
||||
PORTC = 0x00;
|
||||
DDRF = 0x00;
|
||||
PORTF = 0x00;
|
||||
}
|
||||
|
||||
// select a row of matrix for read
|
||||
void select_row(uint8_t row)
|
||||
{
|
||||
switch (row) {
|
||||
case 0:
|
||||
DDRD = (1<<0);
|
||||
PORTD = 0x00;
|
||||
DDRC = 0x00;
|
||||
PORTC = 0x00;
|
||||
DDRF = 0x00;
|
||||
PORTF = 0x00;
|
||||
break;
|
||||
case 1:
|
||||
DDRD = (1<<1);
|
||||
PORTD = 0x00;
|
||||
DDRC = 0x00;
|
||||
PORTC = 0x00;
|
||||
DDRF = 0x00;
|
||||
PORTF = 0x00;
|
||||
break;
|
||||
case 2:
|
||||
DDRD = (1<<2);
|
||||
PORTD = 0x00;
|
||||
DDRC = 0x00;
|
||||
PORTC = 0x00;
|
||||
DDRF = 0x00;
|
||||
PORTF = 0x00;
|
||||
break;
|
||||
case 3:
|
||||
DDRD = (1<<3);
|
||||
PORTD = 0x00;
|
||||
DDRC = 0x00;
|
||||
PORTC = 0x00;
|
||||
DDRF = 0x00;
|
||||
PORTF = 0x00;
|
||||
break;
|
||||
case 4:
|
||||
DDRD = (1<<6);
|
||||
PORTD = 0x00;
|
||||
DDRC = 0x00;
|
||||
PORTC = 0x00;
|
||||
DDRF = 0x00;
|
||||
PORTF = 0x00;
|
||||
break;
|
||||
case 5:
|
||||
DDRD = (1<<7);
|
||||
PORTD = 0x00;
|
||||
DDRC = 0x00;
|
||||
PORTC = 0x00;
|
||||
DDRF = 0x00;
|
||||
PORTF = 0x00;
|
||||
break;
|
||||
case 6:
|
||||
DDRD = 0x00;
|
||||
PORTD = 0x00;
|
||||
DDRC = (1<<6);
|
||||
PORTC = 0x00;
|
||||
DDRF = 0x00;
|
||||
PORTF = 0x00;
|
||||
break;
|
||||
case 7:
|
||||
DDRD = 0x00;
|
||||
PORTD = 0x00;
|
||||
DDRC = (1<<7);
|
||||
PORTC = 0x00;
|
||||
DDRF = 0x00;
|
||||
PORTF = 0x00;
|
||||
break;
|
||||
case 8:
|
||||
DDRD = 0x00;
|
||||
PORTD = 0x00;
|
||||
DDRC = 0x00;
|
||||
PORTC = 0x00;
|
||||
DDRF = (1<<7);
|
||||
PORTF = 0x00;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
uint8_t read_col(void)
|
||||
{
|
||||
return PINB;
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
uint8_t b, d, mask, i, reset_idle;
|
||||
uint8_t b_prev=0xFF, d_prev=0xFF;
|
||||
uint8_t i, reset_idle;
|
||||
uint8_t prev_state[MAX_ROW];
|
||||
for (int i=0; i < MAX_ROW; i++) prev_state[i] = 0xFF;
|
||||
|
||||
// set for 16 MHz clock
|
||||
CPU_PRESCALE(0);
|
||||
|
||||
// Configure all port B and port D pins as inputs with pullup resistors.
|
||||
// See the "Using I/O Pins" page for details.
|
||||
// http://www.pjrc.com/teensy/pins.html
|
||||
DDRD = 0x00;
|
||||
DDRB = 0x00;
|
||||
PORTB = 0xFF;
|
||||
PORTD = 0xFF;
|
||||
port_setup();
|
||||
|
||||
|
||||
// Initialize the USB, and then wait for the host to set configuration.
|
||||
// If the Teensy is powered without a PC connected to the USB port,
|
||||
|
@ -76,32 +183,40 @@ int main(void)
|
|||
print("All Port B or Port D pins are inputs with pullup resistors.\n");
|
||||
print("Any connection to ground on Port B or D pins will result in\n");
|
||||
print("keystrokes sent to the PC (and debug messages here).\n");
|
||||
|
||||
uint8_t col;
|
||||
uint8_t code;
|
||||
while (1) {
|
||||
// read all port B and port D pins
|
||||
b = PINB;
|
||||
d = PIND;
|
||||
// check if any pins are low, but were high previously
|
||||
mask = 1;
|
||||
reset_idle = 0;
|
||||
for (i=0; i<8; i++) {
|
||||
if (((b & mask) == 0) && (b_prev & mask) != 0) {
|
||||
//usb_keyboard_press(KEY_B, KEY_SHIFT);
|
||||
//usb_keyboard_press(number_keys[i], 0);
|
||||
print("Port B, bit ");
|
||||
phex(i);
|
||||
print("\n");
|
||||
reset_idle = 1;
|
||||
}
|
||||
if (((d & mask) == 0) && (d_prev & mask) != 0) {
|
||||
//usb_keyboard_press(KEY_D, KEY_SHIFT);
|
||||
//usb_keyboard_press(number_keys[i], 0);
|
||||
print("Port D, bit ");
|
||||
phex(i);
|
||||
print("\n");
|
||||
reset_idle = 1;
|
||||
}
|
||||
mask = mask << 1;
|
||||
}
|
||||
reset_idle = 0;
|
||||
|
||||
for (uint8_t r=0; r < MAX_ROW; r++) {
|
||||
select_row(r);
|
||||
|
||||
// without this read unstable value.
|
||||
_delay_us(30);
|
||||
|
||||
col = read_col();
|
||||
if (col != prev_state[r]) {
|
||||
prev_state[r] = col;
|
||||
phex(r);
|
||||
print(": ");
|
||||
pbin(col);
|
||||
print("\n");
|
||||
|
||||
for (int c = 0; c < 8; c++) {
|
||||
if (col & 1<<c) continue;
|
||||
code = get_keycode(r, c);
|
||||
phex(code);
|
||||
print("\n");
|
||||
usb_keyboard_press(code, 0);
|
||||
}
|
||||
|
||||
reset_idle = 1;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
// if any keypresses were detected, reset the idle counter
|
||||
if (reset_idle) {
|
||||
// variables shared with interrupt routines must be
|
||||
|
@ -111,11 +226,10 @@ int main(void)
|
|||
idle_count = 0;
|
||||
sei();
|
||||
}
|
||||
|
||||
// now the current pins will be the previous, and
|
||||
// wait a short delay so we're not highly sensitive
|
||||
// to mechanical "bounce".
|
||||
b_prev = b;
|
||||
d_prev = d;
|
||||
_delay_ms(2);
|
||||
}
|
||||
}
|
||||
|
|
8
print.c
8
print.c
|
@ -58,5 +58,9 @@ void phex16(unsigned int i)
|
|||
}
|
||||
|
||||
|
||||
|
||||
|
||||
void pbin(unsigned char c)
|
||||
{
|
||||
for (int i=7; i>=0; i--) {
|
||||
usb_debug_putchar((c & (1<<i)) ? '1' : '0');
|
||||
}
|
||||
}
|
||||
|
|
1
print.h
1
print.h
|
@ -12,5 +12,6 @@
|
|||
void print_P(const char *s);
|
||||
void phex(unsigned char c);
|
||||
void phex16(unsigned int i);
|
||||
void pbin(unsigned char c);
|
||||
|
||||
#endif
|
||||
|
|
|
@ -0,0 +1,273 @@
|
|||
/* Some modified from Keyboard Upgrade 0.3.0
|
||||
* 2010/08/22
|
||||
*/
|
||||
/*
|
||||
* Keyboard Upgrade -- Firmware for homebrew computer keyboard controllers.
|
||||
* Copyright (C) 2009 Robert Homann
|
||||
*
|
||||
* Based on RUMP (http://mg8.org/rump/), Copyright (C) 2008 Chris Lee
|
||||
*
|
||||
* Based on c64key (http://symlink.dk/projects/c64key/),
|
||||
* Copyright (C) 2006-2007 Mikkel Holm Olsen
|
||||
*
|
||||
* Based on HID-Test by Christian Starkjohann, Objective Development
|
||||
*
|
||||
* This file is part of the Keyboard Upgrade package.
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with the Keyboard Upgrade package; if not, write to the
|
||||
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||||
* MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
#ifndef USBKEYCODES_H
|
||||
#define USBKEYCODES_H
|
||||
|
||||
/*
|
||||
* The USB keycodes are enumerated here - the first part is simply
|
||||
* an enumeration of the allowed scan-codes used for USB HID devices.
|
||||
*/
|
||||
/*
|
||||
* see 10 Keyboard/Keypad Page(0x07)
|
||||
* http://www.usb.org/developers/devclass_docs/Hut1_12.pdf
|
||||
*/
|
||||
enum keycodes {
|
||||
KB_NO = 0,
|
||||
KB_ROLL_OVER,
|
||||
KB_POST_FAIL,
|
||||
KB_UNDEFINED,
|
||||
KB_A,
|
||||
KB_B,
|
||||
KB_C,
|
||||
KB_D,
|
||||
KB_E,
|
||||
KB_F,
|
||||
KB_G,
|
||||
KB_H,
|
||||
KB_I,
|
||||
KB_J,
|
||||
KB_K,
|
||||
KB_L,
|
||||
KB_M, /* 0x10 */
|
||||
KB_N,
|
||||
KB_O,
|
||||
KB_P,
|
||||
KB_Q,
|
||||
KB_R,
|
||||
KB_S,
|
||||
KB_T,
|
||||
KB_U,
|
||||
KB_V,
|
||||
KB_W,
|
||||
KB_X,
|
||||
KB_Y,
|
||||
KB_Z,
|
||||
KB_1,
|
||||
KB_2,
|
||||
KB_3, /* 0x20 */
|
||||
KB_4,
|
||||
KB_5,
|
||||
KB_6,
|
||||
KB_7,
|
||||
KB_8,
|
||||
KB_9,
|
||||
KB_0,
|
||||
KB_ENTER,
|
||||
KB_ESCAPE,
|
||||
KB_BSPACE,
|
||||
KB_TAB,
|
||||
KB_SPACE,
|
||||
KB_MINUS,
|
||||
KB_EQUAL,
|
||||
KB_LBRACKET, /* [ */
|
||||
KB_RBRACKET, /* ] */
|
||||
KB_BSLASH, /* \ (and |) */
|
||||
KB_NONUS_HASH, /* Non-US # and ~ */
|
||||
KB_SCOLON, /* ; (and :) */
|
||||
KB_QUOTE, /* ' and " */
|
||||
KB_GRAVE, /* Grave accent and tilde */
|
||||
KB_COMMA, /* , and < */
|
||||
KB_DOT, /* . and > */
|
||||
KB_SLASH, /* / and ? */
|
||||
KB_CAPSLOCK,
|
||||
KB_F1,
|
||||
KB_F2,
|
||||
KB_F3,
|
||||
KB_F4,
|
||||
KB_F5,
|
||||
KB_F6,
|
||||
KB_F7, /* 0x40 */
|
||||
KB_F8,
|
||||
KB_F9,
|
||||
KB_F10,
|
||||
KB_F11,
|
||||
KB_F12,
|
||||
KB_PSCREEN,
|
||||
KB_SCKLOCK,
|
||||
KB_BREAK,
|
||||
KB_INSERT,
|
||||
KB_HOME,
|
||||
KB_PGUP,
|
||||
KB_DELETE,
|
||||
KB_END,
|
||||
KB_PGDOWN,
|
||||
KB_RIGHT,
|
||||
KB_LEFT, /* 0x50 */
|
||||
KB_DOWN,
|
||||
KB_UP,
|
||||
KB_NUMLOCK,
|
||||
KP_SLASH,
|
||||
KP_ASTERISK,
|
||||
KP_MINUS,
|
||||
KP_PLUS,
|
||||
KP_ENTER,
|
||||
KP_1,
|
||||
KP_2,
|
||||
KP_3,
|
||||
KP_4,
|
||||
KP_5,
|
||||
KP_6,
|
||||
KP_7,
|
||||
KP_8, /* 0x60 */
|
||||
KP_9,
|
||||
KP_0,
|
||||
KP_DOT,
|
||||
KB_NONUS_BSLASH, /* Non-US \ and | */
|
||||
KB_APPLICATION,
|
||||
KB_POWER,
|
||||
KP_EQUAL,
|
||||
KB_F13,
|
||||
KB_F14,
|
||||
KB_F15,
|
||||
KB_F16,
|
||||
KB_F17,
|
||||
KB_F18,
|
||||
KB_F19,
|
||||
KB_F20,
|
||||
KB_F21, /* 0x70 */
|
||||
KB_F22,
|
||||
KB_F23,
|
||||
KB_F24,
|
||||
KB_EXECUTE,
|
||||
KB_HELP,
|
||||
KB_MENU,
|
||||
KB_SELECT,
|
||||
KB_STOP,
|
||||
KB_AGAIN,
|
||||
KB_UNDO,
|
||||
KB_CUT,
|
||||
KB_COPY,
|
||||
KB_PASTE,
|
||||
KB_FIND,
|
||||
KB_MUTE,
|
||||
KB_VOLUP, /* 0x80 */
|
||||
KB_VOLDOWN,
|
||||
KB_LOCKING_CAPS, /* locking Caps Lock */
|
||||
KB_LOCKING_NUM, /* locking Num Lock */
|
||||
KB_LOCKING_SCROLL, /* locking Scroll Lock */
|
||||
KP_COMMA,
|
||||
KP_EQUAL_AS400, /* equal sign on AS/400 */
|
||||
KB_INT1,
|
||||
KB_INT2,
|
||||
KB_INT3,
|
||||
KB_INT4,
|
||||
KB_INT5,
|
||||
KB_INT6,
|
||||
KB_INT7,
|
||||
KB_INT8,
|
||||
KB_INT9,
|
||||
KB_LANG1, /* 0x90 */
|
||||
KB_LANG2,
|
||||
KB_LANG3,
|
||||
KB_LANG4,
|
||||
KB_LANG5,
|
||||
KB_LANG6,
|
||||
KB_LANG7,
|
||||
KB_LANG8,
|
||||
KB_LANG9,
|
||||
KB_ALT_ERASE,
|
||||
KB_SYSREQ,
|
||||
KB_CANCEL,
|
||||
KB_CLEAR,
|
||||
KB_PRIOR,
|
||||
KB_RETURN,
|
||||
KB_SEPARATOR,
|
||||
KB_OUT,
|
||||
KB_OPER,
|
||||
KB_CLEAR_AGAIN,
|
||||
KB_CRSEL,
|
||||
KB_EXSEL,
|
||||
|
||||
KP_00 = 0xB0,
|
||||
KP_000,
|
||||
KB_THOUSANDS_SEPARATOR,
|
||||
KB_DECIMAL_SEPARATOR,
|
||||
CURRENCY_UNIT,
|
||||
CURRENCY_SUB_UNIT,
|
||||
KP_LPAREN,
|
||||
KP_RPAREN,
|
||||
KP_LCBRACKET, /* { */
|
||||
KP_RCBRACKET, /* } */
|
||||
KP_TAB,
|
||||
KP_BSPACE,
|
||||
KP_A,
|
||||
KP_B,
|
||||
KP_C,
|
||||
KP_D,
|
||||
KP_E,
|
||||
KP_F,
|
||||
KP_XOR,
|
||||
KP_HAT,
|
||||
KP_PERC,
|
||||
KP_LT,
|
||||
KP_GT,
|
||||
KP_AND,
|
||||
KP_LAZYAND,
|
||||
KP_OR,
|
||||
KP_LAZYOR,
|
||||
KP_COLON,
|
||||
KP_HASH,
|
||||
KP_SPACE,
|
||||
KP_ATMARK,
|
||||
KP_EXCLAMATION,
|
||||
KP_MEM_STORE,
|
||||
KP_MEM_RECALL,
|
||||
KP_MEM_CLEAR,
|
||||
KP_MEM_ADD,
|
||||
KP_MEM_SUB,
|
||||
KP_MEM_MUL,
|
||||
KP_MEM_DIV,
|
||||
KP_PLUS_MINUS,
|
||||
KP_CLEAR,
|
||||
KP_CLEAR_ENTRY,
|
||||
KP_BINARY,
|
||||
KP_OCTAL,
|
||||
KP_DECIMAL,
|
||||
KP_HEXADECIMAL,
|
||||
|
||||
/*
|
||||
* These are NOT standard USB HID - handled specially in decoding,
|
||||
* so they will be mapped to the modifier byte in the USB report.
|
||||
*/
|
||||
MOD_START = 0xE0,
|
||||
KB_LCTRL = 0xE0, /* 0x01 */
|
||||
KB_LSHIFT, /* 0x02 */
|
||||
KB_LALT, /* 0x04 */
|
||||
KB_LGUI, /* 0x08 */
|
||||
KB_RCTRL, /* 0x10 */
|
||||
KB_RSHIFT, /* 0x20 */
|
||||
KB_RALT, /* 0x40 */
|
||||
KB_RGUI, /* 0x80 */
|
||||
};
|
||||
|
||||
#endif /* USBKEYCODES_H */
|
Loading…
Reference in New Issue