From 64263bbb0255df756dc30467ee6643054d325597 Mon Sep 17 00:00:00 2001
From: MechMerlin <30334081+mechmerlin@users.noreply.github.com>
Date: Sun, 29 Dec 2019 11:16:24 -0800
Subject: [PATCH] [Keyboard] Orion Locking Indicator LED Support (#7683)

* add indicator lighting support thanks to Keebology tracing

* set the correct number of underglow LEDs

* add comments and skeletal structure for layer indicator LEDs

* add backlight areas pins

* update readme

* we can't turn leds on based on zones, so we use multiple backlight pin support instead to turn them ALL on and control them.

* add comments to backlight setting

* make a more useful default keymap that can also be used for testing

* add duck orion to list

* add indicator led support

* update readme

* Update keyboards/duck/orion/v3/matrix.c

Co-Authored-By: Drashna Jaelre <drashna@live.com>

* Update keyboards/duck/orion/v3/matrix.c

Co-Authored-By: Drashna Jaelre <drashna@live.com>

* Update keyboards/duck/orion/v3/matrix.c

Co-Authored-By: Drashna Jaelre <drashna@live.com>

* Update keyboards/duck/orion/v3/matrix.c

Co-Authored-By: Drashna Jaelre <drashna@live.com>

* Update keyboards/duck/orion/v3/matrix.c

Co-Authored-By: Drashna Jaelre <drashna@live.com>

* Update keyboards/duck/orion/v3/matrix.c

Co-Authored-By: Drashna Jaelre <drashna@live.com>

* add the appropriate header files

* fix indent so Drashna will not die

* Update keyboards/duck/orion/v3/v3.c

Co-Authored-By: James Young <18669334+noroadsleft@users.noreply.github.com>

* Update keyboards/duck/orion/v3/keymaps/default/keymap.c

Co-Authored-By: fauxpark <fauxpark@gmail.com>

* Update keyboards/duck/orion/v3/keymaps/default/keymap.c

Co-Authored-By: fauxpark <fauxpark@gmail.com>

* Update keyboards/duck/orion/v3/readme.md

Co-Authored-By: fauxpark <fauxpark@gmail.com>

* Update keyboards/duck/orion/v3/readme.md

Co-Authored-By: fauxpark <fauxpark@gmail.com>

* Update keyboards/duck/orion/v3/readme.md

Co-Authored-By: fauxpark <fauxpark@gmail.com>

Co-authored-by: Drashna Jaelre <drashna@live.com>
Co-authored-by: James Young <18669334+noroadsleft@users.noreply.github.com>
Co-authored-by: fauxpark <fauxpark@gmail.com>
---
 keyboards/duck/orion/v3/config.h              | 10 +++-
 .../duck/orion/v3/keymaps/default/keymap.c    | 22 ++++---
 keyboards/duck/orion/v3/matrix.c              | 31 +++++-----
 keyboards/duck/orion/v3/readme.md             | 14 ++---
 keyboards/duck/orion/v3/v3.c                  | 58 ++++++-------------
 keyboards/duck/readme.md                      |  1 +
 6 files changed, 64 insertions(+), 72 deletions(-)

diff --git a/keyboards/duck/orion/v3/config.h b/keyboards/duck/orion/v3/config.h
index 3a17d59416..b04729712e 100644
--- a/keyboards/duck/orion/v3/config.h
+++ b/keyboards/duck/orion/v3/config.h
@@ -37,11 +37,17 @@ along with this program.  If not, see <http://www.gnu.org/licenses/>.
 #define DEBOUNCE 5
 
 /* number of backlight levels */
-#define BACKLIGHT_LEVELS 3
+// #define BACKLIGHT_LEVELS 3
+
+#undef BACKLIGHT_PIN
+#define BACKLIGHT_PINS { B1, B2, B3, E6 }
+#define BACKLIGHT_LED_COUNT 4
+#define BACKLIGHT_LEVELS 10
+#define BACKLIGHT_ON_STATE 1
 
 #define RGBLIGHT_ANIMATIONS
 #define RGB_DI_PIN D6
-#define RGBLED_NUM 17
+#define RGBLED_NUM 18
 
 /* Set to top left most key */
 #define BOOTMAGIC_LITE_ROW 4
diff --git a/keyboards/duck/orion/v3/keymaps/default/keymap.c b/keyboards/duck/orion/v3/keymaps/default/keymap.c
index 83fa4f9a8e..0da0cdaeb3 100644
--- a/keyboards/duck/orion/v3/keymaps/default/keymap.c
+++ b/keyboards/duck/orion/v3/keymaps/default/keymap.c
@@ -16,11 +16,19 @@
 #include QMK_KEYBOARD_H
 
 const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
-[0] = LAYOUT_tkl_ansi(\
-  KC_ESC,  KC_F1,  KC_F2,  KC_F3,  KC_F4,  KC_F5,  KC_F6,  KC_F7,  KC_F8,  KC_F9, KC_F10, KC_F11, KC_F12,           KC_PSCR,KC_SLCK,KC_PAUS, \
-  KC_GRV,   KC_1,   KC_2,   KC_3,   KC_4,   KC_5,   KC_6,   KC_7,   KC_8,   KC_9,   KC_0,KC_MINS, KC_EQL,KC_BSPC,   KC_INS ,KC_HOME,KC_PGUP, \
-  KC_TAB,   KC_Q,   KC_W,   KC_E,   KC_R,   KC_T,   KC_Y,   KC_U,   KC_I,   KC_O,   KC_P,KC_LBRC,KC_RBRC,KC_BSLS,   KC_DEL ,KC_END ,KC_PGDN, \
-  KC_CAPS,   KC_A,   KC_S,   KC_D,   KC_F,   KC_G,   KC_H,   KC_J,   KC_K,   KC_L,KC_SCLN,KC_QUOT,         KC_ENT,                          \
-  KC_LSFT,    KC_Z,   KC_X,   KC_C,   KC_V,   KC_B,   KC_N,   KC_M,KC_COMM, KC_DOT,KC_SLSH,        KC_RSFT,            KC_UP,        \
-  KC_LCTL,KC_LGUI,KC_LALT,                 KC_SPC,                                KC_RALT,KC_RGUI, KC_APP,KC_RCTL,  KC_LEFT,KC_DOWN,KC_RGHT) \
+[0] = LAYOUT_tkl_ansi(
+  KC_ESC,  KC_F1,  KC_F2,  KC_F3,  KC_F4,  KC_F5,  KC_F6,  KC_F7,  KC_F8,  KC_F9, KC_F10, KC_F11, KC_F12,           KC_PSCR,KC_SLCK,KC_PAUS, 
+  KC_GRV,   KC_1,   KC_2,   KC_3,   KC_4,   KC_5,   KC_6,   KC_7,   KC_8,   KC_9,   KC_0,KC_MINS, KC_EQL,KC_BSPC,   KC_INS ,KC_HOME,KC_PGUP, 
+  KC_TAB,   KC_Q,   KC_W,   KC_E,   KC_R,   KC_T,   KC_Y,   KC_U,   KC_I,   KC_O,   KC_P,KC_LBRC,KC_RBRC,KC_BSLS,   KC_DEL ,KC_END ,KC_PGDN, 
+  KC_CAPS,   KC_A,   KC_S,   KC_D,   KC_F,   KC_G,   KC_H,   KC_J,   KC_K,   KC_L,KC_SCLN,KC_QUOT,         KC_ENT,                          
+  KC_LSFT,    KC_Z,   KC_X,   KC_C,   KC_V,   KC_B,   KC_N,   KC_M,KC_COMM, KC_DOT,KC_SLSH,        KC_RSFT,            KC_UP,        
+  KC_LCTL,KC_LGUI,KC_LALT,                 KC_SPC,                                KC_RALT,KC_RGUI, MO(1),KC_RCTL,  KC_LEFT,KC_DOWN,KC_RGHT),
+
+[1] = LAYOUT_tkl_ansi(
+  KC_TRNS,  KC_TRNS,  KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS,          KC_TRNS, KC_TRNS, KC_TRNS, 
+  KC_TRNS,  KC_TRNS,  KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, 
+  BL_TOGG,  BL_STEP,  BL_INC,  BL_DEC,  RESET,   KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, 
+  RGB_TOG,  RGB_MOD,  RGB_HUI, RGB_SAI, RGB_VAI, RGB_SPI, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS,                          
+  KC_TRNS,  RGB_RMOD, RGB_HUD, RGB_SAD, RGB_VAD, RGB_SPI, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS,        
+  KC_TRNS, KC_TRNS,KC_TRNS,                 KC_TRNS,                                 KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS) 
 };
diff --git a/keyboards/duck/orion/v3/matrix.c b/keyboards/duck/orion/v3/matrix.c
index 3c3240b9f4..f93c7a9cd9 100644
--- a/keyboards/duck/orion/v3/matrix.c
+++ b/keyboards/duck/orion/v3/matrix.c
@@ -14,13 +14,7 @@ You should have received a copy of the GNU General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
 
-#include <util/delay.h>
-#include <avr/io.h>
-#include <stdio.h>
-#include "matrix.h"
-#include "util.h"
-#include "print.h"
-#include "debug.h"
+#include "quantum.h"
 
 static uint8_t debouncing = DEBOUNCE;
 
@@ -54,18 +48,23 @@ void matrix_scan_user(void) {
 
 void backlight_init_ports(void)
 {
-  DDRD  |=  0b11010000;
-  PORTD &= ~0b01010000;
-  PORTD |=  0b10000000;
-  DDRB  |=  0b00011111;
-  PORTB &= ~0b00001110;
-  PORTB |=  0b00010001;
-  DDRE  |=  0b01000000;
-  PORTE &= ~0b01000000;
+  
+}
+
+void indicator_init_ports(void) {
+
+  // Num LED
+  setPinOutput(B4); 
+
+  // Caps Lock
+  setPinOutput(B0); 
+
+  // Scroll Lock
+  setPinOutput(D7); 
 }
 
 void matrix_init(void) {
-  backlight_init_ports();
+  indicator_init_ports();
   unselect_cols();
   init_rows();
 
diff --git a/keyboards/duck/orion/v3/readme.md b/keyboards/duck/orion/v3/readme.md
index 50032e93ee..8d1b83401e 100644
--- a/keyboards/duck/orion/v3/readme.md
+++ b/keyboards/duck/orion/v3/readme.md
@@ -3,9 +3,9 @@
 Non official firmware for custom TKL Korean keyboard made by Duck.  
 Group buy was run December 2018 via [geekhack](https://geekhack.org/index.php?topic=98581.0) with 100 keyboards total. 
 
-Keyboard Maintainer: [MechMerlin](https://github.com/mechmerlin)  
-Hardware Supported: Duck Eagle/Viper PCB Ver 2.0, Atmega32u4  
-Hardware Availability: Wait until GB of the next revision
+* Keyboard Maintainer: [MechMerlin](https://github.com/mechmerlin)
+* Hardware Supported: Duck Orion V3 PCB Ver 2.1
+* Hardware Availability: Wait until GB of the next revision
 
 Make example for this keyboard (after setting up your build environment):
 
@@ -13,7 +13,7 @@ Make example for this keyboard (after setting up your build environment):
 
 **Reset Key:** To put the Orion V3 into reset, hold Backspace key (`K4N`) while plugging in. 
 
-**CAUTION:** At this time 10/03/19 lighting has not been tested fully and may not properly work. 
+**CAUTION:** At this time 12/19/19 layer indicator lighting has not been implemented by default. 
 
 See the [build environment setup](https://docs.qmk.fm/#/getting_started_build_tools) and the [make instructions](https://docs.qmk.fm/#/getting_started_make_guide) for more information. Brand new to QMK? Start with our [Complete Newbs Guide](https://docs.qmk.fm/#/newbs).
 
@@ -22,9 +22,9 @@ See the [build environment setup](https://docs.qmk.fm/#/getting_started_build_to
 The Orion V3 PCB consists of:
 
 ### Microchips
-2 74HC237D 3-to-8 line decoders
+2 74HC237D 3-to-8 line decoders U1, U2
 1 Atmega32u4 microcontroller
-2 WS2811 LED controller
+2 WS2811 LED controller U5, U6
 
 ## Notes
-Special thanks to Marcus aka Keebology for doing this remotely and mapping the matrix. 
\ No newline at end of file
+Special thanks to Marcus aka Keebology for doing this remotely and mapping the matrix, indicator LEDs, and backlight LEDs. 
diff --git a/keyboards/duck/orion/v3/v3.c b/keyboards/duck/orion/v3/v3.c
index 95da4b45b6..ca5c512cee 100644
--- a/keyboards/duck/orion/v3/v3.c
+++ b/keyboards/duck/orion/v3/v3.c
@@ -16,48 +16,26 @@
 #include "v3.h"
 #include "indicator_leds.h"
 
-enum BACKLIGHT_AREAS {
-  BACKLIGHT_ALPHAS   = 0b00000010,
-  BACKLIGHT_MODNUM   = 0b00001000
-};
+// Alphas              PB1
+// Navigation Cluster: PB2
+// Number Row, Mods:   PB3
+// Function Row:       PE6
 
-void backlight_set(uint8_t level) {
-  switch(level) {
-  case 0:
-    PORTB |= BACKLIGHT_ALPHAS;
-    PORTB |= BACKLIGHT_MODNUM;
-  break;
-  case 1:
-    PORTB &= ~BACKLIGHT_ALPHAS;
-    PORTB |= BACKLIGHT_MODNUM;
-  break;
-  case 2:
-    PORTB |= BACKLIGHT_ALPHAS;
-    PORTB &= ~BACKLIGHT_MODNUM;
-  break;
-  case 3:
-    PORTB &= ~BACKLIGHT_ALPHAS;
-    PORTB &= ~BACKLIGHT_MODNUM;
-  break;
-  }
-}
+// Other than using RGB or LED matrix, QMK cannot turn on specific zones
+// of backlight LEDs. Unfortunately, Duck PCBs do not follow this design
+// and instead use multiple pins connected to each of these zones. QMK is
+// only able to control them ALL with the current default mechanisms. 
 
-// Port from backlight_update_state
-void led_set_kb(uint8_t usb_led) {
-    bool status[8] = {
-    IS_HOST_LED_ON(USB_LED_SCROLL_LOCK), /* LED 3 */
-    IS_HOST_LED_ON(USB_LED_CAPS_LOCK),   /* LED 2 */
-    IS_HOST_LED_ON(USB_LED_NUM_LOCK),    /* LED 1 */
-
-    layer_state & (1<<2),                            /* LED 6 */
-    layer_state & (1<<1),                            /* LED 5 */
-    layer_state & (1<<0) ? 0: 1,                     /* LED 4 */
-
-    layer_state & (1<<5),                            /* LED 8 */
-    layer_state & (1<<4)                             /* LED 7 */
-  };
-
-  indicator_leds_set(status);
+// Locking indicator LEDs
+// The Duck Orion V3 has 3 locking indicator LEDs and are located to the right
+// of the Escape key. 
+bool led_update_kb(led_t led_state) {
+    if(led_update_user(led_state)) {
+        writePin(B0, !led_state.caps_lock);
+        writePin(B4, !led_state.num_lock);
+        writePin(D7, !led_state.scroll_lock);
+    }
+    return true;
 }
 
 bool process_record_kb(uint16_t keycode, keyrecord_t *record) {
diff --git a/keyboards/duck/readme.md b/keyboards/duck/readme.md
index caf7e156d1..32076da7da 100644
--- a/keyboards/duck/readme.md
+++ b/keyboards/duck/readme.md
@@ -13,4 +13,5 @@ Jetfire V1
 Lightsaver V3  
 Octagon V1  
 Octagon V2  
+Orion V3  
 Viper V2  
\ No newline at end of file