4 #include <avr/sfr_defs.h>
5 #include <avr/timer_avr.h>
14 #include "audio/audio.h"
17 extern rgblight_config_t rgblight_config; // Declared in rgblight.c
19 #ifdef BACKLIGHT_ENABLE
20 const uint8_t backlight_pwm_map[BACKLIGHT_LEVELS] = BACKLIGHT_PWM_MAP;
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
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];
48 matrix = rgb_matrices[1];
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);
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);
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);
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;
70 pwm_value = backlight_pwm_map[level-1];
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);
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);
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);
96 set_rgb(rgb_sequence[i], 0, 0, 0);
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];
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);
116 void force_issi_refresh(){
118 issi_devices[0]->led_dirty = true;
119 update_issi(0, true);
120 issi_devices[3]->led_dirty = true;
121 update_issi(3, true);
127 #ifdef WATCHDOG_ENABLE
128 // This test take a long time to run, disable the WTD until its complete
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();
139 set_rgb(rgb_sequence[x], 0, 255, 0);
140 force_issi_refresh();
142 set_rgb(rgb_sequence[x], 0, 0, 255);
143 force_issi_refresh();
145 set_rgb(rgb_sequence[x], 0, 0, 0);
146 force_issi_refresh();
148 #ifdef WATCHDOG_ENABLE
149 wdt_enable(WDTO_250MS);
154 void backlight_init_ports(void){