Switch to using a bit vector for keypress state. This gets us debouncing and

may be interesting for mouse acceleration
pull/18/head
Jesse Vincent 11 years ago
parent 527a1c28f5
commit 17c7a91e58

@ -62,193 +62,216 @@ static const Key keymaps[LAYERS][ROWS][COLS] = {
}; };
void reset_matrix() { boolean key_was_pressed (byte keyState) {
for (int col = 0; col < COLS; col++) { if ( byte((keyState >> 4)) ^ B00001111 ) {
for (int row = 0; row < ROWS; row++) { return false;
matrixState[row][col] <<= 1;
} }
} else {
return true;
}
} }
void send_key_event() { boolean key_was_not_pressed (byte keyState) {
//for every newly pressed button, figure out what logical key it is and send a key down event if ( byte((keyState >> 4)) ^ B00000000 ) {
// for every newly released button, figure out what logical key it is and send a key up event return false;
}
else {
return true;
}
// TODO:switch to sending raw HID packets }
for (int row = 0; row < ROWS; row++) {
for (int col = 0; col < COLS; col++) { boolean key_is_pressed (byte keyState) {
if (key_toggled_on (matrixState[row][col])) {
Keyboard.press(keymaps[current_keymap][row][col].rawKey); if ( byte((keyState << 4)) ^ B11110000 ) {
} return false;
else if (key_toggled_off (matrixState[row][col])) { }
Keyboard.release(keymaps[current_keymap][row][col].rawKey); else {
} return true;
} }
}
} }
boolean key_is_not_pressed (byte keyState) {
boolean key_was_pressed (byte keyState) { if ( byte((keyState << 4)) ^ B00000000 ) {
if ( byte((keyState >> 4)) ^ B00001111 ){ return false;
return false; }
} else {
else { return true;
return true; }
} }
boolean key_toggled_on(byte keyState) {
if (key_is_pressed(keyState) && ( key_was_not_pressed(keyState))) {
return true;
}
else {
return false;
}
} }
boolean key_was_not_pressed (byte keyState) {
if ( byte((keyState >> 4)) ^ B00000000 ){
return false;
}
else {
return true;
}
boolean key_toggled_off(byte keyState) {
if (key_was_pressed(keyState) && key_is_not_pressed(keyState)) {
return true;
}
else {
return false;
}
} }
boolean key_toggled_on(byte keyState) { void reset_matrix() {
if (key_is_pressed(keyState) && ( key_was_not_pressed(keyState))) { for (int col = 0; col < COLS; col++) {
return true; for (int row = 0; row < ROWS; row++) {
} matrixState[row][col] <<= 1;
else { }
return false; }
}
} }
void send_key_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
boolean key_toggled_off(byte keyState) { // TODO:switch to sending raw HID packets
if (key_was_pressed(keyState) && key_is_not_pressed(keyState)) { for (int row = 0; row < ROWS; row++) {
return true;
}
else {
return false;
}
}
for (int col = 0; col < COLS; col++) {
int x = 0;
int y = 0;
if (keymaps[current_keymap][row][col].flags & MOUSE_KEY ) {
if (keymaps[current_keymap][row][col].rawKey & MOUSE_UP) {
y--;
}
if (keymaps[current_keymap][row][col].rawKey & MOUSE_DN) {
y++;
}
if (keymaps[current_keymap][row][col].rawKey & MOUSE_L) {
x--;
}
boolean key_is_pressed (byte keyState) { if (keymaps[current_keymap][row][col].rawKey & MOUSE_R) {
x++;
}
Mouse.move(x, y, 0);
if ( byte((keyState << 4)) ^ B11110000 ){
return false;
}
else {
return true;
}
}
boolean key_is_not_pressed (byte keyState) {
if ( byte((keyState << 4)) ^ B00000000 ){
return false;
}
else {
return true;
}
}
void setup_matrix() {
//set up the row pins as outputs
for (int row = 0; row < ROWS; row++) {
pinMode(rowPins[row], OUTPUT);
digitalWrite(rowPins[row], HIGH);
}
for (int col = 0; col < COLS; col++) { }
pinMode(colPins[col], INPUT);
digitalWrite(colPins[col], HIGH);
//drive em high by default s it seems to be more reliable than driving em low
} if (key_toggled_on (matrixState[row][col])) {
//blank out the matrix. Keyboard.press(keymaps[current_keymap][row][col].rawKey);
for (int col = 0; col < COLS; col++) { }
for (int row = 0; row < ROWS; row++) { else if (key_toggled_off (matrixState[row][col])) {
matrixState[row][col] = 0; Keyboard.release(keymaps[current_keymap][row][col].rawKey);
}
}
} }
}
} }
void scan_matrix() {
//scan the keyboard matrix looking for connections
for (int row = 0; row < ROWS; row++) {
digitalWrite(rowPins[row], LOW);
for (int col = 0; col < COLS; col++) {
//If we see an electrical connection on I->J,
if (digitalRead(colPins[col])) { void setup_matrix() {
matrixState[row][col] |= 0; // noop. just here for clarity //set up the row pins as outputs
} for (int row = 0; row < ROWS; row++) {
else { pinMode(rowPins[row], OUTPUT);
matrixState[row][col] |= 1; // noop. just here for clarity digitalWrite(rowPins[row], HIGH);
} }
// while we're inspecting the electrical matrix, we look
// to see if the key being held is a firmware level
// metakey, so we can act on it, lest we only discover
// that we should be looking at a seconary keymap halfway through the matrix scan
for (int col = 0; col < COLS; col++) {
pinMode(colPins[col], INPUT);
digitalWrite(colPins[col], HIGH);
//drive em high by default s it seems to be more reliable than driving em low
}
//blank out the matrix.
for (int col = 0; col < COLS; col++) {
for (int row = 0; row < ROWS; row++) {
matrixState[row][col] = 0;
}
}
if ( keymaps[current_keymap][row][col].flags & MOMENTARY ) { }
if (key_toggled_on(matrixState[row][col]) ){ void scan_matrix() {
current_keymap++; //scan the Keyboard matrix looking for connections
} for (int row = 0; row < ROWS; row++) {
else if (key_toggled_off(matrixState[row][col]) ){ digitalWrite(rowPins[row], LOW);
current_keymap--; for (int col = 0; col < COLS; col++) {
//If we see an electrical connection on I->J,
if (digitalRead(colPins[col])) {
matrixState[row][col] |= 0; // noop. just here for clarity
}
else {
matrixState[row][col] |= 1; // noop. just here for clarity
}
// while we're inspecting the electrical matrix, we look
// to see if the Key being held is a firmware level
// metakey, so we can act on it, lest we only discover
// that we should be looking at a seconary Keymap halfway through the matrix scan
if ( keymaps[current_keymap][row][col].flags & MOMENTARY ) {
if (key_toggled_on(matrixState[row][col]) ) {
current_keymap++;
}
else if (key_toggled_off(matrixState[row][col]) ) {
current_keymap--;
}
}
// guard against layer overflow
if (current_keymap >= LAYERS) {
current_keymap = 0;
}
else if (current_keymap < 0) {
current_keymap = LAYERS - 1;
}
} }
} digitalWrite(rowPins[row], HIGH);
// guard against layer overflow
if (current_keymap >= LAYERS) {
current_keymap = 0;
}
else if (current_keymap < 0) {
current_keymap = LAYERS - 1;
}
} }
digitalWrite(rowPins[row], HIGH);
}
} }
void report_matrix() { void report_matrix() {
if (counter++ %100 == 0 ) { if (counter++ %100 == 0 ) {
for (int row = 0; row < ROWS; row++) { for (int row = 0; row < ROWS; row++) {
for (int col = 0; col < COLS; col++) { for (int col = 0; col < COLS; col++) {
Serial.print(matrixState[row][col],HEX); Serial.print(matrixState[row][col],HEX);
Serial.print(", "); Serial.print(", ");
} }
Serial.println(""); Serial.println("");
}
Serial.println("");
} }
Serial.println("");
}
} }
void report(int row, int col, boolean value) { void report(int row, int col, boolean value) {
Serial.print("Detected a change on "); Serial.print("Detected a change on ");
Serial.print(col); Serial.print(col);
Serial.print(" "); Serial.print(" ");
Serial.print(row); Serial.print(row);
Serial.print(" to "); Serial.print(" to ");
Serial.print(value); Serial.print(value);
Serial.println("."); Serial.println(".");
} }
void setup() { void setup() {
Keyboard.begin(); Keyboard.begin();
Mouse.begin();
Serial.begin(115200); Serial.begin(115200);
setup_matrix(); setup_matrix();
} }
void loop() { void loop() {
scan_matrix(); scan_matrix();
// report_matrix(); // report_matrix();
send_key_event(); send_key_event();
reset_matrix(); reset_matrix();
} }

Loading…
Cancel
Save