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