keybrd library is an open source library for creating custom-keyboard firmware.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

Code_LEDLock.cpp 2.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. #include "Code_LEDLock.h"
  2. /* USB_LED_bit are codes from http://www.usb.org/developers/hidpage/HID1_11.pdf keyboard output report
  3. */
  4. Code_LEDLock::Code_LEDLock(const uint16_t scancode, LED& refLED)
  5. : scancode(scancode), refLED(refLED)
  6. {
  7. switch (scancode) //initilize USB_LED_bit for given scancode
  8. {
  9. case KEY_NUM_LOCK:
  10. USB_LED_bit = 1<<0;
  11. break;
  12. case KEY_CAPS_LOCK:
  13. USB_LED_bit = 1<<1;
  14. break;
  15. case KEY_SCROLL_LOCK:
  16. USB_LED_bit = 1<<2;
  17. break;
  18. /* guessing at these case names:
  19. case KEY_COMPOSE: //for separate accent keys
  20. USB_LED_bit = 1<<3; break;
  21. break;
  22. case KEY_KANA: //for Japanese keyboards
  23. USB_LED_bit = 1<<4; break;
  24. break;
  25. */
  26. }
  27. }
  28. void Code_LEDLock::press()
  29. {
  30. Keyboard.press(scancode);
  31. updateLED();
  32. }
  33. void Code_LEDLock::release()
  34. {
  35. Keyboard.release(scancode);
  36. }
  37. /* updateLED() is a separate function from press() because Arduino boards may need a different implementation.
  38. updateLED() has been tested on teensy 2.0.
  39. The variable "keyboard_leds" is in /opt/arduino-1.6.7/hardware/teensy/avr/cores/usb_hid/usb.c
  40. // 1=num lock, 2=caps lock, 4=scroll lock, 8=compose, 16=kana
  41. https://forum.pjrc.com/threads/25368-How-do-I-receive-a-numlock-capslock-LED-signal-from-the-PC
  42. updateLED() has NOT been tested on an Arduino board.
  43. The word "keyboard_leds does not appear in "Arduino\hardware\arduino\cores\
  44. This shows how to hack KeyReport in Arduino: https://www.sparkfun.com/tutorials/337
  45. TMK firmware uses variable "usb_led" instead of "keyboard_leds"
  46. http://deskthority.net/workshop-f7/how-to-build-your-very-own-keyboard-firmware-t7177.html >usb_led
  47. */
  48. void Code_LEDLock::updateLED() const
  49. {
  50. /* KEY_SCROLL_LOCK is not working on Teensy2.0, it prints keyboard_leds=0, maybe Linux doesn't have it.
  51. Here is the debug code:
  52. Keyboard.print(F(" keyboard_leds="));
  53. Keyboard.print(keyboard_leds);//KEY_NUM_LOCK:1, KEY_CAPS_LOCK:2, KEY_SCROLL_LOCK:0
  54. Keyboard.print(" ");
  55. */
  56. if (keyboard_leds & USB_LED_bit) //if LED status bit is set
  57. {
  58. refLED.off(); //LED on/off seem inverted, but it works
  59. }
  60. else
  61. {
  62. refLED.on();
  63. }
  64. }