]> git.donarmstrong.com Git - qmk_firmware.git/blob - quantum/template/template.c
Merge remote-tracking branch 'refs/remotes/jackhumbert/master'
[qmk_firmware.git] / quantum / template / template.c
1 #include "%KEYBOARD%.h"
2
3 __attribute__ ((weak))
4 void matrix_init_user(void) {
5         // leave this function blank - it can be defined in a keymap file
6 };
7
8 __attribute__ ((weak))
9 void matrix_scan_user(void) {
10         // leave this function blank - it can be defined in a keymap file
11 }
12
13 __attribute__ ((weak))
14 void process_action_user(keyrecord_t *record) {
15         // leave this function blank - it can be defined in a keymap file
16 }
17
18 __attribute__ ((weak))
19 void led_set_user(uint8_t usb_led) {
20         // leave this function blank - it can be defined in a keymap file
21 }
22
23 void matrix_init_kb(void) {
24         // put your keyboard start-up code here
25         // runs once when the firmware starts up
26
27         matrix_init_user();
28 }
29
30 void matrix_scan_kb(void) {
31         // put your looping keyboard code here
32         // runs every cycle (a lot)
33
34         matrix_scan_user();
35 }
36
37 void process_action_kb(keyrecord_t *record) {
38         // put your per-action keyboard code here
39         // runs for every action, just before processing by the firmware
40
41         process_action_user(record);
42 }
43
44 void led_set_kb(uint8_t usb_led) {
45         // put your keyboard LED indicator (ex: Caps Lock LED) toggling code here
46
47         led_set_user(usb_led);
48 }
49
50 #ifdef BACKLIGHT_ENABLE
51 #define CHANNEL OCR1C
52
53 void backlight_init_ports()
54 {
55
56     // Setup PB7 as output and output low.
57     DDRB |= (1<<7);
58     PORTB &= ~(1<<7);
59     
60     // Use full 16-bit resolution. 
61     ICR1 = 0xFFFF;
62
63     // I could write a wall of text here to explain... but TL;DW
64     // Go read the ATmega32u4 datasheet.
65     // And this: http://blog.saikoled.com/post/43165849837/secret-konami-cheat-code-to-high-resolution-pwm-on
66     
67     // Pin PB7 = OCR1C (Timer 1, Channel C)
68     // Compare Output Mode = Clear on compare match, Channel C = COM1C1=1 COM1C0=0
69     // (i.e. start high, go low when counter matches.)
70     // WGM Mode 14 (Fast PWM) = WGM13=1 WGM12=1 WGM11=1 WGM10=0
71     // Clock Select = clk/1 (no prescaling) = CS12=0 CS11=0 CS10=1
72     
73     TCCR1A = _BV(COM1C1) | _BV(WGM11); // = 0b00001010;
74     TCCR1B = _BV(WGM13) | _BV(WGM12) | _BV(CS10); // = 0b00011001;
75
76     backlight_init();
77 }
78
79 void backlight_set(uint8_t level)
80 {
81     if ( level == 0 )
82     {
83         // Turn off PWM control on PB7, revert to output low.
84         TCCR1A &= ~(_BV(COM1C1));
85         CHANNEL = 0x0;
86         // Prevent backlight blink on lowest level
87         PORTB &= ~(_BV(PORTB7));
88     }
89     else if ( level == BACKLIGHT_LEVELS )
90     {
91         // Prevent backlight blink on lowest level
92         PORTB &= ~(_BV(PORTB7));
93         // Turn on PWM control of PB7
94         TCCR1A |= _BV(COM1C1);
95         // Set the brightness
96         CHANNEL = 0xFFFF;
97     }
98     else        
99     {
100         // Prevent backlight blink on lowest level
101         PORTB &= ~(_BV(PORTB7));
102         // Turn on PWM control of PB7
103         TCCR1A |= _BV(COM1C1);
104         // Set the brightness
105         CHANNEL = 0xFFFF >> ((BACKLIGHT_LEVELS - level) * ((BACKLIGHT_LEVELS + 1) / 2));
106     }
107 }
108
109 #endif