preonic.c 2.3 KB

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