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