From 484a9b12bc3961fc0dd9481d3cb734959db42191 Mon Sep 17 00:00:00 2001
From: Wilba <Jason.S.Williams@gmail.com>
Date: Tue, 21 Jan 2020 05:18:25 +1100
Subject: [PATCH] Add VIA support for QMK backlight, QMK RGBLight (#7911)

* Add VIA support for QMK backlight, QMK RGBLight

* clang-format changes
---
 keyboards/cannonkeys/an_c/config.h            |   6 +
 keyboards/cannonkeys/instant60/config.h       |   6 +
 keyboards/cannonkeys/iron165/config.h         |   3 +
 keyboards/cannonkeys/satisfaction75/config.h  |   3 +
 .../cannonkeys/satisfaction75/led_custom.h    |   2 +
 .../satisfaction75/satisfaction75.c           |  63 ++++++
 keyboards/cannonkeys/savage65/config.h        |   6 +
 keyboards/cannonkeys/stm32f072/keyboard.c     |  78 ++++++++
 keyboards/cannonkeys/stm32f072/led_custom.h   |   2 +
 keyboards/cannonkeys/tmov2/config.h           |   6 +
 keyboards/hs60/v2/config.h                    |   2 +
 keyboards/nk65/config.h                       |   3 +
 keyboards/projectkb/alice/config.h            |   6 +
 keyboards/wilba_tech/rama_works_koyu/config.h |   5 +-
 .../wilba_tech/rama_works_m60_a/config.h      |   3 +
 keyboards/wilba_tech/rama_works_m6_a/config.h |   3 +
 keyboards/wilba_tech/rama_works_m6_b/config.h |   3 +
 .../wilba_tech/rama_works_u80_a/config.h      |   3 +
 keyboards/wilba_tech/wt60_a/config.h          |   2 +
 keyboards/wilba_tech/wt65_a/config.h          |   2 +
 keyboards/wilba_tech/wt65_b/config.h          |   2 +
 keyboards/wilba_tech/wt75_a/config.h          |   2 +
 keyboards/wilba_tech/wt75_b/config.h          |   2 +
 keyboards/wilba_tech/wt75_c/config.h          |   2 +
 keyboards/wilba_tech/wt80_a/config.h          |   3 +
 keyboards/wilba_tech/wt_main.c                |   6 +-
 keyboards/wilba_tech/wt_mono_backlight.c      |   6 +-
 keyboards/wilba_tech/wt_rgb_backlight.c       |   6 +-
 keyboards/wilba_tech/zeal60/config.h          |   5 +-
 keyboards/wilba_tech/zeal65/config.h          |   3 +
 keyboards/xelus/dawn60/config.h               |   3 +
 quantum/backlight/backlight.c                 |  20 +-
 quantum/backlight/backlight.h                 |   2 +
 quantum/rgblight.c                            |  20 ++
 quantum/rgblight.h                            |   6 +
 quantum/via.c                                 | 182 +++++++++++++++++-
 quantum/via.h                                 |  58 ++++--
 37 files changed, 500 insertions(+), 35 deletions(-)

diff --git a/keyboards/cannonkeys/an_c/config.h b/keyboards/cannonkeys/an_c/config.h
index 55414ba509..6390e07897 100644
--- a/keyboards/cannonkeys/an_c/config.h
+++ b/keyboards/cannonkeys/an_c/config.h
@@ -62,6 +62,12 @@ along with this program.  If not, see <http://www.gnu.org/licenses/>.
 // dynamic keymaps start after this.
 #define VIA_EEPROM_CUSTOM_CONFIG_SIZE 1
 
+// VIA lighting is handled by the keyboard-level code
+#define VIA_CUSTOM_LIGHTING_ENABLE
+
+// Let VIA handle the QMK RGBLIGHT 
+#define VIA_QMK_RGBLIGHT_ENABLE
+
 /*
  * Feature disable options
  *  These options are also useful to firmware size reduction.
diff --git a/keyboards/cannonkeys/instant60/config.h b/keyboards/cannonkeys/instant60/config.h
index 2d23d5f83c..4fa76cb883 100644
--- a/keyboards/cannonkeys/instant60/config.h
+++ b/keyboards/cannonkeys/instant60/config.h
@@ -62,6 +62,12 @@ along with this program.  If not, see <http://www.gnu.org/licenses/>.
 // dynamic keymaps start after this.
 #define VIA_EEPROM_CUSTOM_CONFIG_SIZE 1
 
+// VIA lighting is handled by the keyboard-level code
+#define VIA_CUSTOM_LIGHTING_ENABLE
+
+// Let VIA handle the QMK RGBLIGHT 
+#define VIA_QMK_RGBLIGHT_ENABLE
+
 /*
  * Feature disable options
  *  These options are also useful to firmware size reduction.
diff --git a/keyboards/cannonkeys/iron165/config.h b/keyboards/cannonkeys/iron165/config.h
index 640f6fb659..6350514d75 100644
--- a/keyboards/cannonkeys/iron165/config.h
+++ b/keyboards/cannonkeys/iron165/config.h
@@ -54,6 +54,9 @@ along with this program.  If not, see <http://www.gnu.org/licenses/>.
 // dynamic keymaps start after this.
 #define VIA_EEPROM_CUSTOM_CONFIG_SIZE 1
 
+// VIA lighting is handled by the keyboard-level code
+#define VIA_CUSTOM_LIGHTING_ENABLE
+
 /*
  * Feature disable options
  *  These options are also useful to firmware size reduction.
diff --git a/keyboards/cannonkeys/satisfaction75/config.h b/keyboards/cannonkeys/satisfaction75/config.h
index 7a4354c8a7..69d02806e2 100644
--- a/keyboards/cannonkeys/satisfaction75/config.h
+++ b/keyboards/cannonkeys/satisfaction75/config.h
@@ -73,6 +73,9 @@ along with this program.  If not, see <http://www.gnu.org/licenses/>.
 // 6 for 3x custom encoder settings, left, right, and press (18 bytes)
 #define VIA_EEPROM_CUSTOM_CONFIG_SIZE 21
 
+// VIA lighting is handled by the keyboard-level code
+#define VIA_CUSTOM_LIGHTING_ENABLE
+
 /*
  * Feature disable options
  *  These options are also useful to firmware size reduction.
diff --git a/keyboards/cannonkeys/satisfaction75/led_custom.h b/keyboards/cannonkeys/satisfaction75/led_custom.h
index fe5c9e5dcf..d818b48ce9 100644
--- a/keyboards/cannonkeys/satisfaction75/led_custom.h
+++ b/keyboards/cannonkeys/satisfaction75/led_custom.h
@@ -3,3 +3,5 @@
 void backlight_task(void);
 void breathing_interrupt_disable(void);
 void breathing_interrupt_enable(void);
+void breathing_enable(void);
+void breathing_disable(void);
diff --git a/keyboards/cannonkeys/satisfaction75/satisfaction75.c b/keyboards/cannonkeys/satisfaction75/satisfaction75.c
index 0fe09c384f..f92067c98f 100644
--- a/keyboards/cannonkeys/satisfaction75/satisfaction75.c
+++ b/keyboards/cannonkeys/satisfaction75/satisfaction75.c
@@ -55,6 +55,54 @@ backlight_config_t kb_backlight_config = {
 };
 
 #ifdef VIA_ENABLE
+
+void backlight_get_value( uint8_t *data )
+{
+	uint8_t *value_id = &(data[0]);
+	uint8_t *value_data = &(data[1]);
+  switch (*value_id)
+  {
+    case id_qmk_backlight_brightness:
+    {
+      // level / BACKLIGHT_LEVELS * 255
+      value_data[0] = ((uint16_t)kb_backlight_config.level) * 255 / BACKLIGHT_LEVELS;
+      break;
+    }
+    case id_qmk_backlight_effect:
+    {
+      value_data[0] = kb_backlight_config.breathing ? 1 : 0;
+      break;
+    }
+  }
+}
+
+void backlight_set_value( uint8_t *data )
+{
+	uint8_t *value_id = &(data[0]);
+	uint8_t *value_data = &(data[1]);
+  switch (*value_id)
+  {
+    case id_qmk_backlight_brightness:
+    {
+      // level / 255 * BACKLIGHT_LEVELS
+      kb_backlight_config.level = ((uint16_t)value_data[0]) * BACKLIGHT_LEVELS / 255;
+      backlight_set(kb_backlight_config.level);
+      break;
+    }
+    case id_qmk_backlight_effect:
+    {
+      if ( value_data[0] == 0 ) {
+        kb_backlight_config.breathing = false;
+        breathing_disable();
+      } else {
+        kb_backlight_config.breathing = true;
+        breathing_enable();
+      }
+      break;
+    }
+  }
+}
+
 void raw_hid_receive_kb( uint8_t *data, uint8_t length )
 {
   uint8_t *command_id = &(data[0]);
@@ -139,6 +187,21 @@ void raw_hid_receive_kb( uint8_t *data, uint8_t length )
       }
       break;
     }
+    case id_lighting_set_value:
+    {
+      backlight_set_value(command_data);
+      break;
+    }
+    case id_lighting_get_value:
+    {
+      backlight_get_value(command_data);
+      break;
+    }
+    case id_lighting_save:
+    {
+      backlight_config_save();
+      break;
+    }
     default:
     {
       // Unhandled message.
diff --git a/keyboards/cannonkeys/savage65/config.h b/keyboards/cannonkeys/savage65/config.h
index b69aba39eb..98ec1067a8 100644
--- a/keyboards/cannonkeys/savage65/config.h
+++ b/keyboards/cannonkeys/savage65/config.h
@@ -62,6 +62,12 @@ along with this program.  If not, see <http://www.gnu.org/licenses/>.
 // dynamic keymaps start after this.
 #define VIA_EEPROM_CUSTOM_CONFIG_SIZE 1
 
+// VIA lighting is handled by the keyboard-level code
+#define VIA_CUSTOM_LIGHTING_ENABLE
+
+// Let VIA handle the QMK RGBLIGHT 
+#define VIA_QMK_RGBLIGHT_ENABLE
+
 /*
  * Feature disable options
  *  These options are also useful to firmware size reduction.
diff --git a/keyboards/cannonkeys/stm32f072/keyboard.c b/keyboards/cannonkeys/stm32f072/keyboard.c
index bf35a76c18..fdda018fc4 100644
--- a/keyboards/cannonkeys/stm32f072/keyboard.c
+++ b/keyboards/cannonkeys/stm32f072/keyboard.c
@@ -126,8 +126,86 @@ bool process_record_kb(uint16_t keycode, keyrecord_t *record) {
   return process_record_user(keycode, record);;
 }
 
+#ifdef VIA_ENABLE
 
+void backlight_get_value( uint8_t *data )
+{
+	uint8_t *value_id = &(data[0]);
+	uint8_t *value_data = &(data[1]);
+  switch (*value_id)
+  {
+    case id_qmk_backlight_brightness:
+    {
+      // level / BACKLIGHT_LEVELS * 255
+      value_data[0] = ((uint16_t)kb_backlight_config.level) * 255 / BACKLIGHT_LEVELS;
+      break;
+    }
+    case id_qmk_backlight_effect:
+    {
+      value_data[0] = kb_backlight_config.breathing ? 1 : 0;
+      break;
+    }
+  }
+}
 
+void backlight_set_value( uint8_t *data )
+{
+	uint8_t *value_id = &(data[0]);
+	uint8_t *value_data = &(data[1]);
+  switch (*value_id)
+  {
+    case id_qmk_backlight_brightness:
+    {
+      // level / 255 * BACKLIGHT_LEVELS
+      kb_backlight_config.level = ((uint16_t)value_data[0]) * BACKLIGHT_LEVELS / 255;
+      backlight_set(kb_backlight_config.level);
+      break;
+    }
+    case id_qmk_backlight_effect:
+    {
+      if ( value_data[0] == 0 ) {
+        kb_backlight_config.breathing = false;
+        breathing_disable();
+      } else {
+        kb_backlight_config.breathing = true;
+        breathing_enable();
+      }
+      break;
+    }
+  }
+}
+
+void raw_hid_receive_kb( uint8_t *data, uint8_t length )
+{
+  uint8_t *command_id = &(data[0]);
+  uint8_t *command_data = &(data[1]);
+  switch ( *command_id )
+  {
+    case id_lighting_set_value:
+    {
+      backlight_set_value(command_data);
+      break;
+    }
+    case id_lighting_get_value:
+    {
+      backlight_get_value(command_data);
+      break;
+    }
+    case id_lighting_save:
+    {
+      backlight_config_save();
+      break;
+    }
+    default:
+    {
+      // Unhandled message.
+      *command_id = id_unhandled;
+      break;
+    }
+  }
+  // DO NOT call raw_hid_send(data,length) here, let caller do this
+}
+#endif
 
 //
 // In the case of VIA being disabled, we still need to check if
diff --git a/keyboards/cannonkeys/stm32f072/led_custom.h b/keyboards/cannonkeys/stm32f072/led_custom.h
index 28e0f02ed3..550d5b84cc 100644
--- a/keyboards/cannonkeys/stm32f072/led_custom.h
+++ b/keyboards/cannonkeys/stm32f072/led_custom.h
@@ -4,3 +4,5 @@ void backlight_task(void);
 void breathing_interrupt_disable(void);
 void breathing_interrupt_enable(void);
 void breathing_toggle(void);
+void breathing_enable(void);
+void breathing_disable(void);
diff --git a/keyboards/cannonkeys/tmov2/config.h b/keyboards/cannonkeys/tmov2/config.h
index d5f1d07884..159382fbcb 100644
--- a/keyboards/cannonkeys/tmov2/config.h
+++ b/keyboards/cannonkeys/tmov2/config.h
@@ -62,6 +62,12 @@ along with this program.  If not, see <http://www.gnu.org/licenses/>.
 // dynamic keymaps start after this.
 #define VIA_EEPROM_CUSTOM_CONFIG_SIZE 1
 
+// VIA lighting is handled by the keyboard-level code
+#define VIA_CUSTOM_LIGHTING_ENABLE
+
+// Let VIA handle the QMK RGBLIGHT 
+#define VIA_QMK_RGBLIGHT_ENABLE
+
 /*
  * Feature disable options
  *  These options are also useful to firmware size reduction.
diff --git a/keyboards/hs60/v2/config.h b/keyboards/hs60/v2/config.h
index 91fdf60809..42d10bdb52 100644
--- a/keyboards/hs60/v2/config.h
+++ b/keyboards/hs60/v2/config.h
@@ -138,3 +138,5 @@ along with this program.  If not, see <http://www.gnu.org/licenses/>.
 // dynamic keymaps start after this.
 #define VIA_EEPROM_CUSTOM_CONFIG_SIZE 31
 
+// VIA lighting is handled by the keyboard-level code
+#define VIA_CUSTOM_LIGHTING_ENABLE
diff --git a/keyboards/nk65/config.h b/keyboards/nk65/config.h
index 767959b626..46450a14c8 100755
--- a/keyboards/nk65/config.h
+++ b/keyboards/nk65/config.h
@@ -141,3 +141,6 @@ along with this program.  If not, see <http://www.gnu.org/licenses/>.
 // Backlight config starts after VIA's EEPROM usage,
 // dynamic keymaps start after this.
 #define VIA_EEPROM_CUSTOM_CONFIG_SIZE 31
+
+// VIA lighting is handled by the keyboard-level code
+#define VIA_CUSTOM_LIGHTING_ENABLE
diff --git a/keyboards/projectkb/alice/config.h b/keyboards/projectkb/alice/config.h
index 4a068a8893..94c1fd16d6 100644
--- a/keyboards/projectkb/alice/config.h
+++ b/keyboards/projectkb/alice/config.h
@@ -62,6 +62,12 @@ along with this program.  If not, see <http://www.gnu.org/licenses/>.
 // dynamic keymaps start after this.
 #define VIA_EEPROM_CUSTOM_CONFIG_SIZE 1
 
+// VIA lighting is handled by the keyboard-level code
+#define VIA_CUSTOM_LIGHTING_ENABLE
+
+// Let VIA handle the QMK RGBLIGHT 
+#define VIA_QMK_RGBLIGHT_ENABLE
+
 /*
  * Feature disable options
  *  These options are also useful to firmware size reduction.
diff --git a/keyboards/wilba_tech/rama_works_koyu/config.h b/keyboards/wilba_tech/rama_works_koyu/config.h
index 9cbbd6620b..7f945b4a4a 100644
--- a/keyboards/wilba_tech/rama_works_koyu/config.h
+++ b/keyboards/wilba_tech/rama_works_koyu/config.h
@@ -121,4 +121,7 @@
 
 // Backlight config starts after VIA's EEPROM usage,
 // dynamic keymaps start after this.
-#define VIA_EEPROM_CUSTOM_CONFIG_SIZE 31
\ No newline at end of file
+#define VIA_EEPROM_CUSTOM_CONFIG_SIZE 31
+
+// VIA lighting is handled by the keyboard-level code
+#define VIA_CUSTOM_LIGHTING_ENABLE
diff --git a/keyboards/wilba_tech/rama_works_m60_a/config.h b/keyboards/wilba_tech/rama_works_m60_a/config.h
index 8381a4db00..849d1a4615 100644
--- a/keyboards/wilba_tech/rama_works_m60_a/config.h
+++ b/keyboards/wilba_tech/rama_works_m60_a/config.h
@@ -119,3 +119,6 @@
 // Backlight config starts after VIA's EEPROM usage,
 // dynamic keymaps start after this.
 #define VIA_EEPROM_CUSTOM_CONFIG_SIZE 31
+
+// VIA lighting is handled by the keyboard-level code
+#define VIA_CUSTOM_LIGHTING_ENABLE
diff --git a/keyboards/wilba_tech/rama_works_m6_a/config.h b/keyboards/wilba_tech/rama_works_m6_a/config.h
index 1a61604bb4..cbb75e289c 100644
--- a/keyboards/wilba_tech/rama_works_m6_a/config.h
+++ b/keyboards/wilba_tech/rama_works_m6_a/config.h
@@ -109,3 +109,6 @@
 // Backlight config starts after VIA's EEPROM usage,
 // dynamic keymaps start after this.
 #define VIA_EEPROM_CUSTOM_CONFIG_SIZE 43
+
+// VIA lighting is handled by the keyboard-level code
+#define VIA_CUSTOM_LIGHTING_ENABLE
diff --git a/keyboards/wilba_tech/rama_works_m6_b/config.h b/keyboards/wilba_tech/rama_works_m6_b/config.h
index 3ebfd80dab..1af1cf694b 100644
--- a/keyboards/wilba_tech/rama_works_m6_b/config.h
+++ b/keyboards/wilba_tech/rama_works_m6_b/config.h
@@ -152,3 +152,6 @@
 // Backlight config starts after VIA's EEPROM usage,
 // dynamic keymaps start after this.
 #define VIA_EEPROM_CUSTOM_CONFIG_SIZE 43
+
+// VIA lighting is handled by the keyboard-level code
+#define VIA_CUSTOM_LIGHTING_ENABLE
diff --git a/keyboards/wilba_tech/rama_works_u80_a/config.h b/keyboards/wilba_tech/rama_works_u80_a/config.h
index 748b2cb291..2523b962e7 100644
--- a/keyboards/wilba_tech/rama_works_u80_a/config.h
+++ b/keyboards/wilba_tech/rama_works_u80_a/config.h
@@ -235,3 +235,6 @@
 // Backlight config starts after VIA's EEPROM usage,
 // dynamic keymaps start after this.
 #define VIA_EEPROM_CUSTOM_CONFIG_SIZE 31
+
+// VIA lighting is handled by the keyboard-level code
+#define VIA_CUSTOM_LIGHTING_ENABLE
diff --git a/keyboards/wilba_tech/wt60_a/config.h b/keyboards/wilba_tech/wt60_a/config.h
index 269afbcb8b..e07b911661 100644
--- a/keyboards/wilba_tech/wt60_a/config.h
+++ b/keyboards/wilba_tech/wt60_a/config.h
@@ -204,3 +204,5 @@
 // dynamic keymaps start after this.
 #define VIA_EEPROM_CUSTOM_CONFIG_SIZE 7
 
+// VIA lighting is handled by the keyboard-level code
+#define VIA_CUSTOM_LIGHTING_ENABLE
diff --git a/keyboards/wilba_tech/wt65_a/config.h b/keyboards/wilba_tech/wt65_a/config.h
index ad4c120f4d..bf19fe38cc 100644
--- a/keyboards/wilba_tech/wt65_a/config.h
+++ b/keyboards/wilba_tech/wt65_a/config.h
@@ -201,3 +201,5 @@
 // dynamic keymaps start after this.
 #define VIA_EEPROM_CUSTOM_CONFIG_SIZE 7
 
+// VIA lighting is handled by the keyboard-level code
+#define VIA_CUSTOM_LIGHTING_ENABLE
diff --git a/keyboards/wilba_tech/wt65_b/config.h b/keyboards/wilba_tech/wt65_b/config.h
index f98fa4e729..d74275a287 100644
--- a/keyboards/wilba_tech/wt65_b/config.h
+++ b/keyboards/wilba_tech/wt65_b/config.h
@@ -201,3 +201,5 @@
 // dynamic keymaps start after this.
 #define VIA_EEPROM_CUSTOM_CONFIG_SIZE 7
 
+// VIA lighting is handled by the keyboard-level code
+#define VIA_CUSTOM_LIGHTING_ENABLE
diff --git a/keyboards/wilba_tech/wt75_a/config.h b/keyboards/wilba_tech/wt75_a/config.h
index 0c0a2ff082..9b058eec41 100644
--- a/keyboards/wilba_tech/wt75_a/config.h
+++ b/keyboards/wilba_tech/wt75_a/config.h
@@ -204,3 +204,5 @@
 // dynamic keymaps start after this.
 #define VIA_EEPROM_CUSTOM_CONFIG_SIZE 7
 
+// VIA lighting is handled by the keyboard-level code
+#define VIA_CUSTOM_LIGHTING_ENABLE
diff --git a/keyboards/wilba_tech/wt75_b/config.h b/keyboards/wilba_tech/wt75_b/config.h
index 0c7b3dbfad..d22ac97e02 100644
--- a/keyboards/wilba_tech/wt75_b/config.h
+++ b/keyboards/wilba_tech/wt75_b/config.h
@@ -204,3 +204,5 @@
 // dynamic keymaps start after this.
 #define VIA_EEPROM_CUSTOM_CONFIG_SIZE 7
 
+// VIA lighting is handled by the keyboard-level code
+#define VIA_CUSTOM_LIGHTING_ENABLE
diff --git a/keyboards/wilba_tech/wt75_c/config.h b/keyboards/wilba_tech/wt75_c/config.h
index b84f2444d8..08b3c2ec49 100644
--- a/keyboards/wilba_tech/wt75_c/config.h
+++ b/keyboards/wilba_tech/wt75_c/config.h
@@ -204,3 +204,5 @@
 // dynamic keymaps start after this.
 #define VIA_EEPROM_CUSTOM_CONFIG_SIZE 7
 
+// VIA lighting is handled by the keyboard-level code
+#define VIA_CUSTOM_LIGHTING_ENABLE
diff --git a/keyboards/wilba_tech/wt80_a/config.h b/keyboards/wilba_tech/wt80_a/config.h
index b706956ef9..5916970159 100644
--- a/keyboards/wilba_tech/wt80_a/config.h
+++ b/keyboards/wilba_tech/wt80_a/config.h
@@ -200,3 +200,6 @@
 // Backlight config starts after VIA's EEPROM usage,
 // dynamic keymaps start after this.
 #define VIA_EEPROM_CUSTOM_CONFIG_SIZE 7
+
+// VIA lighting is handled by the keyboard-level code
+#define VIA_CUSTOM_LIGHTING_ENABLE
diff --git a/keyboards/wilba_tech/wt_main.c b/keyboards/wilba_tech/wt_main.c
index 1ad12ee4a5..32721b654e 100644
--- a/keyboards/wilba_tech/wt_main.c
+++ b/keyboards/wilba_tech/wt_main.c
@@ -123,17 +123,17 @@ void raw_hid_receive_kb(uint8_t *data, uint8_t length) {
     switch ( *command_id )
     {
 #if RGB_BACKLIGHT_ENABLED || MONO_BACKLIGHT_ENABLED
-        case id_backlight_config_set_value:
+        case id_lighting_set_value:
         {
             backlight_config_set_value(command_data);
             break;
         }
-        case id_backlight_config_get_value:
+        case id_lighting_get_value:
         {
             backlight_config_get_value(command_data);
             break;
         }
-        case id_backlight_config_save:
+        case id_lighting_save:
         {
             backlight_config_save();
             break;
diff --git a/keyboards/wilba_tech/wt_mono_backlight.c b/keyboards/wilba_tech/wt_mono_backlight.c
index daf738da2e..fbc3102e8c 100644
--- a/keyboards/wilba_tech/wt_mono_backlight.c
+++ b/keyboards/wilba_tech/wt_mono_backlight.c
@@ -26,9 +26,13 @@
 #include "quantum/color.h"
 #include "tmk_core/common/eeprom.h"
 
-#include "via.h" // uses only the EEPROM address
+#include "via.h" // uses EEPROM address, lighting value IDs
 #define MONO_BACKLIGHT_CONFIG_EEPROM_ADDR (VIA_EEPROM_CUSTOM_CONFIG_ADDR)
 
+#if VIA_EEPROM_CUSTOM_CONFIG_SIZE == 0
+#error VIA_EEPROM_CUSTOM_CONFIG_SIZE was not defined to store backlight_config struct
+#endif
+
 #include "drivers/issi/is31fl3736.h"
 
 #define ISSI_ADDR_DEFAULT 0x50
diff --git a/keyboards/wilba_tech/wt_rgb_backlight.c b/keyboards/wilba_tech/wt_rgb_backlight.c
index babb844d26..4af8e15e3a 100644
--- a/keyboards/wilba_tech/wt_rgb_backlight.c
+++ b/keyboards/wilba_tech/wt_rgb_backlight.c
@@ -50,9 +50,13 @@ LED_TYPE g_ws2812_leds[WS2812_LED_TOTAL];
 #include "quantum/color.h"
 #include "tmk_core/common/eeprom.h"
 
-#include "via.h" // uses only the EEPROM address
+#include "via.h" // uses EEPROM address, lighting value IDs
 #define RGB_BACKLIGHT_CONFIG_EEPROM_ADDR (VIA_EEPROM_CUSTOM_CONFIG_ADDR)
 
+#if VIA_EEPROM_CUSTOM_CONFIG_SIZE == 0
+#error VIA_EEPROM_CUSTOM_CONFIG_SIZE was not defined to store backlight_config struct
+#endif
+
 #if defined(RGB_BACKLIGHT_M6_B)
 #include "drivers/issi/is31fl3218.h"
 #define BACKLIGHT_LED_COUNT 6
diff --git a/keyboards/wilba_tech/zeal60/config.h b/keyboards/wilba_tech/zeal60/config.h
index cb2f6a96bd..9e2badd2ad 100644
--- a/keyboards/wilba_tech/zeal60/config.h
+++ b/keyboards/wilba_tech/zeal60/config.h
@@ -117,4 +117,7 @@
 
 // Backlight config starts after VIA's EEPROM usage,
 // dynamic keymaps start after this.
-#define VIA_EEPROM_CUSTOM_CONFIG_SIZE 31
\ No newline at end of file
+#define VIA_EEPROM_CUSTOM_CONFIG_SIZE 31
+
+// VIA lighting is handled by the keyboard-level code
+#define VIA_CUSTOM_LIGHTING_ENABLE
diff --git a/keyboards/wilba_tech/zeal65/config.h b/keyboards/wilba_tech/zeal65/config.h
index 01649d76fe..df32aff77c 100644
--- a/keyboards/wilba_tech/zeal65/config.h
+++ b/keyboards/wilba_tech/zeal65/config.h
@@ -118,3 +118,6 @@
 // Backlight config starts after VIA's EEPROM usage,
 // dynamic keymaps start after this.
 #define VIA_EEPROM_CUSTOM_CONFIG_SIZE 31
+
+// VIA lighting is handled by the keyboard-level code
+#define VIA_CUSTOM_LIGHTING_ENABLE
\ No newline at end of file
diff --git a/keyboards/xelus/dawn60/config.h b/keyboards/xelus/dawn60/config.h
index 7691bb301f..59eb828fa6 100644
--- a/keyboards/xelus/dawn60/config.h
+++ b/keyboards/xelus/dawn60/config.h
@@ -140,3 +140,6 @@
 // Backlight config starts after VIA's EEPROM usage,
 // dynamic keymaps start after this.
 #define VIA_EEPROM_CUSTOM_CONFIG_SIZE 31
+
+// VIA lighting is handled by the keyboard-level code
+#define VIA_CUSTOM_LIGHTING_ENABLE
diff --git a/quantum/backlight/backlight.c b/quantum/backlight/backlight.c
index e57b31d103..4aa74667d9 100644
--- a/quantum/backlight/backlight.c
+++ b/quantum/backlight/backlight.c
@@ -130,18 +130,30 @@ void backlight_step(void) {
     backlight_set(backlight_config.level);
 }
 
+/** \brief Backlight set level without EEPROM update
+ *
+ */
+void backlight_level_noeeprom(uint8_t level) {
+    if (level > BACKLIGHT_LEVELS) level = BACKLIGHT_LEVELS;
+    backlight_config.level  = level;
+    backlight_config.enable = !!backlight_config.level;
+    backlight_set(backlight_config.level);
+}
+
 /** \brief Backlight set level
  *
  * FIXME: needs doc
  */
 void backlight_level(uint8_t level) {
-    if (level > BACKLIGHT_LEVELS) level = BACKLIGHT_LEVELS;
-    backlight_config.level  = level;
-    backlight_config.enable = !!backlight_config.level;
+    backlight_level_noeeprom(level);
     eeconfig_update_backlight(backlight_config.raw);
-    backlight_set(backlight_config.level);
 }
 
+/** \brief Update current backlight state to EEPROM
+ *
+ */
+void eeconfig_update_backlight_current(void) { eeconfig_update_backlight(backlight_config.raw); }
+
 /** \brief Get backlight level
  *
  * FIXME: needs doc
diff --git a/quantum/backlight/backlight.h b/quantum/backlight/backlight.h
index 9f0a5e81d7..08acf942ff 100644
--- a/quantum/backlight/backlight.h
+++ b/quantum/backlight/backlight.h
@@ -48,8 +48,10 @@ bool    is_backlight_enabled(void);
 void    backlight_step(void);
 void    backlight_increase(void);
 void    backlight_decrease(void);
+void    backlight_level_noeeprom(uint8_t level);
 void    backlight_level(uint8_t level);
 uint8_t get_backlight_level(void);
+void    eeconfig_update_backlight_current(void);
 
 // implementation specific
 void backlight_init_ports(void);
diff --git a/quantum/rgblight.c b/quantum/rgblight.c
index 26887f057e..85b3191105 100644
--- a/quantum/rgblight.c
+++ b/quantum/rgblight.c
@@ -160,6 +160,10 @@ void eeconfig_update_rgblight(uint32_t val) {
 #endif
 }
 
+void eeconfig_update_rgblight_current(void) {
+    eeconfig_update_rgblight(rgblight_config.raw);
+}
+
 void eeconfig_update_rgblight_default(void) {
     rgblight_config.enable = 1;
     rgblight_config.mode   = RGBLIGHT_MODE_STATIC_LIGHT;
@@ -501,6 +505,22 @@ void rgblight_sethsv(uint8_t hue, uint8_t sat, uint8_t val) { rgblight_sethsv_ee
 
 void rgblight_sethsv_noeeprom(uint8_t hue, uint8_t sat, uint8_t val) { rgblight_sethsv_eeprom_helper(hue, sat, val, false); }
 
+uint8_t rgblight_get_speed(void) { return rgblight_config.speed; }
+
+void rgblight_set_speed_eeprom_helper(uint8_t speed, bool write_to_eeprom) {
+    rgblight_config.speed = speed;
+    if (write_to_eeprom) {
+        eeconfig_update_rgblight(rgblight_config.raw);   // EECONFIG needs to be increased to support this
+        dprintf("rgblight set speed [EEPROM]: %u\n", rgblight_config.speed);
+    } else {
+        dprintf("rgblight set speed [NOEEPROM]: %u\n", rgblight_config.speed);
+    }
+}
+
+void rgblight_set_speed(uint8_t speed) { rgblight_set_speed_eeprom_helper(speed, true); }
+
+void rgblight_set_speed_noeeprom(uint8_t speed) { rgblight_set_speed_eeprom_helper(speed, false); }
+
 uint8_t rgblight_get_hue(void) { return rgblight_config.hue; }
 
 uint8_t rgblight_get_sat(void) { return rgblight_config.sat; }
diff --git a/quantum/rgblight.h b/quantum/rgblight.h
index e3aa098e4d..f6746e50ff 100644
--- a/quantum/rgblight.h
+++ b/quantum/rgblight.h
@@ -233,6 +233,11 @@ void rgblight_decrease_speed(void);
 void rgblight_sethsv(uint8_t hue, uint8_t sat, uint8_t val);
 void rgblight_sethsv_noeeprom(uint8_t hue, uint8_t sat, uint8_t val);
 
+/*   effect speed */
+uint8_t rgblight_get_speed(void);
+void rgblight_set_speed(uint8_t speed);
+void rgblight_set_speed_noeeprom(uint8_t speed);
+
 /*       query */
 uint8_t rgblight_get_mode(void);
 uint8_t rgblight_get_hue(void);
@@ -245,6 +250,7 @@ uint32_t rgblight_read_dword(void);
 void     rgblight_update_dword(uint32_t dword);
 uint32_t eeconfig_read_rgblight(void);
 void     eeconfig_update_rgblight(uint32_t val);
+void     eeconfig_update_rgblight_current(void);
 void     eeconfig_update_rgblight_default(void);
 void     eeconfig_debug_rgblight(void);
 
diff --git a/quantum/via.c b/quantum/via.c
index b3934d9f00..f85af8d9e1 100644
--- a/quantum/via.c
+++ b/quantum/via.c
@@ -22,14 +22,42 @@
 #    error "DYNAMIC_KEYMAP_ENABLE is not enabled"
 #endif
 
+// If VIA_CUSTOM_LIGHTING_ENABLE is not defined, then VIA_QMK_BACKLIGHT_ENABLE is set
+// if BACKLIGHT_ENABLE is set, so handling of QMK Backlight values happens here by default.
+// if VIA_CUSTOM_LIGHTING_ENABLE is defined, then VIA_QMK_BACKLIGHT_ENABLE must be explicitly
+// set in keyboard-level config.h, so handling of QMK Backlight values happens here
+#if defined(BACKLIGHT_ENABLE) && !defined(VIA_CUSTOM_LIGHTING_ENABLE)
+#    define VIA_QMK_BACKLIGHT_ENABLE
+#endif
+
+// If VIA_CUSTOM_LIGHTING_ENABLE is not defined, then VIA_QMK_RGBLIGHT_ENABLE is set
+// if RGBLIGHT_ENABLE is set, so handling of QMK RGBLIGHT values happens here by default.
+// If VIA_CUSTOM_LIGHTING_ENABLE is defined, then VIA_QMK_RGBLIGHT_ENABLE must be explicitly
+// set in keyboard-level config.h, so handling of QMK RGBLIGHT values happens here
+#if defined(RGBLIGHT_ENABLE) && !defined(VIA_CUSTOM_LIGHTING_ENABLE)
+#    define VIA_QMK_RGBLIGHT_ENABLE
+#endif
+
 #include "quantum.h"
 
 #include "via.h"
+
 #include "raw_hid.h"
 #include "dynamic_keymap.h"
 #include "tmk_core/common/eeprom.h"
 #include "version.h"  // for QMK_BUILDDATE used in EEPROM magic
 
+// Forward declare some helpers.
+#if defined(VIA_QMK_BACKLIGHT_ENABLE)
+void via_qmk_backlight_set_value(uint8_t *data);
+void via_qmk_backlight_get_value(uint8_t *data);
+#endif
+
+#if defined(VIA_QMK_RGBLIGHT_ENABLE)
+void via_qmk_rgblight_set_value(uint8_t *data);
+void via_qmk_rgblight_get_value(uint8_t *data);
+#endif
+
 // Can be called in an overriding via_init_kb() to test if keyboard level code usage of
 // EEPROM is invalid and use/save defaults.
 bool via_eeprom_is_valid(void) {
@@ -282,10 +310,52 @@ void raw_hid_receive(uint8_t *data, uint8_t length) {
             dynamic_keymap_reset();
             break;
         }
-        case id_backlight_config_set_value:
-        case id_backlight_config_get_value:
-        case id_backlight_config_save: {
+        case id_lighting_set_value: {
+#if defined(VIA_QMK_BACKLIGHT_ENABLE)
+            via_qmk_backlight_set_value(command_data);
+#endif
+#if defined(VIA_QMK_RGBLIGHT_ENABLE)
+            via_qmk_rgblight_set_value(command_data);
+#endif
+#if defined(VIA_CUSTOM_LIGHTING_ENABLE)
             raw_hid_receive_kb(data, length);
+#endif
+#if !defined(VIA_QMK_BACKLIGHT_ENABLE) && !defined(VIA_QMK_RGBLIGHT_ENABLE) && !defined(VIA_CUSTOM_LIGHTING_ENABLE)
+            // Return the unhandled state
+            *command_id = id_unhandled;
+#endif
+            break;
+        }
+        case id_lighting_get_value: {
+#if defined(VIA_QMK_BACKLIGHT_ENABLE)
+            via_qmk_backlight_get_value(command_data);
+#endif
+#if defined(VIA_QMK_RGBLIGHT_ENABLE)
+            via_qmk_rgblight_get_value(command_data);
+#endif
+#if defined(VIA_CUSTOM_LIGHTING_ENABLE)
+            raw_hid_receive_kb(data, length);
+#endif
+#if !defined(VIA_QMK_BACKLIGHT_ENABLE) && !defined(VIA_QMK_RGBLIGHT_ENABLE) && !defined(VIA_CUSTOM_LIGHTING_ENABLE)
+            // Return the unhandled state
+            *command_id = id_unhandled;
+#endif
+            break;
+        }
+        case id_lighting_save: {
+#if defined(VIA_QMK_BACKLIGHT_ENABLE)
+            eeconfig_update_backlight_current();
+#endif
+#if defined(VIA_QMK_RGBLIGHT_ENABLE)
+            eeconfig_update_rgblight_current();
+#endif
+#if defined(VIA_CUSTOM_LIGHTING_ENABLE)
+            raw_hid_receive_kb(data, length);
+#endif
+#if !defined(VIA_QMK_BACKLIGHT_ENABLE) && !defined(VIA_QMK_RGBLIGHT_ENABLE) && !defined(VIA_CUSTOM_LIGHTING_ENABLE)
+            // Return the unhandled state
+            *command_id = id_unhandled;
+#endif
             break;
         }
         case id_dynamic_keymap_macro_get_count: {
@@ -355,3 +425,109 @@ void raw_hid_receive(uint8_t *data, uint8_t length) {
     // (i.e. returning state to the host, or the unhandled state).
     raw_hid_send(data, length);
 }
+
+#if defined(VIA_QMK_BACKLIGHT_ENABLE)
+
+#    if BACKLIGHT_LEVELS == 0
+#        error BACKLIGHT_LEVELS == 0
+#    endif
+
+void via_qmk_backlight_get_value(uint8_t *data) {
+    uint8_t *value_id   = &(data[0]);
+    uint8_t *value_data = &(data[1]);
+    switch (*value_id) {
+        case id_qmk_backlight_brightness: {
+            // level / BACKLIGHT_LEVELS * 255
+            value_data[0] = ((uint16_t)get_backlight_level()) * 255 / BACKLIGHT_LEVELS;
+            break;
+        }
+        case id_qmk_backlight_effect: {
+#    ifdef BACKLIGHT_BREATHING
+            value_data[0] = is_backlight_breathing() ? 1 : 0;
+#    else
+            value_data[0] = 0;
+#    endif
+            break;
+        }
+    }
+}
+
+void via_qmk_backlight_set_value(uint8_t *data) {
+    uint8_t *value_id   = &(data[0]);
+    uint8_t *value_data = &(data[1]);
+    switch (*value_id) {
+        case id_qmk_backlight_brightness: {
+            // level / 255 * BACKLIGHT_LEVELS
+            backlight_level_noeeprom(((uint16_t)value_data[0]) * BACKLIGHT_LEVELS / 255);
+            break;
+        }
+        case id_qmk_backlight_effect: {
+#    ifdef BACKLIGHT_BREATHING
+            if (value_data[0] == 0) {
+                backlight_disable_breathing();
+            } else {
+                backlight_enable_breathing();
+            }
+#    endif
+            break;
+        }
+    }
+}
+
+#endif  // #if defined(VIA_QMK_BACKLIGHT_ENABLE)
+
+#if defined(VIA_QMK_RGBLIGHT_ENABLE)
+
+void via_qmk_rgblight_get_value(uint8_t *data) {
+    uint8_t *value_id   = &(data[0]);
+    uint8_t *value_data = &(data[1]);
+    switch (*value_id) {
+        case id_qmk_rgblight_brightness: {
+            value_data[0] = rgblight_get_val();
+            break;
+        }
+        case id_qmk_rgblight_effect: {
+            value_data[0] = rgblight_get_mode();
+            break;
+        }
+        case id_qmk_rgblight_effect_speed: {
+            value_data[0] = rgblight_get_speed();
+            break;
+        }
+        case id_qmk_rgblight_color: {
+            value_data[0] = rgblight_get_hue();
+            value_data[1] = rgblight_get_sat();
+            break;
+        }
+    }
+}
+
+void via_qmk_rgblight_set_value(uint8_t *data) {
+    uint8_t *value_id   = &(data[0]);
+    uint8_t *value_data = &(data[1]);
+    switch (*value_id) {
+        case id_qmk_rgblight_brightness: {
+            rgblight_sethsv_noeeprom(rgblight_get_hue(), rgblight_get_sat(), value_data[0]);
+            break;
+        }
+        case id_qmk_rgblight_effect: {
+            rgblight_mode_noeeprom(value_data[0]);
+            if (value_data[0] == 0) {
+                rgblight_disable_noeeprom();
+            } else {
+                rgblight_enable_noeeprom();
+            }
+            break;
+        }
+        case id_qmk_rgblight_effect_speed: {
+            rgblight_set_speed_noeeprom(value_data[0]);
+            break;
+        }
+        case id_qmk_rgblight_color: {
+            rgblight_sethsv_noeeprom(value_data[0], value_data[1], rgblight_get_val());
+            break;
+        }
+    }
+}
+
+#endif  // #if defined(VIA_QMK_RGBLIGHT_ENABLE)
diff --git a/quantum/via.h b/quantum/via.h
index 98f8dea8c3..012547e055 100644
--- a/quantum/via.h
+++ b/quantum/via.h
@@ -51,29 +51,45 @@
 #define VIA_PROTOCOL_VERSION 0x0009
 
 enum via_command_id {
-    id_get_protocol_version = 0x01,  // always 0x01
-    id_get_keyboard_value,
-    id_set_keyboard_value,
-    id_dynamic_keymap_get_keycode,
-    id_dynamic_keymap_set_keycode,
-    id_dynamic_keymap_reset,
-    id_backlight_config_set_value,
-    id_backlight_config_get_value,
-    id_backlight_config_save,
-    id_eeprom_reset,
-    id_bootloader_jump,
-    id_dynamic_keymap_macro_get_count,
-    id_dynamic_keymap_macro_get_buffer_size,
-    id_dynamic_keymap_macro_get_buffer,
-    id_dynamic_keymap_macro_set_buffer,
-    id_dynamic_keymap_macro_reset,
-    id_dynamic_keymap_get_layer_count,
-    id_dynamic_keymap_get_buffer,
-    id_dynamic_keymap_set_buffer,
-    id_unhandled = 0xFF,
+    id_get_protocol_version                 = 0x01,  // always 0x01
+    id_get_keyboard_value                   = 0x02,
+    id_set_keyboard_value                   = 0x03,
+    id_dynamic_keymap_get_keycode           = 0x04,
+    id_dynamic_keymap_set_keycode           = 0x05,
+    id_dynamic_keymap_reset                 = 0x06,
+    id_lighting_set_value                   = 0x07,
+    id_lighting_get_value                   = 0x08,
+    id_lighting_save                        = 0x09,
+    id_eeprom_reset                         = 0x0A,
+    id_bootloader_jump                      = 0x0B,
+    id_dynamic_keymap_macro_get_count       = 0x0C,
+    id_dynamic_keymap_macro_get_buffer_size = 0x0D,
+    id_dynamic_keymap_macro_get_buffer      = 0x0E,
+    id_dynamic_keymap_macro_set_buffer      = 0x0F,
+    id_dynamic_keymap_macro_reset           = 0x10,
+    id_dynamic_keymap_get_layer_count       = 0x11,
+    id_dynamic_keymap_get_buffer            = 0x12,
+    id_dynamic_keymap_set_buffer            = 0x13,
+    id_unhandled                            = 0xFF,
 };
 
-enum via_keyboard_value_id { id_uptime = 0x01, id_layout_options, id_switch_matrix_state };
+enum via_keyboard_value_id {
+    id_uptime              = 0x01,  //
+    id_layout_options      = 0x02,
+    id_switch_matrix_state = 0x03
+};
+
+enum via_lighting_value {
+    // QMK BACKLIGHT
+    id_qmk_backlight_brightness = 0x09,
+    id_qmk_backlight_effect     = 0x0A,
+
+    // QMK RGBLIGHT
+    id_qmk_rgblight_brightness   = 0x80,
+    id_qmk_rgblight_effect       = 0x81,
+    id_qmk_rgblight_effect_speed = 0x82,
+    id_qmk_rgblight_color        = 0x83,
+};
 
 // Can't use SAFE_RANGE here, it might change if someone adds
 // new values to enum quantum_keycodes.