satan.c 2.1 KB

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