pull/18/head
Jesse Vincent 9 years ago
parent 04ec58cced
commit fbb5e1e163

@ -10,8 +10,9 @@
#ifdef SYMMETRIC60 #ifdef SYMMETRIC60
#define ROWS 5 #define ROWS 5
#define COLS 14 #define COLS 14
static const byte colPins[COLS] = { static const byte colPins[COLS] = {
A0, 3,13, 5, 10, 9, 8, 6, 12, 4, 11, 1, 0, 2 }; A0, 3,13, 5, 10, 9, 8, 6, 12, 4, 11, 1, 0, 2
};
static const byte rowPins[ROWS] = { A5,A4,A3,A2,A1}; static const byte rowPins[ROWS] = { A5,A4,A3,A2,A1};
#define LED_DATA_PIN 7 #define LED_DATA_PIN 7
@ -28,11 +29,11 @@ int RIGHT_ROWS=4;
int LEFT_COLS=8; int LEFT_COLS=8;
int LEFT_ROWS=4; int LEFT_ROWS=4;
int left_colpins[]={7,6,5,4,3,2,1,0}; int left_colpins[]= {7,6,5,4,3,2,1,0};
int left_rowpins[]={8,9,10,11}; int left_rowpins[]= {8,9,10,11};
int right_colpins[]={0,1,2,3,4,5,6,7}; int right_colpins[]= {0,1,2,3,4,5,6,7};
int right_rowpins[]={8,9,10,11}; int right_rowpins[]= {8,9,10,11};

@ -53,7 +53,7 @@ bool commandMode;
bool commandPromptPrinted; bool commandPromptPrinted;
// Console related // Console related
void process_command_buffer(); void process_command_buffer();
// EEPROM related // EEPROM related

@ -33,122 +33,120 @@ int left_initted = 0;
#define TS(X) //Serial.print(micros() );Serial.print("\t");Serial.println(X); #define TS(X) //Serial.print(micros() );Serial.print("\t");Serial.println(X);
void setup_matrix() void setup_matrix() {
{ reset_key_report();
reset_key_report(); //blank out the matrix.
//blank out the matrix. for (byte col = 0; col < COLS; col++) {
for (byte col = 0; col < COLS; col++) { for (byte row = 0; row < ROWS; row++) {
for (byte row = 0; row < ROWS; row++) { matrixState[row][col] = 0;
matrixState[row][col] = 0; }
} }
}
} }
void set_keymap(Key keymapEntry, byte matrixStateEntry) { void set_keymap(Key keymapEntry, byte matrixStateEntry) {
if (keymapEntry.flags & SWITCH_TO_KEYMAP) { if (keymapEntry.flags & SWITCH_TO_KEYMAP) {
// this logic sucks. there is a better way TODO this // this logic sucks. there is a better way TODO this
if (! (keymapEntry.flags ^ ( MOMENTARY | SWITCH_TO_KEYMAP))) { if (! (keymapEntry.flags ^ ( MOMENTARY | SWITCH_TO_KEYMAP))) {
if (key_toggled_on(matrixStateEntry)) { if (key_toggled_on(matrixStateEntry)) {
if ( keymapEntry.rawKey == KEYMAP_NEXT) { if ( keymapEntry.rawKey == KEYMAP_NEXT) {
temporary_keymap++; temporary_keymap++;
} else if ( keymapEntry.rawKey == KEYMAP_PREVIOUS) { } else if ( keymapEntry.rawKey == KEYMAP_PREVIOUS) {
temporary_keymap--; temporary_keymap--;
} else { } else {
temporary_keymap = keymapEntry.rawKey; temporary_keymap = keymapEntry.rawKey;
} }
} }
if (key_toggled_off(matrixStateEntry)) { if (key_toggled_off(matrixStateEntry)) {
temporary_keymap = primary_keymap; temporary_keymap = primary_keymap;
} }
} else if (! (keymapEntry.flags ^ ( SWITCH_TO_KEYMAP))) { } else if (! (keymapEntry.flags ^ ( SWITCH_TO_KEYMAP))) {
// switch keymap and stay there // switch keymap and stay there
if (key_toggled_on(matrixStateEntry)) { if (key_toggled_on(matrixStateEntry)) {
temporary_keymap = primary_keymap = keymapEntry.rawKey; temporary_keymap = primary_keymap = keymapEntry.rawKey;
save_primary_keymap(primary_keymap); save_primary_keymap(primary_keymap);
#ifdef DEBUG_SERIAL #ifdef DEBUG_SERIAL
Serial.print("keymap is now:"); Serial.print("keymap is now:");
Serial.print(temporary_keymap); Serial.print(temporary_keymap);
#endif #endif
} }
}
} }
}
} }
void scan_matrix() void scan_matrix() {
{ x = 0;
x = 0; y = 0;
y = 0; //scan the Keyboard matrix looking for connections
//scan the Keyboard matrix looking for connections for (byte row = 0; row < LEFT_ROWS; row++) {
for (byte row = 0; row < LEFT_ROWS; row++) { TS("Scanning row ")
TS("Scanning row ") if (left_initted) {
if (left_initted) { leftsx1509.updatePinState(left_rowpins[row], LOW);
leftsx1509.updatePinState(left_rowpins[row], LOW); leftsx1509.sendPinStates();
leftsx1509.sendPinStates(); leftsx1509.fetchPinStates();
leftsx1509.fetchPinStates(); }
} if (right_initted) {
if (right_initted) { rightsx1509.updatePinState(right_rowpins[row], LOW);
rightsx1509.updatePinState(right_rowpins[row], LOW); rightsx1509.sendPinStates();
rightsx1509.sendPinStates(); rightsx1509.fetchPinStates();
rightsx1509.fetchPinStates();
} }
for (byte col = 0; col < LEFT_COLS; col++) { for (byte col = 0; col < LEFT_COLS; col++) {
TS("Scanning col") TS("Scanning col")
//If we see an electrical connection on I->J, //If we see an electrical connection on I->J,
matrixState[row][col] <<= 1; matrixState[row][col] <<= 1;
matrixState[row][(COLS - 1) - col] <<= 1; matrixState[row][(COLS - 1) - col] <<= 1;
TS("Reading left pin") TS("Reading left pin")
if (left_initted && leftsx1509.readPrefetchedPin(left_colpins[col])) { if (left_initted && leftsx1509.readPrefetchedPin(left_colpins[col])) {
matrixState[row][col] |= 0; matrixState[row][col] |= 0;
} else { } else {
matrixState[row][col] |= 1; matrixState[row][col] |= 1;
} }
TS("Reading right pin") TS("Reading right pin")
if (right_initted && rightsx1509.readPrefetchedPin(right_colpins[col])) { if (right_initted && rightsx1509.readPrefetchedPin(right_colpins[col])) {
matrixState[row][(COLS - 1) - col] |= 0; matrixState[row][(COLS - 1) - col] |= 0;
} else { } else {
matrixState[row][(COLS - 1) - col] |= 1; matrixState[row][(COLS - 1) - col] |= 1;
} }
// while we're inspecting the electrical matrix, we look // while we're inspecting the electrical matrix, we look
// to see if the Key being held is a firmware level // to see if the Key being held is a firmware level
// metakey, so we can act on it, lest we only discover // metakey, so we can act on it, lest we only discover
// that we should be looking at a seconary Keymap halfway // that we should be looking at a seconary Keymap halfway
// through the matrix scan // through the matrix scan
TS("calling send_key_event") TS("calling send_key_event")
send_key_event(row, col); send_key_event(row, col);
if (right_initted) if (right_initted)
send_key_event(row, (COLS - 1) - col); send_key_event(row, (COLS - 1) - col);
}
TS("clearing output pins")
if (left_initted)
leftsx1509.updatePinState(left_rowpins[row], HIGH);
if (right_initted)
rightsx1509.updatePinState(right_rowpins[row], HIGH);
} }
TS("clearing output pins") TS("Releasing keys not being pressed")
if (left_initted) release_keys_not_being_pressed();
leftsx1509.updatePinState(left_rowpins[row], HIGH); TS("Sending key report");
if (right_initted) Keyboard.sendReport();
rightsx1509.updatePinState(right_rowpins[row], HIGH); TS("clearing internal key report")
} reset_key_report();
TS("Releasing keys not being pressed") handle_mouse_movement(x, y);
release_keys_not_being_pressed();
TS("Sending key report");
Keyboard.sendReport();
TS("clearing internal key report")
reset_key_report();
handle_mouse_movement(x, y);
} }
// Command mode // Command mode
@ -156,161 +154,157 @@ void scan_matrix()
void command_reboot_bootloader() { void command_reboot_bootloader() {
Keyboard.println("Rebooting to bootloader"); Keyboard.println("Rebooting to bootloader");
Serial.end(); Serial.end();
// Set the magic bits to get a Caterina-based device // Set the magic bits to get a Caterina-based device
// to reboot into the bootloader and stay there, rather // to reboot into the bootloader and stay there, rather
// than run move onward // than run move onward
// //
// These values are the same as those defined in // These values are the same as those defined in
// Caterina.c // Caterina.c
uint16_t bootKey = 0x7777; uint16_t bootKey = 0x7777;
uint16_t *const bootKeyPtr = (uint16_t *)0x0800; uint16_t *const bootKeyPtr = (uint16_t *)0x0800;
// Stash the magic key // Stash the magic key
*bootKeyPtr = bootKey; *bootKeyPtr = bootKey;
// Set a watchdog timer // Set a watchdog timer
wdt_enable(WDTO_120MS); wdt_enable(WDTO_120MS);
while (1) {} // This infinite loop ensures nothing else while (1) {} // This infinite loop ensures nothing else
// happens before the watchdog reboots us // happens before the watchdog reboots us
} }
void command_plugh() { void command_plugh() {
commandMode = !commandMode; commandMode = !commandMode;
if (commandMode) { if (commandMode) {
Keyboard.println(""); Keyboard.println("");
Keyboard.println("Entering command mode!"); Keyboard.println("Entering command mode!");
} else { } else {
Keyboard.println("Leaving command mode!"); Keyboard.println("Leaving command mode!");
Keyboard.println(""); Keyboard.println("");
} }
} }
void setup_command_mode() { void setup_command_mode() {
commandBufferSize = 0; commandBufferSize = 0;
commandMode = false; commandMode = false;
commandPromptPrinted = false; commandPromptPrinted = false;
} }
boolean command_ends_in_return() { boolean command_ends_in_return() {
if ( if (
commandBuffer[commandBufferSize - 1] == KEY_ENTER || commandBuffer[commandBufferSize - 1] == KEY_ENTER ||
commandBuffer[commandBufferSize - 1] == KEY_RETURN ) { commandBuffer[commandBufferSize - 1] == KEY_RETURN ) {
return true; return true;
} else { } else {
return false; return false;
} }
} }
boolean is_command_buffer(byte* myCommand) { boolean is_command_buffer(byte* myCommand) {
if (!command_ends_in_return()) { if (!command_ends_in_return()) {
return false; return false;
}
int i = 0;
do {
if (commandBuffer[i] != myCommand[i]) {
return false;
} }
} while (myCommand[++i] != NULL); int i = 0;
return true; do {
if (commandBuffer[i] != myCommand[i]) {
return false;
}
} while (myCommand[++i] != NULL);
return true;
} }
void process_command_buffer() { void process_command_buffer() {
if (!command_ends_in_return()) { if (!command_ends_in_return()) {
return; return;
} }
// This is the only command we might want to execute when // This is the only command we might want to execute when
// we're not in command mode, as it's the only way to toggle // we're not in command mode, as it's the only way to toggle
// command mode on // command mode on
static byte cmd_plugh[] = {KEY_P, KEY_L, KEY_U, KEY_G, KEY_H, NULL}; static byte cmd_plugh[] = {KEY_P, KEY_L, KEY_U, KEY_G, KEY_H, NULL};
if (is_command_buffer(cmd_plugh)) { if (is_command_buffer(cmd_plugh)) {
command_plugh(); command_plugh();
} }
// if we've toggled command mode off, get out of here. // if we've toggled command mode off, get out of here.
if (!commandMode) { if (!commandMode) {
commandBufferSize = 0; commandBufferSize = 0;
return; return;
} }
// Handle all the other commands here // Handle all the other commands here
static byte cmd_reboot_bootloader[] = { KEY_B, KEY_O, KEY_O, KEY_T, KEY_L, KEY_O, KEY_A, KEY_D, KEY_E, KEY_R, NULL}; static byte cmd_reboot_bootloader[] = { KEY_B, KEY_O, KEY_O, KEY_T, KEY_L, KEY_O, KEY_A, KEY_D, KEY_E, KEY_R, NULL};
static byte cmd_version[] = { KEY_V, KEY_E, KEY_R, KEY_S, KEY_I, KEY_O, KEY_N, NULL}; static byte cmd_version[] = { KEY_V, KEY_E, KEY_R, KEY_S, KEY_I, KEY_O, KEY_N, NULL};
if (is_command_buffer(cmd_reboot_bootloader)) { if (is_command_buffer(cmd_reboot_bootloader)) {
command_reboot_bootloader(); command_reboot_bootloader();
} else if (is_command_buffer(cmd_version)) { } else if (is_command_buffer(cmd_version)) {
Keyboard.println(""); Keyboard.println("");
Keyboard.print("This is Keyboardio Firmware "); Keyboard.print("This is Keyboardio Firmware ");
Keyboard.println(VERSION); Keyboard.println(VERSION);
} }
if (!commandPromptPrinted ) { if (!commandPromptPrinted ) {
Keyboard.print(">>> "); Keyboard.print(">>> ");
commandPromptPrinted = true; commandPromptPrinted = true;
commandBufferSize = 0; commandBufferSize = 0;
} }
} }
void setup() void setup() {
{ wdt_disable();
wdt_disable(); Serial.begin(115200);
Serial.begin(115200); //usbMaxPower = 100;
//usbMaxPower = 100; Keyboard.begin();
Keyboard.begin(); Mouse.begin();
Mouse.begin(); setup_leds();
setup_leds(); led_bootup();
led_bootup(); setup_command_mode();
setup_command_mode(); setup_matrix();
setup_matrix(); setup_pins();
setup_pins(); rightsx1509.fetchPinStates();
rightsx1509.fetchPinStates();
temporary_keymap = primary_keymap = load_primary_keymap();
temporary_keymap = primary_keymap = load_primary_keymap();
} }
String myApp; String myApp;
void loop() void loop() {
{ // if(Serial.available()) {
// if(Serial.available()) { // myApp = Serial.readString();
// myApp = Serial.readString(); // myApp.trim();
// myApp.trim(); // }
// } TS("A noop takes...")
TS("A noop takes...") TS("about to scan the matrix")
TS("about to scan the matrix") scan_matrix();
scan_matrix(); TS("updating LEDs");
TS("updating LEDs"); update_leds(temporary_keymap == NUMPAD_KEYMAP);
update_leds(temporary_keymap == NUMPAD_KEYMAP);
} }
void save_primary_keymap(byte keymap) void save_primary_keymap(byte keymap) {
{ EEPROM.write(EEPROM_KEYMAP_LOCATION, keymap);
EEPROM.write(EEPROM_KEYMAP_LOCATION, keymap);
} }
byte load_primary_keymap() byte load_primary_keymap() {
{ byte keymap = EEPROM.read(EEPROM_KEYMAP_LOCATION);
byte keymap = EEPROM.read(EEPROM_KEYMAP_LOCATION); if (keymap >= KEYMAPS ) {
if (keymap >= KEYMAPS ) { return 0; // undefined positions get saved as 255
return 0; // undefined positions get saved as 255 }
} return 0; // return keymap;
return 0; // return keymap;
} }
@ -319,33 +313,31 @@ return 0; // return keymap;
// Debugging Reporting // Debugging Reporting
// //
void report_matrix() void report_matrix() {
{
#ifdef DEBUG_SERIAL #ifdef DEBUG_SERIAL
if (reporting_counter++ % 100 == 0 ) { if (reporting_counter++ % 100 == 0 ) {
for (byte row = 0; row < ROWS; row++) { for (byte row = 0; row < ROWS; row++) {
for (byte col = 0; col < COLS; col++) { for (byte 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("");
}
#endif #endif
} }
void report(byte row, byte col, boolean value) void report(byte row, byte col, boolean value) {
{
#ifdef DEBUG_SERIAL #ifdef DEBUG_SERIAL
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(".");
#endif #endif
} }
@ -353,48 +345,45 @@ void report(byte row, byte col, boolean value)
// Key Reports // Key Reports
// //
void release_keys_not_being_pressed() void release_keys_not_being_pressed() {
{ // we use charsReportedLastTime to figure out what we might
// we use charsReportedLastTime to figure out what we might // not be holding anymore and can now release. this is
// not be holding anymore and can now release. this is // destructive to charsReportedLastTime
// destructive to charsReportedLastTime
for (byte i = 0; i < KEYS_HELD_BUFFER; i++) {
for (byte i = 0; i < KEYS_HELD_BUFFER; i++) { // for each key we were holding as of the end of the last cycle
// for each key we were holding as of the end of the last cycle // see if we're still holding it
// see if we're still holding it // if we're not, call an explicit Release
// if we're not, call an explicit Release
if (charsReportedLastTime[i] != 0x00) {
if (charsReportedLastTime[i] != 0x00) { // if there _was_ a character in this slot, go check the
// if there _was_ a character in this slot, go check the // currently held characters
// currently held characters for (byte j = 0; j < KEYS_HELD_BUFFER; j++) {
for (byte j = 0; j < KEYS_HELD_BUFFER; j++) { if (charsReportedLastTime[i] == charsBeingReported[j]) {
if (charsReportedLastTime[i] == charsBeingReported[j]) { // if's still held, we don't need to do anything.
// if's still held, we don't need to do anything. charsReportedLastTime[i] = 0x00;
charsReportedLastTime[i] = 0x00; break;
break; }
} }
} Keyboard.release(charsReportedLastTime[i]);
Keyboard.release(charsReportedLastTime[i]);
}
} }
}
} }
void record_key_being_pressed(byte character) void record_key_being_pressed(byte character) {
{ for (byte i = 0; i < KEYS_HELD_BUFFER; i++) {
for (byte i = 0; i < KEYS_HELD_BUFFER; i++) { // todo - deal with overflowing the 12 key buffer here
// todo - deal with overflowing the 12 key buffer here if (charsBeingReported[i] == 0x00) {
if (charsBeingReported[i] == 0x00) { charsBeingReported[i] = character;
charsBeingReported[i] = character; break;
break; }
} }
}
} }
void reset_key_report() void reset_key_report() {
{ memcpy( charsReportedLastTime, charsBeingReported, KEYS_HELD_BUFFER);
memcpy( charsReportedLastTime, charsBeingReported, KEYS_HELD_BUFFER); memset(charsBeingReported, 0, KEYS_HELD_BUFFER);
memset(charsBeingReported, 0, KEYS_HELD_BUFFER);
} }
@ -402,176 +391,172 @@ void reset_key_report()
// Sending events to the usb host // Sending events to the usb host
void handle_synthetic_key_press(byte switchState, Key mappedKey) { void handle_synthetic_key_press(byte switchState, Key mappedKey) {
if (mappedKey.flags & IS_CONSUMER) { if (mappedKey.flags & IS_CONSUMER) {
if (key_toggled_on (switchState)) {
ConsumerControl.press(mappedKey.rawKey);
}
}
else if (mappedKey.flags & IS_INTERNAL) {
if (key_toggled_on (switchState)) { if (key_toggled_on (switchState)) {
if (mappedKey.rawKey == LED_TOGGLE) { ConsumerControl.press(mappedKey.rawKey);
next_led_mode();
}
} }
}
else if (mappedKey.flags & IS_SYSCTL) {
if (key_toggled_on (switchState)) {
SystemControl.press(mappedKey.rawKey);
}
}
else if (mappedKey.flags & IS_MACRO) {
if (key_toggled_on (switchState)) {
if (mappedKey.rawKey == 1) {
Serial.print("Keyboard.IO keyboard driver v0.00");
}
} }
} else if (mappedKey.rawKey == KEY_MOUSE_BTN_L
|| mappedKey.rawKey == KEY_MOUSE_BTN_M else if (mappedKey.flags & IS_INTERNAL) {
|| mappedKey.rawKey == KEY_MOUSE_BTN_R) { if (key_toggled_on (switchState)) {
if (key_toggled_on (switchState)) { if (mappedKey.rawKey == LED_TOGGLE) {
Mouse.press(mappedKey.rawKey); next_led_mode();
end_warping(); }
} else if (key_is_pressed(switchState)) { }
} else if (Mouse.isPressed(mappedKey.rawKey) ) { } else if (mappedKey.flags & IS_SYSCTL) {
Mouse.release(mappedKey.rawKey); if (key_toggled_on (switchState)) {
SystemControl.press(mappedKey.rawKey);
}
} else if (mappedKey.flags & IS_MACRO) {
if (key_toggled_on (switchState)) {
if (mappedKey.rawKey == 1) {
Serial.print("Keyboard.IO keyboard driver v0.00");
}
}
} else if (mappedKey.rawKey == KEY_MOUSE_BTN_L
|| mappedKey.rawKey == KEY_MOUSE_BTN_M
|| mappedKey.rawKey == KEY_MOUSE_BTN_R) {
if (key_toggled_on (switchState)) {
Mouse.press(mappedKey.rawKey);
end_warping();
} else if (key_is_pressed(switchState)) {
} else if (Mouse.isPressed(mappedKey.rawKey) ) {
Mouse.release(mappedKey.rawKey);
}
} }
}
} }
void send_key_event(byte row, byte col) void send_key_event(byte row, byte col) {
{ //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
// really, these are signed small ints // really, these are signed small ints
byte switchState = matrixState[row][col]; byte switchState = matrixState[row][col];
Key mappedKey = keymaps[temporary_keymap][row][col]; Key mappedKey = keymaps[temporary_keymap][row][col];
set_keymap(keymaps[primary_keymap][row][col], switchState); set_keymap(keymaps[primary_keymap][row][col], switchState);
if (mappedKey.flags & MOUSE_KEY ) { if (mappedKey.flags & MOUSE_KEY ) {
if (mappedKey.rawKey & MOUSE_WARP) { if (mappedKey.rawKey & MOUSE_WARP) {
if (key_toggled_on(switchState)) { if (key_toggled_on(switchState)) {
warp_mouse(mappedKey); warp_mouse(mappedKey);
} }
} else { } else {
handle_mouse_key_press(switchState, mappedKey, x, y); handle_mouse_key_press(switchState, mappedKey, x, y);
} }
} else if (mappedKey.flags & SYNTHETIC_KEY) { } else if (mappedKey.flags & SYNTHETIC_KEY) {
handle_synthetic_key_press(switchState, mappedKey); handle_synthetic_key_press(switchState, mappedKey);
} } else {
else { if (key_is_pressed(switchState)) {
if (key_is_pressed(switchState)) { record_key_being_pressed(mappedKey.rawKey);
record_key_being_pressed(mappedKey.rawKey); if (key_toggled_on (switchState)) {
if (key_toggled_on (switchState)) { press_key(mappedKey);
press_key(mappedKey); }
} else if (key_toggled_off (switchState)) {
release_key(mappedKey);
} }
} else if (key_toggled_off (switchState)) {
release_key(mappedKey);
}
} }
} }
void press_key(Key mappedKey) { void press_key(Key mappedKey) {
if (mappedKey.flags & SHIFT_HELD) { if (mappedKey.flags & SHIFT_HELD) {
Keyboard.press(Key_LShift.rawKey); Keyboard.press(Key_LShift.rawKey);
} }
Keyboard.press(mappedKey.rawKey); Keyboard.press(mappedKey.rawKey);
if (commandBufferSize >= 31) { if (commandBufferSize >= 31) {
commandBufferSize = 0; commandBufferSize = 0;
} }
commandBuffer[commandBufferSize++] = mappedKey.rawKey; commandBuffer[commandBufferSize++] = mappedKey.rawKey;
if ( mappedKey.rawKey == KEY_ENTER || if ( mappedKey.rawKey == KEY_ENTER ||
mappedKey.rawKey == KEY_RETURN ) { mappedKey.rawKey == KEY_RETURN ) {
commandPromptPrinted = false; commandPromptPrinted = false;
process_command_buffer(); process_command_buffer();
commandBufferSize = 0; commandBufferSize = 0;
} }
} }
void release_key(Key mappedKey) { void release_key(Key mappedKey) {
if (mappedKey.flags & SHIFT_HELD) { if (mappedKey.flags & SHIFT_HELD) {
Keyboard.release(Key_LShift.rawKey); Keyboard.release(Key_LShift.rawKey);
} }
Keyboard.release(mappedKey.rawKey); Keyboard.release(mappedKey.rawKey);
} }
void make_input(sx1509Class sx1509, int pin) { void make_input(sx1509Class sx1509, int pin) {
sx1509.pinDir(pin, INPUT); // Set SX1509 pin 1 as an input sx1509.pinDir(pin, INPUT); // Set SX1509 pin 1 as an input
sx1509.writePin(pin, HIGH); // Activate pull-up sx1509.writePin(pin, HIGH); // Activate pull-up
} }
void make_output(sx1509Class sx1509, int pin) { void make_output(sx1509Class sx1509, int pin) {
sx1509.pinDir(pin, OUTPUT); sx1509.pinDir(pin, OUTPUT);
sx1509.writePin(pin, HIGH); sx1509.writePin(pin, HIGH);
} }
void setup_pins() { void setup_pins() {
right_initted = setup_sx1509(rightsx1509, right_colpins, right_rowpins); right_initted = setup_sx1509(rightsx1509, right_colpins, right_rowpins);
left_initted = setup_sx1509(leftsx1509, left_colpins, left_rowpins); left_initted = setup_sx1509(leftsx1509, left_colpins, left_rowpins);
} }
int setup_sx1509 (sx1509Class sx1509, int colpins[], int rowpins[]) { int setup_sx1509 (sx1509Class sx1509, int colpins[], int rowpins[]) {
byte initted; byte initted;
for (int counter = 0; counter < 10; counter++) { for (int counter = 0; counter < 10; counter++) {
initted = sx1509.init(); initted = sx1509.init();
if (initted) if (initted)
break; break;
} }
if (initted) { // init ok if (initted) { // init ok
// In order to use the keypad, the clock must first be // In order to use the keypad, the clock must first be
// configured. We can call configureClock() with the default // configured. We can call configureClock() with the default
// parameters (2MHz internal oscillator, no clock in/out). // parameters (2MHz internal oscillator, no clock in/out).
sx1509.configClock(); sx1509.configClock();
// the debounceConfig function sets the debounce time. This // the debounceConfig function sets the debounce time. This
// function's parameter should be a 3-bit value. // function's parameter should be a 3-bit value.
// 0: 0.5ms * 2MHz/fOSC // 0: 0.5ms * 2MHz/fOSC
// 1: 1ms * 2MHz/fOSC // 1: 1ms * 2MHz/fOSC
// 2: 2ms * 2MHz/fOSC // 2: 2ms * 2MHz/fOSC
// 3: 4ms * 2MHz/fOSC // 3: 4ms * 2MHz/fOSC
// 4: 8ms * 2MHz/fOSC // 4: 8ms * 2MHz/fOSC
// 5: 16ms * 2MHz/fOSC // 5: 16ms * 2MHz/fOSC
// 6: 32ms * 2MHz/fOSC // 6: 32ms * 2MHz/fOSC
// 7: 64ms * 2MHz/fOSC // 7: 64ms * 2MHz/fOSC
sx1509.debounceConfig(4); // maximum debuonce time sx1509.debounceConfig(4); // maximum debuonce time
for (int i = 0; i < LEFT_ROWS; i++) { for (int i = 0; i < LEFT_ROWS; i++) {
make_output(sx1509, rowpins[i]); make_output(sx1509, rowpins[i]);
} }
for (int j = 0; j < LEFT_COLS; j++) { for (int j = 0; j < LEFT_COLS; j++) {
make_input(sx1509, colpins[j]); make_input(sx1509, colpins[j]);
sx1509.debounceEnable(colpins[j]); sx1509.debounceEnable(colpins[j]);
} }
} }
return initted; return initted;
} }

@ -4,60 +4,54 @@
// switch debouncing and status // switch debouncing and status
boolean key_was_pressed (byte keyState) boolean key_was_pressed (byte keyState) {
{ return (! key_was_not_pressed(keyState));
return (! key_was_not_pressed(keyState));
} }
boolean key_was_not_pressed (byte keyState) boolean key_was_not_pressed (byte keyState) {
{
if ( keyState & B00001000 || keyState & B00000100) { if ( keyState & B00001000 || keyState & B00000100) {
return false; return false;
} else { } else {
return true; return true;
} }
} }
boolean key_is_pressed (byte keyState) boolean key_is_pressed (byte keyState) {
{
if ( keyState & B00000001 && keyState & B00000010 ) {
if ( keyState & B00000001 && keyState & B00000010 ){ return true;
} else {
return true; return false;
} else { }
return false;
}
} }
boolean key_is_not_pressed (byte keyState) boolean key_is_not_pressed (byte keyState) {
{ return !key_is_pressed(keyState);
return !key_is_pressed(keyState);
} }
boolean key_toggled_on(byte keyState) boolean key_toggled_on(byte keyState) {
{ if (key_is_pressed(keyState) && key_was_not_pressed(keyState)) {
if (key_is_pressed(keyState) && key_was_not_pressed(keyState)) { return true;
return true; } else {
} else { return false;
return false; }
}
} }
boolean key_toggled_off(byte keyState) boolean key_toggled_off(byte keyState) {
{ if (key_was_pressed(keyState) && key_is_not_pressed(keyState)) {
if (key_was_pressed(keyState) && key_is_not_pressed(keyState)) { return true;
return true; } else {
} else { return false;
return false; }
}
} }

@ -1,8 +1,8 @@
#include "HIDTables.h" #include "HIDTables.h"
#include "HIDAliases.h" #include "HIDAliases.h"
typedef struct { typedef struct {
byte flags; byte flags;
byte rawKey; byte rawKey;
} Key; } Key;

@ -50,8 +50,8 @@
#define KEYMAPS 3 #define KEYMAPS 3
#define NUMPAD_KEYMAP 2 #define NUMPAD_KEYMAP 2
static const Key keymaps[KEYMAPS][ROWS][COLS] = { static const Key keymaps[KEYMAPS][ROWS][COLS] = {
KEYMAP_QWERTY KEYMAP_QWERTY
KEYMAP_GENERIC_FN2 KEYMAP_GENERIC_FN2
KEYMAP_NUMPAD KEYMAP_NUMPAD
}; };

@ -43,77 +43,77 @@ int current_chase_counter = 0;
// End RGB stuff // End RGB stuff
void setup_leds() { void setup_leds() {
led_off.r = 0; led_off.r = 0;
led_off.g = 0; led_off.g = 0;
led_off.b = 0; led_off.b = 0;
led_steady.r = 0; led_steady.r = 0;
led_steady.g = 255; led_steady.g = 255;
led_steady.b = 0; led_steady.b = 0;
led_blue.r = 0; led_blue.r = 0;
led_blue.g = 0; led_blue.g = 0;
led_blue.b = 255; led_blue.b = 255;
led_dark_blue.r = 0; led_dark_blue.r = 0;
led_dark_blue.g = 0; led_dark_blue.g = 0;
led_dark_blue.b = 127; led_dark_blue.b = 127;
led_bright_red.r=255; led_bright_red.r=255;
led_bright_red.g=0; led_bright_red.g=0;
led_bright_red.b=0; led_bright_red.b=0;
LED.setOutput(LED_DATA_PIN); LED.setOutput(LED_DATA_PIN);
LED.setColorOrderGRB(); // Uncomment for RGB color order LED.setColorOrderGRB(); // Uncomment for RGB color order
} }
byte key_to_led(byte row, byte col) { byte key_to_led(byte row, byte col) {
return key_led_map[row][col]; return key_led_map[row][col];
} }
void set_key_color(byte row, byte col, cRGB color) { void set_key_color(byte row, byte col, cRGB color) {
LED.set_crgb_at(key_to_led(row, col), color); LED.set_crgb_at(key_to_led(row, col), color);
} }
cRGB get_key_color(byte row, byte col) { cRGB get_key_color(byte row, byte col) {
return LED.get_crgb_at(key_to_led(row, col)); return LED.get_crgb_at(key_to_led(row, col));
} }
void initialize_led_mode(int mode) { void initialize_led_mode(int mode) {
set_all_leds_to(led_off); set_all_leds_to(led_off);
if (mode == LED_MODE_OFF) { if (mode == LED_MODE_OFF) {
// set_all_leds_to(led_off); // set_all_leds_to(led_off);
} else if (mode == LED_MODE_HEATMAP) { } else if (mode == LED_MODE_HEATMAP) {
} else if (mode == LED_MODE_BREATHE) { } else if (mode == LED_MODE_BREATHE) {
} else if (mode == LED_MODE_RAINBOW) { } else if (mode == LED_MODE_RAINBOW) {
} else if (mode == LED_MODE_RAINBOW_WAVE) { } else if (mode == LED_MODE_RAINBOW_WAVE) {
} else if (mode == LED_MODE_CHASE) { } else if (mode == LED_MODE_CHASE) {
} else if (mode == LED_MODE_STEADY) { } else if (mode == LED_MODE_STEADY) {
set_all_leds_to(led_steady); set_all_leds_to(led_steady);
} }
} }
void set_all_leds_to(cRGB color) { void set_all_leds_to(cRGB color) {
for (int i = 0; i < LED_COUNT; i++) { for (int i = 0; i < LED_COUNT; i++) {
LED.set_crgb_at(i, color); LED.set_crgb_at(i, color);
} }
} }
void next_led_mode() { void next_led_mode() {
led_mode++; led_mode++;
if (led_mode >= LED_MODES) { if (led_mode >= LED_MODES) {
led_mode = 0; led_mode = 0;
} }
} }
void set_led_mode(int mode) { void set_led_mode(int mode) {
led_mode = mode; led_mode = mode;
} }
@ -127,91 +127,91 @@ void update_leds(int numlock_enabled) {
} }
led_mode = LED_SPECIAL_MODE_NUMLOCK; led_mode = LED_SPECIAL_MODE_NUMLOCK;
} }
if (!numlock_enabled && if (!numlock_enabled &&
led_mode == LED_SPECIAL_MODE_NUMLOCK led_mode == LED_SPECIAL_MODE_NUMLOCK
) { ) {
led_mode = stored_led_mode; led_mode = stored_led_mode;
} }
if (led_mode != last_led_mode) {
initialize_led_mode(led_mode);
}
if (led_mode == LED_MODE_OFF) {
} else if (led_mode == LED_MODE_HEATMAP) {
} else if (led_mode == LED_MODE_BREATHE) {
led_effect_breathe_update();
} else if (led_mode == LED_MODE_RAINBOW) {
led_effect_rainbow_update();
} else if (led_mode == LED_MODE_RAINBOW_WAVE) {
led_effect_rainbow_wave_update();
} else if (led_mode == LED_MODE_CHASE) {
led_effect_chase_update();
} else if (led_mode == LED_MODE_STEADY) {
led_effect_steady_update();
} else if (led_mode == LED_SPECIAL_MODE_NUMLOCK) {
led_effect_numlock_update();
}
if (led_mode != last_led_mode) { last_led_mode = led_mode;
initialize_led_mode(led_mode);
}
if (led_mode == LED_MODE_OFF) {
} else if (led_mode == LED_MODE_HEATMAP) {
} else if (led_mode == LED_MODE_BREATHE) {
led_effect_breathe_update();
} else if (led_mode == LED_MODE_RAINBOW) {
led_effect_rainbow_update();
} else if (led_mode == LED_MODE_RAINBOW_WAVE) {
led_effect_rainbow_wave_update();
} else if (led_mode == LED_MODE_CHASE) {
led_effect_chase_update();
} else if (led_mode == LED_MODE_STEADY) {
led_effect_steady_update();
} else if (led_mode == LED_SPECIAL_MODE_NUMLOCK) {
led_effect_numlock_update();
}
last_led_mode = led_mode;
} }
void led_effect_numlock_update() { void led_effect_numlock_update() {
for (int i = 0; i < 44; i++) { for (int i = 0; i < 44; i++) {
LED.set_crgb_at(i, led_off); LED.set_crgb_at(i, led_off);
} }
for (int i = 44; i < LED_COUNT; i++) { for (int i = 44; i < LED_COUNT; i++) {
LED.set_crgb_at(i, led_bright_red); LED.set_crgb_at(i, led_bright_red);
} }
led_compute_breath(); led_compute_breath();
LED.set_crgb_at(60, led_breathe); // make numlock breathe LED.set_crgb_at(60, led_breathe); // make numlock breathe
LED.sync(); LED.sync();
} }
void led_effect_steady_update() { void led_effect_steady_update() {
LED.sync(); LED.sync();
} }
void led_compute_breath() { void led_compute_breath() {
// algorithm from http://sean.voisen.org/blog/2011/10/breathing-led-with-arduino/ // algorithm from http://sean.voisen.org/blog/2011/10/breathing-led-with-arduino/
breathe_brightness = (exp(sin(millis()/2000.0*PI)) - 0.36787944)*108.0; breathe_brightness = (exp(sin(millis()/2000.0*PI)) - 0.36787944)*108.0;
// change the brightness for next time through the loop: // change the brightness for next time through the loop:
//breathe_brightness = breathe_brightness + breathe_fadeAmount; //breathe_brightness = breathe_brightness + breathe_fadeAmount;
// reverse the direction of the fading at the ends of the fade: // reverse the direction of the fading at the ends of the fade:
if (breathe_brightness == 0 || breathe_brightness == 150) { if (breathe_brightness == 0 || breathe_brightness == 150) {
breathe_fadeAmount = -breathe_fadeAmount ; breathe_fadeAmount = -breathe_fadeAmount ;
} }
led_breathe.SetHSV(200, 255, breathe_brightness); led_breathe.SetHSV(200, 255, breathe_brightness);
} }
void led_effect_breathe_update() { void led_effect_breathe_update() {
led_compute_breath(); led_compute_breath();
set_all_leds_to(led_breathe); set_all_leds_to(led_breathe);
LED.sync(); LED.sync();
} }
void void
led_effect_chase_update() { led_effect_chase_update() {
if (current_chase_counter++ < chase_threshold) { if (current_chase_counter++ < chase_threshold) {
return; return;
} }
current_chase_counter = 0; current_chase_counter = 0;
LED.set_crgb_at(pos - chase_pixels, led_off); LED.set_crgb_at(pos - chase_pixels, led_off);
LED.set_crgb_at(pos, led_dark_blue); LED.set_crgb_at(pos, led_dark_blue);
pos += chase_pixels;
if (pos > LED_COUNT || pos < 0) {
chase_pixels = -chase_pixels;
pos += chase_pixels; pos += chase_pixels;
if (pos > LED_COUNT || pos < 0) {
chase_pixels = -chase_pixels;
pos += chase_pixels;
} }
LED.set_crgb_at(pos, led_blue); LED.set_crgb_at(pos, led_blue);
LED.sync(); LED.sync();
} }
@ -221,20 +221,22 @@ led_effect_chase_update() {
void led_effect_rainbow_update() { void led_effect_rainbow_update() {
if (rainbow_current_ticks++ < rainbow_ticks) { if (rainbow_current_ticks++ < rainbow_ticks) {
return; return;
} else { rainbow_current_ticks = 0; } } else {
rainbow_current_ticks = 0;
rainbow.SetHSV(rainbow_hue, rainbow_saturation, rainbow_value); }
rainbow_hue += rainbow_steps; rainbow.SetHSV(rainbow_hue, rainbow_saturation, rainbow_value);
if (rainbow_hue >= 360) {
rainbow_hue %= 360; rainbow_hue += rainbow_steps;
} if (rainbow_hue >= 360) {
set_all_leds_to(rainbow); rainbow_hue %= 360;
LED.sync(); }
set_all_leds_to(rainbow);
LED.sync();
} }
@ -242,30 +244,32 @@ void led_effect_rainbow_update() {
void led_effect_rainbow_wave_update() { void led_effect_rainbow_wave_update() {
if (rainbow_current_ticks++ < rainbow_wave_ticks) { if (rainbow_current_ticks++ < rainbow_wave_ticks) {
return; return;
} else { rainbow_current_ticks = 0; } } else {
rainbow_current_ticks = 0;
for (int i = 0; i < LED_COUNT; i++) {
int key_hue = rainbow_hue +16*(i/4);
if (key_hue >= 360) {
key_hue %= 360;
} }
rainbow.SetHSV(key_hue, rainbow_saturation, rainbow_value);
LED.set_crgb_at(i,rainbow);
} for (int i = 0; i < LED_COUNT; i++) {
rainbow_hue += rainbow_wave_steps;
if (rainbow_hue >= 360) { int key_hue = rainbow_hue +16*(i/4);
rainbow_hue %= 360; if (key_hue >= 360) {
key_hue %= 360;
}
rainbow.SetHSV(key_hue, rainbow_saturation, rainbow_value);
LED.set_crgb_at(i,rainbow);
}
rainbow_hue += rainbow_wave_steps;
if (rainbow_hue >= 360) {
rainbow_hue %= 360;
} }
LED.sync(); LED.sync();
} }
void led_bootup() { void led_bootup() {
@ -285,17 +289,17 @@ void led_bootup() {
led_type_letter(LED_0); led_type_letter(LED_0);
led_type_letter(LED_PERIOD); led_type_letter(LED_PERIOD);
led_type_letter(LED_9); led_type_letter(LED_9);
led_mode = LED_MODE_RAINBOW_WAVE; led_mode = LED_MODE_RAINBOW_WAVE;
} }
void led_type_letter(int letter) { void led_type_letter(int letter) {
LED.set_crgb_at(letter,led_bright_red); LED.set_crgb_at(letter,led_bright_red);
LED.sync(); LED.sync();
delay(400); delay(400);
LED.set_crgb_at(letter,led_off); LED.set_crgb_at(letter,led_off);
LED.sync(); LED.sync();
delay(30); delay(30);
} }

@ -16,8 +16,7 @@
#define LED_COUNT 64 #define LED_COUNT 64
static const int key_led_map[4][16] = static const int key_led_map[4][16] = {
{
{3,4,11,12,19,20,26,27, 36,37,43,44,51,52,59,60}, {3,4,11,12,19,20,26,27, 36,37,43,44,51,52,59,60},
{2,5,10,13,18,21,31,28, 35,32,42,45,50,53,58,61}, {2,5,10,13,18,21,31,28, 35,32,42,45,50,53,58,61},
{1,6,9,14, 17,22,25,29, 34,38,41,46,49,54,57,62}, {1,6,9,14, 17,22,25,29, 34,38,41,46,49,54,57,62},

@ -11,7 +11,7 @@
void _warp_jump(long left, long top, long height, long width) { void _warp_jump(long left, long top, long height, long width) {
long x_center = left + width/2; long x_center = left + width/2;
long y_center = top + height/2; long y_center = top + height/2;
AbsoluteMouse.moveTo(x_center,y_center); AbsoluteMouse.moveTo(x_center,y_center);
} }
@ -38,11 +38,11 @@ int section_left;
boolean is_warping = false; boolean is_warping = false;
void begin_warping() { void begin_warping() {
section_left = abs_left; section_left = abs_left;
section_top = abs_top; section_top = abs_top;
next_width = 32767; next_width = 32767;
next_height = 32767; next_height = 32767;
is_warping = true; is_warping = true;
} }
void end_warping() { void end_warping() {
@ -50,40 +50,40 @@ void end_warping() {
} }
void warp_mouse(Key ninth) { void warp_mouse(Key ninth) {
if (is_warping == false) { if (is_warping == false) {
begin_warping(); begin_warping();
} }
if ( ninth.rawKey & MOUSE_END_WARP) { if ( ninth.rawKey & MOUSE_END_WARP) {
end_warping(); end_warping();
return; return;
} }
next_width = next_width / 2; next_width = next_width / 2;
next_height = next_height/2; next_height = next_height/2;
if (ninth.rawKey & MOUSE_UP) { if (ninth.rawKey & MOUSE_UP) {
// Serial.print(" - up "); // Serial.print(" - up ");
} else if (ninth.rawKey & MOUSE_DN) { } else if (ninth.rawKey & MOUSE_DN) {
// Serial.print(" - down "); // Serial.print(" - down ");
section_top = section_top + next_height; section_top = section_top + next_height;
} }
if (ninth.rawKey & MOUSE_L) { if (ninth.rawKey & MOUSE_L) {
// Serial.print(" - left "); // Serial.print(" - left ");
} else if (ninth.rawKey & MOUSE_R) { } else if (ninth.rawKey & MOUSE_R) {
// Serial.print(" - right "); // Serial.print(" - right ");
section_left = section_left + next_width; section_left = section_left + next_width;
} }
_warp_jump(section_left, section_top, next_height,next_width); _warp_jump(section_left, section_top, next_height,next_width);
} }
// we want the whole s curve, not just the bit // we want the whole s curve, not just the bit
// that's usually above the x and y axes; // that's usually above the x and y axes;
#define ATAN_LIMIT 1.57079633 #define ATAN_LIMIT 1.57079633
#define ACCELERATION_FLOOR 0.25 #define ACCELERATION_FLOOR 0.25
@ -96,64 +96,62 @@ void warp_mouse(Key ninth) {
#define ACCELERATION_CLIMB_SPEED 0.05 #define ACCELERATION_CLIMB_SPEED 0.05
double mouse_accel (double cycles) double mouse_accel (double cycles) {
{ double accel = (atan((cycles * ACCELERATION_CLIMB_SPEED)-ACCELERATION_RUNWAY) + ATAN_LIMIT) * ACCELERATION_MULTIPLIER;
double accel = (atan((cycles * ACCELERATION_CLIMB_SPEED)-ACCELERATION_RUNWAY) + ATAN_LIMIT) * ACCELERATION_MULTIPLIER; if (accel < ACCELERATION_FLOOR) {
if (accel < ACCELERATION_FLOOR) { accel = ACCELERATION_FLOOR;
accel = ACCELERATION_FLOOR;
}
return accel;
}
void handle_mouse_movement( char x, char y)
{
if (x != 0 || y != 0) {
mouseActiveForCycles++;
double accel = (double) mouse_accel(mouseActiveForCycles);
float moveX = 0;
float moveY = 0;
if (x > 0) {
moveX = (x * accel) + carriedOverX;
carriedOverX = moveX - floor(moveX);
} else if (x < 0) {
moveX = (x * accel) - carriedOverX;
carriedOverX = ceil(moveX) - moveX;
} }
return accel;
}
if (y > 0) { void handle_mouse_movement( char x, char y) {
moveY = (y * accel) + carriedOverY;
carriedOverY = moveY - floor(moveY); if (x != 0 || y != 0) {
} else if (y < 0) { mouseActiveForCycles++;
moveY = (y * accel) - carriedOverY; double accel = (double) mouse_accel(mouseActiveForCycles);
carriedOverY = ceil(moveY) - moveY; float moveX = 0;
float moveY = 0;
if (x > 0) {
moveX = (x * accel) + carriedOverX;
carriedOverX = moveX - floor(moveX);
} else if (x < 0) {
moveX = (x * accel) - carriedOverX;
carriedOverX = ceil(moveX) - moveX;
}
if (y > 0) {
moveY = (y * accel) + carriedOverY;
carriedOverY = moveY - floor(moveY);
} else if (y < 0) {
moveY = (y * accel) - carriedOverY;
carriedOverY = ceil(moveY) - moveY;
}
end_warping();
Mouse.move(moveX, moveY, 0);
} else {
mouseActiveForCycles = 0;
} }
end_warping();
Mouse.move(moveX, moveY, 0);
} else {
mouseActiveForCycles = 0;
}
} }
void handle_mouse_key_press(byte switchState, Key mappedKey, char &x, char &y) { void handle_mouse_key_press(byte switchState, Key mappedKey, char &x, char &y) {
if (key_is_pressed(switchState)) { if (key_is_pressed(switchState)) {
if (mappedKey.rawKey & MOUSE_UP) { if (mappedKey.rawKey & MOUSE_UP) {
y -= 1; y -= 1;
} }
if (mappedKey.rawKey & MOUSE_DN) { if (mappedKey.rawKey & MOUSE_DN) {
y += 1; y += 1;
} }
if (mappedKey.rawKey & MOUSE_L) { if (mappedKey.rawKey & MOUSE_L) {
x -= 1; x -= 1;
} }
if (mappedKey.rawKey & MOUSE_R) { if (mappedKey.rawKey & MOUSE_R) {
x += 1 ; x += 1 ;
}
} }
}
} }

Loading…
Cancel
Save