lighting.c 4.3 KB

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