From 0feb01eef8239fe8c856a3c27e0e8829b18e4dbe Mon Sep 17 00:00:00 2001
From: Ivan Gromov <38141348+key10iq@users.noreply.github.com>
Date: Sat, 27 Jul 2024 20:01:57 +0400
Subject: [PATCH] Add Vial support for keyten/lisa (#761)

* Update lisa.c

* Create config.h

* Create config.h

* Add files via upload

* Update keyboard.json
---
 keyboards/keyten/lisa/config.h               |   1 +
 keyboards/keyten/lisa/keyboard.json          |   4 -
 keyboards/keyten/lisa/keymaps/vial/config.h  |   8 +
 keyboards/keyten/lisa/keymaps/vial/keymap.c  |  27 ++
 keyboards/keyten/lisa/keymaps/vial/rules.mk  |   3 +
 keyboards/keyten/lisa/keymaps/vial/vial.json | 365 +++++++++++++++++++
 keyboards/keyten/lisa/lisa.c                 |  82 ++++-
 7 files changed, 481 insertions(+), 9 deletions(-)
 create mode 100644 keyboards/keyten/lisa/config.h
 create mode 100644 keyboards/keyten/lisa/keymaps/vial/config.h
 create mode 100644 keyboards/keyten/lisa/keymaps/vial/keymap.c
 create mode 100644 keyboards/keyten/lisa/keymaps/vial/rules.mk
 create mode 100644 keyboards/keyten/lisa/keymaps/vial/vial.json

diff --git a/keyboards/keyten/lisa/config.h b/keyboards/keyten/lisa/config.h
new file mode 100644
index 00000000000..406288331b7
--- /dev/null
+++ b/keyboards/keyten/lisa/config.h
@@ -0,0 +1 @@
+#define VIA_EEPROM_LAYOUT_OPTIONS_SIZE 2
diff --git a/keyboards/keyten/lisa/keyboard.json b/keyboards/keyten/lisa/keyboard.json
index deac0f37408..c08bb4ba00c 100644
--- a/keyboards/keyten/lisa/keyboard.json
+++ b/keyboards/keyten/lisa/keyboard.json
@@ -16,10 +16,6 @@
         "cols": ["B7", "B6", "B5", "B4", "B3", "A15", "A3", "A4", "A5", "A6", "A7", "B0", "B1"],
         "rows": ["B13", "B15", "B14", "A8"]
     },
-	"indicators": {
-        "caps_lock": "B10",
-        "num_lock": "B11"
-    }
     "processor": "STM32F072",
     "usb": {
         "vid": "0xEB69",
diff --git a/keyboards/keyten/lisa/keymaps/vial/config.h b/keyboards/keyten/lisa/keymaps/vial/config.h
new file mode 100644
index 00000000000..fd6ed169800
--- /dev/null
+++ b/keyboards/keyten/lisa/keymaps/vial/config.h
@@ -0,0 +1,8 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+
+#pragma once
+
+#define VIAL_KEYBOARD_UID {0x53, 0xB6, 0xC9, 0x0C, 0xCF, 0x17, 0xD4, 0x8B}
+
+#define VIAL_UNLOCK_COMBO_ROWS {0, 3}
+#define VIAL_UNLOCK_COMBO_COLS {0, 12}
diff --git a/keyboards/keyten/lisa/keymaps/vial/keymap.c b/keyboards/keyten/lisa/keymaps/vial/keymap.c
new file mode 100644
index 00000000000..3239b4d8f8f
--- /dev/null
+++ b/keyboards/keyten/lisa/keymaps/vial/keymap.c
@@ -0,0 +1,27 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+
+#include QMK_KEYBOARD_H
+
+const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
+
+    [0] = LAYOUT(
+        KC_ESC,  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_TAB,  KC_A,    KC_S,    KC_D,    KC_F,    KC_G,    KC_H,    KC_J,    KC_K,    KC_L,     KC_SCLN,          KC_QUOT,
+        KC_LSFT, KC_Z,    KC_X,    KC_C,    KC_V,    KC_B,    KC_ENT,  KC_N,    KC_M,    KC_COMM,  KC_DOT,  KC_SLSH, KC_RSFT,
+        KC_LCTL, KC_LGUI,          KC_LALT,     LT(1,KC_SPC), LT(2,KC_BSPC),    KC_RALT,                    KC_RGUI, KC_RCTL
+    ),
+
+    [1] = LAYOUT(
+        KC_TRNS, 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_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
+	),
+
+    [2] = LAYOUT(
+        KC_TRNS, 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_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
+	)
+};
diff --git a/keyboards/keyten/lisa/keymaps/vial/rules.mk b/keyboards/keyten/lisa/keymaps/vial/rules.mk
new file mode 100644
index 00000000000..ad04b494978
--- /dev/null
+++ b/keyboards/keyten/lisa/keymaps/vial/rules.mk
@@ -0,0 +1,3 @@
+VIA_ENABLE = yes
+VIAL_ENABLE = yes
+LTO_ENABLE = yes
diff --git a/keyboards/keyten/lisa/keymaps/vial/vial.json b/keyboards/keyten/lisa/keymaps/vial/vial.json
new file mode 100644
index 00000000000..96a6d1111de
--- /dev/null
+++ b/keyboards/keyten/lisa/keymaps/vial/vial.json
@@ -0,0 +1,365 @@
+{
+  "name": "Lisa",
+  "vendorId": "0xEB69",
+  "productId": "0x4001",
+  "lighting": "none",
+  "matrix": {
+    "rows": 4,
+    "cols": 13
+  },
+  "layouts": {
+    "labels": [
+      [
+        "Left Spacebar",
+        "1.25U | 2U",
+        "2U | 1.25U"
+      ],
+      [
+        "Right Spacebar",
+        "2.25U | 1.25U",
+        "1.25U | 2.25U"
+      ],
+      [
+        "Upper LED",
+        "Off",
+        "On",
+        "Caps Lock",
+        "Num Lock",
+        "Scroll Lock",
+        "Layer 1",
+        "Layer 2",
+        "Layer 3"
+      ],
+      [
+        "Middle LED",
+        "Off",
+        "On",
+        "Caps Lock",
+        "Num Lock",
+        "Scroll Lock",
+        "Layer 1",
+        "Layer 2",
+        "Layer 3"
+      ],
+      [
+        "Lower LED",
+        "Off",
+        "On",
+        "Caps Lock",
+        "Num Lock",
+        "Scroll Lock",
+        "Layer 1",
+        "Layer 2",
+        "Layer 3"
+      ]
+    ],
+    "keymap": [
+      [
+        {
+          "y": 0.38,
+          "x": 0.36
+        },
+        "0,0",
+        "0,1"
+      ],
+      [
+        {
+          "y": -0.99,
+          "x": 11.4
+        },
+        "0,10",
+        "0,11",
+        "0,12"
+      ],
+      [
+        {
+          "y": -0.41,
+          "x": 5.25,
+          "d": true
+        },
+        "0,0\n\n\n2,2",
+        {
+          "d": true
+        },
+        "0,0\n\n\n2,3",
+        {
+          "d": true
+        },
+        "0,0\n\n\n2,4",
+        {
+          "d": true
+        },
+        "0,0\n\n\n2,5",
+        {
+          "d": true
+        },
+        "0,0\n\n\n2,6",
+        {
+          "d": true
+        },
+        "0,0\n\n\n2,7"
+      ],
+      [
+        {
+          "y": -0.98,
+          "x": 3.25,
+          "d": true
+        },
+        "0,0\n\n\n2,0",
+        {
+          "d": true
+        },
+        "0,0\n\n\n2,1"
+      ],
+      [
+        {
+          "y": -0.61,
+          "x": 0.22,
+          "w": 1.25
+        },
+        "1,0",
+        "1,1"
+      ],
+      [
+        {
+          "y": -0.99,
+          "x": 11.79
+        },
+        "1,10",
+        {
+          "w": 1.75
+        },
+        "1,12"
+      ],
+      [
+        {
+          "y": -0.42,
+          "x": 5.25,
+          "d": true
+        },
+        "0,1\n\n\n3,2",
+        {
+          "d": true
+        },
+        "0,1\n\n\n3,3",
+        {
+          "d": true
+        },
+        "0,1\n\n\n3,4",
+        {
+          "d": true
+        },
+        "0,1\n\n\n3,5",
+        {
+          "d": true
+        },
+        "0,1\n\n\n3,6",
+        {
+          "d": true
+        },
+        "0,1\n\n\n3,7"
+      ],
+      [
+        {
+          "y": -0.98,
+          "x": 3.25,
+          "d": true
+        },
+        "0,1\n\n\n3,0",
+        {
+          "d": true
+        },
+        "0,1\n\n\n3,1"
+      ],
+      [
+        {
+          "y": -0.6,
+          "x": 0.07,
+          "w": 1.75
+        },
+        "2,0",
+        "2,1"
+      ],
+      [
+        {
+          "y": -0.99,
+          "x": 11.43
+        },
+        "2,10",
+        "2,11",
+        {
+          "w": 1.25
+        },
+        "2,12"
+      ],
+      [
+        {
+          "y": -0.43,
+          "x": 5.25,
+          "d": true
+        },
+        "0,2\n\n\n4,2",
+        {
+          "d": true
+        },
+        "0,2\n\n\n4,3",
+        {
+          "d": true
+        },
+        "0,2\n\n\n4,4",
+        {
+          "d": true
+        },
+        "0,2\n\n\n4,5",
+        {
+          "d": true
+        },
+        "0,2\n\n\n4,6",
+        {
+          "d": true
+        },
+        "0,2\n\n\n4,7"
+      ],
+      [
+        {
+          "y": -0.98,
+          "x": 3.25,
+          "d": true
+        },
+        "0,2\n\n\n4,0",
+        {
+          "d": true
+        },
+        "0,2\n\n\n4,1"
+      ],
+      [
+        {
+          "y": -0.59,
+          "x": 0.17,
+          "w": 1.25
+        },
+        "3,0",
+        {
+          "w": 1.25
+        },
+        "3,1"
+      ],
+      [
+        {
+          "y": -0.99,
+          "x": 12.069,
+          "w": 1.25
+        },
+        "3,11",
+        {
+          "w": 1.25
+        },
+        "3,12"
+      ],
+      [
+        {
+          "r": 8,
+          "y": -4.42,
+          "x": 2.5
+        },
+        "0,2",
+        "0,3",
+        "0,4",
+        "0,5"
+      ],
+      [
+        {
+          "x": 2.75
+        },
+        "1,2",
+        "1,3",
+        "1,4",
+        "1,5"
+      ],
+      [
+        {
+          "x": 3.25
+        },
+        "2,2",
+        "2,3",
+        "2,4",
+        "2,5"
+      ],
+      [
+        {
+          "x": 4,
+          "w": 1.25
+        },
+        "3,3\n\n\n0,0",
+        {
+          "w": 2
+        },
+        "3,5\n\n\n0,0"
+      ],
+      [
+        {
+          "y": 0.25,
+          "x": 4,
+          "w": 2
+        },
+        "3,3\n\n\n0,1",
+        {
+          "w": 1.25
+        },
+        "3,5\n\n\n0,1"
+      ],
+      [
+        {
+          "r": -8,
+          "y": -3.27,
+          "x": 7.1
+        },
+        "0,6",
+        "0,7",
+        "0,8",
+        "0,9"
+      ],
+      [
+        {
+          "x": 7.35
+        },
+        "1,6",
+        "1,7",
+        "1,8",
+        "1,9"
+      ],
+      [
+        {
+          "x": 6.85
+        },
+        "2,6",
+        "2,7",
+        "2,8",
+        "2,9"
+      ],
+      [
+        {
+          "x": 6.85,
+          "w": 2.25
+        },
+        "3,6\n\n\n1,0",
+        {
+          "w": 1.25
+        },
+        "3,8\n\n\n1,0"
+      ],
+      [
+        {
+          "y": 0.25,
+          "x": 6.85,
+          "w": 1.25
+        },
+        "3,6\n\n\n1,1",
+        {
+          "w": 2.25
+        },
+        "3,8\n\n\n1,1"
+      ]
+    ]
+  }
+}
diff --git a/keyboards/keyten/lisa/lisa.c b/keyboards/keyten/lisa/lisa.c
index 2ecff3a642c..149d5d3c8cc 100644
--- a/keyboards/keyten/lisa/lisa.c
+++ b/keyboards/keyten/lisa/lisa.c
@@ -3,10 +3,82 @@
 
 #include "quantum.h"
 
-#define LED_INDICATOR_PIN B2
+#define LOWER_LED_PIN B11
+#define MIDDLE_LED_PIN B10
+#define UPPER_LED_PIN B2
 
-void matrix_init_kb(void) {
-    gpio_set_pin_output(LED_INDICATOR_PIN);
-    gpio_write_pin_high(LED_INDICATOR_PIN);
-    matrix_init_user();
+typedef union {
+    uint16_t raw;
+    struct {
+        uint16_t lower_led : 3;
+        uint16_t middle_led: 3;
+        uint16_t upper_led : 3;
+    };
+} layout_options_t;
+
+static layout_options_t layout_options;
+
+static layer_state_t current_layer_state = 0;
+
+void via_set_layout_options_kb(uint32_t value) {
+    layout_options.raw = value;
+    led_update_kb(host_keyboard_led_state());
+}
+
+enum led_mode_t {
+    LED_MODE_OFF,
+    LED_MODE_ON,
+    LED_MODE_CAPS_LOCK,
+    LED_MODE_NUM_LOCK,
+    LED_MODE_SCROLL_LOCK,
+    LED_MODE_LAYER_1,
+    LED_MODE_LAYER_2,
+    LED_MODE_LAYER_3,
+};
+
+void set_led_state(uint32_t pin, uint8_t mode, led_t led_state) {
+    switch (mode) {
+        case LED_MODE_OFF:
+            gpio_write_pin_low(pin);
+            break;
+        case LED_MODE_ON:
+            gpio_write_pin_high(pin);
+            break;
+        case LED_MODE_CAPS_LOCK:
+            gpio_write_pin(pin, led_state.caps_lock);
+            break;
+        case LED_MODE_NUM_LOCK:
+            gpio_write_pin(pin, led_state.num_lock);
+            break;
+        case LED_MODE_SCROLL_LOCK:
+            gpio_write_pin(pin, led_state.scroll_lock);
+            break;
+        case LED_MODE_LAYER_1:
+            gpio_write_pin(pin, (current_layer_state & (1 << 1)) != 0);
+            break;
+        case LED_MODE_LAYER_2:
+            gpio_write_pin(pin, (current_layer_state & (1 << 2)) != 0);
+            break;
+        case LED_MODE_LAYER_3:
+            gpio_write_pin(pin, (current_layer_state & (1 << 3)) != 0);
+            break;
+        default:
+            gpio_write_pin_low(pin);
+            break;
+    }
+}
+
+void led_update_ports(led_t led_state) {
+    gpio_set_pin_output(LOWER_LED_PIN);
+    gpio_set_pin_output(MIDDLE_LED_PIN);
+    gpio_set_pin_output(UPPER_LED_PIN);	
+	
+    set_led_state(LOWER_LED_PIN, layout_options.lower_led, led_state);
+    set_led_state(MIDDLE_LED_PIN, layout_options.middle_led, led_state);
+    set_led_state(UPPER_LED_PIN, layout_options.upper_led, led_state);
+}
+
+layer_state_t layer_state_set_user(layer_state_t state) {
+    current_layer_state = state;
+    return current_layer_state;
 }