template.c 2.8 KB

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