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.

162 lines
5.2 KiB

  1. /* Copyright 2022 mly32
  2. *
  3. * This program is free software: you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation, either version 2 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. **/
  16. #include "binds.c"
  17. /*
  18. Index values of keys
  19. 0 |6 |12 |18 |23 |28 |34 |39 |44 |50 |56 |61 |66 |69 |----|----|
  20. 1 |7 |13 |19 |24 |29 |35 |40 |45 |51 |57 |62 |78 |85 |----|72 |
  21. 2 |8 |14 |20 |25 |30 |36 |41 |46 |52 |58 |63 |89 |93 |----|75 |
  22. 3 |9 |15 |21 |26 |31 |37 |42 |47 |53 |59 |64 |----|96 |----|86 |
  23. 4 |----|10 |16 |22 |27 |32 |38 |43 |48 |54 |60 |----|90 |94 |82 |
  24. 5 |11 |17 |--------------|33 |--------------|49 |55 |65 |95 |97 |79 |
  25. Index values of underglow keys
  26. 67,Side 01 | 68,Side 12
  27. 70,Side 02 | 71,Side 13
  28. 73,Side 03 | 74,Side 14
  29. 76,Side 04 | 77,Side 15
  30. 80,Side 05 | 81,Side 16
  31. 83,Side 06 | 84,Side 17
  32. 87,Side 07 | 88,Side 18
  33. 91,Side 08 | 92,Side 19
  34. */
  35. #ifdef ENCODER_ENABLE
  36. bool encoder_update_user(uint8_t index, bool clockwise) {
  37. if (get_mods() & MOD_MASK_CA && !(get_mods() & ~MOD_MASK_CA)) {
  38. /* only active mods are LCTRL, RCTRL, LALT, or RALT */
  39. tap_code16(clockwise ? KC_TAB : LSFT(KC_TAB));
  40. } else {
  41. tap_code16(clockwise ? KC_VOLU : KC_VOLD);
  42. }
  43. return false;
  44. }
  45. #endif
  46. /* expects [0..255], [0..255], [0..255] */
  47. RGB relative_hsv(uint8_t h, uint8_t s, uint8_t v) {
  48. uint8_t scaled_v = (uint16_t)v * rgb_matrix_get_val() / 255;
  49. return hsv_to_rgb((HSV){.h = h, .s = s, .v = scaled_v});
  50. }
  51. #ifdef RGB_MATRIX_ENABLE
  52. /* rgb data tied to rgb_matrix_indicators_advanced_user evaluation */
  53. typedef struct rgb_data {
  54. /* maps a key to is row and column position
  55. * [0, 98) -> {0..255, 0..255}
  56. * non 255-valued keys: 82 (88 - 6; 5 keys with no led and 1 rotary knob)
  57. **/
  58. led_point_t led_to_pos[DRIVER_LED_TOTAL];
  59. /* keys that will always have no rgb */
  60. uint8_t dark_keys_sz;
  61. uint8_t* dark_keys;
  62. /* momentary layer keys */
  63. uint8_t mo_keys_sz;
  64. uint8_t* mo_keys;
  65. /* color values */
  66. uint8_t dark_v;
  67. uint8_t white_v;
  68. uint8_t white_s;
  69. uint8_t nkro_h;
  70. uint8_t caps_h;
  71. } rgb_data_t;
  72. void init_rgb_data(rgb_data_t* t) {
  73. for (uint8_t i = 0; i < DRIVER_LED_TOTAL; i++) {
  74. t->led_to_pos[i] = (led_point_t){.x = 255, .y = 255};
  75. }
  76. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  77. for (uint8_t j = 0; j < MATRIX_COLS; j++) {
  78. uint8_t idx = g_led_config.matrix_co[i][j];
  79. if (idx != NO_LED) {
  80. t->led_to_pos[idx] = (led_point_t){.x = i, .y = j};
  81. }
  82. }
  83. }
  84. uint8_t dark_keys[] = {33};
  85. t->dark_keys_sz = sizeof(dark_keys) / sizeof(dark_keys[0]);
  86. t->dark_keys = (uint8_t*)malloc(sizeof(dark_keys));
  87. for (uint8_t i = 0; i < t->dark_keys_sz; ++i) {
  88. t->dark_keys[i] = dark_keys[i];
  89. }
  90. uint8_t mo_keys[] = {49, 55};
  91. t->mo_keys_sz = sizeof(mo_keys) / sizeof(mo_keys[0]);
  92. t->mo_keys = (uint8_t*)malloc(sizeof(mo_keys));
  93. for (uint8_t i = 0; i < t->mo_keys_sz; ++i) {
  94. t->mo_keys[i] = mo_keys[i];
  95. t->led_to_pos[mo_keys[i]] = (led_point_t){.x = 255, .y = 255};
  96. }
  97. t->dark_v = 0x30;
  98. t->white_v = 0xFF;
  99. t->white_s = 0xFF;
  100. t->nkro_h = 0x55;
  101. t->caps_h = 0xAA;
  102. }
  103. rgb_data_t rgb;
  104. void keyboard_post_init_user(void) { init_rgb_data(&rgb); }
  105. void rgb_matrix_indicators_advanced_user(uint8_t led_min, uint8_t led_max) {
  106. uint8_t l = get_highest_layer(layer_state);
  107. /* darken all dark keys (regardless of layer) */
  108. RGB dark = relative_hsv(0, 0, rgb.dark_v);
  109. for (uint8_t i = 0; i < rgb.dark_keys_sz; i++) {
  110. rgb_matrix_set_color(rgb.dark_keys[i], dark.r, dark.g, dark.b);
  111. }
  112. /* color non-default keys in non-default layers */
  113. RGB gray = relative_hsv(0, 0, rgb.white_v);
  114. bool nkro_off = !keymap_config.nkro;
  115. bool caps_on = host_keyboard_led_state().caps_lock;
  116. if (nkro_off && caps_on) {
  117. gray = relative_hsv((rgb.nkro_h + rgb.caps_h) / 2, /* average hue */
  118. rgb.white_s, rgb.white_v);
  119. } else if (nkro_off) {
  120. gray = relative_hsv(rgb.nkro_h, rgb.white_s, rgb.white_v);
  121. } else if (caps_on) {
  122. gray = relative_hsv(rgb.caps_h, rgb.white_s, rgb.white_v);
  123. }
  124. if (l != get_highest_layer(default_layer_state)) {
  125. for (uint8_t i = led_min; i <= led_max; i++) {
  126. led_point_t p = rgb.led_to_pos[i];
  127. if (p.x != 255 && keymaps[l][p.x][p.y] != KC_TRNS) {
  128. rgb_matrix_set_color(i, gray.r, gray.g, gray.b);
  129. }
  130. }
  131. for (uint8_t i = 0; i < rgb.mo_keys_sz; i++) {
  132. if (l == i + 1) {
  133. rgb_matrix_set_color(rgb.mo_keys[i], gray.r, gray.g, gray.b);
  134. }
  135. }
  136. }
  137. }
  138. #endif