arrow_pad.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #include "arrow_pad.h"
  2. __attribute__ ((weak))
  3. void matrix_init_user(void) {
  4. // leave this function blank - it can be defined in a keymap file
  5. };
  6. __attribute__ ((weak))
  7. void matrix_scan_user(void) {
  8. // leave this function blank - it can be defined in a keymap file
  9. }
  10. __attribute__ ((weak))
  11. bool process_action_user(keyrecord_t *record) {
  12. // leave this function blank - it can be defined in a keymap file
  13. return true;
  14. }
  15. __attribute__ ((weak))
  16. void led_set_user(uint8_t usb_led) {
  17. // leave this function blank - it can be defined in a keymap file
  18. }
  19. void matrix_init_kb(void) {
  20. // put your keyboard start-up code here
  21. // runs once when the firmware starts up
  22. #ifdef BACKLIGHT_ENABLE
  23. backlight_init_ports();
  24. #endif
  25. matrix_init_user();
  26. }
  27. void matrix_scan_kb(void) {
  28. // put your looping keyboard code here
  29. // runs every cycle (a lot)
  30. matrix_scan_user();
  31. }
  32. bool process_action_kb(keyrecord_t *record) {
  33. // put your per-action keyboard code here
  34. // runs for every action, just before processing by the firmware
  35. return process_action_user(record);
  36. }
  37. void led_set_kb(uint8_t usb_led) {
  38. // put your keyboard LED indicator (ex: Caps Lock LED) toggling code here
  39. led_set_user(usb_led);
  40. }
  41. #ifdef BACKLIGHT_ENABLE
  42. #define CHANNEL OCR1C
  43. void backlight_init_ports()
  44. {
  45. // Setup PB7 as output and output low.
  46. DDRB |= (1<<7);
  47. PORTB &= ~(1<<7);
  48. // Use full 16-bit resolution.
  49. ICR1 = 0xFFFF;
  50. // I could write a wall of text here to explain... but TL;DW
  51. // Go read the ATmega32u4 datasheet.
  52. // And this: http://blog.saikoled.com/post/43165849837/secret-konami-cheat-code-to-high-resolution-pwm-on
  53. // Pin PB7 = OCR1C (Timer 1, Channel C)
  54. // Compare Output Mode = Clear on compare match, Channel C = COM1C1=1 COM1C0=0
  55. // (i.e. start high, go low when counter matches.)
  56. // WGM Mode 14 (Fast PWM) = WGM13=1 WGM12=1 WGM11=1 WGM10=0
  57. // Clock Select = clk/1 (no prescaling) = CS12=0 CS11=0 CS10=1
  58. TCCR1A = _BV(COM1C1) | _BV(WGM11); // = 0b00001010;
  59. TCCR1B = _BV(WGM13) | _BV(WGM12) | _BV(CS10); // = 0b00011001;
  60. backlight_init();
  61. }
  62. void backlight_set(uint8_t level)
  63. {
  64. if ( level == 0 )
  65. {
  66. // Turn off PWM control on PB7, revert to output low.
  67. TCCR1A &= ~(_BV(COM1C1));
  68. CHANNEL = 0x0;
  69. // Prevent backlight blink on lowest level
  70. PORTB &= ~(_BV(PORTB7));
  71. }
  72. else if ( level == BACKLIGHT_LEVELS )
  73. {
  74. // Prevent backlight blink on lowest level
  75. PORTB &= ~(_BV(PORTB7));
  76. // Turn on PWM control of PB7
  77. TCCR1A |= _BV(COM1C1);
  78. // Set the brightness
  79. CHANNEL = 0xFFFF;
  80. }
  81. else
  82. {
  83. // Prevent backlight blink on lowest level
  84. PORTB &= ~(_BV(PORTB7));
  85. // Turn on PWM control of PB7
  86. TCCR1A |= _BV(COM1C1);
  87. // Set the brightness
  88. CHANNEL = 0xFFFF >> ((BACKLIGHT_LEVELS - level) * ((BACKLIGHT_LEVELS + 1) / 2));
  89. }
  90. }
  91. #endif