Skip to content

Add LED Reporting support to Keyboard emulation. #38

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 57 additions & 29 deletions src/Keyboard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,32 +35,42 @@ static const uint8_t _hidReportDescriptor[] PROGMEM = {
0xa1, 0x01, // COLLECTION (Application)
0x85, 0x02, // REPORT_ID (2)
0x05, 0x07, // USAGE_PAGE (Keyboard)

0x19, 0xe0, // USAGE_MINIMUM (Keyboard LeftControl)
0x29, 0xe7, // USAGE_MAXIMUM (Keyboard Right GUI)
0x15, 0x00, // LOGICAL_MINIMUM (0)
0x25, 0x01, // LOGICAL_MAXIMUM (1)
0x75, 0x01, // REPORT_SIZE (1)

0x95, 0x08, // REPORT_COUNT (8)
0x81, 0x02, // INPUT (Data,Var,Abs)
0x95, 0x01, // REPORT_COUNT (1)
0x75, 0x08, // REPORT_SIZE (8)
0x81, 0x03, // INPUT (Cnst,Var,Abs)


0x95, 0x05, // REPORT_COUNT (5)
0x75, 0x01, // REPORT_SIZE (1)
0x05, 0x08, // USAGE_PAGE (LEDs)
0x19, 0x01, // USAGE_MINIMUM (1)
0x29, 0x05, // USAGE_MAXIMUM (5)
0x91, 0x02, // OUTPUT (Data,Var,Abs) // LED report
0x95, 0x01, // REPORT_COUNT (1)
0x75, 0x03, // REPORT_SIZE (3)
0x91, 0x01, // OUTPUT (Constant) // padding

0x95, 0x06, // REPORT_COUNT (6)
0x75, 0x08, // REPORT_SIZE (8)
0x15, 0x00, // LOGICAL_MINIMUM (0)
0x25, 0x73, // LOGICAL_MAXIMUM (115)
0x05, 0x07, // USAGE_PAGE (Keyboard)

0x19, 0x00, // USAGE_MINIMUM (Reserved (no event indicated))
0x29, 0x73, // USAGE_MAXIMUM (Keyboard Application)
0x81, 0x00, // INPUT (Data,Ary,Abs)
0xc0, // END_COLLECTION
};

Keyboard_::Keyboard_(void)
Keyboard_::Keyboard_(void)
{
static HIDSubDescriptor node(_hidReportDescriptor, sizeof(_hidReportDescriptor));
HID().AppendDescriptor(&node);
Expand Down Expand Up @@ -91,16 +101,16 @@ const uint8_t _asciimap[128] =
0x00, // ETX
0x00, // EOT
0x00, // ENQ
0x00, // ACK
0x00, // ACK
0x00, // BEL
0x2a, // BS Backspace
0x2b, // TAB Tab
0x28, // LF Enter
0x00, // VT
0x00, // FF
0x00, // CR
0x00, // SO
0x00, // SI
0x00, // VT
0x00, // FF
0x00, // CR
0x00, // SO
0x00, // SI
0x00, // DEL
0x00, // DC1
0x00, // DC2
Expand All @@ -110,13 +120,13 @@ const uint8_t _asciimap[128] =
0x00, // SYN
0x00, // ETB
0x00, // CAN
0x00, // EM
0x00, // EM
0x00, // SUB
0x00, // ESC
0x00, // FS
0x00, // GS
0x00, // RS
0x00, // US
0x00, // FS
0x00, // GS
0x00, // RS
0x00, // US

0x2c, // ' '
0x1e|SHIFT, // !
Expand Down Expand Up @@ -220,10 +230,10 @@ const uint8_t _asciimap[128] =
uint8_t USBPutChar(uint8_t c);

// press() adds the specified key (printing, non-printing, or modifier)
// to the persistent key report and sends the report. Because of the way
// USB HID works, the host acts like the key remains pressed until we
// to the persistent key report and sends the report. Because of the way
// USB HID works, the host acts like the key remains pressed until we
// call release(), releaseAll(), or otherwise clear the report and resend.
size_t Keyboard_::press(uint8_t k)
size_t Keyboard_::press(uint8_t k)
{
uint8_t i;
if (k >= 136) { // it's a non-printing key (not a modifier)
Expand All @@ -242,13 +252,13 @@ size_t Keyboard_::press(uint8_t k)
k &= 0x7F;
}
}

// 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 &&
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;
Expand All @@ -258,7 +268,7 @@ size_t Keyboard_::press(uint8_t k)
if (i == 6) {
setWriteError();
return 0;
}
}
}
sendReport(&_keyReport);
return 1;
Expand All @@ -267,7 +277,7 @@ size_t Keyboard_::press(uint8_t k)
// release() takes the specified key out of the persistent key report and
// sends the report. This tells the OS the key is no longer pressed and that
// it shouldn't be repeated any more.
size_t Keyboard_::release(uint8_t k)
size_t Keyboard_::release(uint8_t k)
{
uint8_t i;
if (k >= 136) { // it's a non-printing key (not a modifier)
Expand All @@ -285,7 +295,7 @@ size_t Keyboard_::release(uint8_t k)
k &= 0x7F;
}
}

// Test the key report to see if k is present. Clear it if it exists.
// Check all positions in case the key is present more than once (which it shouldn't be)
for (i=0; i<6; i++) {
Expand All @@ -301,11 +311,11 @@ size_t Keyboard_::release(uint8_t k)
void Keyboard_::releaseAll(void)
{
_keyReport.keys[0] = 0;
_keyReport.keys[1] = 0;
_keyReport.keys[1] = 0;
_keyReport.keys[2] = 0;
_keyReport.keys[3] = 0;
_keyReport.keys[3] = 0;
_keyReport.keys[4] = 0;
_keyReport.keys[5] = 0;
_keyReport.keys[5] = 0;
_keyReport.modifiers = 0;
sendReport(&_keyReport);
}
Expand All @@ -332,7 +342,25 @@ size_t Keyboard_::write(const uint8_t *buffer, size_t size) {
return n;
}

bool Keyboard_::getLedStatus(uint8_t led)
{
uint8_t _keyboardLedsStatus = HID().getKeyboardLedsStatus();
if (led == LED_CAPS_LOCK) {
if (_keyboardLedsStatus == 2 || _keyboardLedsStatus == 3 || _keyboardLedsStatus == 6 || _keyboardLedsStatus == 7) {
return true;
}
} else if (led == LED_NUM_LOCK) {
if (_keyboardLedsStatus == 1 || _keyboardLedsStatus == 3 || _keyboardLedsStatus == 5 || _keyboardLedsStatus == 7) {
return true;
}
} else if (led == LED_SCROLL_LOCK) {
if (_keyboardLedsStatus == 4 || _keyboardLedsStatus == 5 || _keyboardLedsStatus == 6 || _keyboardLedsStatus == 7) {
return true;
}
}
return false;
}

Keyboard_ Keyboard;

#endif

8 changes: 7 additions & 1 deletion src/Keyboard.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@
#define KEY_HOME 0xD2
#define KEY_END 0xD5
#define KEY_CAPS_LOCK 0xC1
#define KEY_NUM_LOCK 0xDB
#define KEY_SCROLL_LOCK 0xCF
#define KEY_F1 0xC2
#define KEY_F2 0xC3
#define KEY_F3 0xC4
Expand All @@ -84,6 +86,10 @@
#define KEY_F24 0xFB


#define LED_CAPS_LOCK 0x02
#define LED_NUM_LOCK 0x01
#define LED_SCROLL_LOCK 0x04

// Low level key report: up to 6 keys and shift, ctrl etc at once
typedef struct
{
Expand All @@ -106,9 +112,9 @@ class Keyboard_ : public Print
size_t press(uint8_t k);
size_t release(uint8_t k);
void releaseAll(void);
bool getLedStatus(uint8_t led);
};
extern Keyboard_ Keyboard;

#endif
#endif