bb-rgb.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /* Copyright 2021 Batuhan Başerdem
  2. * <baserdem.batuhan@gmail.com> @bbaserdem
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include "bb-rgb.h"
  18. #include "color.h"
  19. #define X_DIV 224/2
  20. /* Code relating to per-key RGB LED stuff
  21. */
  22. // Allow hooking into the RGB matrix indications using keymap code
  23. // Modulates the brightness of indicator
  24. RGB helper_dimmer(uint8_t r, uint8_t g, uint8_t b) {
  25. RGB output;
  26. output.r = r / 2;
  27. output.g = g / 2;
  28. output.b = b / 2;
  29. return output;
  30. }
  31. // x range from 0-left to 224-right
  32. // y range from 0-top to 64-bottom
  33. void helper_painter(uint8_t led_min, uint8_t led_max, RGB col, uint8_t side) {
  34. if (side == 1) {
  35. // Left
  36. for(uint8_t i = led_min; i <= led_max; i++) {
  37. if((g_led_config.point[i].x < X_DIV) && (g_led_config.flags[i] & LED_FLAG_INDICATOR)) {
  38. rgb_matrix_set_color(i, col.r, col.g, col.b);
  39. }
  40. }
  41. } else if (side == 2) {
  42. // Right
  43. for(uint8_t i = led_min; i <= led_max; i++) {
  44. if((g_led_config.point[i].x > X_DIV) && (g_led_config.flags[i] & LED_FLAG_INDICATOR)) {
  45. rgb_matrix_set_color(i, col.r, col.g, col.b);
  46. }
  47. }
  48. } else if (side == 0) {
  49. // Both
  50. for(uint8_t i = led_min; i <= led_max; i++) {
  51. if(g_led_config.flags[i] & LED_FLAG_INDICATOR) {
  52. rgb_matrix_set_color(i, col.r, col.g, col.b);
  53. }
  54. }
  55. }
  56. }
  57. // Allow to turn off global handling
  58. __attribute__ ((weak)) bool rgb_matrix_indicators_advanced_keymap(uint8_t led_min, uint8_t led_max) {
  59. return false;
  60. }
  61. // Set RGB state depending on layer
  62. void rgb_matrix_indicators_advanced_user(uint8_t led_min, uint8_t led_max) {
  63. uint8_t thisInd = 3;
  64. RGB thisCol;
  65. // Load keymap hooks
  66. if(rgb_matrix_indicators_advanced_keymap(led_min, led_max)) {
  67. return;
  68. }
  69. // Grab color info
  70. switch (get_highest_layer(layer_state)) {
  71. case _GAME: // Set left side as purple
  72. thisCol = helper_dimmer(RGB_PURPLE);
  73. thisInd = 1;
  74. break;
  75. case _CHAR: // Set full board as gold
  76. thisCol = helper_dimmer(RGB_GOLD);
  77. thisInd = 0;
  78. break;
  79. case _MEDI: // Set right side as pink
  80. thisCol = helper_dimmer(RGB_MAGENTA);
  81. thisInd = 2;
  82. break;
  83. case _NAVI: // Set right side as green
  84. thisCol = helper_dimmer(RGB_GREEN);
  85. thisInd = 2;
  86. break;
  87. case _SYMB: // Set right side as yellow
  88. thisCol = helper_dimmer(RGB_YELLOW);
  89. thisInd = 2;
  90. break;
  91. case _NUMB: // Set left side as blue
  92. thisCol = helper_dimmer(RGB_BLUE);
  93. thisInd = 1;
  94. break;
  95. case _FUNC: // Set left side as red
  96. thisCol = helper_dimmer(RGB_RED);
  97. thisInd = 1;
  98. break;
  99. case _MOUS: // Set left side as blue-green
  100. thisCol = helper_dimmer(RGB_SPRINGGREEN);
  101. thisInd = 1;
  102. break;
  103. case _MUSI: // Set full board as orange
  104. thisCol = helper_dimmer(RGB_ORANGE);
  105. thisInd = 0;
  106. break;
  107. }
  108. helper_painter(led_min, led_max, thisCol, thisInd);
  109. }
  110. // Hook into shutdown code to make all perkey LED's red on hitting reset
  111. void shutdown_rgb(void) {
  112. // Flash all the key LED's red on shutdown
  113. uint16_t timer_start = timer_read();
  114. rgb_matrix_set_color_all(RGB_CORAL);
  115. // Delay until this registers
  116. while(timer_elapsed(timer_start) < 250) {wait_ms(1);}
  117. }
  118. // Hook into suspend code
  119. void suspend_power_down_rgb(void) {
  120. rgb_matrix_set_suspend_state(true);
  121. }
  122. void suspend_wakeup_init_rgb(void) {
  123. rgb_matrix_set_suspend_state(false);
  124. }