Browse Source

[Bugfix] WS2812 indexing in split boards (#25407)

* initial

* oops

* Update quantum/rgb_matrix/rgb_matrix.c

Co-authored-by: フィルターペーパー <76888457+filterpaper@users.noreply.github.com>

---------

Co-authored-by: フィルターペーパー <76888457+filterpaper@users.noreply.github.com>
Pablo Martínez 7 months ago
parent
commit
fe1c3fc835
2 changed files with 27 additions and 3 deletions
  1. 19 3
      quantum/rgb_matrix/rgb_matrix.c
  2. 8 0
      quantum/rgb_matrix/rgb_matrix.h

+ 19 - 3
quantum/rgb_matrix/rgb_matrix.c

@@ -158,15 +158,31 @@ void rgb_matrix_update_pwm_buffers(void) {
 
 __attribute__((weak)) int rgb_matrix_led_index(int index) {
 #if defined(RGB_MATRIX_SPLIT)
-    if (!is_keyboard_left() && index >= k_rgb_matrix_split[0]) {
-        return index - k_rgb_matrix_split[0];
+    const bool index_in_left_side = index < k_rgb_matrix_split[0];
+
+    if (is_keyboard_left()) {
+        if (index_in_left_side) {
+            return index;
+        }
+        return -1;
+    }
+
+    if (index_in_left_side) {
+        return -1;
     }
+
+    return index - k_rgb_matrix_split[0];
 #endif
     return index;
 }
 
 void rgb_matrix_set_color(int index, uint8_t red, uint8_t green, uint8_t blue) {
-    rgb_matrix_driver.set_color(rgb_matrix_led_index(index), red, green, blue);
+    const int led_index = rgb_matrix_led_index(index);
+    if (led_index < 0) {
+        return;
+    }
+
+    rgb_matrix_driver.set_color(led_index, red, green, blue);
 }
 
 void rgb_matrix_set_color_all(uint8_t red, uint8_t green, uint8_t blue) {

+ 8 - 0
quantum/rgb_matrix/rgb_matrix.h

@@ -151,6 +151,14 @@ void eeconfig_force_flush_rgb_matrix(void);
 uint8_t rgb_matrix_map_row_column_to_led_kb(uint8_t row, uint8_t column, uint8_t *led_i);
 uint8_t rgb_matrix_map_row_column_to_led(uint8_t row, uint8_t column, uint8_t *led_i);
 
+/**
+ * @brief Convert an index to the addressing used by underlying driver
+ *
+ * @param index  Logical index of the LED we want to work on
+ *
+ * @return       Resolved index to be used on the driver
+ * @retval < 0   Error (eg: trying to address a LED on the other half of a split board)
+ */
 int rgb_matrix_led_index(int index);
 
 void rgb_matrix_set_color(int index, uint8_t red, uint8_t green, uint8_t blue);