1 /* Copyright 2018 Jason Williams (Wilba)
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.
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.
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/>.
16 #if RGB_BACKLIGHT_ENABLED
18 #include "rgb_backlight.h"
19 //#include "rgb_backlight_api.h"
22 #include <util/delay.h>
23 #include <avr/interrupt.h>
26 #include "quantum/color.h"
27 #include "drivers/avr/i2c_master.h"
28 #include "drivers/issi/is31fl3218.h"
30 bool g_suspend_state = false;
32 // Global tick at 20 Hz
34 uint8_t g_config_effect_speed = 0;
35 uint8_t g_config_brightness = 255;
37 void backlight_update_pwm_buffers(void)
39 IS31FL3218_update_pwm_buffers();
42 void backlight_set_color( int index, uint8_t red, uint8_t green, uint8_t blue )
44 IS31FL3218_set_color( index, red, green, blue );
47 void backlight_set_color_all( uint8_t red, uint8_t green, uint8_t blue )
49 IS31FL3218_set_color_all( red, green, blue );
53 // This is (F_CPU/1024) / 20 Hz
56 #define TIMER3_TOP 260
58 void backlight_timer_init(void)
60 static uint8_t backlight_timer_is_init = 0;
61 if ( backlight_timer_is_init )
65 backlight_timer_is_init = 1;
68 TCCR3B = _BV(WGM32) | // CTC mode OCR3A as TOP
69 _BV(CS32) | _BV(CS30); // prescale by /1024
74 OCR3AH = (TIMER3_TOP >> 8) & 0xff;
75 OCR3AL = TIMER3_TOP & 0xff;
79 void backlight_timer_enable(void)
81 TIMSK3 |= _BV(OCIE3A);
84 void backlight_timer_disable(void)
86 TIMSK3 &= ~_BV(OCIE3A);
89 void backlight_set_suspend_state(bool state)
91 g_suspend_state = state;
94 void backlight_effect_cycle_all(void)
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++ )
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 );
107 ISR(TIMER3_COMPA_vect)
109 // delay 1 second before driving LEDs or doing anything else
110 static uint8_t startup_tick = 0;
111 if ( startup_tick < 20 )
119 if ( g_suspend_state )
121 backlight_set_color_all( 0, 0, 0 );
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();
132 void backlight_init_drivers(void)
139 #endif // RGB_BACKLIGHT_ENABLED