(NKRO) Keyboard improvements
This commit is contained in:
parent
3d86fb20d4
commit
b617fb0b3d
6 changed files with 127 additions and 89 deletions
|
|
@ -4,7 +4,7 @@
|
|||
|
||||
NKROKeyboard example
|
||||
|
||||
Press a button to press a lot of keys at the same time.
|
||||
Press a button to hold a lot of keys at the same time.
|
||||
NKRO can push 113 keys at the same time,
|
||||
the other Keyboards only 6 keys + 8 modifiers!
|
||||
|
||||
|
|
@ -26,36 +26,24 @@ void setup() {
|
|||
}
|
||||
|
||||
void loop() {
|
||||
// Press a lot of keys at the same time
|
||||
// Hold a lot of keys at the same time
|
||||
if (!digitalRead(pinButton)) {
|
||||
digitalWrite(pinLed, HIGH);
|
||||
|
||||
NKROKeyboard.press('a');
|
||||
NKROKeyboard.press('b');
|
||||
NKROKeyboard.press('c');
|
||||
NKROKeyboard.press('d');
|
||||
NKROKeyboard.press('e');
|
||||
NKROKeyboard.press('f');
|
||||
NKROKeyboard.press('g');
|
||||
NKROKeyboard.press('h');
|
||||
NKROKeyboard.press('i');
|
||||
NKROKeyboard.press('j');
|
||||
NKROKeyboard.press('k');
|
||||
NKROKeyboard.press('l');
|
||||
NKROKeyboard.press('m');
|
||||
NKROKeyboard.press('n');
|
||||
NKROKeyboard.press('o');
|
||||
NKROKeyboard.press('p');
|
||||
NKROKeyboard.press('q');
|
||||
NKROKeyboard.press('r');
|
||||
NKROKeyboard.press('s');
|
||||
NKROKeyboard.press('t');
|
||||
NKROKeyboard.press('u');
|
||||
NKROKeyboard.press('v');
|
||||
NKROKeyboard.press('w');
|
||||
NKROKeyboard.press('x');
|
||||
NKROKeyboard.press('y');
|
||||
NKROKeyboard.press('z');
|
||||
// Do not press to many at once or some OS will have problems.
|
||||
// Note that the resulting pressed order might differ,
|
||||
// because all keys are pressed at the same time.
|
||||
NKROKeyboard.addKeyToReport('0');
|
||||
NKROKeyboard.addKeyToReport('1');
|
||||
NKROKeyboard.addKeyToReport('2');
|
||||
NKROKeyboard.addKeyToReport('3');
|
||||
NKROKeyboard.addKeyToReport('4');
|
||||
NKROKeyboard.addKeyToReport('5');
|
||||
NKROKeyboard.addKeyToReport('6');
|
||||
NKROKeyboard.addKeyToReport('7');
|
||||
NKROKeyboard.addKeyToReport('8');
|
||||
NKROKeyboard.addKeyToReport('9');
|
||||
NKROKeyboard.send_now();
|
||||
|
||||
// Release all keys and hit enter
|
||||
NKROKeyboard.releaseAll();
|
||||
|
|
|
|||
|
|
@ -25,7 +25,9 @@ getLeds KEYWORD2
|
|||
pressKeycode KEYWORD2
|
||||
releaseKeycode KEYWORD2
|
||||
writeKeycode KEYWORD2
|
||||
addKeyToReport KEYWORD2
|
||||
addKeycodeToReport KEYWORD2
|
||||
removeKeyFromReport KEYWORD2
|
||||
removeKeycodeFromReport KEYWORD2
|
||||
|
||||
write_unicode KEYWORD2
|
||||
|
|
|
|||
|
|
@ -111,6 +111,10 @@ void Keyboard_::sendReport(HID_KeyboardReport_Data_t* keys)
|
|||
SendReport(keys,sizeof(HID_KeyboardReport_Data_t));
|
||||
}
|
||||
|
||||
void Keyboard_::send_now(void){
|
||||
sendReport(&_keyReport);
|
||||
}
|
||||
|
||||
#if defined(HID_KEYBOARD_LEDS_ENABLED)
|
||||
void Keyboard_::setReportData(const void* data, int len){
|
||||
// Save led state
|
||||
|
|
@ -130,43 +134,11 @@ uint8_t Keyboard_::getLeds(void){
|
|||
// call release(), releaseAll(), or otherwise clear the report and resend.
|
||||
size_t Keyboard_::press(uint8_t k)
|
||||
{
|
||||
uint8_t i;
|
||||
if (k >= 136) { // it's a non-printing key (not a modifier)
|
||||
k = k - 136;
|
||||
} else if (k >= 128) { // it's a modifier key
|
||||
_keyReport.modifiers |= (1<<(k-128));
|
||||
k = 0;
|
||||
} else { // it's a printing key
|
||||
k = pgm_read_byte(_asciimap + k);
|
||||
if (!k) {
|
||||
setWriteError();
|
||||
return 0;
|
||||
}
|
||||
if (k & SHIFT) { // it's a capital letter or other character reached with shift
|
||||
_keyReport.modifiers |= 0x02; // the left shift modifier
|
||||
k = k ^ SHIFT;
|
||||
}
|
||||
size_t ret = addKeyToReport(k);
|
||||
if(ret){
|
||||
sendReport(&_keyReport);
|
||||
}
|
||||
|
||||
// Add k to the key report only if it's not already present
|
||||
// and if there is an empty slot.
|
||||
if (_keyReport.keys[0] != k && _keyReport.keys[1] != k &&
|
||||
_keyReport.keys[2] != k && _keyReport.keys[3] != k &&
|
||||
_keyReport.keys[4] != k && _keyReport.keys[5] != k) {
|
||||
|
||||
for (i=0; i<6; i++) {
|
||||
if (_keyReport.keys[i] == 0x00) {
|
||||
_keyReport.keys[i] = k;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (i == 6) {
|
||||
setWriteError();
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
sendReport(&_keyReport);
|
||||
return 1;
|
||||
return ret;
|
||||
}
|
||||
|
||||
// release() takes the specified key out of the persistent key report and
|
||||
|
|
@ -229,6 +201,28 @@ size_t Keyboard_::pressKeycode(uint8_t k)
|
|||
sendReport(&_keyReport);
|
||||
}
|
||||
|
||||
size_t Keyboard_::addKeyToReport(uint8_t k)
|
||||
{
|
||||
if (k >= 136) { // it's a non-printing key (not a modifier)
|
||||
k = k - 136;
|
||||
} else if (k >= 128) { // it's a modifier key
|
||||
_keyReport.modifiers |= (1<<(k-128));
|
||||
k = 0;
|
||||
} else { // it's a printing key
|
||||
k = pgm_read_byte(_asciimap + k);
|
||||
if (!k) {
|
||||
setWriteError();
|
||||
return 0;
|
||||
}
|
||||
if (k & SHIFT) { // it's a capital letter or other character reached with shift
|
||||
_keyReport.modifiers |= 0x02; // the left shift modifier
|
||||
k = k ^ SHIFT;
|
||||
}
|
||||
}
|
||||
|
||||
return addKeycodeToReport(k);
|
||||
}
|
||||
|
||||
size_t Keyboard_::addKeycodeToReport(uint8_t k)
|
||||
{
|
||||
uint8_t index = 0;
|
||||
|
|
@ -283,6 +277,27 @@ size_t Keyboard_::releaseKeycode(uint8_t k)
|
|||
sendReport(&_keyReport);
|
||||
}
|
||||
|
||||
size_t Keyboard_::removeKeyFromReport(uint8_t k)
|
||||
{
|
||||
if (k >= 136) { // it's a non-printing key (not a modifier)
|
||||
k = k - 136;
|
||||
} else if (k >= 128) { // it's a modifier key
|
||||
_keyReport.modifiers &= ~(1<<(k-128));
|
||||
k = 0;
|
||||
} else { // it's a printing key
|
||||
k = pgm_read_byte(_asciimap + k);
|
||||
if (!k) {
|
||||
return 0;
|
||||
}
|
||||
if (k & SHIFT) { // it's a capital letter or other character reached with shift
|
||||
_keyReport.modifiers &= ~(0x02); // the left shift modifier
|
||||
k = k ^ SHIFT;
|
||||
}
|
||||
}
|
||||
|
||||
return removeKeycodeFromReport(k);
|
||||
}
|
||||
|
||||
size_t Keyboard_::removeKeycodeFromReport(uint8_t k)
|
||||
{
|
||||
uint8_t indexA;
|
||||
|
|
|
|||
|
|
@ -68,11 +68,14 @@ public:
|
|||
size_t press(uint8_t k);
|
||||
size_t release(uint8_t k);
|
||||
void releaseAll(void);
|
||||
void send_now(void);
|
||||
|
||||
size_t writeKeycode(uint8_t k);
|
||||
size_t pressKeycode(uint8_t k);
|
||||
size_t releaseKeycode(uint8_t k);
|
||||
size_t addKeyToReport(uint8_t k);
|
||||
size_t addKeycodeToReport(uint8_t k);
|
||||
size_t removeKeyFromReport(uint8_t k);
|
||||
size_t removeKeycodeFromReport(uint8_t k);
|
||||
|
||||
#if defined(HID_KEYBOARD_LEDS_ENABLED)
|
||||
|
|
|
|||
|
|
@ -116,6 +116,10 @@ void NKROKeyboard_::sendReport(HID_NKROKeyboardReport_Data_t* keys)
|
|||
SendReport(keys, sizeof(HID_NKROKeyboardReport_Data_t));
|
||||
}
|
||||
|
||||
void NKROKeyboard_::send_now(void){
|
||||
sendReport(&_keyReport);
|
||||
}
|
||||
|
||||
#if defined(HID_KEYBOARD_LEDS_ENABLED)
|
||||
void NKROKeyboard_::setReportData(const void* data, int len){
|
||||
// Save led state
|
||||
|
|
@ -134,31 +138,11 @@ uint8_t NKROKeyboard_::getLeds(void){
|
|||
// call release(), releaseAll(), or otherwise clear the report and resend.
|
||||
size_t NKROKeyboard_::press(uint8_t k)
|
||||
{
|
||||
// it's a non-printing key (not a modifier)
|
||||
if (k >= 136)
|
||||
k = k - 136;
|
||||
|
||||
// it's a modifier key
|
||||
else if (k >= 128)
|
||||
k = k - 128;
|
||||
|
||||
// it's a printing key
|
||||
else {
|
||||
k = pgm_read_byte(_asciimap + k);
|
||||
if (!k)
|
||||
return 0;
|
||||
|
||||
// it's a capital letter or other character reached with shift
|
||||
if (k & SHIFT) {
|
||||
// the left shift modifier
|
||||
_keyReport.modifiers |= 0x02;
|
||||
k = k ^ SHIFT;
|
||||
}
|
||||
size_t ret = addKeyToReport(k);
|
||||
if(ret){
|
||||
sendReport(&_keyReport);
|
||||
}
|
||||
|
||||
addKeycodeToReport(k);
|
||||
sendReport(&_keyReport);
|
||||
return 1;
|
||||
return ret;
|
||||
}
|
||||
|
||||
// release() takes the specified key out of the persistent key report and
|
||||
|
|
@ -189,7 +173,7 @@ size_t NKROKeyboard_::release(uint8_t k)
|
|||
}
|
||||
|
||||
removeKeycodeFromReport(k);
|
||||
sendReport(&_keyReport);
|
||||
sendReport(&_keyReport);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
|
@ -221,6 +205,28 @@ size_t NKROKeyboard_::pressKeycode(uint8_t k)
|
|||
sendReport(&_keyReport);
|
||||
}
|
||||
|
||||
size_t NKROKeyboard_::addKeyToReport(uint8_t k)
|
||||
{
|
||||
if (k >= 136) { // it's a non-printing key (not a modifier)
|
||||
k = k - 136;
|
||||
} else if (k >= 128) { // it's a modifier key
|
||||
_keyReport.modifiers |= (1<<(k-128));
|
||||
k = 0;
|
||||
} else { // it's a printing key
|
||||
k = pgm_read_byte(_asciimap + k);
|
||||
if (!k) {
|
||||
setWriteError();
|
||||
return 0;
|
||||
}
|
||||
if (k & SHIFT) { // it's a capital letter or other character reached with shift
|
||||
_keyReport.modifiers |= 0x02; // the left shift modifier
|
||||
k = k ^ SHIFT;
|
||||
}
|
||||
}
|
||||
|
||||
return addKeycodeToReport(k);
|
||||
}
|
||||
|
||||
size_t NKROKeyboard_::addKeycodeToReport(uint8_t k)
|
||||
{
|
||||
// keymap key
|
||||
|
|
@ -252,6 +258,27 @@ size_t NKROKeyboard_::releaseKeycode(uint8_t k)
|
|||
sendReport(&_keyReport);
|
||||
}
|
||||
|
||||
size_t NKROKeyboard_::removeKeyFromReport(uint8_t k)
|
||||
{
|
||||
if (k >= 136) { // it's a non-printing key (not a modifier)
|
||||
k = k - 136;
|
||||
} else if (k >= 128) { // it's a modifier key
|
||||
_keyReport.modifiers &= ~(1<<(k-128));
|
||||
k = 0;
|
||||
} else { // it's a printing key
|
||||
k = pgm_read_byte(_asciimap + k);
|
||||
if (!k) {
|
||||
return 0;
|
||||
}
|
||||
if (k & SHIFT) { // it's a capital letter or other character reached with shift
|
||||
_keyReport.modifiers &= ~(0x02); // the left shift modifier
|
||||
k = k ^ SHIFT;
|
||||
}
|
||||
}
|
||||
|
||||
return removeKeycodeFromReport(k);
|
||||
}
|
||||
|
||||
size_t NKROKeyboard_::removeKeycodeFromReport(uint8_t k)
|
||||
{
|
||||
// keymap key
|
||||
|
|
|
|||
|
|
@ -67,11 +67,14 @@ public:
|
|||
size_t press(uint8_t k);
|
||||
size_t release(uint8_t k);
|
||||
void releaseAll(void);
|
||||
void send_now(void);
|
||||
|
||||
size_t writeKeycode(uint8_t k);
|
||||
size_t pressKeycode(uint8_t k);
|
||||
size_t releaseKeycode(uint8_t k);
|
||||
size_t addKeyToReport(uint8_t k);
|
||||
size_t addKeycodeToReport(uint8_t k);
|
||||
size_t removeKeyFromReport(uint8_t k);
|
||||
size_t removeKeycodeFromReport(uint8_t k);
|
||||
|
||||
#if defined(HID_KEYBOARD_LEDS_ENABLED)
|
||||
|
|
|
|||
Loading…
Reference in a new issue