upload
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.

template.c 2.8KB

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