pull/18/head
Jesse Vincent 11 years ago
parent f35aad770b
commit 7f2b3dfa15

@ -33,44 +33,44 @@
/* These are defined by the platform /* These are defined by the platform
#define KEY_LEFT_CTRL 0x80 #define KEY_LEFT_CTRL 0x80
#define KEY_LEFT_SHIFT 0x81 #define KEY_LEFT_SHIFT 0x81
#define KEY_LEFT_ALT 0x82 #define KEY_LEFT_ALT 0x82
#define KEY_LEFT_GUI 0x83 #define KEY_LEFT_GUI 0x83
#define KEY_RIGHT_CTRL 0x84 #define KEY_RIGHT_CTRL 0x84
#define KEY_RIGHT_SHIFT 0x85 #define KEY_RIGHT_SHIFT 0x85
#define KEY_RIGHT_ALT 0x86 #define KEY_RIGHT_ALT 0x86
#define KEY_RIGHT_GUI 0x87 #define KEY_RIGHT_GUI 0x87
#define KEY_UP_ARROW 0xDA #define KEY_UP_ARROW 0xDA
#define KEY_DOWN_ARROW 0xD9 #define KEY_DOWN_ARROW 0xD9
#define KEY_LEFT_ARROW 0xD8 #define KEY_LEFT_ARROW 0xD8
#define KEY_RIGHT_ARROW 0xD7 #define KEY_RIGHT_ARROW 0xD7
#define KEY_BACKSPACE 0xB2 #define KEY_BACKSPACE 0xB2
#define KEY_TAB 0xB3 #define KEY_TAB 0xB3
#define KEY_RETURN 0xB0 #define KEY_RETURN 0xB0
#define KEY_ESC 0xB1 #define KEY_ESC 0xB1
#define KEY_INSERT 0xD1 #define KEY_INSERT 0xD1
#define KEY_DELETE 0xD4 #define KEY_DELETE 0xD4
#define KEY_PAGE_UP 0xD3 #define KEY_PAGE_UP 0xD3
#define KEY_PAGE_DOWN 0xD6 #define KEY_PAGE_DOWN 0xD6
#define KEY_HOME 0xD2 #define KEY_HOME 0xD2
#define KEY_END 0xD5 #define KEY_END 0xD5
#define KEY_CAPS_LOCK 0xC1 #define KEY_CAPS_LOCK 0xC1
#define KEY_F1 0xC2 #define KEY_F1 0xC2
#define KEY_F2 0xC3 #define KEY_F2 0xC3
#define KEY_F3 0xC4 #define KEY_F3 0xC4
#define KEY_F4 0xC5 #define KEY_F4 0xC5
#define KEY_F5 0xC6 #define KEY_F5 0xC6
#define KEY_F6 0xC7 #define KEY_F6 0xC7
#define KEY_F7 0xC8 #define KEY_F7 0xC8
#define KEY_F8 0xC9 #define KEY_F8 0xC9
#define KEY_F9 0xCA #define KEY_F9 0xCA
#define KEY_F10 0xCB #define KEY_F10 0xCB
#define KEY_F11 0xCC #define KEY_F11 0xCC
#define KEY_F12 0xCD #define KEY_F12 0xCD
*/ */
#define KEY_0 '0' #define KEY_0 '0'
@ -129,7 +129,7 @@ static const int ROWS = 5;
static int colPins[COLS] = { static int colPins[COLS] = {
16,0,1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 16,0,1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12
}; };
static int rowPins[ROWS] = { static int rowPins[ROWS] = {
A2, A3, A4,A5,15 A2, A3, A4,A5,15
@ -153,24 +153,24 @@ void
setup() setup()
{ {
Serial.begin(115200); Serial.begin(115200);
setup_matrix(); setup_matrix();
init_keymap(); init_keymap();
Keyboard.begin(); Keyboard.begin();
//set up the col pins as inputs //set up the col pins as inputs
// initialize the previous button matrix // initialize the previous button matrix
// initialize the current button matrix // initialize the current button matrix
} }
void void
loop() loop()
{ {
scan_matrix(); scan_matrix();
send_key_event(); send_key_event();
reset_matrix(); reset_matrix();
} }
@ -179,25 +179,25 @@ loop()
void void
reset_matrix() reset_matrix()
{ {
//TODO:be smarter and just move pointers around //TODO:be smarter and just move pointers around
for (int col = 0; col < COLS; col++) { for (int col = 0; col < COLS; col++) {
for (int row = 0; row < ROWS; row++) { for (int row = 0; row < ROWS; row++) {
previousMatrix[col][row] = currentMatrix[col][row]; previousMatrix[col][row] = currentMatrix[col][row];
currentMatrix[col][row] = 0; currentMatrix[col][row] = 0;
} }
} }
} }
void void
send_key_event() send_key_event()
{ {
//for every newly pressed button, figure out what logical key it is and send a key down event //for every newly pressed button, figure out what logical key it is and send a key down event
// for every newly released button, figure out what logical key it is and send a key up event // for every newly released button, figure out what logical key it is and send a key up event
// TODO:switch to sending raw HID packets // TODO:switch to sending raw HID packets
// set last_matrix to matrix and clear matrix // set last_matrix to matrix and clear matrix
} }
@ -205,26 +205,26 @@ send_key_event()
void void
setup_matrix() setup_matrix()
{ {
//set up the row pins as outputs //set up the row pins as outputs
for (int row = 0; row < ROWS; row++) { for (int row = 0; row < ROWS; row++) {
pinMode(rowPins[row], OUTPUT); pinMode(rowPins[row], OUTPUT);
digitalWrite(rowPins[row], HIGH); digitalWrite(rowPins[row], HIGH);
} }
for (int col = 0; col < COLS; col++) { for (int col = 0; col < COLS; col++) {
pinMode(colPins[col], INPUT); pinMode(colPins[col], INPUT);
digitalWrite(colPins[col], HIGH); digitalWrite(colPins[col], HIGH);
//drive em high by default s it seems to be more reliable than driving em low //drive em high by default s it seems to be more reliable than driving em low
} }
//blank out the matrix.blank out the previous version of the matrix //blank out the matrix.blank out the previous version of the matrix
for (int col = 0; col < COLS; col++) { for (int col = 0; col < COLS; col++) {
for (int row = 0; row < ROWS; row++) { for (int row = 0; row < ROWS; row++) {
previousMatrix[col][col] = 0; previousMatrix[col][col] = 0;
currentMatrix[col][row] = 0; currentMatrix[col][row] = 0;
} }
} }
} }
@ -232,112 +232,114 @@ void
scan_matrix() scan_matrix()
{ {
//scan the keyboard matrix looking for connections //scan the keyboard matrix looking for connections
for (int row = 0; row < ROWS; row++) { for (int row = 0; row < ROWS; row++) {
digitalWrite(rowPins[row], LOW); digitalWrite(rowPins[row], LOW);
for (int col = 0; col < COLS; col++) { for (int col = 0; col < COLS; col++) {
//If we see an electrical connection on I->J, //If we see an electrical connection on I->J,
int value = !digitalRead(colPins[col]); int value = !digitalRead(colPins[col]);
if (previousMatrix[col][row] != value) { if (previousMatrix[col][row] != value) {
if (value) { if (value) {
Keyboard.press(keymap[row][col]); Keyboard.press(keymap[row][col]);
} else { }
Keyboard.release(keymap[row][col]); else {
} Keyboard.release(keymap[row][col]);
}
Serial.print("Detected a change on ");
Serial.print(col); Serial.print("Detected a change on ");
Serial.print(" "); Serial.print(col);
Serial.print(row); Serial.print(" ");
Serial.print(" to "); Serial.print(row);
Serial.print(value); Serial.print(" to ");
Serial.println("."); Serial.print(value);
} Serial.println(".");
currentMatrix[col][row] = value; }
currentMatrix[col][row] = value;
}
digitalWrite(rowPins[row], HIGH); }
digitalWrite(rowPins[row], HIGH);
}
}
} }
void init_keymap() { void init_keymap() {
keymap[ROW_A][COL_1] = NO_KEY; keymap[ROW_A][COL_1] = NO_KEY;
keymap[ROW_A][COL_2] = KEY_1; keymap[ROW_A][COL_2] = KEY_1;
keymap[ROW_A][COL_3] = KEY_2; keymap[ROW_A][COL_3] = KEY_2;
keymap[ROW_A][COL_4] = KEY_3; keymap[ROW_A][COL_4] = KEY_3;
keymap[ROW_A][COL_5] = KEY_4; keymap[ROW_A][COL_5] = KEY_4;
keymap[ROW_A][COL_6] = KEY_5; keymap[ROW_A][COL_6] = KEY_5;
keymap[ROW_A][COL_7] = NO_KEY; keymap[ROW_A][COL_7] = NO_KEY;
keymap[ROW_A][COL_8] = NO_KEY; keymap[ROW_A][COL_8] = NO_KEY;
keymap[ROW_A][COL_9] = KEY_6; keymap[ROW_A][COL_9] = KEY_6;
keymap[ROW_A][COL_10] = KEY_7; keymap[ROW_A][COL_10] = KEY_7;
keymap[ROW_A][COL_11] = KEY_8; keymap[ROW_A][COL_11] = KEY_8;
keymap[ROW_A][COL_12] = KEY_9; keymap[ROW_A][COL_12] = KEY_9;
keymap[ROW_A][COL_13] = KEY_0; keymap[ROW_A][COL_13] = KEY_0;
keymap[ROW_A][COL_14] = NO_KEY; keymap[ROW_A][COL_14] = NO_KEY;
keymap[ROW_B][COL_1] = KEY_BACKTICK; keymap[ROW_B][COL_1] = KEY_BACKTICK;
keymap[ROW_B][COL_2] = KEY_Q; keymap[ROW_B][COL_2] = KEY_Q;
keymap[ROW_B][COL_3] = KEY_W; keymap[ROW_B][COL_3] = KEY_W;
keymap[ROW_B][COL_4] = KEY_E; keymap[ROW_B][COL_4] = KEY_E;
keymap[ROW_B][COL_5] = KEY_R; keymap[ROW_B][COL_5] = KEY_R;
keymap[ROW_B][COL_6] = KEY_T; keymap[ROW_B][COL_6] = KEY_T;
keymap[ROW_B][COL_7] = NO_KEY; keymap[ROW_B][COL_7] = NO_KEY;
keymap[ROW_B][COL_8] = NO_KEY; keymap[ROW_B][COL_8] = NO_KEY;
keymap[ROW_B][COL_9] = KEY_Y; keymap[ROW_B][COL_9] = KEY_Y;
keymap[ROW_B][COL_10] = KEY_U; keymap[ROW_B][COL_10] = KEY_U;
keymap[ROW_B][COL_11] = KEY_I; keymap[ROW_B][COL_11] = KEY_I;
keymap[ROW_B][COL_12] = KEY_O; keymap[ROW_B][COL_12] = KEY_O;
keymap[ROW_B][COL_13] = KEY_P; keymap[ROW_B][COL_13] = KEY_P;
keymap[ROW_B][COL_14] = KEY_EQUALS; keymap[ROW_B][COL_14] = KEY_EQUALS;
keymap[ROW_C][COL_1] = KEY_PAGE_UP; keymap[ROW_C][COL_1] = KEY_PAGE_UP;
keymap[ROW_C][COL_2] = KEY_A; keymap[ROW_C][COL_2] = KEY_A;
keymap[ROW_C][COL_3] = KEY_S; keymap[ROW_C][COL_3] = KEY_S;
keymap[ROW_C][COL_4] = KEY_D; keymap[ROW_C][COL_4] = KEY_D;
keymap[ROW_C][COL_5] = KEY_F; keymap[ROW_C][COL_5] = KEY_F;
keymap[ROW_C][COL_6] = KEY_G; keymap[ROW_C][COL_6] = KEY_G;
keymap[ROW_C][COL_7] = KEY_TAB; keymap[ROW_C][COL_7] = KEY_TAB;
keymap[ROW_C][COL_8] = KEY_RETURN; keymap[ROW_C][COL_8] = KEY_RETURN;
keymap[ROW_C][COL_9] = KEY_H; keymap[ROW_C][COL_9] = KEY_H;
keymap[ROW_C][COL_10] = KEY_J; keymap[ROW_C][COL_10] = KEY_J;
keymap[ROW_C][COL_11] = KEY_K; keymap[ROW_C][COL_11] = KEY_K;
keymap[ROW_C][COL_12] = KEY_L; keymap[ROW_C][COL_12] = KEY_L;
keymap[ROW_C][COL_13] = KEY_SEMICOLON; keymap[ROW_C][COL_13] = KEY_SEMICOLON;
keymap[ROW_C][COL_14] = KEY_QUOTE; keymap[ROW_C][COL_14] = KEY_QUOTE;
keymap[ROW_D][COL_1] = KEY_PAGE_DOWN; keymap[ROW_D][COL_1] = KEY_PAGE_DOWN;
keymap[ROW_D][COL_2] = KEY_Z; keymap[ROW_D][COL_2] = KEY_Z;
keymap[ROW_D][COL_3] = KEY_X; keymap[ROW_D][COL_3] = KEY_X;
keymap[ROW_D][COL_4] = KEY_C; keymap[ROW_D][COL_4] = KEY_C;
keymap[ROW_D][COL_5] = KEY_V; keymap[ROW_D][COL_5] = KEY_V;
keymap[ROW_D][COL_6] = KEY_B; keymap[ROW_D][COL_6] = KEY_B;
keymap[ROW_D][COL_7] = KEY_ESC; keymap[ROW_D][COL_7] = KEY_ESC;
keymap[ROW_D][COL_8] = KEY_RETURN; keymap[ROW_D][COL_8] = KEY_RETURN;
keymap[ROW_D][COL_9] = KEY_N; keymap[ROW_D][COL_9] = KEY_N;
keymap[ROW_D][COL_10] = KEY_M; keymap[ROW_D][COL_10] = KEY_M;
keymap[ROW_D][COL_11] = KEY_COMMA; keymap[ROW_D][COL_11] = KEY_COMMA;
keymap[ROW_D][COL_12] = KEY_PERIOD; keymap[ROW_D][COL_12] = KEY_PERIOD;
keymap[ROW_D][COL_13] = KEY_SLASH; keymap[ROW_D][COL_13] = KEY_SLASH;
keymap[ROW_D][COL_14] = KEY_MINUS; keymap[ROW_D][COL_14] = KEY_MINUS;
keymap[ROW_E][COL_1] = KEY_LEFT_GUI; keymap[ROW_E][COL_1] = KEY_LEFT_GUI;
keymap[ROW_E][COL_2] = KEY_BACKSPACE; keymap[ROW_E][COL_2] = KEY_BACKSPACE;
keymap[ROW_E][COL_3] = KEY_LEFT_SHIFT; keymap[ROW_E][COL_3] = KEY_LEFT_SHIFT;
keymap[ROW_E][COL_4] = KEY_LEFT_CTRL; keymap[ROW_E][COL_4] = KEY_LEFT_CTRL;
keymap[ROW_E][COL_5] = KEY_LEFT_FN2; keymap[ROW_E][COL_5] = KEY_LEFT_FN2;
keymap[ROW_E][COL_6] = NO_KEY; keymap[ROW_E][COL_6] = NO_KEY;
keymap[ROW_E][COL_7] = NO_KEY; keymap[ROW_E][COL_7] = NO_KEY;
keymap[ROW_E][COL_8] = NO_KEY; keymap[ROW_E][COL_8] = NO_KEY;
keymap[ROW_E][COL_9] = NO_KEY; keymap[ROW_E][COL_9] = NO_KEY;
keymap[ROW_E][COL_10] = KEY_RIGHT_FN2; keymap[ROW_E][COL_10] = KEY_RIGHT_FN2;
keymap[ROW_E][COL_11] = KEY_RIGHT_CTRL; keymap[ROW_E][COL_11] = KEY_RIGHT_CTRL;
keymap[ROW_E][COL_12] = KEY_RIGHT_SHIFT; keymap[ROW_E][COL_12] = KEY_RIGHT_SHIFT;
keymap[ROW_E][COL_13] = KEY_SPACE; keymap[ROW_E][COL_13] = KEY_SPACE;
keymap[ROW_E][COL_14] = KEY_RIGHT_ALT; keymap[ROW_E][COL_14] = KEY_RIGHT_ALT;
} }

Loading…
Cancel
Save