]> git.donarmstrong.com Git - qmk_firmware.git/blob - keyboards/winkeyless/bface/backlight_ps2avrGB.c
remove custom matrix support
[qmk_firmware.git] / keyboards / winkeyless / bface / backlight_ps2avrGB.c
1 /* Copyright 2017 Sebastian Kaim
2  *
3  * This program is free software: you can redistribute it and/or modify
4  * it under the terms of the GNU General Public License as published by
5  * the Free Software Foundation, either version 2 of the License, or
6  * (at your option) any later version.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
15  */
16
17 #ifdef BACKLIGHT_ENABLE
18
19 #include "backlight_ps2avrGB.h"
20 #define sbi(reg,bit)    reg |= (_BV(bit))
21 #define cbi(reg,bit)    reg &= (~_BV(bit))
22 #define PWM10   WGM10
23 #define PWM11   WGM11
24 #define COM1x1 COM1B1
25 #define OCR1x  OCR1B
26
27 void backlight_init_ports(void)
28 {
29 #if BACKLIGHT_ON_STATE == 0
30         backlight_off();
31 #else
32         backlight_on();
33 #endif
34
35         // setup pwm
36         // this bitmagic is sourced from the original firmware
37         /*TCCR1B = ((TCCR1B & ~0x07) | 0x03);
38         TCNT1H = 0;
39         TCNT1L = 0;
40         sbi(TIMSK, TOIE1);
41         OCR1BH = 0;
42         OCR1BL = 0;
43         cbi(TCCR1A,PWM11);
44         sbi(TCCR1A,PWM10);
45         sbi(TCCR1A,COM1B1);
46         cbi(TCCR1A,COM1B0);*/
47         ICR1 = 0xFFFF;
48
49     TCCR1A = _BV(COM1x1) | _BV(WGM11); // = 0b00001010;
50     TCCR1B = _BV(WGM13) | _BV(WGM12) | _BV(CS10); // = 0b00011001;
51
52         backlight_init();
53 }
54
55 void backlight_set(uint8_t level)
56 {
57         if( level == 0 ) {
58                 backlight_off();
59         }
60         else {
61                 backlight_on();
62                 /*uint8_t pwm = get_pwm_for_brightness(level);
63                 set_backlight_pwm(pwm);
64                 TCCR1A |= _BV(COM1x1);
65                 OCR1x = (level >= 2) ? 0xFFFF : 0x00FF;*/
66         }
67 }
68
69 #define PWM_MAX 0xFF
70 uint8_t get_pwm_for_brightness(uint8_t level)
71 {
72         // we need to cast up here to allow multiplication with 0xFF. We could also use floats, but this is probably a lot faster.
73         uint16_t brightness = (uint16_t)level * (uint16_t)PWM_MAX / (uint16_t)BACKLIGHT_LEVELS;
74         return (uint8_t)brightness;
75 }
76
77 void backlight_on(void)
78 {
79         //_SFR_IO8(0x12) |= _BV(0x4);
80         LED_PIN |= BACKLIGHT_PORT_NUM;
81 }
82
83 void backlight_off(void)
84 {
85         //_SFR_IO8(0x12) &= ~_BV(0x4);
86         LED_PIN &= ~BACKLIGHT_PORT_NUM;
87 }
88
89 void set_backlight_pwm(uint8_t level) {
90         // this does not work (yet)
91         //OCR1B = level;
92 }
93
94 #endif // BACKLIGHT_ENABLE