You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

159 lines
4.3 KiB

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