Update to IDE 1.8.5

And latest version of USB Host Shield Library 2.0
This commit is contained in:
gdsports 2018-08-13 12:56:20 -10:00
parent 32119ce9d0
commit cc7b243650
2 changed files with 1687 additions and 1686 deletions

View File

@ -1,69 +1,69 @@
/* /*
Usage: Convert standard QWERTY keyboard to any keyboard layout you want Usage: Convert standard QWERTY keyboard to any keyboard layout you want
Default layout: QWERTY Default layout: QWERTY
Reserved key combination to switch layout: Reserved key combination to switch layout:
Ctrl-Shift 0 => QWERTY (DEFAULT) Ctrl-Shift 0 => QWERTY (DEFAULT)
Ctrl-Shift 1 => tarmak1 Ctrl-Shift 1 => tarmak1
Ctrl-Shift 2 => tarmak2 Ctrl-Shift 2 => tarmak2
Ctrl-Shift 3 => tarmak3 Ctrl-Shift 3 => tarmak3
Ctrl-Shift 4 => tarmak4 Ctrl-Shift 4 => tarmak4
Ctrl-Shift 5 => Colemak Ctrl-Shift 5 => Colemak
Ctrl-Shift 6 => Dvorak Ctrl-Shift 6 => Dvorak
Ctrl-Shift 7 => Workman Ctrl-Shift 7 => Workman
*/ */
#include <avr/pgmspace.h> #include <avr/pgmspace.h>
#include <Usb.h> #include <Usb.h>
#include <hidboot.h> #include <hidboot.h>
#include <Keyboard.h>
#include "keymapper_game.h" #include "keymapper_game.h"
//#define DEBUG #define DEBUG
#define modeLED 13 #define modeLED LED_BUILTIN
//#define TEENSY //uncomment this line if you are usin Teensy
// function definitions // function definitions
bool HandleReservedKeystrokes(HID *hid, uint8_t *buf); bool HandleReservedKeystrokes(USBHID *hid, uint8_t *buf);
inline void SendKeysToHost (uint8_t *buf); inline void SendKeysToHost (uint8_t *buf);
void play_word_game(void); void play_word_game(void);
inline void LatchKey (uint8_t keyToLatch); inline void LatchKey (uint8_t keyToLatch);
// variable definitions // variable definitions
typedef enum typedef enum
{ {
qwerty=0, qwerty = 0,
tarmak1, tarmak1,
tarmak2, tarmak2,
tarmak3, tarmak3,
tarmak4, tarmak4,
colemak, colemak,
dvorak, dvorak,
workman workman
} KeyboardLayout; } KeyboardLayout;
// Keymap based on the scancodes from 4 to 57, refer to the HID usage table on the meaning of each element // Keymap based on the scancodes from 4 to 57, refer to the HID usage table on the meaning of each element
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}; PROGMEM const 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};
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 const 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 const 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 const 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 const 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}; PROGMEM const 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};
PROGMEM prog_uint8_t dvorakKeymap[] = {4, 27, 13, 8, 55, 24, 12, 7, 6, 11, 23, 17, 16, 5, 21, 15, 52, 19, 18, 28, 10, 14, 54, 20, 9, 51, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 47, 48, 56, 46, 49, 50, 22, 45, 53, 26, 25, 29, 57}; PROGMEM const uint8_t dvorakKeymap[] = {4, 27, 13, 8, 55, 24, 12, 7, 6, 11, 23, 17, 16, 5, 21, 15, 52, 19, 18, 28, 10, 14, 54, 20, 9, 51, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 47, 48, 56, 46, 49, 50, 22, 45, 53, 26, 25, 29, 57};
PROGMEM prog_uint8_t workmanKeymap[] = {4, 25, 16, 11, 21, 23, 10, 28, 24, 17, 8, 18, 15, 14, 19, 51, 20, 26, 22, 5, 9, 6, 7, 27, 13, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 12, 52, 53, 54, 55, 56, 42}; PROGMEM const uint8_t workmanKeymap[] = {4, 25, 16, 11, 21, 23, 10, 28, 24, 17, 8, 18, 15, 14, 19, 51, 20, 26, 22, 5, 9, 6, 7, 27, 13, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 12, 52, 53, 54, 55, 56, 42};
const uint8_t *Keymap[] = const uint8_t *Keymap[] =
{ {
qwertyKeymap, qwertyKeymap,
tarmak1Keymap, tarmak1Keymap,
tarmak2Keymap, tarmak2Keymap,
tarmak3Keymap, tarmak3Keymap,
tarmak4Keymap, tarmak4Keymap,
colemakKeymap, colemakKeymap,
dvorakKeymap, dvorakKeymap,
workmanKeymap workmanKeymap
}; };
// global variables // global variables
@ -71,15 +71,15 @@ const uint8_t *Keymap[] =
//uint16_t ledBlinkDelay = 500; //uint16_t ledBlinkDelay = 500;
KeyboardLayout CurrentLayout = qwerty; KeyboardLayout CurrentLayout = qwerty;
uint8_t KeyBuffer[8] = {0,0,0,0,0,0,0,0}; uint8_t KeyBuffer[8] = {0, 0, 0, 0, 0, 0, 0, 0};
uint8_t specialKeyLatch=0; uint8_t specialKeyLatch = 0;
bool specialKeyLatchReleased = false; bool specialKeyLatchReleased = false;
class KbdRptParser : public KeyboardReportParser class KbdRptParser : public KeyboardReportParser
{ {
protected: protected:
virtual void Parse(HID *hid, bool is_rpt_id, uint8_t len, uint8_t *buf); virtual void Parse(USBHID *hid, bool is_rpt_id, uint8_t len, uint8_t *buf);
}; };
@ -87,179 +87,179 @@ protected:
// Parse // Parse
// ******************************************************************************************* // *******************************************************************************************
void KbdRptParser::Parse(HID *hid, bool is_rpt_id, uint8_t len, uint8_t *buf) void KbdRptParser::Parse(USBHID *hid, bool is_rpt_id, uint8_t len, uint8_t *buf)
{ {
uint8_t i; uint8_t i;
// On error - return
if (buf[2] == 1)
return;
// for (uint8_t i=0; i<8; i++) // On error - return
// { if (buf[2] == 1)
// PrintHex(buf[i]); return;
// Serial.print(" ");
// }
// Serial.println("");
KeyBuffer[0] = buf[0]; // for (uint8_t i=0; i<8; i++)
// {
// PrintHex(buf[i]);
if (!HandleReservedKeystrokes(hid, buf)) // Serial.print(" ");
// }
// Serial.println("");
KeyBuffer[0] = buf[0];
if (!HandleReservedKeystrokes(hid, buf))
{
specialKeyLatchReleased = true;
// remap all keys according to the existing keymap
for (i = 2; i < 8; i++)
{ {
specialKeyLatchReleased = true;
// handle special case of Shift-CAPSLOCK to be ignored by the remapper
// remap all keys according to the existing keymap if (buf[i] == KEY_CAPS_LOCK && buf[0] & 0x22)
for (i=2; i<8; i++)
{ {
KeyBuffer[i] = KEY_CAPS_LOCK;
// handle special case of Shift-CAPSLOCK to be ignored by the remapper LatchKey(KEY_CAPS_LOCK);
if (buf[i] == KEY_CAPS_LOCK && buf[0] & 0x22) }
else
{
// 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)
{ {
KeyBuffer[i] = KEY_CAPS_LOCK; // if it was a special key of shift-CAPS, then only allow mapping if the key has been released at least once
LatchKey(KEY_CAPS_LOCK); 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 else
{ KeyBuffer[i] = buf[i];
// 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];
}
// check locking keys
HandleLockingKeys(hid, KeyBuffer[i]); // check locking keys
} HandleLockingKeys(hid, KeyBuffer[i]);
}
// reset latch if key is released
if (specialKeyLatchReleased) // reset latch if key is released
{ if (specialKeyLatchReleased)
// Serial.println("latch is released"); {
specialKeyLatch = 0; // Serial.println("latch is released");
} specialKeyLatch = 0;
}
// send out key press
SendKeysToHost (KeyBuffer); // send out key press
SendKeysToHost (KeyBuffer);
// for (uint8_t i=0; i<8; i++) // for (uint8_t i=0; i<8; i++)
// { // {
// PrintHex(KeyBuffer[i]); // PrintHex(KeyBuffer[i]);
// Serial.print(" "); // Serial.print(" ");
// } // }
// Serial.println(""); // Serial.println("");
// Serial.println(""); // Serial.println("");
} }
}; };
bool HandleReservedKeystrokes(HID *hid, uint8_t *buf) // return true if it is a reserved keystroke bool HandleReservedKeystrokes(USBHID *hid, uint8_t *buf) // return true if it is a reserved keystroke
{ {
uint8_t mod = buf[0]; // read the modifier byte uint8_t mod = buf[0]; // read the modifier byte
uint8_t numKeysPressed = 0; uint8_t numKeysPressed = 0;
uint8_t keyPosition = 0; uint8_t keyPosition = 0;
// check that there is only 1 single key that is pressed // check that there is only 1 single key that is pressed
for (uint8_t i=2; i<8; i++) if (buf[i] > 0) { for (uint8_t i = 2; i < 8; i++) if (buf[i] > 0) {
numKeysPressed++; numKeysPressed++;
keyPosition = i; keyPosition = i;
} }
if (numKeysPressed != 1) return false; // only allow single keypress for reserved keystrokes (besides modifiers)
// check if we are changing layouts
if ((mod & 0x22) && (mod & 0x11)) { // Shift-Alt keystrokes
switch (buf[keyPosition]) {
case 0x27: // 0
CurrentLayout = qwerty;
digitalWrite(modeLED, LOW);
LatchKey(buf[keyPosition]);
return true;
case 0x1e: // 1 if (numKeysPressed != 1) return false; // only allow single keypress for reserved keystrokes (besides modifiers)
CurrentLayout = tarmak1;
digitalWrite(modeLED, HIGH);
LatchKey(buf[keyPosition]);
return true;
case 0x1f: // 2
CurrentLayout = tarmak2;
digitalWrite(modeLED, HIGH);
LatchKey(buf[keyPosition]);
return true;
case 0x20: // 3
CurrentLayout = tarmak3;
digitalWrite(modeLED, HIGH);
LatchKey(buf[keyPosition]);
return true;
case 0x21: // 4 // check if we are changing layouts
CurrentLayout = tarmak4; if ((mod & 0x22) && (mod & 0x11)) { // Shift-Alt keystrokes
digitalWrite(modeLED, HIGH); switch (buf[keyPosition]) {
LatchKey(buf[keyPosition]); case 0x27: // 0
return true; CurrentLayout = qwerty;
digitalWrite(modeLED, LOW);
LatchKey(buf[keyPosition]);
return true;
case 0x22: // 5 case 0x1e: // 1
CurrentLayout = colemak; CurrentLayout = tarmak1;
digitalWrite(modeLED, HIGH); digitalWrite(modeLED, HIGH);
LatchKey(buf[keyPosition]); LatchKey(buf[keyPosition]);
return true; return true;
case 0x23: // 6
CurrentLayout = dvorak;
digitalWrite(modeLED, HIGH);
LatchKey(buf[keyPosition]);
return true;
case 0x24: // 7
CurrentLayout = workman;
digitalWrite(modeLED, HIGH);
LatchKey(buf[keyPosition]);
return true;
case 0x2c: // space bar case 0x1f: // 2
play_word_game(); CurrentLayout = tarmak2;
LatchKey(buf[keyPosition]); digitalWrite(modeLED, HIGH);
return true; LatchKey(buf[keyPosition]);
} return true;
case 0x20: // 3
CurrentLayout = tarmak3;
digitalWrite(modeLED, HIGH);
LatchKey(buf[keyPosition]);
return true;
case 0x21: // 4
CurrentLayout = tarmak4;
digitalWrite(modeLED, HIGH);
LatchKey(buf[keyPosition]);
return true;
case 0x22: // 5
CurrentLayout = colemak;
digitalWrite(modeLED, HIGH);
LatchKey(buf[keyPosition]);
return true;
case 0x23: // 6
CurrentLayout = dvorak;
digitalWrite(modeLED, HIGH);
LatchKey(buf[keyPosition]);
return true;
case 0x24: // 7
CurrentLayout = workman;
digitalWrite(modeLED, HIGH);
LatchKey(buf[keyPosition]);
return true;
case 0x2c: // space bar
play_word_game();
LatchKey(buf[keyPosition]);
return true;
} }
return false; }
return false;
} }
inline void SendKeysToHost (uint8_t *buf) inline void SendKeysToHost (uint8_t *buf)
{ {
#ifdef TEENSY #ifdef TEENSYDUINO
Keyboard.set_modifier(buf[0]); Keyboard.set_modifier(buf[0]);
Keyboard.set_key1(buf[2]); Keyboard.set_key1(buf[2]);
Keyboard.set_key2(buf[3]); Keyboard.set_key2(buf[3]);
Keyboard.set_key3(buf[4]); Keyboard.set_key3(buf[4]);
Keyboard.set_key4(buf[5]); Keyboard.set_key4(buf[5]);
Keyboard.set_key5(buf[6]); Keyboard.set_key5(buf[6]);
Keyboard.set_key6(buf[7]); Keyboard.set_key6(buf[7]);
Keyboard.send_now(); Keyboard.send_now();
#else #else
HID_SendReport(2,buf,8); HID().SendReport(2, buf, 8);
#endif #endif
} }
@ -283,101 +283,101 @@ inline void LatchKey (uint8_t keyToLatch)
void play_word_game(void) void play_word_game(void)
{ {
char buffer[GAME_MAXWORDLENGTH]; char buffer[GAME_MAXWORDLENGTH];
char priorityAlphabets[10]; char priorityAlphabets[10];
char *instrPtr; char *instrPtr;
uint16_t randNum; uint16_t randNum;
switch (CurrentLayout) { switch (CurrentLayout) {
case tarmak1: case tarmak1:
strcpy (priorityAlphabets, "nek"); strcpy (priorityAlphabets, "nek");
break; break;
case tarmak2: case tarmak2:
strcpy (priorityAlphabets, "ftg"); strcpy (priorityAlphabets, "ftg");
break; break;
case tarmak3: case tarmak3:
strcpy (priorityAlphabets, "jyo"); strcpy (priorityAlphabets, "jyo");
break; break;
case tarmak4: case tarmak4:
strcpy (priorityAlphabets, "lui"); strcpy (priorityAlphabets, "lui");
break; break;
case colemak: case colemak:
strcpy (priorityAlphabets, "rspd"); strcpy (priorityAlphabets, "rspd");
break; break;
default: default:
strcpy (priorityAlphabets, ""); 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( "Word game! Letters being prioritised: " ); Keyboard.print( buffer );
Keyboard.println( priorityAlphabets ); Keyboard.print( " " );
}
for (int i = 0; i < 15; i++) { Keyboard.println( "" );
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( "" );
} }
USB Usb; USB Usb;
//USBHub Hub(&Usb); //USBHub Hub(&Usb);
HIDBoot<HID_PROTOCOL_KEYBOARD> ExtKeyboard(&Usb); HIDBoot<USB_HID_PROTOCOL_KEYBOARD> ExtKeyboard(&Usb);
uint32_t next_time; uint32_t next_time;
KbdRptParser Prs; KbdRptParser Prs;
void setup() void setup()
{ {
randomSeed(analogRead(0)); randomSeed(analogRead(0));
// initialize the digital pin as an output. // initialize the digital pin as an output.
pinMode(modeLED, OUTPUT); pinMode(modeLED, OUTPUT);
Keyboard.begin(); Keyboard.begin();
#ifdef DEBUG #ifdef DEBUG
Serial.begin( 115200 ); Serial.begin( 115200 );
Serial.println("Start"); while (!Serial) delay(1);
Serial.println("Start");
#endif #endif
if (Usb.Init() == -1) if (Usb.Init() == -1)
#ifdef DEBUG #ifdef DEBUG
Serial.println("OSC did not start."); Serial.println("USB host did not start.");
#else #else
delay( 1 ); delay( 1 );
#endif #endif
delay( 200 ); delay( 200 );
next_time = millis() + 5000; next_time = millis() + 5000;
ExtKeyboard.SetReportParser(0, (HIDReportParser*)&Prs); ExtKeyboard.SetReportParser(0, (HIDReportParser*)&Prs);
} }
void loop() void loop()
{ {
Usb.Task(); Usb.Task();
} }

File diff suppressed because it is too large Load Diff