From cf9f6bbd917c56e7347993f9116953628b16bc6a Mon Sep 17 00:00:00 2001
From: Jack Humbert <jack.humb@gmail.com>
Date: Mon, 6 Nov 2017 08:38:36 -0500
Subject: [PATCH] adds per-layer rgb color option to ez

---
 keyboards/ergodox_ez/keymaps/default/config.h | 30 +++++++++++++
 keyboards/ergodox_ez/keymaps/default/keymap.c | 42 +++++++++++++++----
 tmk_core/common/action_layer.c                |  7 +++-
 tmk_core/common/action_layer.h                |  2 +
 4 files changed, 72 insertions(+), 9 deletions(-)
 create mode 100644 keyboards/ergodox_ez/keymaps/default/config.h

diff --git a/keyboards/ergodox_ez/keymaps/default/config.h b/keyboards/ergodox_ez/keymaps/default/config.h
new file mode 100644
index 0000000000..850848c011
--- /dev/null
+++ b/keyboards/ergodox_ez/keymaps/default/config.h
@@ -0,0 +1,30 @@
+/*
+Copyright 2017 Jack Humbert <jack.humbgmail.com>
+
+This program is free software: you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation, either version 2 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program.  If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#ifndef ERGODOX_EZ_USER_CONFIG_H
+#define ERGODOX_EZ_USRE_CONFIG_H
+
+#include "config_common.h"
+
+#define RGBLIGHT_COLOR_LAYER_0 0x00, 0x00, 0xFF
+#define RGBLIGHT_COLOR_LAYER_1 0xFF, 0x00, 0x00
+#define RGBLIGHT_COLOR_LAYER_2 0x00, 0xFF, 0x00
+#define RGBLIGHT_COLOR_LAYER_3 0xFF, 0xFF, 0x00
+#define RGBLIGHT_COLOR_LAYER_4 0x00, 0xFF, 0xFF
+#define RGBLIGHT_COLOR_LAYER_5 0xFF, 0x00, 0xFF
+
+#endif
\ No newline at end of file
diff --git a/keyboards/ergodox_ez/keymaps/default/keymap.c b/keyboards/ergodox_ez/keymaps/default/keymap.c
index 4f0136880e..ccb4ae3ada 100644
--- a/keyboards/ergodox_ez/keymaps/default/keymap.c
+++ b/keyboards/ergodox_ez/keymaps/default/keymap.c
@@ -197,27 +197,53 @@ void matrix_init_user(void) {
 
 };
 
-
 // Runs constantly in the background, in a loop.
 void matrix_scan_user(void) {
 
-  uint8_t layer = biton32(layer_state);
+};
 
+// Runs whenever there is a layer state change.
+uint32_t layer_state_set_user(uint32_t state) {
   ergodox_board_led_off();
   ergodox_right_led_1_off();
   ergodox_right_led_2_off();
   ergodox_right_led_3_off();
+
+  uint8_t layer = biton32(state);
   switch (layer) {
-    // TODO: Make this relevant to the ErgoDox EZ.
-    case SYMB:
+    case 0:
+      #ifdef RGBLIGHT_COLOR_LAYER_0
+        rgblight_setrgb(RGBLIGHT_COLOR_LAYER_0);
+      #endif
+      break;
+    case 1:
       ergodox_right_led_1_on();
+      #ifdef RGBLIGHT_COLOR_LAYER_1
+        rgblight_setrgb(RGBLIGHT_COLOR_LAYER_1);
+      #endif
       break;
-    case MDIA:
+    case 2:
       ergodox_right_led_2_on();
+      #ifdef RGBLIGHT_COLOR_LAYER_2
+        rgblight_setrgb(RGBLIGHT_COLOR_LAYER_2);
+      #endif
       break;
-    default:
-      // none
+    case 3:
+      #ifdef RGBLIGHT_COLOR_LAYER_3
+        rgblight_setrgb(RGBLIGHT_COLOR_LAYER_3);
+      #endif
+      break;
+    case 4:
+      #ifdef RGBLIGHT_COLOR_LAYER_4
+        rgblight_setrgb(RGBLIGHT_COLOR_LAYER_4);
+      #endif
+      break;
+    case 5:
+      #ifdef RGBLIGHT_COLOR_LAYER_5
+        rgblight_setrgb(RGBLIGHT_COLOR_LAYER_5);
+      #endif
       break;
   }
 
-};
+  return state;
+};
\ No newline at end of file
diff --git a/tmk_core/common/action_layer.c b/tmk_core/common/action_layer.c
index c6a166a823..277a8f5d95 100644
--- a/tmk_core/common/action_layer.c
+++ b/tmk_core/common/action_layer.c
@@ -64,10 +64,15 @@ void default_layer_xor(uint32_t state)
 uint32_t layer_state = 0;
 
 __attribute__((weak))
-uint32_t layer_state_set_kb(uint32_t state) {
+uint32_t layer_state_set_user(uint32_t state) {
     return state;
 }
 
+__attribute__((weak))
+uint32_t layer_state_set_kb(uint32_t state) {
+    return layer_state_set_user(state);
+}
+
 static void layer_state_set(uint32_t state)
 {
     state = layer_state_set_kb(state);
diff --git a/tmk_core/common/action_layer.h b/tmk_core/common/action_layer.h
index d89ed6e5ce..e71604d872 100644
--- a/tmk_core/common/action_layer.h
+++ b/tmk_core/common/action_layer.h
@@ -72,6 +72,8 @@ void layer_xor(uint32_t state);
 #define layer_xor(state)
 #define layer_debug()
 
+__attribute__((weak))
+uint32_t layer_state_set_user(uint32_t state);
 __attribute__((weak))
 uint32_t layer_state_set_kb(uint32_t state);
 #endif