rgb_matrix_user.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. #include "quantum.h"
  2. #include "md_rgb_matrix.h"
  3. extern issi3733_led_t *led_cur;
  4. extern uint8_t led_per_run;
  5. extern issi3733_led_t *lede;
  6. extern issi3733_led_t led_map[];
  7. static uint16_t last_boost_update;
  8. static uint8_t led_boosts[ISSI3733_LED_COUNT];
  9. static uint8_t led_boost_index;
  10. static uint8_t led_cur_index;
  11. #define LED_BOOST_REFRESH_INTERVAL_IN_MS 40
  12. #define LED_BOOST_DECAY 0.7
  13. #define LED_BOOST_PROPAGATE 0.5
  14. #define LED_BOOST_PEAK 100
  15. #define MIN_RGB 0x050008
  16. #define MIN_R (MIN_RGB >> 16 & 0xff)
  17. #define MIN_G (MIN_RGB >> 8 & 0xff)
  18. #define MIN_B (MIN_RGB & 0xff)
  19. #define MAX_RGB 0xc26eff
  20. #define MAX_R (MAX_RGB >> 16 & 0xff)
  21. #define MAX_G (MAX_RGB >> 8 & 0xff)
  22. #define MAX_B (MAX_RGB & 0xff)
  23. #define UNDERGLOW_RGB 0x4f002e
  24. #define UNDERGLOW_R (UNDERGLOW_RGB >> 16 & 0xff)
  25. #define UNDERGLOW_G (UNDERGLOW_RGB >> 8 & 0xff)
  26. #define UNDERGLOW_B (UNDERGLOW_RGB & 0xff)
  27. #define UNDERGLOW_SCAN_CODE 255
  28. #define max(a, b) (((a) > (b)) ? (a) : (b))
  29. #define __ -1
  30. static const uint8_t KEY_TO_LED_MAP[MATRIX_ROWS][MATRIX_COLS] = {
  31. { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14},
  32. {15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29},
  33. {30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, __, 42, 43},
  34. {44, __, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57},
  35. {58, 59, 60, __, __, __, 61, __, __, __, 62, 63, 64, 65, 66},
  36. };
  37. #define KEY_LED_COUNT 67
  38. #define KP(c, r) { .col = c, .row = r } // shorthand for keypos_t
  39. static const keypos_t LED_TO_KEY_MAP[KEY_LED_COUNT] = {
  40. KP(0, 0), KP(1, 0), KP(2, 0), KP(3, 0), KP(4, 0), KP(5, 0), KP(6, 0), KP(7, 0), KP(8, 0), KP(9, 0), KP(10, 0), KP(11, 0), KP(12, 0), KP(13, 0), KP(14, 0),
  41. KP(0, 1), KP(1, 1), KP(2, 1), KP(3, 1), KP(4, 1), KP(5, 1), KP(6, 1), KP(7, 1), KP(8, 1), KP(9, 1), KP(10, 1), KP(11, 1), KP(12, 1), KP(13, 1), KP(14, 1),
  42. KP(0, 2), KP(1, 2), KP(2, 2), KP(3, 2), KP(4, 2), KP(5, 2), KP(6, 2), KP(7, 2), KP(8, 2), KP(9, 2), KP(10, 2), KP(11, 2), KP(13, 2), KP(14, 2),
  43. KP(0, 3), KP(2, 3), KP(3, 3), KP(4, 3), KP(5, 3), KP(6, 3), KP(7, 3), KP(8, 3), KP(9, 3), KP(10, 3), KP(11, 3), KP(12, 3), KP(13, 3), KP(14, 3),
  44. KP(0, 4), KP(1, 4), KP(2, 4), KP(6, 4), KP(10, 4), KP(11, 4), KP(12, 4), KP(13, 4), KP(14, 4),
  45. };
  46. static void update_led_boosts(void);
  47. static void update_led_cur_rgb_values(void);
  48. static void set_nearest_led_to_max(uint8_t col, uint8_t row);
  49. static uint8_t calculate_new_color_component_value(uint8_t max, uint8_t min);
  50. static void calculate_new_led_boosts(uint8_t new_led_boosts[]);
  51. static uint8_t calculate_new_led_boost_at(int index);
  52. static uint8_t get_propagated_boost_from_neighbors(int led_position);
  53. static uint8_t get_led_boost_at_keypos(uint8_t row, uint8_t col);
  54. static void set_new_led_boosts(uint8_t* new_led_boosts);
  55. static uint8_t map_key_position_to_led_index(uint8_t col, uint8_t row);
  56. void rgb_matrix_init_user(void) {
  57. for (int i = 0; i < ISSI3733_LED_COUNT; i++) {
  58. led_boosts[i] = 0;
  59. }
  60. last_boost_update = timer_read();
  61. led_boost_index = 0;
  62. led_cur_index = 0;
  63. }
  64. void md_rgb_matrix_run(void) {
  65. uint8_t led_this_run = 0;
  66. if (led_cur == 0) { //Denotes start of new processing cycle in the case of chunked processing
  67. led_cur = led_map;
  68. led_cur_index = 0;
  69. }
  70. update_led_boosts();
  71. while (led_cur < lede && led_this_run < led_per_run) {
  72. update_led_cur_rgb_values();
  73. led_cur++;
  74. led_cur_index++;
  75. led_this_run++;
  76. }
  77. }
  78. void rgb_matrix_record_key_press(keyrecord_t *record) {
  79. if (record->event.pressed) {
  80. keypos_t key = record->event.key;
  81. set_nearest_led_to_max(key.col, key.row);
  82. }
  83. }
  84. static void update_led_boosts(void) {
  85. if (timer_elapsed(last_boost_update) > LED_BOOST_REFRESH_INTERVAL_IN_MS) {
  86. last_boost_update = timer_read();
  87. uint8_t new_led_boosts[ISSI3733_LED_COUNT];
  88. calculate_new_led_boosts(new_led_boosts);
  89. set_new_led_boosts(new_led_boosts);
  90. }
  91. }
  92. static void update_led_cur_rgb_values(void) {
  93. if (led_cur->scan == UNDERGLOW_SCAN_CODE) {
  94. *led_cur->rgb.r = UNDERGLOW_R;
  95. *led_cur->rgb.g = UNDERGLOW_G;
  96. *led_cur->rgb.b = UNDERGLOW_B;
  97. } else {
  98. *led_cur->rgb.r = calculate_new_color_component_value(MAX_R, MIN_R);
  99. *led_cur->rgb.g = calculate_new_color_component_value(MAX_G, MIN_G);
  100. *led_cur->rgb.b = calculate_new_color_component_value(MAX_B, MIN_B);
  101. }
  102. }
  103. static void set_nearest_led_to_max(uint8_t col, uint8_t row) {
  104. uint8_t led_index = map_key_position_to_led_index(col, row);
  105. if (led_index >= 0 && led_index < ISSI3733_LED_COUNT) {
  106. led_boosts[led_index] = LED_BOOST_PEAK;
  107. }
  108. }
  109. static uint8_t calculate_new_color_component_value(uint8_t max, uint8_t min) {
  110. uint8_t current_boost = led_boosts[led_cur_index];
  111. return (float)(max - min) * current_boost / LED_BOOST_PEAK + min;
  112. }
  113. static void calculate_new_led_boosts(uint8_t new_led_boosts[]) {
  114. for (int i = 0; i < ISSI3733_LED_COUNT; i++) {
  115. new_led_boosts[i] = calculate_new_led_boost_at(i);
  116. }
  117. }
  118. static uint8_t calculate_new_led_boost_at(int index) {
  119. uint8_t decayed_boost = led_boosts[index] * LED_BOOST_DECAY;
  120. uint8_t propagated_boost = get_propagated_boost_from_neighbors(index);
  121. uint8_t new_boost = (propagated_boost > decayed_boost) ? propagated_boost : decayed_boost;
  122. if (new_boost > LED_BOOST_PEAK) {
  123. new_boost = LED_BOOST_PEAK;
  124. }
  125. return new_boost;
  126. }
  127. static uint8_t get_propagated_boost_from_neighbors(int led_position) {
  128. if (led_position < 0 || led_position >= KEY_LED_COUNT) {
  129. return 0;
  130. }
  131. keypos_t led_keypos = LED_TO_KEY_MAP[led_position];
  132. uint8_t top_boost = get_led_boost_at_keypos(led_keypos.row - 1, led_keypos.col);
  133. uint8_t bottom_boost = get_led_boost_at_keypos(led_keypos.row + 1, led_keypos.col);
  134. uint8_t left_boost = get_led_boost_at_keypos(led_keypos.row, led_keypos.col - 1);
  135. uint8_t right_boost = get_led_boost_at_keypos(led_keypos.row, led_keypos.col + 1);
  136. uint8_t max_boost = max(max(top_boost, bottom_boost), max(left_boost, right_boost));
  137. if (max_boost > LED_BOOST_PEAK) {
  138. max_boost = LED_BOOST_PEAK;
  139. }
  140. return max_boost * LED_BOOST_PROPAGATE;
  141. }
  142. static uint8_t get_led_boost_at_keypos(uint8_t row, uint8_t col) {
  143. if (row < 0 || row >= MATRIX_ROWS || col < 0 || col >= MATRIX_COLS) {
  144. return 0;
  145. }
  146. uint8_t led_index = KEY_TO_LED_MAP[row][col];
  147. if (led_index < 0) {
  148. return 0;
  149. }
  150. return led_boosts[led_index];
  151. }
  152. static void set_new_led_boosts(uint8_t* new_led_boosts) {
  153. for (int i = 0; i < ISSI3733_LED_COUNT; i++) {
  154. led_boosts[i] = new_led_boosts[i];
  155. }
  156. }
  157. static uint8_t map_key_position_to_led_index(uint8_t col, uint8_t row) {
  158. if (row >= 0 && row < MATRIX_ROWS && col >= 0 && col < MATRIX_COLS) {
  159. return KEY_TO_LED_MAP[row][col];
  160. }
  161. return -1;
  162. }