4 void matrix_init_user(void) {
5 // leave this function blank - it can be defined in a keymap file
9 void matrix_scan_user(void) {
10 // leave this function blank - it can be defined in a keymap file
13 __attribute__ ((weak))
14 void process_action_user(keyrecord_t *record) {
15 // leave this function blank - it can be defined in a keymap file
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
23 void matrix_init_kb(void) {
24 // put your keyboard start-up code here
25 // runs once when the firmware starts up
30 #ifdef BACKLIGHT_ENABLE
31 backlight_init_ports();
41 void matrix_scan_kb(void) {
42 // put your looping keyboard code here
43 // runs every cycle (a lot)
48 void process_action_kb(keyrecord_t *record) {
49 // put your per-action keyboard code here
50 // runs for every action, just before processing by the firmware
52 process_action_user(record);
55 void led_set_kb(uint8_t usb_led) {
56 // put your keyboard LED indicator (ex: Caps Lock LED) toggling code here
58 led_set_user(usb_led);
61 #ifdef BACKLIGHT_ENABLE
64 void backlight_init_ports()
67 // Setup PB7 as output and output low.
71 // Use full 16-bit resolution.
74 // I could write a wall of text here to explain... but TL;DW
75 // Go read the ATmega32u4 datasheet.
76 // And this: http://blog.saikoled.com/post/43165849837/secret-konami-cheat-code-to-high-resolution-pwm-on
78 // Pin PB7 = OCR1C (Timer 1, Channel C)
79 // Compare Output Mode = Clear on compare match, Channel C = COM1C1=1 COM1C0=0
80 // (i.e. start high, go low when counter matches.)
81 // WGM Mode 14 (Fast PWM) = WGM13=1 WGM12=1 WGM11=1 WGM10=0
82 // Clock Select = clk/1 (no prescaling) = CS12=0 CS11=0 CS10=1
84 TCCR1A = _BV(COM1C1) | _BV(WGM11); // = 0b00001010;
85 TCCR1B = _BV(WGM13) | _BV(WGM12) | _BV(CS10); // = 0b00011001;
90 void backlight_set(uint8_t level)
94 // Turn off PWM control on PB7, revert to output low.
95 TCCR1A &= ~(_BV(COM1C1));
97 // Prevent backlight blink on lowest level
98 PORTB &= ~(_BV(PORTB7));
100 else if ( level == BACKLIGHT_LEVELS )
102 // Prevent backlight blink on lowest level
103 PORTB &= ~(_BV(PORTB7));
104 // Turn on PWM control of PB7
105 TCCR1A |= _BV(COM1C1);
106 // Set the brightness
111 // Prevent backlight blink on lowest level
112 PORTB &= ~(_BV(PORTB7));
113 // Turn on PWM control of PB7
114 TCCR1A |= _BV(COM1C1);
115 // Set the brightness
116 CHANNEL = 0xFFFF >> ((BACKLIGHT_LEVELS - level) * ((BACKLIGHT_LEVELS + 1) / 2));