juancgarcia / HID-Relay

Arduino project to convert wired USB HID device to Bluetooth (pipes USB HID reports out through a Bluetooth HID module)
426 stars 77 forks source link

status indicators (Num, Caps) #11

Open kolivusha opened 4 years ago

kolivusha commented 4 years ago

I noticed that while pressing NumLock it changes actually switches the status, but keyboard LED is not showing this. Interestingly with the USBHIDBootKbd.ino Indicators work fine ( without switching it the status on the connected device) I was trying to compere this code to Code here to USBHIDBootKbd (part of USB 2.0 shiled library, and could not isolate the difference. Do you have any ideas on how to fix this ? (Thanks a lot for the code and the effort!)

ManoShu commented 4 years ago

I was able to set the indicator keys with those modifications:

1) On the command mode of the Bluetooth module, I changed the SH command from SH,0230 to SH,0238 (this enables bit 3, sending reports to the TX pin of the module). Restart the module (R,1) Now every time the module connects to a device (at least on windows, on android it doesn't send), it might send the current key state with this format: 0xFE 0x2 0x1 0x[LEDS], where LEDS is the byte containing the bits for each state (Num = bit 0, Caps = 1, Scroll = 2, etc.)

2) On my sketch (sadly Juan's sketch is not enough for my purpose), I listen to the bytes sent by the Bluetooth module for the key state message. When found, I pass the LEDS byte to the USB device like this:

uint8_t lockLeds = ...;  // the LEDS byte from the message
//hidSelector is an HIDComposite-inherited class that I use to receive keyboard/mouse raw HID data
//the HIDBoot<> classes should have access to the same method
auto res = hidSelector.SetReport(0, 0, 2, 0, 1, &lockLeds);
//error result
if (res != 0)
{
  Serial.print("[!!!!!!!] SetReport returned ");
  Serial.print(res, HEX);
  Serial.print(" for lock key state ");
  Serial.println(lockLeds, HEX);
}

The sketch files are here. I made a separate class to check for this message (bt_reporter).

Sorry if the code is a bit messy, but I hope it helped you.

EDIT: I also set the Bluetooth module baud rate to 57600 instead do 115200, as I am using SoftwareSerial for communication and was worried about not receiving the data properly. At this speed it's very rare to the key state message to become corrupted.

trantuananh1996 commented 3 years ago

Thanks @ManoShu, I've use your idea to modify my sketch, now my kbd can display status indicator :D At the time I made my sketch, I already set BT module to SH,0238, but I don't know why it not working, but with your code it working now :D