]> git.donarmstrong.com Git - qmk_firmware.git/blob - keyboard/planck/planck.c
65585015044ed5d7f268cb0c3242029ba9b1b935
[qmk_firmware.git] / keyboard / planck / planck.c
1 #include "planck.h"
2
3 __attribute__ ((weak))
4 void matrix_init_user(void) {}
5
6 __attribute__ ((weak))
7 void matrix_scan_user(void) {}
8
9 __attribute__ ((weak))
10 void process_action_user(keyrecord_t *record) {}
11
12 void matrix_init_kb(void) {
13 #ifdef BACKLIGHT_ENABLE
14         backlight_init_ports();
15 #endif
16
17 #ifdef RGBLIGHT_ENABLE
18         rgblight_init();
19 #endif
20
21         // Turn status LED on
22         DDRE |= (1<<6);
23         PORTE |= (1<<6);
24
25         matrix_init_user();
26 }
27
28 void matrix_scan_kb(void) {
29         matrix_scan_user();
30 }
31
32 void process_action_kb(keyrecord_t *record) {
33         process_action_user(record);
34 }
35
36 #ifdef BACKLIGHT_ENABLE
37 #define CHANNEL OCR1C
38
39 void backlight_init_ports()
40 {
41
42     // Setup PB7 as output and output low.
43     DDRB |= (1<<7);
44     PORTB &= ~(1<<7);
45     
46     // Use full 16-bit resolution. 
47     ICR1 = 0xFFFF;
48
49     // I could write a wall of text here to explain... but TL;DW
50     // Go read the ATmega32u4 datasheet.
51     // And this: http://blog.saikoled.com/post/43165849837/secret-konami-cheat-code-to-high-resolution-pwm-on
52     
53     // Pin PB7 = OCR1C (Timer 1, Channel C)
54     // Compare Output Mode = Clear on compare match, Channel C = COM1C1=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     
59     TCCR1A = _BV(COM1C1) | _BV(WGM11); // = 0b00001010;
60     TCCR1B = _BV(WGM13) | _BV(WGM12) | _BV(CS10); // = 0b00011001;
61
62     backlight_init();
63 }
64
65 void backlight_set(uint8_t level)
66 {
67     if ( level == 0 )
68     {
69         // Turn off PWM control on PB7, revert to output low.
70         TCCR1A &= ~(_BV(COM1C1));
71         CHANNEL = 0x0;
72         // Prevent backlight blink on lowest level
73         PORTB &= ~(_BV(PORTB7));
74     }
75     else if ( level == BACKLIGHT_LEVELS )
76     {
77         // Prevent backlight blink on lowest level
78         PORTB &= ~(_BV(PORTB7));
79         // Turn on PWM control of PB7
80         TCCR1A |= _BV(COM1C1);
81         // Set the brightness
82         CHANNEL = 0xFFFF;
83     }
84     else        
85     {
86         // Prevent backlight blink on lowest level
87         PORTB &= ~(_BV(PORTB7));
88         // Turn on PWM control of PB7
89         TCCR1A |= _BV(COM1C1);
90         // Set the brightness
91         CHANNEL = 0xFFFF >> ((BACKLIGHT_LEVELS - level) * ((BACKLIGHT_LEVELS + 1) / 2));
92     }
93 }
94
95 #endif