backlight.c 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. /**
  2. * Backlighting code for PS2AVRGB boards (ATMEGA32A)
  3. * Kenneth A. (github.com/krusli | krusli.me)
  4. */
  5. #include "quantum.h"
  6. #include <avr/pgmspace.h>
  7. #include <avr/interrupt.h>
  8. // Port D: digital pins of the AVR chipset
  9. #define NUMLOCK_PORT (1 << 1) // 1st pin of Port D (digital)
  10. #define CAPSLOCK_PORT (1 << 2) // 2nd pin
  11. #define BACKLIGHT_PORT (1 << 4) // 4th pin
  12. #define SCROLLLOCK_PORT (1 << 6) // 6th pin
  13. /**
  14. * References
  15. * Port Registers: https://www.arduino.cc/en/Reference/PortManipulation
  16. * TCCR1A: https://electronics.stackexchange.com/questions/92350/what-is-the-difference-between-tccr1a-and-tccr1b
  17. * Timers: http://www.avrbeginners.net/architecture/timers/timers.html
  18. * 16-bit timer setup: http://sculland.com/ATmega168/Interrupts-And-Timers/16-Bit-Timer-Setup/
  19. * PS2AVRGB firmware: https://github.com/showjean/ps2avrU/tree/master/firmware
  20. */
  21. // @Override
  22. // turn LEDs on and off depending on USB caps/num/scroll lock states.
  23. void led_set_user(uint8_t usb_led) {
  24. /* It appears that these cause the v1 JJ40 PCB to hang.
  25. * I haven't looked into why, but I don't have any LEDs on my board anyway. */
  26. #if 0
  27. if (usb_led & (1 << USB_LED_NUM_LOCK)) {
  28. // turn on
  29. DDRD |= NUMLOCK_PORT;
  30. PORTD |= NUMLOCK_PORT;
  31. } else {
  32. // turn off
  33. DDRD &= ~NUMLOCK_PORT;
  34. PORTD &= ~NUMLOCK_PORT;
  35. }
  36. if (usb_led & (1 << USB_LED_CAPS_LOCK)) {
  37. DDRD |= CAPSLOCK_PORT;
  38. PORTD |= CAPSLOCK_PORT;
  39. } else {
  40. DDRD &= ~CAPSLOCK_PORT;
  41. PORTD &= ~CAPSLOCK_PORT;
  42. }
  43. if (usb_led & (1 << USB_LED_SCROLL_LOCK)) {
  44. DDRD |= SCROLLLOCK_PORT;
  45. PORTD |= SCROLLLOCK_PORT;
  46. } else {
  47. DDRD &= ~SCROLLLOCK_PORT;
  48. PORTD &= ~SCROLLLOCK_PORT;
  49. }
  50. #endif
  51. }