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

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

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

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

@ -4,60 +4,54 @@
// switch debouncing and status
boolean key_was_pressed (byte keyState)
{
return (! key_was_not_pressed(keyState));
boolean key_was_pressed (byte 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) {
return false;
} else {
return true;
}
return false;
} else {
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 false;
}
return true;
} else {
return false;
}
}
boolean key_is_not_pressed (byte keyState)
{
return !key_is_pressed(keyState);
boolean key_is_not_pressed (byte keyState) {
return !key_is_pressed(keyState);
}
boolean key_toggled_on(byte keyState)
{
if (key_is_pressed(keyState) && key_was_not_pressed(keyState)) {
return true;
} else {
return false;
}
boolean key_toggled_on(byte keyState) {
if (key_is_pressed(keyState) && key_was_not_pressed(keyState)) {
return true;
} else {
return false;
}
}
boolean key_toggled_off(byte keyState)
{
if (key_was_pressed(keyState) && key_is_not_pressed(keyState)) {
return true;
} else {
return false;
}
boolean key_toggled_off(byte keyState) {
if (key_was_pressed(keyState) && key_is_not_pressed(keyState)) {
return true;
} else {
return false;
}
}

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

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

@ -43,77 +43,77 @@ int current_chase_counter = 0;
// End RGB stuff
void setup_leds() {
led_off.r = 0;
led_off.g = 0;
led_off.b = 0;
led_steady.r = 0;
led_steady.g = 255;
led_steady.b = 0;
led_blue.r = 0;
led_blue.g = 0;
led_blue.b = 255;
led_off.r = 0;
led_off.g = 0;
led_off.b = 0;
led_steady.r = 0;
led_steady.g = 255;
led_steady.b = 0;
led_blue.r = 0;
led_blue.g = 0;
led_blue.b = 255;
led_dark_blue.r = 0;
led_dark_blue.g = 0;
led_dark_blue.b = 127;
led_dark_blue.r = 0;
led_dark_blue.g = 0;
led_dark_blue.b = 127;
led_bright_red.r=255;
led_bright_red.g=0;
led_bright_red.b=0;
led_bright_red.r=255;
led_bright_red.g=0;
led_bright_red.b=0;
LED.setOutput(LED_DATA_PIN);
LED.setColorOrderGRB(); // Uncomment for RGB color order
LED.setOutput(LED_DATA_PIN);
LED.setColorOrderGRB(); // Uncomment for RGB color order
}
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) {
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) {
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) {
set_all_leds_to(led_off);
if (mode == LED_MODE_OFF) {
// set_all_leds_to(led_off);
} else if (mode == LED_MODE_HEATMAP) {
} else if (mode == LED_MODE_BREATHE) {
} else if (mode == LED_MODE_RAINBOW) {
} else if (mode == LED_MODE_RAINBOW_WAVE) {
} else if (mode == LED_MODE_CHASE) {
} else if (mode == LED_MODE_STEADY) {
set_all_leds_to(led_steady);
}
set_all_leds_to(led_off);
if (mode == LED_MODE_OFF) {
// set_all_leds_to(led_off);
} else if (mode == LED_MODE_HEATMAP) {
} else if (mode == LED_MODE_BREATHE) {
} else if (mode == LED_MODE_RAINBOW) {
} else if (mode == LED_MODE_RAINBOW_WAVE) {
} else if (mode == LED_MODE_CHASE) {
} else if (mode == LED_MODE_STEADY) {
set_all_leds_to(led_steady);
}
}
void set_all_leds_to(cRGB color) {
for (int i = 0; i < LED_COUNT; i++) {
LED.set_crgb_at(i, color);
}
for (int i = 0; i < LED_COUNT; i++) {
LED.set_crgb_at(i, color);
}
}
void next_led_mode() {
led_mode++;
if (led_mode >= LED_MODES) {
led_mode = 0;
}
led_mode++;
if (led_mode >= LED_MODES) {
led_mode = 0;
}
}
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;
}
if (!numlock_enabled &&
led_mode == LED_SPECIAL_MODE_NUMLOCK
) {
if (!numlock_enabled &&
led_mode == LED_SPECIAL_MODE_NUMLOCK
) {
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) {
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;
last_led_mode = led_mode;
}
void led_effect_numlock_update() {
for (int i = 0; i < 44; i++) {
LED.set_crgb_at(i, led_off);
}
for (int i = 44; i < LED_COUNT; i++) {
LED.set_crgb_at(i, led_bright_red);
}
for (int i = 0; i < 44; i++) {
LED.set_crgb_at(i, led_off);
}
for (int i = 44; i < LED_COUNT; i++) {
LED.set_crgb_at(i, led_bright_red);
}
led_compute_breath();
LED.set_crgb_at(60, led_breathe); // make numlock breathe
LED.sync();
}
void led_effect_steady_update() {
void led_effect_steady_update() {
LED.sync();
}
}
void led_compute_breath() {
// 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;
// change the brightness for next time through the loop:
//breathe_brightness = breathe_brightness + breathe_fadeAmount;
// reverse the direction of the fading at the ends of the fade:
if (breathe_brightness == 0 || breathe_brightness == 150) {
breathe_fadeAmount = -breathe_fadeAmount ;
}
// 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;
// change the brightness for next time through the loop:
//breathe_brightness = breathe_brightness + breathe_fadeAmount;
// reverse the direction of the fading at the ends of the fade:
if (breathe_brightness == 0 || breathe_brightness == 150) {
breathe_fadeAmount = -breathe_fadeAmount ;
}
led_breathe.SetHSV(200, 255, breathe_brightness);
led_breathe.SetHSV(200, 255, breathe_brightness);
}
void led_effect_breathe_update() {
led_compute_breath();
set_all_leds_to(led_breathe);
LED.sync();
led_compute_breath();
set_all_leds_to(led_breathe);
LED.sync();
}
void
void
led_effect_chase_update() {
if (current_chase_counter++ < chase_threshold) {
return;
}
current_chase_counter = 0;
LED.set_crgb_at(pos - chase_pixels, led_off);
LED.set_crgb_at(pos, led_dark_blue);
pos += chase_pixels;
if (pos > LED_COUNT || pos < 0) {
chase_pixels = -chase_pixels;
if (current_chase_counter++ < chase_threshold) {
return;
}
current_chase_counter = 0;
LED.set_crgb_at(pos - chase_pixels, led_off);
LED.set_crgb_at(pos, led_dark_blue);
pos += chase_pixels;
if (pos > LED_COUNT || pos < 0) {
chase_pixels = -chase_pixels;
pos += chase_pixels;
}
LED.set_crgb_at(pos, led_blue);
LED.sync();
}
LED.set_crgb_at(pos, led_blue);
LED.sync();
}
@ -221,20 +221,22 @@ led_effect_chase_update() {
void led_effect_rainbow_update() {
if (rainbow_current_ticks++ < rainbow_ticks) {
return;
} else { rainbow_current_ticks = 0; }
rainbow.SetHSV(rainbow_hue, rainbow_saturation, rainbow_value);
rainbow_hue += rainbow_steps;
if (rainbow_hue >= 360) {
rainbow_hue %= 360;
}
set_all_leds_to(rainbow);
LED.sync();
if (rainbow_current_ticks++ < rainbow_ticks) {
return;
} else {
rainbow_current_ticks = 0;
}
rainbow.SetHSV(rainbow_hue, rainbow_saturation, rainbow_value);
rainbow_hue += rainbow_steps;
if (rainbow_hue >= 360) {
rainbow_hue %= 360;
}
set_all_leds_to(rainbow);
LED.sync();
}
@ -242,30 +244,32 @@ void led_effect_rainbow_update() {
void led_effect_rainbow_wave_update() {
if (rainbow_current_ticks++ < rainbow_wave_ticks) {
return;
} 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;
if (rainbow_current_ticks++ < rainbow_wave_ticks) {
return;
} else {
rainbow_current_ticks = 0;
}
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;
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);
}
rainbow_hue += rainbow_wave_steps;
if (rainbow_hue >= 360) {
rainbow_hue %= 360;
}
LED.sync();
LED.sync();
}
void led_bootup() {
@ -285,17 +289,17 @@ void led_bootup() {
led_type_letter(LED_0);
led_type_letter(LED_PERIOD);
led_type_letter(LED_9);
led_mode = LED_MODE_RAINBOW_WAVE;
led_mode = LED_MODE_RAINBOW_WAVE;
}
void led_type_letter(int letter) {
LED.set_crgb_at(letter,led_bright_red);
LED.sync();
delay(400);
LED.sync();
delay(400);
LED.set_crgb_at(letter,led_off);
LED.sync();
delay(30);
LED.sync();
delay(30);
}

@ -16,8 +16,7 @@
#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},
{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},

@ -11,7 +11,7 @@
void _warp_jump(long left, long top, long height, long width) {
long x_center = left + width/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;
void begin_warping() {
section_left = abs_left;
section_top = abs_top;
next_width = 32767;
next_height = 32767;
is_warping = true;
section_left = abs_left;
section_top = abs_top;
next_width = 32767;
next_height = 32767;
is_warping = true;
}
void end_warping() {
@ -50,40 +50,40 @@ void end_warping() {
}
void warp_mouse(Key ninth) {
if (is_warping == false) {
begin_warping();
if (is_warping == false) {
begin_warping();
}
if ( ninth.rawKey & MOUSE_END_WARP) {
end_warping();
return;
}
if ( ninth.rawKey & MOUSE_END_WARP) {
end_warping();
return;
}
next_width = next_width / 2;
next_height = next_height/2;
next_width = next_width / 2;
next_height = next_height/2;
if (ninth.rawKey & MOUSE_UP) {
if (ninth.rawKey & MOUSE_UP) {
// Serial.print(" - up ");
} else if (ninth.rawKey & MOUSE_DN) {
// Serial.print(" - down ");
section_top = section_top + next_height;
}
if (ninth.rawKey & MOUSE_L) {
// Serial.print(" - left ");
} else if (ninth.rawKey & MOUSE_R) {
// Serial.print(" - right ");
section_left = section_left + next_width;
}
} else if (ninth.rawKey & MOUSE_DN) {
// Serial.print(" - down ");
section_top = section_top + next_height;
}
if (ninth.rawKey & MOUSE_L) {
// Serial.print(" - left ");
} else if (ninth.rawKey & MOUSE_R) {
// Serial.print(" - right ");
section_left = section_left + 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;
#define ATAN_LIMIT 1.57079633
#define ACCELERATION_FLOOR 0.25
@ -96,64 +96,62 @@ void warp_mouse(Key ninth) {
#define ACCELERATION_CLIMB_SPEED 0.05
double mouse_accel (double cycles)
{
double accel = (atan((cycles * ACCELERATION_CLIMB_SPEED)-ACCELERATION_RUNWAY) + ATAN_LIMIT) * ACCELERATION_MULTIPLIER;
if (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;
double mouse_accel (double cycles) {
double accel = (atan((cycles * ACCELERATION_CLIMB_SPEED)-ACCELERATION_RUNWAY) + ATAN_LIMIT) * ACCELERATION_MULTIPLIER;
if (accel < ACCELERATION_FLOOR) {
accel = ACCELERATION_FLOOR;
}
return accel;
}
if (y > 0) {
moveY = (y * accel) + carriedOverY;
carriedOverY = moveY - floor(moveY);
} else if (y < 0) {
moveY = (y * accel) - carriedOverY;
carriedOverY = ceil(moveY) - moveY;
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;
}
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) {
if (key_is_pressed(switchState)) {
if (mappedKey.rawKey & MOUSE_UP) {
y -= 1;
}
if (mappedKey.rawKey & MOUSE_DN) {
y += 1;
}
if (mappedKey.rawKey & MOUSE_L) {
x -= 1;
}
if (mappedKey.rawKey & MOUSE_R) {
x += 1 ;
if (key_is_pressed(switchState)) {
if (mappedKey.rawKey & MOUSE_UP) {
y -= 1;
}
if (mappedKey.rawKey & MOUSE_DN) {
y += 1;
}
if (mappedKey.rawKey & MOUSE_L) {
x -= 1;
}
if (mappedKey.rawKey & MOUSE_R) {
x += 1 ;
}
}
}
}

Loading…
Cancel
Save