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