]> git.donarmstrong.com Git - qmk_firmware.git/blob - keyboards/rama/m6_b/rgb_backlight.c
8f7ac06303e43989c197cdf2bb51d4f87b48e9cf
[qmk_firmware.git] / keyboards / rama / m6_b / rgb_backlight.c
1 /* Copyright 2018 Jason Williams (Wilba)
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 #if RGB_BACKLIGHT_ENABLED
17
18 #include "rgb_backlight.h"
19 //#include "rgb_backlight_api.h"
20
21 #include <avr/io.h>
22 #include <util/delay.h>
23 #include <avr/interrupt.h>
24 #include "progmem.h"
25
26 #include "quantum/color.h"
27 #include "drivers/avr/i2c_master.h"
28 #include "drivers/issi/is31fl3218.h"
29
30 bool g_suspend_state = false;
31
32 // Global tick at 20 Hz
33 uint32_t g_tick = 0;
34 uint8_t g_config_effect_speed = 0;
35 uint8_t g_config_brightness = 255;
36
37 void backlight_update_pwm_buffers(void)
38 {
39         IS31FL3218_update_pwm_buffers();
40 }
41
42 void backlight_set_color( int index, uint8_t red, uint8_t green, uint8_t blue )
43 {
44         IS31FL3218_set_color( index, red, green, blue );
45 }
46
47 void backlight_set_color_all( uint8_t red, uint8_t green, uint8_t blue )
48 {
49         IS31FL3218_set_color_all( red, green, blue );
50 }
51
52
53 // This is (F_CPU/1024) / 20 Hz
54 // = 15625 Hz / 20 Hz
55 // = 781
56 #define TIMER3_TOP 260
57
58 void backlight_timer_init(void)
59 {
60         static uint8_t backlight_timer_is_init = 0;
61         if ( backlight_timer_is_init )
62         {
63                 return;
64         }
65         backlight_timer_is_init = 1;
66
67         // Timer 3 setup
68         TCCR3B = _BV(WGM32) |                   // CTC mode OCR3A as TOP
69                          _BV(CS32) | _BV(CS30); // prescale by /1024
70         // Set TOP value
71         uint8_t sreg = SREG;
72         cli();
73
74         OCR3AH = (TIMER3_TOP >> 8) & 0xff;
75         OCR3AL = TIMER3_TOP & 0xff;
76         SREG = sreg;
77 }
78
79 void backlight_timer_enable(void)
80 {
81         TIMSK3 |= _BV(OCIE3A);
82 }
83
84 void backlight_timer_disable(void)
85 {
86         TIMSK3 &= ~_BV(OCIE3A);
87 }
88
89 void backlight_set_suspend_state(bool state)
90 {
91         g_suspend_state = state;
92 }
93
94 void backlight_effect_cycle_all(void)
95 {
96         uint8_t hueOffset = ( g_tick << g_config_effect_speed ) & 0xFF;
97         uint8_t satOffset = 127;
98         // Relies on hue being 8-bit and wrapping
99         for ( int i=0; i<6; i++ )
100         {
101                 HSV hsv = { .h = hueOffset, .s = satOffset, .v = g_config_brightness };
102                 RGB rgb = hsv_to_rgb( hsv );
103                 backlight_set_color( i, rgb.r, rgb.g, rgb.b );
104         }
105 }
106
107 ISR(TIMER3_COMPA_vect)
108 {
109         // delay 1 second before driving LEDs or doing anything else
110         static uint8_t startup_tick = 0;
111         if ( startup_tick < 20 )
112         {
113                 startup_tick++;
114                 return;
115         }
116
117         g_tick++;
118
119         if ( g_suspend_state )
120         {
121                 backlight_set_color_all( 0, 0, 0 );
122         }
123         else
124         {
125                 //HSV hsv = { .h = 240, .s = 255, .v = g_config_brightness };
126                 //RGB rgb = hsv_to_rgb( hsv );
127                 //backlight_set_color_all( rgb.r, rgb.g, rgb.b );
128                 backlight_effect_cycle_all();
129         }
130 }
131
132 void backlight_init_drivers(void)
133 {
134         // Initialize I2C
135         i2c_init();
136         IS31FL3218_init();
137 }
138
139 #endif // RGB_BACKLIGHT_ENABLED