Anybody attempted something like this before?
*EDIT
All done. Here's pics and code.
Imgur album of all the pics: http://imgur.com/a/zK1BN
Main KB Code:
Code: Select all
#include <usb_private.h>
#define KEY_MENU 0x76
const int numLockPin = PIN_D2;
const int capsLockPin = PIN_D1;
const int scrollLockPin = PIN_D0;-
int rowCount = 18;
int colCount = 8;
int rowPins[18] = {9, 10, 11, 12, 13, 14, 15, 16, 17, 20, 23, 24, 25, 26, 40, 41, 42, 45};
int colPins[9] = {18, 19, 21, 22, 38, 39, 43, 44};
int keys[6] = {0, 0, 0, 0, 0, 0};
int modifiers[4] = {0, 0, 0, 0};
void setup() {
resetColumns();
pinMode(numLockPin, OUTPUT);
pinMode(capsLockPin, OUTPUT);
pinMode(scrollLockPin, OUTPUT);
}
void loop() {
scanMatrix();
if (isNumOn()) {
digitalWrite(numLockPin, HIGH);
}
else {
digitalWrite(numLockPin, LOW);
}
if (isCapsOn()) {
digitalWrite(capsLockPin, HIGH);
}
else {
digitalWrite(capsLockPin, LOW);
}
if (isScrollOn()) {
digitalWrite(scrollLockPin, HIGH);
}
else {
digitalWrite(scrollLockPin, LOW);
}
}
int ledkeys(void) {
return int(keyboard_leds);
}
boolean isScrollOn(void) {
return ((ledkeys() & 4) == 4) ? true : false;
}
boolean isCapsOn(void) {
return ((ledkeys() & 2) == 2) ? true : false;
}
boolean isNumOn(void) {
return ((ledkeys() & 1) == 1) ? true : false;
}
void scanMatrix() {
for (int i = 0; i < rowCount; i++) {
int row = rowPins[i];
activateRow(row);
for (int z = 0; z < colCount; z++) {
int col = colPins[z];
int key = getKeyFromCombo(row, col);
if (checkColumn(col)) {
Keyboard.press(key);
}
else {
Keyboard.release(key);
}
}
digitalWrite(row, HIGH);
}
}
void resetColumns() {
for (int i = 0; i < colCount; i++) {
int col = colPins[i];
pinMode(col, INPUT_PULLUP);
}
}
void activateRow(int row) {
pinMode(row, OUTPUT);
digitalWrite(row, LOW);
}
boolean checkColumn(int col) {
boolean returnValue = false;
pinMode(col, INPUT_PULLUP);
if (digitalRead(col) == LOW) {
returnValue = true;
}
else {
returnValue = false;
}
return returnValue;
}
boolean setNextKey(int key) {
boolean isSet = false;
if (keys[0] == 0) {
keys[0] = key;
isSet = true;
}
else if (keys[1] == 0) {
keys[1] = key;
isSet = true;
}
else if (keys[2] == 0) {
keys[2] = key;
isSet = true;
}
else if (keys[3] == 0) {
keys[3] = key;
isSet = true;
}
else if (keys[4] == 0) {
keys[4] = key;
isSet = true;
}
else if (keys[5] == 0) {
keys[5] = key;
isSet = true;
}
return isSet;
}
boolean resetKey(int key) {
boolean isReset = false;
if (keys[0] == key) {
keys[0] = 0;
isReset = true;
}
else if (keys[1] == key) {
keys[1] = 0;
isReset = true;
}
else if (keys[2] == key) {
keys[2] = 0;
isReset = true;
}
else if (keys[3] == key) {
keys[3] = 0;
isReset = true;
}
else if (keys[4] == key) {
keys[4] = 0;
isReset = true;
}
else if (keys[5] == key) {
keys[5] = 0;
isReset = true;
}
return isReset;
}
int getKeyFromCombo(int rowPin, int colPin) {
int key = -1;
switch (rowPin) {
case 9:
switch (colPin) {
case 18:
key = -1;
case 19:
key = KEY_PAUSE;
break;
case 21:
key = KEY_RIGHT_CTRL;
break;
case 22:
key = -1;
break;
case 38:
key = -1;
break;
case 39:
key = KEY_LEFT_CTRL;
break;
case 43:
key = KEY_F5;
break;
case 44:
key = -1;
break;
}
break;
case 10:
switch (colPin) {
case 18:
key = KEY_TAB;
break;
case 19:
key = KEY_Q;
break;
case 21:
key = KEY_Z;
break;
case 22:
key = -1;
break;
case 38:
key = KEY_A;
break;
case 39:
key = KEY_TILDE;
break;
case 43:
key = KEY_1;
break;
case 44:
key = KEY_ESC;
break;
}
break;
case 11:
switch (colPin) {
case 18:
key = KEY_CAPS_LOCK;
break;
case 19:
key = KEY_W;
break;
case 21:
key = KEY_X;
break;
case 22:
key = -1;
break;
case 38:
key = KEY_S;
break;
case 39:
key = KEY_F1;
break;
case 43:
key = KEY_2;
break;
case 44:
key = -1;
break;
}
break;
case 12:
switch (colPin) {
case 18:
key = KEY_F3;
break;
case 19:
key = KEY_E;
break;
case 21:
key = KEY_C;
break;
case 22:
key = -1;
break;
case 38:
key = KEY_D;
break;
case 39:
key = KEY_F12;
break;
case 43:
key = KEY_3;
break;
case 44:
key = KEY_F4;
break;
}
break;
case 13:
switch (colPin) {
case 18:
key = KEY_T;
break;
case 19:
key = KEY_R;
break;
case 21:
key = KEY_V;
break;
case 22:
key = KEY_B;
break;
case 38:
key = KEY_F;
break;
case 39:
key = KEY_5;
break;
case 43:
key = KEY_4;
break;
case 44:
key = KEY_G;
break;
}
break;
case 14:
switch (colPin) {
case 18:
key = KEY_Y;
break;
case 19:
key = KEY_U;
break;
case 21:
key = KEY_M;
break;
case 22:
key = KEY_N;
break;
case 38:
key = KEY_J;
break;
case 39:
key = KEY_6;
break;
case 43:
key = KEY_7;
break;
case 44:
key = KEY_H;
break;
}
break;
case 15:
switch (colPin) {
case 18:
key = KEY_F7;
break;
case 19:
key = KEY_O;
break;
case 21:
key = KEY_PERIOD;
break;
case 22:
key = KEY_MENU; // Menu Key
break;
case 38:
key = KEY_L;
break;
case 39:
key = KEY_F8;
break;
case 43:
key = KEY_9;
break;
case 44:
key = -1;
break;
}
break;
case 16:
switch (colPin) {
case 18:
key = -1;
break;
case 19:
key = KEY_SCROLL_LOCK;
break;
case 21:
key = -1;
break;
case 22:
key = KEY_RIGHT_ALT;
break;
case 38:
key = -1;
break;
case 39:
key = -1;
break;
case 43:
key = KEY_PRINTSCREEN;
break;
case 44:
key = KEY_LEFT_ALT;
break;
}
break;
case 17:
switch (colPin) {
case 18:
key = KEY_RIGHT_BRACE;
break;
case 19:
key = KEY_I;
break;
case 21:
key = KEY_COMMA;
break;
case 22:
key = -1;
break;
case 38:
key = KEY_K;
break;
case 39:
key = KEY_EQUAL;
break;
case 43:
key = KEY_8;
break;
case 44:
key = KEY_F6;
break;
}
break;
case 20:
switch (colPin) {
case 18:
key = KEYPAD_6;
break;
case 19:
key = KEYPAD_9;
break;
case 21:
key = KEYPAD_ASTERIX;
break;
case 22:
key = KEYPAD_MINUS;
break;
case 38:
key = KEYPAD_3;
break;
case 39:
key = KEY_PAGE_UP;
break;
case 43:
key = KEY_PAGE_DOWN;
break;
case 44:
key = KEYPAD_PERIOD;
break;
}
break;
case 23:
switch (colPin) {
case 18:
key = -1;
break;
case 19:
key = -1;
break;
case 21:
key = -1;
break;
case 22:
key = -1;
break;
case 38:
key = KEY_RIGHT_GUI;
break;
case 39:
key = -1;
break;
case 43:
key = -1;
break;
case 44:
key = -1;
break;
}
break;
case 24:
switch (colPin) {
case 18:
key = KEY_LEFT_GUI;
break;
case 19:
key = -1;
break;
case 21:
key = -1;
break;
case 22:
key = -1;
break;
case 38:
key = -1;
break;
case 39:
key = -1;
break;
case 43:
key = -1;
break;
case 44:
key = -1;
break;
}
break;
case 25:
switch (colPin) {
case 18:
key = KEY_LEFT_SHIFT;
break;
case 19:
key = -1;
break;
case 21:
key = -1;
break;
case 22:
key = -1;
break;
case 38:
key = KEY_RIGHT_SHIFT;
break;
case 39:
key = -1;
break;
case 43:
key = -1;
break;
case 44:
key = -1;
break;
}
break;
case 26:
switch (colPin) {
case 18:
key = -1;
break;
case 19:
key = KEYPAD_PLUS;
break;
case 21:
key = -1;
break;
case 22:
key = KEY_LEFT;
break;
case 38:
key = KEYPAD_ENTER;
break;
case 39:
key = KEY_HOME;
break;
case 43:
key = KEY_END;
break;
case 44:
key = KEY_UP;
break;
}
break;
case 40:
switch (colPin) {
case 18:
key = KEYPAD_4;
break;
case 19:
key = KEYPAD_7;
break;
case 21:
key = KEY_NUM_LOCK;
break;
case 22:
key = KEY_DOWN;
break;
case 38:
key = KEYPAD_1;
break;
case 39:
key = KEY_DELETE;
break;
case 43:
key = -1;
break;
case 44:
key = KEY_SPACE;
break;
}
break;
case 41:
switch (colPin) {
case 18:
key = KEY_LEFT_BRACE;
break;
case 19:
key = KEY_P;
break;
case 21:
key = -1;
break;
case 22:
key = KEY_SLASH;
break;
case 38:
key = KEY_SEMICOLON;
break;
case 39:
key = KEY_MINUS;
break;
case 43:
key = KEY_0;
break;
case 44:
key = KEY_QUOTE;
break;
}
break;
case 42:
switch (colPin) {
case 18:
key = KEYPAD_5;
break;
case 19:
key = KEYPAD_8;
break;
case 21:
key = KEYPAD_SLASH;
break;
case 22:
key = KEY_RIGHT;
break;
case 38:
key = KEYPAD_2;
break;
case 39:
key = KEY_INSERT;
break;
case 43:
key = -1;
break;
case 44:
key = KEYPAD_0;
break;
}
break;
case 45:
switch (colPin) {
case 18:
key = KEY_BACKSPACE;
break;
case 19:
key = -1;
break;
case 21:
key = KEY_ENTER;
break;
case 22:
key = KEY_F12;
break;
case 38:
key = KEY_BACKSLASH;
break;
case 39:
key = KEY_F9;
break;
case 43:
key = KEY_F10;
break;
case 44:
key = KEY_F11;
break;
}
break;
}
return key;
}
Code: Select all
/*
*** INSTRUCTIONS ***
You need to change the pins array and the pinCount int.
Just follow the comments for each of those fields.
Upload this sucker to your teensy, open a text editor,
and then start pushing buttons. You need to keep track of
what row/column combos show up for which button you are currently pressing.
The format is (row#/column#).
*/
// Array of all physical pin numbers on your particular Teensy
int pins[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26,
27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38,
39, 40, 41, 42, 43, 44, 45};
// The number of physical pins on your particular Teensy
int pinCount = 45;
void setup() {
resetPins();
}
void loop() {
scanMatrix();
}
void resetPins() {
for (int i = 0; i < pinCount; i++) {
pinMode(pins[i], INPUT_PULLUP);
}
}
void activateRow(int row) {
pinMode(row, OUTPUT);
digitalWrite(row, LOW);
}
boolean checkColumn(int col) {
boolean returnValue = false;
pinMode(col, INPUT_PULLUP);
if (digitalRead(col) == LOW) {
returnValue = true;
}
else {
returnValue = false;
}
return returnValue;
}
void scanMatrix() {
for (int i = 0; i < pinCount; i++) {
int row = pins[i];
for (int z = 0; z < pinCount; z++) {
int col = pins[z];
resetPins();
activateRow(row);
if (row != col && checkColumn(col)) {
printCombo(row, col);
}
}
}
}
void printCombo(int rowPin, int colPin) {
Keyboard.print("(");
Keyboard.print(rowPin);
Keyboard.print(",");
Keyboard.print(colPin);
Keyboard.print(")");
}