Merge pull request #6 from Chris-Torrence
Improve caps lock behavior, clean up code
This commit is contained in:
commit
dfd4cbd33b
|
@ -8,47 +8,48 @@ http://www.pjrc.com/teensy/teensyduino.html
|
|||
#include <Keypad.h>
|
||||
|
||||
|
||||
|
||||
/*
|
||||
Declares the matrix rows/cols of the Apple IIe keyboard.
|
||||
|
||||
More information here:
|
||||
http://apple2.info/wiki/index.php?title=Pinouts#Apple_.2F.2Fe_Motherboard_keyboard_connector
|
||||
|
||||
|
||||
*/
|
||||
|
||||
// Prevent compiler warning about casts from unsigned ints to chars
|
||||
#define C(x) ((char) (x))
|
||||
|
||||
const byte ROWS = 10; // rows
|
||||
const byte COLS = 8; // columns
|
||||
char keys[ROWS][COLS] = {
|
||||
|
||||
|
||||
{ KEY_ESC, KEY_TAB, KEY_A, KEY_Z, KEYPAD_SLASH , 0 , KEYPAD_ASTERIX, KEY_ESC},
|
||||
{ C(KEY_ESC), C(KEY_TAB), C(KEY_A), C(KEY_Z), C(KEYPAD_SLASH), 0, C(KEYPAD_ASTERIX), C(KEY_ESC)},
|
||||
|
||||
{ KEY_1, KEY_Q, KEY_D, KEY_X, 0, 0, 0, 0},
|
||||
{ C(KEY_1), C(KEY_Q), C(KEY_D), C(KEY_X), 0, 0, 0, 0},
|
||||
|
||||
{ KEY_2, KEY_W, KEY_S, KEY_C, KEYPAD_0, KEYPAD_4, KEYPAD_8, 0},
|
||||
{ C(KEY_2), C(KEY_W), C(KEY_S), C(KEY_C), C(KEYPAD_0), C(KEYPAD_4), C(KEYPAD_8), 0},
|
||||
|
||||
{ KEY_3, KEY_E, KEY_H, KEY_V, KEYPAD_1, KEYPAD_5, KEYPAD_9, KEYPAD_MINUS},
|
||||
{ C(KEY_3), C(KEY_E), C(KEY_H), C(KEY_V), C(KEYPAD_1), C(KEYPAD_5), C(KEYPAD_9), C(KEYPAD_MINUS)},
|
||||
|
||||
{ KEY_4, KEY_R, KEY_F, KEY_B, KEYPAD_2, KEYPAD_6, KEYPAD_PERIOD, KEYPAD_ENTER},
|
||||
{ C(KEY_4), C(KEY_R), C(KEY_F), C(KEY_B), C(KEYPAD_2), C(KEYPAD_6), C(KEYPAD_PERIOD), C(KEYPAD_ENTER)},
|
||||
|
||||
{ KEY_6, KEY_Y, KEY_G, KEY_N, KEYPAD_3, KEYPAD_7, KEYPAD_PLUS, 0},
|
||||
{ C(KEY_6), C(KEY_Y), C(KEY_G), C(KEY_N), C(KEYPAD_3), C(KEYPAD_7), C(KEYPAD_PLUS), 0},
|
||||
|
||||
{ KEY_5, KEY_T, KEY_J, KEY_M, KEY_BACKSLASH, KEY_TILDE, KEY_ENTER, KEY_BACKSPACE},
|
||||
{ C(KEY_5), C(KEY_T), C(KEY_J), C(KEY_M), C(KEY_BACKSLASH), C(KEY_TILDE), C(KEY_ENTER), C(KEY_BACKSPACE)},
|
||||
|
||||
{ KEY_7, KEY_U, KEY_K, KEY_COMMA, KEY_EQUAL, KEY_P, KEY_UP, KEY_DOWN },
|
||||
{ C(KEY_7), C(KEY_U), C(KEY_K), C(KEY_COMMA), C(KEY_EQUAL), C(KEY_P), C(KEY_UP), C(KEY_DOWN) },
|
||||
|
||||
{ KEY_8, KEY_I, KEY_SEMICOLON, KEY_PERIOD, KEY_0, KEY_LEFT_BRACE, KEY_SPACE, KEY_LEFT },
|
||||
{ C(KEY_8), C(KEY_I), C(KEY_SEMICOLON), C(KEY_PERIOD), C(KEY_0), C(KEY_LEFT_BRACE), C(KEY_SPACE), C(KEY_LEFT) },
|
||||
|
||||
{ KEY_9, KEY_O, KEY_L, KEY_SLASH, KEY_MINUS, KEY_RIGHT_BRACE, KEY_QUOTE, KEY_RIGHT },
|
||||
{ C(KEY_9), C(KEY_O), C(KEY_L), C(KEY_SLASH), C(KEY_MINUS), C(KEY_RIGHT_BRACE), C(KEY_QUOTE), C(KEY_RIGHT) },
|
||||
|
||||
};
|
||||
|
||||
|
||||
char Fkeys[2][10] = {
|
||||
{KEY_1, KEY_2, KEY_3, KEY_4, KEY_5, KEY_6, KEY_7, KEY_8, KEY_9, KEY_0 },
|
||||
{KEY_F1, KEY_F2, KEY_F3, KEY_F4, KEY_F5, KEY_F6, KEY_F7, KEY_F8, KEY_F9, KEY_F10 }
|
||||
{C(KEY_1), C(KEY_2), C(KEY_3), C(KEY_4), C(KEY_5), C(KEY_6), C(KEY_7), C(KEY_8), C(KEY_9), C(KEY_0)},
|
||||
{C(KEY_F1), C(KEY_F2), C(KEY_F3), C(KEY_F4), C(KEY_F5), C(KEY_F6), C(KEY_F7), C(KEY_F8), C(KEY_F9), C(KEY_F10)}
|
||||
};
|
||||
|
||||
|
||||
|
@ -119,79 +120,88 @@ byte colPins[COLS] = { // X0 - X7
|
|||
Keypad KPD = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
|
||||
|
||||
|
||||
// these pins are special in that they are dis/connected to ground, instead of to a row/col
|
||||
const int SHIFTPin = 21; // the pin that the shift key is attached to
|
||||
const int CTRLPin = 5; // the pin that the control key is attached to
|
||||
const int APPLEPin1 = 8; // the pin that the open-apple key is attached to
|
||||
const int APPLEPin2 = 9; // the pin that the closed-apple key is attached to
|
||||
// these pins are special in that they are dis/connected to ground, instead of to a row/col
|
||||
const int CAPSPin = 7;
|
||||
const int CAPSPin = 7;
|
||||
|
||||
#define LED 6
|
||||
|
||||
uint16_t modifierKeys[4];
|
||||
|
||||
|
||||
#define KEY_CAPS_UNLOCK 0
|
||||
|
||||
char modifierKeys[4];
|
||||
|
||||
|
||||
|
||||
|
||||
#define KEY_CAPS_UNLOCK 0
|
||||
|
||||
boolean resetCapsLock = false; // Allows one caps unlock signal.
|
||||
unsigned long dTime = 0;
|
||||
char CAPSState; // Initialize this to a reasonable value.
|
||||
boolean resetCapsLock = false; // Allows one caps unlock signal.
|
||||
unsigned long dTime = 0;
|
||||
boolean CAPSLock = false; // Initialize this to a reasonable value.
|
||||
|
||||
boolean FKEYS = false; // used to set numbers to F-Key equivalent. currently tied to caps lock
|
||||
boolean FKEYS = false; // used to set numbers to F-Key equivalent. currently tied to caps lock
|
||||
|
||||
|
||||
void setup(){
|
||||
void setup()
|
||||
{
|
||||
pinMode(SHIFTPin, INPUT_PULLUP);
|
||||
digitalWrite(SHIFTPin, HIGH);
|
||||
|
||||
pinMode(CTRLPin, INPUT_PULLUP);
|
||||
digitalWrite(CTRLPin, HIGH);
|
||||
|
||||
pinMode(SHIFTPin, INPUT);
|
||||
pinMode(CTRLPin, INPUT);
|
||||
pinMode(APPLEPin1, INPUT);
|
||||
pinMode(APPLEPin2, INPUT);
|
||||
|
||||
digitalWrite(APPLEPin1, LOW);
|
||||
|
||||
pinMode(APPLEPin2, INPUT);
|
||||
digitalWrite(APPLEPin2, LOW);
|
||||
|
||||
digitalWrite(SHIFTPin, HIGH);
|
||||
digitalWrite(CTRLPin, HIGH);
|
||||
|
||||
pinMode(CAPSPin, INPUT);
|
||||
pinMode(CAPSPin, INPUT_PULLUP);
|
||||
digitalWrite(CAPSPin, HIGH);
|
||||
|
||||
|
||||
|
||||
// DEBUG
|
||||
// Serial.begin(115200);
|
||||
pinMode(LED, OUTPUT);
|
||||
|
||||
//Serial.begin(115200);
|
||||
//Serial.println(CAPSState);
|
||||
// This gives time for the keyboard to hook up to the PC.
|
||||
// Otherwise the caps lock state may be incorrect.
|
||||
delay(1000);
|
||||
|
||||
}
|
||||
|
||||
|
||||
void loop()
|
||||
{
|
||||
//probably should be on an interrupt, to catch high->low transition
|
||||
|
||||
// Only do something if the pin is different from previous state.
|
||||
if ( (CAPSState!=digitalRead(CAPSPin)) && !resetCapsLock) {
|
||||
CAPSState = digitalRead(CAPSPin); // Remember new CAPSState.
|
||||
Keyboard.set_key6(KEY_CAPS_LOCK); // Send KEY_CAPS_LOCK.
|
||||
dTime = millis(); // Reset delay timer.
|
||||
resetCapsLock = true;
|
||||
|
||||
Serial.print("Caps = ");
|
||||
Serial.println(CAPSState);
|
||||
|
||||
}
|
||||
if ( resetCapsLock && (millis()-dTime) > 10) {
|
||||
Keyboard.set_key6(KEY_CAPS_UNLOCK);
|
||||
resetCapsLock = false;
|
||||
// Only do something if the pin is different from previous state.
|
||||
boolean newCaps = digitalRead(CAPSPin) ? false : true;
|
||||
|
||||
if ( (CAPSLock != newCaps) && !resetCapsLock)
|
||||
{
|
||||
CAPSLock = newCaps; // Remember new CAPSLock.
|
||||
Keyboard.set_key6((uint8_t) KEY_CAPS_LOCK); // Send KEY_CAPS_LOCK.
|
||||
dTime = millis(); // Reset delay timer.
|
||||
resetCapsLock = true;
|
||||
|
||||
// Serial.print("Caps = ");
|
||||
// Serial.println(CAPSLock);
|
||||
|
||||
// Turn on the LED for caps lock.
|
||||
digitalWrite(LED, CAPSLock ? HIGH : LOW);
|
||||
|
||||
}
|
||||
|
||||
FKEYS = !CAPSState;
|
||||
if ( resetCapsLock && (millis()-dTime) > 10)
|
||||
{
|
||||
Keyboard.set_key6(KEY_CAPS_UNLOCK);
|
||||
resetCapsLock = false;
|
||||
}
|
||||
|
||||
// If caps lock is set, then turn number keys into function keys.
|
||||
FKEYS = CAPSLock;
|
||||
|
||||
/*char CAPSState = digitalRead(CAPSPin);
|
||||
if (CAPSState == LOW) {
|
||||
/*char CAPSLock = digitalRead(CAPSPin);
|
||||
if (CAPSLock == LOW) {
|
||||
Keyboard.set_key6(KEY_CAPS_LOCK);
|
||||
} else {
|
||||
Keyboard.set_key6(0);89
|
||||
|
@ -202,9 +212,7 @@ FKEYS = !CAPSState;
|
|||
|
||||
if (SHIFTState == LOW) {
|
||||
modifierKeys[0] = MODIFIERKEY_SHIFT;
|
||||
digitalWrite(SHIFTPin, HIGH);
|
||||
} else {
|
||||
digitalWrite(SHIFTPin, HIGH);
|
||||
modifierKeys[0] = 0;
|
||||
}
|
||||
|
||||
|
@ -212,10 +220,8 @@ FKEYS = !CAPSState;
|
|||
|
||||
if (CTRLState == LOW) {
|
||||
modifierKeys[1] = MODIFIERKEY_CTRL;
|
||||
digitalWrite(CTRLPin, HIGH);
|
||||
} else {
|
||||
modifierKeys[1] = 0;
|
||||
digitalWrite(CTRLPin, HIGH);
|
||||
}
|
||||
|
||||
char OAPPLEState = digitalRead(APPLEPin1);
|
||||
|
@ -233,28 +239,20 @@ FKEYS = !CAPSState;
|
|||
*/
|
||||
|
||||
|
||||
|
||||
// *** NOW USING CLOSED APPLE AS ALT/OPTION
|
||||
if (OAPPLEState == HIGH) {
|
||||
modifierKeys[2] = MODIFIERKEY_GUI;
|
||||
digitalWrite(APPLEPin1, LOW);
|
||||
} else {
|
||||
modifierKeys[2] = 0;
|
||||
digitalWrite(APPLEPin1, LOW);
|
||||
}
|
||||
|
||||
if (CAPPLEState == HIGH) {
|
||||
modifierKeys[3] = MODIFIERKEY_ALT;
|
||||
digitalWrite(APPLEPin2, LOW);
|
||||
} else {
|
||||
modifierKeys[3] = 0;
|
||||
digitalWrite(APPLEPin2, LOW);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
// to use the TILDE key as ALT/OPTION
|
||||
/* modifierKeys[3] = 0;
|
||||
if( KPD.isPressed(KEY_TILDE) ) {
|
||||
|
@ -264,13 +262,9 @@ FKEYS = !CAPSState;
|
|||
|
||||
// *** NOW USING CLOSED APPLE AS ALT/OPTION
|
||||
|
||||
Keyboard.set_modifier( modifierKeys[0] | modifierKeys[1] | modifierKeys[2] | modifierKeys[3] );
|
||||
|
||||
KPD.getKeys(); // Scan for all pressed keys. 6 Max, + 4 modifiers. Should be plenty, but can be extended to 10+
|
||||
|
||||
|
||||
|
||||
Keyboard.set_modifier( modifierKeys[0] | modifierKeys[1] | modifierKeys[2] | modifierKeys[3] );
|
||||
|
||||
KPD.getKeys(); // Scan for all pressed keys. 6 Max, + 4 modifiers. Should be plenty, but can be extended to 10+
|
||||
|
||||
// Set keyboard keys to default values.
|
||||
Keyboard.set_key1(0);
|
||||
|
@ -280,42 +274,37 @@ FKEYS = !CAPSState;
|
|||
Keyboard.set_key5(0);
|
||||
//Keyboard.set_key6(0);
|
||||
|
||||
|
||||
|
||||
/* based on suggestion from Craig Brooks <s.craig.brooks@gmail.com>
|
||||
uses CAPS LOCK to turn number keys into F-Key equivalent.
|
||||
*/
|
||||
|
||||
|
||||
/* based on suggestion from Craig Brooks <s.craig.brooks@gmail.com>
|
||||
uses CAPS LOCK to turn number keys into F-Key equivalent.
|
||||
*/
|
||||
|
||||
|
||||
// Update keyboard keys to active values.
|
||||
if( KPD.key[0].kchar && ( KPD.key[0].kstate==PRESSED || KPD.key[0].kstate==HOLD )) {
|
||||
if ( KPD.key[0].kchar && ( KPD.key[0].kstate==PRESSED || KPD.key[0].kstate==HOLD ))
|
||||
{
|
||||
//Serial.println(FKEYS);
|
||||
|
||||
if (FKEYS)
|
||||
{
|
||||
// number keys 1 through 0 for f1 - f10
|
||||
if ((KPD.key[0].kchar >= 0x1E) && (KPD.key[0].kchar <= 0x27))
|
||||
{
|
||||
KPD.key[0].kchar += 0x1C;
|
||||
// Serial.println( KPD.key[0].kchar, HEX );
|
||||
}
|
||||
else if ( KPD.key[0].kchar == 0x2D || KPD.key[0].kchar == 0x2E )
|
||||
{
|
||||
// - and = for f11 and f12
|
||||
KPD.key[0].kchar += 0x17;
|
||||
}
|
||||
}
|
||||
|
||||
Keyboard.set_key1( KPD.key[0].kchar );
|
||||
}
|
||||
|
||||
//Serial.println(FKEYS);
|
||||
|
||||
|
||||
if (FKEYS) {
|
||||
// number keys 1 through 0 for f1 - f10
|
||||
if((KPD.key[0].kchar >= 0x1E) && (KPD.key[0].kchar <= 0x27)){
|
||||
KPD.key[0].kchar += 0x1C;
|
||||
|
||||
// Serial.println( KPD.key[0].kchar, HEX );
|
||||
|
||||
// - and = for f11 and f12
|
||||
} else if( KPD.key[0].kchar == 0x2D || KPD.key[0].kchar == 0x2E ) {
|
||||
KPD.key[0].kchar += 0x17;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
Keyboard.set_key1( KPD.key[0].kchar );
|
||||
|
||||
|
||||
}
|
||||
|
||||
if( KPD.key[1].kchar && ( KPD.key[1].kstate==PRESSED || KPD.key[1].kstate==HOLD ))
|
||||
if ( KPD.key[1].kchar && ( KPD.key[1].kstate==PRESSED || KPD.key[1].kstate==HOLD ))
|
||||
Keyboard.set_key2( KPD.key[1].kchar );
|
||||
|
||||
if( KPD.key[2].kchar && ( KPD.key[2].kstate==PRESSED || KPD.key[2].kstate==HOLD ))
|
||||
|
|
Loading…
Reference in New Issue