2012-11-25 14:53:55 -05:00
|
|
|
#include <avr/pgmspace.h>
|
|
|
|
|
|
|
|
#include <avrpins.h>
|
|
|
|
#include <max3421e.h>
|
|
|
|
#include <usbhost.h>
|
|
|
|
#include <usb_ch9.h>
|
|
|
|
#include <Usb.h>
|
|
|
|
#include <usbhub.h>
|
|
|
|
#include <address.h>
|
|
|
|
#include <hidboot.h>
|
|
|
|
|
|
|
|
#include <printhex.h>
|
|
|
|
#include <message.h>
|
|
|
|
#include <hexdump.h>
|
|
|
|
#include <parsetools.h>
|
|
|
|
|
|
|
|
#include "keymapper_game.h"
|
|
|
|
|
|
|
|
//#define DEBUG
|
2012-11-27 10:51:51 -05:00
|
|
|
#define modeLED 13
|
2013-01-06 18:14:49 -05:00
|
|
|
#define TEENSY
|
2012-11-27 10:51:51 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// function definitions
|
2012-12-02 06:03:33 -05:00
|
|
|
bool HandleReservedKeystrokes(HID *hid, uint8_t *buf);
|
2013-01-06 18:14:49 -05:00
|
|
|
inline void SendKeysToHost (uint8_t *buf);
|
2012-11-27 10:51:51 -05:00
|
|
|
void play_word_game(void);
|
2013-02-04 13:19:14 -05:00
|
|
|
inline void LatchKey (uint8_t keyToLatch);
|
2012-11-25 14:53:55 -05:00
|
|
|
|
|
|
|
|
2012-11-27 10:51:51 -05:00
|
|
|
// variable definitions
|
2012-11-25 14:53:55 -05:00
|
|
|
typedef enum
|
|
|
|
{
|
2012-11-27 10:51:51 -05:00
|
|
|
qwerty=0,
|
|
|
|
tarmak1,
|
|
|
|
tarmak2,
|
|
|
|
tarmak3,
|
|
|
|
tarmak4,
|
|
|
|
colemak,
|
|
|
|
dvorak
|
2012-11-25 14:53:55 -05:00
|
|
|
} KeyboardLayout;
|
|
|
|
|
2012-12-02 06:03:33 -05:00
|
|
|
// Keymap based on the scancodes from 4 to 57, refer to the HID usage table on the meaning of each element
|
2012-11-25 14:53:55 -05:00
|
|
|
PROGMEM prog_uint8_t qwertyKeymap[] = {4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57};
|
2012-12-02 06:03:33 -05:00
|
|
|
PROGMEM prog_uint8_t tarmak1Keymap[] = {4, 5, 6, 7, 13, 9, 10, 11, 12, 17, 8, 15, 16, 14, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 42};
|
|
|
|
PROGMEM prog_uint8_t tarmak2Keymap[] = {4, 5, 6, 7, 9, 23, 13, 11, 12, 17, 8, 15, 16, 14, 18, 19, 20, 21, 22, 10, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 42};
|
|
|
|
PROGMEM prog_uint8_t tarmak3Keymap[] = {4, 5, 6, 7, 9, 23, 51, 11, 12, 17, 8, 15, 16, 14, 28, 19, 20, 21, 22, 10, 24, 25, 26, 27, 13, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 18, 52, 53, 54, 55, 56, 42};
|
|
|
|
PROGMEM prog_uint8_t tarmak4Keymap[] = {4, 5, 6, 7, 9, 23, 51, 11, 24, 17, 8, 12, 16, 14, 28, 19, 20, 21, 22, 10, 15, 25, 26, 27, 13, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 18, 52, 53, 54, 55, 56, 42};
|
|
|
|
PROGMEM prog_uint8_t colemakKeymap[] = {4, 5, 6, 22, 9, 23, 7, 11, 24, 17, 8, 12, 16, 14, 28, 51, 20, 19, 21, 10, 15, 25, 26, 27, 13, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 18, 52, 53, 54, 55, 56, 42};
|
2012-11-25 14:53:55 -05:00
|
|
|
|
2012-12-02 06:03:33 -05:00
|
|
|
const uint8_t *Keymap[] =
|
2012-11-25 14:53:55 -05:00
|
|
|
{
|
2012-11-27 10:51:51 -05:00
|
|
|
qwertyKeymap,
|
|
|
|
tarmak1Keymap,
|
|
|
|
tarmak2Keymap,
|
|
|
|
tarmak3Keymap,
|
|
|
|
tarmak4Keymap,
|
|
|
|
colemakKeymap
|
2012-11-25 14:53:55 -05:00
|
|
|
};
|
|
|
|
|
2012-12-02 06:03:33 -05:00
|
|
|
// global variables
|
2012-11-25 14:53:55 -05:00
|
|
|
//uint32_t ledBlinkTime = millis();
|
|
|
|
//uint16_t ledBlinkDelay = 500;
|
2013-02-04 13:19:14 -05:00
|
|
|
|
2012-12-02 06:03:33 -05:00
|
|
|
KeyboardLayout CurrentLayout = qwerty;
|
|
|
|
uint8_t KeyBuffer[8] = {0,0,0,0,0,0,0,0};
|
2013-02-04 13:19:14 -05:00
|
|
|
uint8_t specialKeyLatch=0;
|
|
|
|
bool specialKeyLatchReleased = false;
|
|
|
|
|
2012-11-25 14:53:55 -05:00
|
|
|
|
|
|
|
class KbdRptParser : public KeyboardReportParser
|
|
|
|
{
|
|
|
|
protected:
|
2012-11-27 10:51:51 -05:00
|
|
|
virtual void Parse(HID *hid, bool is_rpt_id, uint8_t len, uint8_t *buf);
|
2012-11-25 14:53:55 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2012-12-02 06:03:33 -05:00
|
|
|
// *******************************************************************************************
|
|
|
|
// Parse
|
|
|
|
// *******************************************************************************************
|
2012-11-25 14:53:55 -05:00
|
|
|
|
2012-12-02 06:03:33 -05:00
|
|
|
void KbdRptParser::Parse(HID *hid, bool is_rpt_id, uint8_t len, uint8_t *buf)
|
2012-11-25 14:53:55 -05:00
|
|
|
{
|
2013-01-06 18:14:49 -05:00
|
|
|
uint8_t i;
|
|
|
|
|
2012-12-02 06:03:33 -05:00
|
|
|
// On error - return
|
|
|
|
if (buf[2] == 1)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// for (uint8_t i=0; i<8; i++)
|
|
|
|
// {
|
|
|
|
// PrintHex(buf[i]);
|
|
|
|
// Serial.print(" ");
|
|
|
|
// }
|
|
|
|
// Serial.println("");
|
|
|
|
|
|
|
|
KeyBuffer[0] = buf[0];
|
2012-11-27 10:51:51 -05:00
|
|
|
|
2013-01-06 18:14:49 -05:00
|
|
|
|
2012-12-02 06:03:33 -05:00
|
|
|
if (!HandleReservedKeystrokes(hid, buf))
|
2012-11-27 10:51:51 -05:00
|
|
|
{
|
2013-02-04 13:19:14 -05:00
|
|
|
specialKeyLatchReleased = true;
|
|
|
|
|
|
|
|
// remap all keys according to the existing keymap
|
|
|
|
for (i=2; i<8; i++)
|
|
|
|
{
|
|
|
|
|
|
|
|
// handle special case of Shift-CAPSLOCK to be ignored by the remapper
|
|
|
|
if (buf[i] == KEY_CAPS_LOCK && buf[0] & 0x22)
|
|
|
|
{
|
|
|
|
KeyBuffer[i] = KEY_CAPS_LOCK;
|
|
|
|
LatchKey(KEY_CAPS_LOCK);
|
|
|
|
}
|
|
|
|
else
|
2012-12-02 06:03:33 -05:00
|
|
|
{
|
2013-02-04 13:19:14 -05:00
|
|
|
// print the key based on the current layout
|
|
|
|
if (buf[i]>=4 && buf[i] <= 57) // transpose of 4 becoz our array starts from 0 but A is 4
|
|
|
|
// limit check to 57, which is the last mappable key (CAPSLOCK)
|
|
|
|
{
|
|
|
|
// if it was a special key of shift-CAPS, then only allow mapping if the key has been released at least once
|
|
|
|
if (buf[i] != specialKeyLatch)
|
|
|
|
KeyBuffer[i] = pgm_read_byte(Keymap[CurrentLayout]+buf[i]-4);
|
|
|
|
else // key is not released yet. do not allow mapping
|
|
|
|
{
|
|
|
|
// Serial.println("key is not released");
|
|
|
|
KeyBuffer[i] = 0;
|
|
|
|
specialKeyLatchReleased = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
KeyBuffer[i] = buf[i];
|
2012-12-02 06:03:33 -05:00
|
|
|
}
|
|
|
|
|
2013-02-04 13:19:14 -05:00
|
|
|
|
|
|
|
// check locking keys
|
|
|
|
HandleLockingKeys(hid, KeyBuffer[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
// reset latch if key is released
|
|
|
|
if (specialKeyLatchReleased)
|
|
|
|
{
|
|
|
|
// Serial.println("latch is released");
|
|
|
|
specialKeyLatch = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// send out key press
|
|
|
|
SendKeysToHost (KeyBuffer);
|
|
|
|
|
|
|
|
// for (uint8_t i=0; i<8; i++)
|
|
|
|
// {
|
|
|
|
// PrintHex(KeyBuffer[i]);
|
|
|
|
// Serial.print(" ");
|
|
|
|
// }
|
|
|
|
// Serial.println("");
|
|
|
|
// Serial.println("");
|
2012-11-27 10:51:51 -05:00
|
|
|
|
|
|
|
}
|
2012-11-25 14:53:55 -05:00
|
|
|
|
2012-12-02 06:03:33 -05:00
|
|
|
};
|
2012-11-27 10:51:51 -05:00
|
|
|
|
|
|
|
|
2012-12-02 06:03:33 -05:00
|
|
|
bool HandleReservedKeystrokes(HID *hid, uint8_t *buf) // return true if it is a reserved keystroke
|
2012-11-27 10:51:51 -05:00
|
|
|
{
|
2012-12-02 06:03:33 -05:00
|
|
|
uint8_t mod = buf[0]; // read the modifier byte
|
|
|
|
|
|
|
|
uint8_t numKeysPressed = 0;
|
|
|
|
uint8_t keyPosition = 0;
|
|
|
|
|
|
|
|
// check that there is only 1 single key that is pressed
|
|
|
|
for (uint8_t i=2; i<8; i++) if (buf[i] > 0) {
|
|
|
|
numKeysPressed++;
|
|
|
|
keyPosition = i;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (numKeysPressed != 1) return false; // only allow single keypress for reserved keystrokes (besides modifiers)
|
|
|
|
|
2013-01-06 18:14:49 -05:00
|
|
|
|
|
|
|
|
2012-11-27 10:51:51 -05:00
|
|
|
// check if we are changing layouts
|
|
|
|
if ((mod & 0x22) && (mod & 0x11)) { // Shift-Alt keystrokes
|
2012-12-02 06:03:33 -05:00
|
|
|
switch (buf[keyPosition]) {
|
2012-11-25 14:53:55 -05:00
|
|
|
case 0x27: // 0
|
2012-12-02 06:03:33 -05:00
|
|
|
CurrentLayout = qwerty;
|
2012-11-25 14:53:55 -05:00
|
|
|
digitalWrite(modeLED, LOW);
|
2013-02-04 13:19:14 -05:00
|
|
|
LatchKey(buf[keyPosition]);
|
2012-11-27 10:51:51 -05:00
|
|
|
return true;
|
2012-11-25 14:53:55 -05:00
|
|
|
|
2012-11-27 10:51:51 -05:00
|
|
|
case 0x1e: // 1
|
2012-12-02 06:03:33 -05:00
|
|
|
CurrentLayout = tarmak1;
|
2012-11-25 14:53:55 -05:00
|
|
|
digitalWrite(modeLED, HIGH);
|
2013-02-04 13:19:14 -05:00
|
|
|
LatchKey(buf[keyPosition]);
|
2012-11-27 10:51:51 -05:00
|
|
|
return true;
|
2012-11-25 14:53:55 -05:00
|
|
|
|
2012-11-27 10:51:51 -05:00
|
|
|
case 0x1f: // 2
|
2012-12-02 06:03:33 -05:00
|
|
|
CurrentLayout = tarmak2;
|
2012-11-25 14:53:55 -05:00
|
|
|
digitalWrite(modeLED, HIGH);
|
2013-02-04 13:19:14 -05:00
|
|
|
LatchKey(buf[keyPosition]);
|
2012-11-27 10:51:51 -05:00
|
|
|
return true;
|
2012-11-25 14:53:55 -05:00
|
|
|
|
2012-11-27 10:51:51 -05:00
|
|
|
case 0x20: // 3
|
2012-12-02 06:03:33 -05:00
|
|
|
CurrentLayout = tarmak3;
|
2012-11-25 14:53:55 -05:00
|
|
|
digitalWrite(modeLED, HIGH);
|
2013-02-04 13:19:14 -05:00
|
|
|
LatchKey(buf[keyPosition]);
|
2012-11-27 10:51:51 -05:00
|
|
|
return true;
|
2012-11-25 14:53:55 -05:00
|
|
|
|
2012-11-27 10:51:51 -05:00
|
|
|
case 0x21: // 4
|
2012-12-02 06:03:33 -05:00
|
|
|
CurrentLayout = tarmak4;
|
2012-11-25 14:53:55 -05:00
|
|
|
digitalWrite(modeLED, HIGH);
|
2013-02-04 13:19:14 -05:00
|
|
|
LatchKey(buf[keyPosition]);
|
2012-11-27 10:51:51 -05:00
|
|
|
return true;
|
2012-11-25 14:53:55 -05:00
|
|
|
|
2012-11-27 10:51:51 -05:00
|
|
|
case 0x22: // 5
|
2012-12-02 06:03:33 -05:00
|
|
|
CurrentLayout = colemak;
|
2012-11-25 14:53:55 -05:00
|
|
|
digitalWrite(modeLED, HIGH);
|
2013-02-04 13:19:14 -05:00
|
|
|
LatchKey(buf[keyPosition]);
|
2012-11-27 10:51:51 -05:00
|
|
|
return true;
|
2012-11-25 14:53:55 -05:00
|
|
|
|
2012-11-27 10:51:51 -05:00
|
|
|
case 0x2c: // space bar
|
2012-11-25 14:53:55 -05:00
|
|
|
play_word_game();
|
2013-02-04 13:19:14 -05:00
|
|
|
LatchKey(buf[keyPosition]);
|
2012-11-27 10:51:51 -05:00
|
|
|
return true;
|
2012-11-25 14:53:55 -05:00
|
|
|
}
|
2013-02-04 13:19:14 -05:00
|
|
|
|
2012-11-27 10:51:51 -05:00
|
|
|
}
|
2012-12-02 06:03:33 -05:00
|
|
|
|
2012-11-27 10:51:51 -05:00
|
|
|
return false;
|
2012-11-25 14:53:55 -05:00
|
|
|
|
2012-11-27 10:51:51 -05:00
|
|
|
}
|
2012-11-25 14:53:55 -05:00
|
|
|
|
2013-01-06 18:14:49 -05:00
|
|
|
inline void SendKeysToHost (uint8_t *buf)
|
|
|
|
{
|
|
|
|
#ifdef TEENSY
|
2013-02-04 13:19:14 -05:00
|
|
|
Keyboard.set_modifier(buf[0]);
|
2013-01-06 18:14:49 -05:00
|
|
|
Keyboard.set_key1(buf[2]);
|
|
|
|
Keyboard.set_key2(buf[3]);
|
|
|
|
Keyboard.set_key3(buf[4]);
|
|
|
|
Keyboard.set_key4(buf[5]);
|
|
|
|
Keyboard.set_key5(buf[6]);
|
|
|
|
Keyboard.set_key6(buf[7]);
|
|
|
|
Keyboard.send_now();
|
|
|
|
#else
|
|
|
|
HID_SendReport(2,buf,8);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-02-04 13:19:14 -05:00
|
|
|
inline void LatchKey (uint8_t keyToLatch)
|
|
|
|
{
|
|
|
|
specialKeyLatch = keyToLatch;
|
|
|
|
specialKeyLatchReleased = false;
|
|
|
|
// Serial.print(keyToLatch);
|
|
|
|
// Serial.println(" is latched");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-01-06 18:14:49 -05:00
|
|
|
|
|
|
|
|
2012-11-25 14:53:55 -05:00
|
|
|
|
2012-12-02 06:03:33 -05:00
|
|
|
// *******************************************************************************************
|
|
|
|
// WORD GAME!!!
|
|
|
|
// *******************************************************************************************
|
2012-11-25 14:53:55 -05:00
|
|
|
|
2012-11-27 10:51:51 -05:00
|
|
|
void play_word_game(void)
|
2012-11-25 14:53:55 -05:00
|
|
|
{
|
2012-11-27 10:51:51 -05:00
|
|
|
char buffer[GAME_MAXWORDLENGTH];
|
|
|
|
char priorityAlphabets[10];
|
|
|
|
char *instrPtr;
|
|
|
|
|
|
|
|
uint16_t randNum;
|
|
|
|
|
2012-12-02 06:03:33 -05:00
|
|
|
switch (CurrentLayout) {
|
2012-11-27 10:51:51 -05:00
|
|
|
case tarmak1:
|
|
|
|
strcpy (priorityAlphabets, "nek");
|
|
|
|
break;
|
|
|
|
case tarmak2:
|
|
|
|
strcpy (priorityAlphabets, "ftg");
|
|
|
|
break;
|
|
|
|
case tarmak3:
|
|
|
|
strcpy (priorityAlphabets, "jyo");
|
|
|
|
break;
|
|
|
|
case tarmak4:
|
|
|
|
strcpy (priorityAlphabets, "lui");
|
|
|
|
break;
|
|
|
|
case colemak:
|
|
|
|
strcpy (priorityAlphabets, "rspd");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
strcpy (priorityAlphabets, "");
|
|
|
|
}
|
|
|
|
|
|
|
|
Keyboard.print( "Word game! Letters being prioritised: " );
|
|
|
|
Keyboard.println( priorityAlphabets );
|
|
|
|
|
|
|
|
for (int i = 0; i < 15; i++) {
|
|
|
|
if (priorityAlphabets[0] != 0) {
|
|
|
|
instrPtr = NULL;
|
|
|
|
while (instrPtr == NULL) {
|
|
|
|
randNum = random(GAME_NUMWORDS);
|
|
|
|
strcpy_P(buffer, (char*)pgm_read_word(&(game_word_list[randNum])));
|
|
|
|
instrPtr = strpbrk (buffer, priorityAlphabets);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
randNum = random(GAME_NUMWORDS);
|
|
|
|
strcpy_P(buffer, (char*)pgm_read_word(&(game_word_list[randNum])));
|
|
|
|
}
|
|
|
|
|
|
|
|
Keyboard.print( buffer );
|
|
|
|
Keyboard.print( " " );
|
|
|
|
}
|
|
|
|
Keyboard.println( "" );
|
2012-11-25 14:53:55 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-11-27 10:51:51 -05:00
|
|
|
USB Usb;
|
|
|
|
//USBHub Hub(&Usb);
|
2013-01-06 18:14:49 -05:00
|
|
|
HIDBoot<HID_PROTOCOL_KEYBOARD> ExtKeyboard(&Usb);
|
2012-11-25 14:53:55 -05:00
|
|
|
|
2012-11-27 10:51:51 -05:00
|
|
|
uint32_t next_time;
|
|
|
|
|
|
|
|
KbdRptParser Prs;
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-11-25 14:53:55 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
2012-11-27 10:51:51 -05:00
|
|
|
void setup()
|
|
|
|
{
|
|
|
|
randomSeed(analogRead(0));
|
|
|
|
|
|
|
|
// initialize the digital pin as an output.
|
|
|
|
pinMode(modeLED, OUTPUT);
|
|
|
|
|
|
|
|
Keyboard.begin();
|
2012-11-25 14:53:55 -05:00
|
|
|
|
|
|
|
#ifdef DEBUG
|
2012-11-27 10:51:51 -05:00
|
|
|
Serial.begin( 115200 );
|
|
|
|
Serial.println("Start");
|
2012-11-25 14:53:55 -05:00
|
|
|
#endif
|
|
|
|
|
2012-11-27 10:51:51 -05:00
|
|
|
if (Usb.Init() == -1)
|
2012-11-25 14:53:55 -05:00
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
Serial.println("OSC did not start.");
|
|
|
|
#else
|
|
|
|
delay( 1 );
|
|
|
|
#endif
|
|
|
|
|
2012-11-27 10:51:51 -05:00
|
|
|
delay( 200 );
|
|
|
|
|
|
|
|
next_time = millis() + 5000;
|
|
|
|
|
2013-01-06 18:14:49 -05:00
|
|
|
ExtKeyboard.SetReportParser(0, (HIDReportParser*)&Prs);
|
2012-11-25 14:53:55 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void loop()
|
|
|
|
{
|
2012-11-27 10:51:51 -05:00
|
|
|
Usb.Task();
|
2012-11-25 14:53:55 -05:00
|
|
|
|
|
|
|
}
|
|
|
|
|