kc60.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. #include "kc60.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 OCR1B
  43. void backlight_init_ports()
  44. {
  45. // Setup PB6 as output and output low.
  46. DDRB |= (1<<6);
  47. PORTB &= ~(1<<6);
  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 = COM1B1=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(COM1B1) | _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. // Prevent backlight blink on lowest level
  65. PORTB &= ~(_BV(PORTB6));
  66. if ( level == 0 )
  67. {
  68. // Turn off PWM control on PB6, revert to output low.
  69. TCCR1A &= ~(_BV(COM1B1));
  70. CHANNEL = 0x0;
  71. }
  72. else if ( level == BACKLIGHT_LEVELS)
  73. {
  74. // Turn on PWM control of PB6
  75. TCCR1A |= _BV(COM1B1);
  76. // Set the brightness
  77. CHANNEL = 0xFFFF;
  78. }
  79. else
  80. {
  81. // Turn on PWM control of PB6
  82. TCCR1A |= _BV(COM1B1);
  83. // Set the brightness
  84. CHANNEL = 0xFFFF >> ((BACKLIGHT_LEVELS - level) * ((BACKLIGHT_LEVELS + 1) / 2));
  85. }
  86. }
  87. #endif