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