]> git.donarmstrong.com Git - qmk_firmware.git/blob - keyboards/wilba_tech/wt_mono_backlight.c
RAMA U80-A, wilba.tech WT60-A, WT65-A, WT80-A, IS31FL3736 driver (#3925)
[qmk_firmware.git] / keyboards / wilba_tech / wt_mono_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
17 #include "wt_mono_backlight.h"
18 #include "drivers/avr/i2c_master.h"
19 #include "drivers/issi/is31fl3736.h"
20
21 #include <avr/interrupt.h>
22
23 #define ISSI_ADDR_DEFAULT 0x50
24
25 bool g_suspend_state = false;
26
27 // Global tick at 20 Hz
28 uint32_t g_tick = 0;
29 uint8_t g_config_effect_speed = 0;
30 uint8_t g_config_brightness = 255;
31
32 void backlight_init_drivers(void)
33 {
34         // Initialize I2C
35         i2c_init();
36         IS31FL3736_init( ISSI_ADDR_DEFAULT );
37
38         for ( uint8_t index = 0; index < 96; index++ )  {
39                 IS31FL3736_mono_set_led_control_register( index, true );
40         }
41         IS31FL3736_update_led_control_registers( ISSI_ADDR_DEFAULT, 0x00 );
42 }
43
44
45 // This is (F_CPU/1024) / 20 Hz
46 // = 15625 Hz / 20 Hz
47 // = 781
48 #define TIMER3_TOP 781
49
50 void backlight_timer_init(void)
51 {
52         static uint8_t backlight_timer_is_init = 0;
53         if ( backlight_timer_is_init ) {
54                 return;
55         }
56         backlight_timer_is_init = 1;
57
58         // Timer 3 setup
59         TCCR3B = _BV(WGM32) |                   // CTC mode OCR3A as TOP
60                          _BV(CS32) | _BV(CS30); // prescale by /1024
61         // Set TOP value
62         uint8_t sreg = SREG;
63         cli();
64
65         OCR3AH = (TIMER3_TOP >> 8) & 0xff;
66         OCR3AL = TIMER3_TOP & 0xff;
67         SREG = sreg;
68 }
69
70 void backlight_timer_enable(void)
71 {
72         TIMSK3 |= _BV(OCIE3A);
73 }
74
75 void backlight_timer_disable(void)
76 {
77         TIMSK3 &= ~_BV(OCIE3A);
78 }
79
80 void backlight_set_suspend_state(bool state)
81 {
82         g_suspend_state = state;
83 }
84
85 void backlight_effect_cycle_all(void)
86 {
87         uint8_t offset = ( g_tick << g_config_effect_speed ) & 0xFF;
88
89         backlight_set_brightness_all( offset );
90 }
91
92 ISR(TIMER3_COMPA_vect)
93 {
94         // delay 1 second before driving LEDs or doing anything else
95         static uint8_t startup_tick = 0;
96         if ( startup_tick < 20 ) {
97                 startup_tick++;
98                 return;
99         }
100
101         g_tick++;
102
103         //backlight_effect_cycle_all();
104         backlight_set_brightness_all( 255 );
105 }
106
107 void backlight_update_pwm_buffers(void)
108 {
109         IS31FL3736_update_pwm_buffers(ISSI_ADDR_DEFAULT,0x00);
110 }
111
112 void backlight_set_brightness_all( uint8_t value )
113 {
114         IS31FL3736_mono_set_brightness_all( value );
115 }
116