lighting.c 4.3 KB

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