diff --git a/firmware/keyboard.c b/firmware/keyboard.c index df495af..7bbb9b5 100644 --- a/firmware/keyboard.c +++ b/firmware/keyboard.c @@ -14,14 +14,14 @@ static uchar newReport = 1; /* current report */ uint16_t memAddr = 0; uint8_t cMode = 0; // 0->Keyup 1->Keydown uint8_t oldKey=0; -uint8_t hasReleased=0; +uint8_t hasReleased=1; uint8_t flag_keyPress=0; +uint8_t keyAction=0; uint8_t keyType[16]; // Will contain whether the key has to be handled as a ... #define KEYTYPE_TEXT 0 // Text-Button (-> only button presses) or whether we need to pay attention to #define KEYTYPE_KEY 1 // push and release events - // MEMORY ARCHITECTURE (INTERNAL) #define MEM_SERIAL_OFFSET 0 #define MEM_SERIAL_LENGTH 16 @@ -44,6 +44,7 @@ uint8_t keyType[16]; // Will contain whether the key has to be handled as a ... #define CMD_IEEWRITE 7 //internal eeprom #define CMD_IEEREAD 8 // int. eep. #define CMD_EXEC 9 // execute from addr. +#define CMD_INITKEYS 10 // Auto-detect keytypes /*Keys[] 0-133 codes.pdf Page 53 (10. Table 12) ModKeys 224-231 codes.pdf Page 59 (10. Table 12) @@ -58,6 +59,9 @@ ModKeys 224-231 codes.pdf Page 59 (10. Table 12) #define KEYMODE_DOWN 0 #define KEYMODE_PRESS 2 //push and release +#define KEYACTION_DOWN 0 +#define KEYACTION_UP 1 + // Very slow programming? // -> Disable verifying the entered data in CMD_EEWRITE @@ -170,7 +174,7 @@ static uchar reportBuffer[24]; } else if(rq->bRequest == CMD_SWVERSION) { reportBuffer[0] = '0'; - reportBuffer[1] = '1'; + reportBuffer[1] = '2'; reportBuffer[2] = 'B'; return 3; } @@ -218,6 +222,11 @@ static uchar reportBuffer[24]; newReport = 0; //activate continued exec continueExecution(); // exec NOW } + else if (rq->bRequest == CMD_INITKEYS) { + detectKeyType(); + reportBuffer[0] = 1; + return 1; + } } return 0; } @@ -231,9 +240,9 @@ static void buildReport(void){ else if (oldKey) { // One of the 16 keys was pressed //reportBuffer[2] = 125 + oldKey; // 1+getKey() // Set memAddr to the first byte of the pressed key - memAddr = MEM_KEY_LENGTH*(oldKey-1); - cMode = KEYMODE_PRESS; //default to keymode_press - oldKey = 0; + //memAddr = MEM_KEY_LENGTH*(oldKey-1); + //cMode = KEYMODE_PRESS; //default to keymode_press + //oldKey = 0; continueExecution(); } else { @@ -251,18 +260,22 @@ void continueExecution() { uint8_t instr = EEReadByte(memAddr); if (instr == INSTR_BREAK) { // Release all keys (?) - uint8_t x; - for (x=0; x < 8; x++) { - reportBuffer[x] = 0; + if (keyType[oldKey-1] == KEYTYPE_TEXT) { + uint8_t x; + for (x=0; x < 8; x++) { + reportBuffer[x] = 0; + } } newReport = 1; // done newData = 1; //exit loop } else if (instr == INSTR_KEYUP) { - cMode = KEYMODE_UP; + if ((keyType[oldKey-1] == KEYTYPE_TEXT) || keyAction == KEYACTION_DOWN) cMode = KEYMODE_UP; + else cMode = KEYMODE_DOWN; // When the key on the keyboard was released, we play everything back invertedly. } else if (instr == INSTR_KEYDOWN) { - cMode = KEYMODE_DOWN; + if ((keyType[oldKey-1] == KEYTYPE_TEXT) || keyAction == KEYACTION_DOWN) cMode = KEYMODE_DOWN; + else cMode = KEYMODE_UP; // See four lines above. } else if (instr == INSTR_KEYPRESS) { cMode = KEYMODE_PRESS; @@ -323,29 +336,30 @@ void continueExecution() { } } +void detectKeyType() { + uint8_t keyId, x; + for (keyId=0;keyId < 16; ++keyId) { + for (x=0; x < MEM_KEY_LENGTH; ++x) { + uint8_t instr = EEReadByte(keyId*MEM_KEY_LENGTH+x); + if (instr == INSTR_KEYDOWN || instr == INSTR_KEYUP) { // If KEYPRESS is used at any time, it is a text-command + keyType[keyId] = KEYTYPE_KEY; + break; + } + else if (instr == INSTR_BREAK) { // If only KEYDOWN and KEYUP are used KEYDOWN it is a standard key + keyType[keyId] = KEYTYPE_TEXT; + break; + } + } + } +} + void keyboard(void) { wdt_enable(WDTO_2S); EEOpen(); // We open our eeprom uchar i; // First, we scan through the codes so we can determine the key types. - { - uint8_t keyId, x; - for (keyId=0;keyId < 16; ++keyId) { - for (x=0; x < MEM_KEY_LENGTH; ++x) { - uint8_t instr = EEReadByte(keyId*MEM_KEY_LENGTH+x); - if (instr == INSTR_KEYPRESS) { // If KEYPRESS is used at any time, it is a text-command - keyType[keyId] = KEYTYPE_TEXT; - break; - } - else if (instr == INSTR_BREAK) { // If only KEYDOWN and KEYUP are used KEYDOWN it is a standard key - keyType[keyId] = KEYTYPE_KEY; - break; - } - } - } - } - + detectKeyType(); cli(); usbInit(); @@ -379,10 +393,31 @@ void keyboard(void) { void checkBtn() { if (newReport == 0) return; // ignore all keypresses if there's a current key in progress - if (getKey() == 0) hasReleased = 1; - if (getKey() != oldKey && hasReleased == 1) { + if (getKey() != oldKey && getKey() == 0) { + + keyAction = KEYACTION_UP; + if (keyType[oldKey-1] == KEYTYPE_KEY && hasReleased == 0) { + newReport = 0; + hasReleased = 1; + memAddr = MEM_KEY_LENGTH*(oldKey-1); + cMode = KEYMODE_PRESS; //default to keymode_press + } + else { + oldKey=0; + hasReleased = 1; + } + + _delay_ms(10); // Debounce switches + } + else if (getKey() != oldKey && hasReleased == 1) { + keyAction = KEYACTION_DOWN; hasReleased = 0; oldKey = getKey(); newReport = 0; + + memAddr = MEM_KEY_LENGTH*(oldKey-1); + cMode = KEYMODE_PRESS; //default to keymode_press + + } }