diff --git a/Firmware/OpeNITHM/USBOutput.cpp b/Firmware/OpeNITHM/USBOutput.cpp index c53bd5f..38df788 100644 --- a/Firmware/OpeNITHM/USBOutput.cpp +++ b/Firmware/OpeNITHM/USBOutput.cpp @@ -24,62 +24,121 @@ void USBOutput::sendKeyEvent(int key, bool pressed, bool doublePressed) void USBOutput::sendSensorEvent(float position) { // Send hand up / hand down key - if (position > lastPosition) { + if (position > lastPosition) + { writeKey(KEY_PAGE_UP); + if (wasAirHeld) + { + // Send Air Action + writeKey(KEY_END); + } } - if (position < lastPosition) { + if (position < lastPosition) + { writeKey(KEY_PAGE_DOWN); + if (wasAirHeld) + { + // Send Air Action + writeKey(KEY_END); + } } // Send hand seen / unseen key - if (position > 0.05f) { + if (position > 0.05f) + { pressKey(KEY_HOME); + wasAirHeld = true; } else { releaseKey(KEY_HOME); + wasAirHeld = false; } - // Send hand moved key - writeKey(KEY_END); - + // Save last position for next loop + lastPosition = position; } -USBOutput::USBOutput() { +USBOutput::USBOutput() +{ #ifndef TEENSY NKROKeyboard.begin(); #endif lastPosition = 0; } -void USBOutput::writeKey(char key) +void USBOutput::writeKey(uint16_t key) { #ifndef TEENSY NKROKeyboard.write(key); #else - Nkro.set_key_ascii((uint8_t)key); - Nkro.send_nkro_now(); - Nkro.reset_key_ascii((uint8_t)key); - Nkro.send_nkro_now(); + switch (key) + { + case KEY_PAGE_UP: + case KEY_PAGE_DOWN: + case KEY_HOME: + case KEY_END: + // Don't use ASCII method + Nkro.set_key(key); + Nkro.send_nkro_now(); + Nkro.reset_key(key); + Nkro.send_nkro_now(); + break; + default: + // Use NKRO for ASCII + Nkro.set_key_ascii(key); + Nkro.send_nkro_now(); + Nkro.reset_key_ascii(key); + Nkro.send_nkro_now(); + break; + } #endif } -void USBOutput::pressKey(char key) +void USBOutput::pressKey(uint16_t key) { #ifndef TEENSY pressKey(key); #else - Nkro.set_key_ascii((uint8_t)key); - Nkro.send_nkro_now(); + switch (key) + { + case KEY_PAGE_UP: + case KEY_PAGE_DOWN: + case KEY_HOME: + case KEY_END: + // Don't use ASCII method + Nkro.set_key(key); + Nkro.send_nkro_now(); + break; + default: + // Use NKRO for ASCII + Nkro.set_key_ascii(key); + Nkro.send_nkro_now(); + break; + } #endif } -void USBOutput::releaseKey(char key) +void USBOutput::releaseKey(uint16_t key) { #ifndef TEENSY NKROKeyboard.release(key); #else - Nkro.reset_key_ascii((uint8_t)key); - Nkro.send_nkro_now(); + switch (key) + { + case KEY_PAGE_UP: + case KEY_PAGE_DOWN: + case KEY_HOME: + case KEY_END: + // Don't use ASCII method + Nkro.reset_key(key); + Nkro.send_nkro_now(); + break; + default: + // Use NKRO for ASCII + Nkro.reset_key_ascii(key); + Nkro.send_nkro_now(); + break; + } #endif } diff --git a/Firmware/OpeNITHM/USBOutput.h b/Firmware/OpeNITHM/USBOutput.h index f753f8b..734c301 100644 --- a/Firmware/OpeNITHM/USBOutput.h +++ b/Firmware/OpeNITHM/USBOutput.h @@ -20,9 +20,10 @@ class USBOutput : public Output { private: float lastPosition; - void writeKey(char key); - void pressKey(char key); - void releaseKey(char key); + bool wasAirHeld; + void writeKey(uint16_t key); + void pressKey(uint16_t key); + void releaseKey(uint16_t key); public: void sendKeyEvent(int key, bool pressed, bool doublePressed) override; void sendSensorEvent(float position) override;