logo

qmk_firmware

custom branch of QMK firmware git clone https://anongit.hacktivis.me/git/qmk_firmware.git

rgb_matrix_kb.inc (4604B)


  1. /*
  2. * Copyright (C) 2021 System76
  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 3 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 <https://www.gnu.org/licenses/>.
  16. */
  17. RGB_MATRIX_EFFECT(active_keys)
  18. RGB_MATRIX_EFFECT(raw_rgb)
  19. RGB_MATRIX_EFFECT(unlocked)
  20. #ifdef RGB_MATRIX_CUSTOM_EFFECT_IMPLS
  21. #include "dynamic_keymap.h"
  22. #include "action_layer.h"
  23. static bool active_keys_initialized = false;
  24. static uint8_t active_keys_table[RGB_MATRIX_LED_COUNT] = {0};
  25. static void active_keys_initialize(void) {
  26. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  27. for (uint8_t col = 0; col < MATRIX_COLS; col++) {
  28. uint8_t led = g_led_config.matrix_co[row][col];
  29. if (led < RGB_MATRIX_LED_COUNT && row < 16 && col < 16) {
  30. active_keys_table[led] = (row << 4) | col;
  31. }
  32. }
  33. }
  34. active_keys_initialized = true;
  35. }
  36. static bool active_keys(effect_params_t* params) {
  37. if (!active_keys_initialized) {
  38. active_keys_initialize();
  39. }
  40. RGB_MATRIX_USE_LIMITS(led_min, led_max);
  41. uint8_t layer = get_highest_layer(layer_state);
  42. rgb_t rgb = hsv_to_rgb(rgb_matrix_config.hsv);
  43. for (uint8_t i = led_min; i < led_max; i++) {
  44. RGB_MATRIX_TEST_LED_FLAGS();
  45. uint8_t rowcol = active_keys_table[i];
  46. uint8_t row = rowcol >> 4;
  47. uint8_t col = rowcol & 0xF;
  48. uint16_t keycode = dynamic_keymap_get_keycode(layer, row, col);
  49. switch (keycode) {
  50. case KC_NO:
  51. case KC_TRNS:
  52. rgb_matrix_set_color(i, 0, 0, 0);
  53. break;
  54. default:
  55. rgb_matrix_set_color(i, rgb.r, rgb.g, rgb.b);
  56. break;
  57. }
  58. }
  59. return led_max < RGB_MATRIX_LED_COUNT;
  60. }
  61. rgb_t raw_rgb_data[RGB_MATRIX_LED_COUNT] = {0};
  62. static uint8_t normalize_component(uint8_t component) {
  63. uint16_t x = (uint16_t)component;
  64. x *= rgb_matrix_config.hsv.v; // Multiply by current brightness
  65. x /= 255; // Divide by maximum brightness
  66. return (uint8_t)x;
  67. }
  68. static rgb_t normalize_index(uint8_t i) {
  69. rgb_t raw = raw_rgb_data[i];
  70. rgb_t rgb = {
  71. .r = normalize_component(raw.r),
  72. .g = normalize_component(raw.g),
  73. .b = normalize_component(raw.b),
  74. };
  75. return rgb;
  76. }
  77. static bool raw_rgb(effect_params_t* params) {
  78. RGB_MATRIX_USE_LIMITS(led_min, led_max);
  79. for (uint8_t i = led_min; i < led_max; i++) {
  80. RGB_MATRIX_TEST_LED_FLAGS();
  81. rgb_t rgb = normalize_index(i);
  82. rgb_matrix_set_color(i, rgb.r, rgb.g, rgb.b);
  83. }
  84. return led_max < RGB_MATRIX_LED_COUNT;
  85. }
  86. static uint8_t unlocked_keys[8][2] = {
  87. {2, 7}, // U
  88. {4, 6}, // N
  89. {3, 9}, // L
  90. {2, 9}, // O
  91. {4, 3}, // C
  92. {3, 8}, // K
  93. {2, 3}, // E
  94. {3, 3}, // D
  95. };
  96. static uint8_t unlocked_ticks = 0;
  97. static uint8_t unlocked_i = 0;
  98. static uint8_t unlocked_leds_count = 0;
  99. static uint8_t unlocked_leds[2] = {0, 0};
  100. static bool unlocked(effect_params_t* params) {
  101. RGB_MATRIX_USE_LIMITS(led_min, led_max);
  102. unlocked_ticks++;
  103. if (params->init) {
  104. unlocked_ticks = 0;
  105. unlocked_i = 0;
  106. }
  107. if (unlocked_ticks == 0) {
  108. if (unlocked_i == 8) {
  109. unlocked_leds_count = 0;
  110. unlocked_i = 0;
  111. } else {
  112. unlocked_leds_count = rgb_matrix_map_row_column_to_led(unlocked_keys[unlocked_i][0], unlocked_keys[unlocked_i][1], unlocked_leds);
  113. unlocked_i++;
  114. }
  115. }
  116. for (uint8_t i = led_min; i < led_max; i++) {
  117. RGB_MATRIX_TEST_LED_FLAGS();
  118. hsv_t hsv = {
  119. .h = i + unlocked_ticks,
  120. .s = 0xFF,
  121. .v = 0x70,
  122. };
  123. for (uint8_t j = 0; j < unlocked_leds_count; j++) {
  124. if (i == unlocked_leds[j]) {
  125. hsv.s = 0;
  126. hsv.v = 0xFF;
  127. }
  128. }
  129. rgb_t rgb = hsv_to_rgb(hsv);
  130. rgb_matrix_set_color(i, rgb.r, rgb.g, rgb.b);
  131. }
  132. return led_max < RGB_MATRIX_LED_COUNT;
  133. }
  134. #endif // RGB_MATRIX_CUSTOM_EFFECT_IMPLS