]> git.donarmstrong.com Git - qmk_firmware.git/blob - keyboards/lfkeyboards/lighting.c
42d247ae701344bc38e8e4d5060f4c05f8276fd8
[qmk_firmware.git] / keyboards / lfkeyboards / lighting.c
1 #ifdef ISSI_ENABLE
2
3
4 #include <avr/sfr_defs.h>
5 #include <avr/timer_avr.h>
6 #include <avr/wdt.h>
7 #include "quantum.h"
8 // #include "lfk87.h"
9 #include "issi.h"
10 #include "TWIlib.h"
11 #include "lighting.h"
12 #include "debug.h"
13 #include "rgblight.h"
14 #include "audio/audio.h"
15
16
17 extern rgblight_config_t rgblight_config; // Declared in rgblight.c
18
19 const uint8_t backlight_pwm_map[BACKLIGHT_LEVELS] = BACKLIGHT_PWM_MAP;
20
21 // RGB# to ISSI matrix, this is the same across all revisions
22 const uint8_t rgb_leds[][3][2] = {
23         {{0, 0}, {0, 0}, {0, 0}},
24         {{1, 1}, {2, 3}, {2, 4}},   // RGB1/RGB17
25         {{2, 1}, {2, 2}, {3, 4}},   // RGB2/RGB18
26         {{3, 1}, {3, 2}, {3, 3}},   // RGB3/RGB19
27         {{4, 1}, {4, 2}, {4, 3}},   // RGB4/RGB20
28         {{5, 1}, {5, 2}, {5, 3}},   // RGB5/RGB21
29         {{6, 1}, {6, 2}, {6, 3}},   // RGB6/RGB22
30         {{7, 1}, {7, 2}, {7, 3}},   // RGB6/RGB23
31         {{8, 1}, {8, 2}, {8, 3}},   // RGB8/RGB24
32         {{1, 9}, {1, 8}, {1, 7}},   // RGB9/RGB25
33         {{2, 9}, {2, 8}, {2, 7}},   // RGB10/RGB26
34         {{3, 9}, {3, 8}, {3, 7}},   // RGB11/RGB27
35         {{4, 9}, {4, 8}, {4, 7}},   // RGB12/RGB28
36         {{5, 9}, {5, 8}, {5, 7}},   // RGB13/RGB29
37         {{6, 9}, {6, 8}, {6, 7}},   // RGB14/RGB30
38         {{7, 9}, {7, 8}, {6, 6}},   // RGB15/RGB31
39         {{8, 9}, {7, 7}, {7, 6}}    // RGB16/RGB32
40     };
41
42 void set_rgb(uint8_t rgb_led, uint8_t red, uint8_t green, uint8_t blue){
43 #ifdef RGBLIGHT_ENABLE
44     uint8_t matrix = rgb_matrices[0];
45     if(rgb_led >= 17){
46         matrix = rgb_matrices[1];
47         rgb_led -= 16;
48     }
49     if(rgb_leds[rgb_led][0][1] != 0){
50         activateLED(matrix, rgb_leds[rgb_led][0][0], rgb_leds[rgb_led][0][1], red);
51     }
52     if(rgb_leds[rgb_led][1][1] != 0){
53         activateLED(matrix, rgb_leds[rgb_led][1][0], rgb_leds[rgb_led][1][1], green);
54     }
55     if(rgb_leds[rgb_led][2][1] != 0){
56         activateLED(matrix, rgb_leds[rgb_led][2][0], rgb_leds[rgb_led][2][1], blue);
57     }
58 #endif
59 }
60
61 void backlight_set(uint8_t level){
62 #ifdef BACKLIGHT_ENABLE
63     uint8_t pwm_value = 0;
64     if(level >= BACKLIGHT_LEVELS){
65         level = BACKLIGHT_LEVELS;
66     }
67     if(level > 0){
68         pwm_value = backlight_pwm_map[level-1];
69     }
70     for(int x = 1; x <= 9; x++){
71         for(int y = 1; y <= 9; y++){
72             activateLED(switch_matrices[0], x, y, pwm_value);
73             activateLED(switch_matrices[1], x, y, pwm_value);
74         }
75     }
76 #endif
77 }
78
79 void set_underglow(uint8_t red, uint8_t green, uint8_t blue){
80 #ifdef RGBLIGHT_ENABLE
81     for(uint8_t x = 1; x <= 32; x++){
82         set_rgb(x, red, green, blue);
83     }
84 #endif
85 }
86
87
88 void rgblight_set(void) {
89 #ifdef RGBLIGHT_ENABLE
90     for(uint8_t i = 0; (i < sizeof(rgb_sequence)) && (i < RGBLED_NUM); i++){
91         if(rgblight_config.enable){
92             set_rgb(rgb_sequence[i], led[i].r, led[i].g, led[i].b);
93         }else{
94             set_rgb(rgb_sequence[i], 0, 0, 0);
95         }
96     }
97 #endif
98 }
99
100 void set_backlight_by_keymap(uint8_t col, uint8_t row){
101 #ifdef RGBLIGHT_ENABLE
102     uint8_t lookup_value = switch_leds[row][col];
103     uint8_t matrix = switch_matrices[0];
104     if(lookup_value & 0x80){
105         matrix = switch_matrices[1];
106     }
107     issi_devices[0]->led_dirty = 1;
108     uint8_t led_col = (lookup_value & 0x70) >> 4;
109     uint8_t led_row = lookup_value & 0x0F;
110     activateLED(matrix, led_col, led_row, 255);
111 #endif
112 }
113
114 void force_issi_refresh(){
115 #ifdef ISSI_ENABLE
116     issi_devices[0]->led_dirty = true;
117     update_issi(0, true);
118     issi_devices[3]->led_dirty = true;
119     update_issi(3, true);
120 #endif
121 }
122
123 void led_test(){
124 #ifdef ISSI_ENABLE
125 #ifdef WATCHDOG_ENABLE
126     // This test take a long time to run, disable the WTD until its complete
127     wdt_disable();
128 #endif
129     backlight_set(0);
130     set_underglow(0, 0, 0);
131     force_issi_refresh();
132     set_underglow(0, 0, 0);
133     for(uint8_t x = 0; x < sizeof(rgb_sequence); x++){
134         set_rgb(rgb_sequence[x], 255, 0, 0);
135         force_issi_refresh();
136         _delay_ms(250);
137         set_rgb(rgb_sequence[x], 0, 255, 0);
138         force_issi_refresh();
139         _delay_ms(250);
140         set_rgb(rgb_sequence[x], 0, 0, 255);
141         force_issi_refresh();
142         _delay_ms(250);
143         set_rgb(rgb_sequence[x], 0, 0, 0);
144         force_issi_refresh();
145     }
146 #ifdef WATCHDOG_ENABLE
147     wdt_enable(WDTO_250MS);
148 #endif
149 #endif
150 }
151
152 void backlight_init_ports(void){
153     issi_init();
154 }
155
156 #endif
157