From f7324ec68494026c27b9c4af4b34bf4a98d2c75b Mon Sep 17 00:00:00 2001
From: Joel Challis <git@zvecr.com>
Date: Fri, 1 May 2020 17:59:22 +0100
Subject: [PATCH] Add xd002 support (#8598)

* Add xd002 support

* tidy some whitespace

* align filename

* Update keyboards/xd002/info.json

Co-authored-by: Erovia <Erovia@users.noreply.github.com>

Co-authored-by: Erovia <Erovia@users.noreply.github.com>
---
 keyboards/xd002/config.h                   | 60 ++++++++++++++++++++++
 keyboards/xd002/info.json                  | 16 ++++++
 keyboards/xd002/keymaps/default/keymap.c   |  7 +++
 keyboards/xd002/keymaps/rgb/keymap.c       | 46 +++++++++++++++++
 keyboards/xd002/keymaps/rgb/rules.mk       |  1 +
 keyboards/xd002/keymaps/rgb_lite/keymap.c  | 31 +++++++++++
 keyboards/xd002/keymaps/rgb_lite/rgblite.h | 26 ++++++++++
 keyboards/xd002/keymaps/rgb_lite/rules.mk  |  1 +
 keyboards/xd002/keymaps/tap_dance/config.h |  3 ++
 keyboards/xd002/keymaps/tap_dance/keymap.c | 19 +++++++
 keyboards/xd002/keymaps/tap_dance/rules.mk |  1 +
 keyboards/xd002/keymaps/volume/keymap.c    |  7 +++
 keyboards/xd002/keymaps/volume/rules.mk    |  1 +
 keyboards/xd002/readme.md                  | 49 ++++++++++++++++++
 keyboards/xd002/rules.mk                   | 34 ++++++++++++
 keyboards/xd002/xd002.c                    | 16 ++++++
 keyboards/xd002/xd002.h                    | 33 ++++++++++++
 17 files changed, 351 insertions(+)
 create mode 100644 keyboards/xd002/config.h
 create mode 100644 keyboards/xd002/info.json
 create mode 100644 keyboards/xd002/keymaps/default/keymap.c
 create mode 100644 keyboards/xd002/keymaps/rgb/keymap.c
 create mode 100644 keyboards/xd002/keymaps/rgb/rules.mk
 create mode 100644 keyboards/xd002/keymaps/rgb_lite/keymap.c
 create mode 100644 keyboards/xd002/keymaps/rgb_lite/rgblite.h
 create mode 100644 keyboards/xd002/keymaps/rgb_lite/rules.mk
 create mode 100644 keyboards/xd002/keymaps/tap_dance/config.h
 create mode 100644 keyboards/xd002/keymaps/tap_dance/keymap.c
 create mode 100644 keyboards/xd002/keymaps/tap_dance/rules.mk
 create mode 100644 keyboards/xd002/keymaps/volume/keymap.c
 create mode 100644 keyboards/xd002/keymaps/volume/rules.mk
 create mode 100644 keyboards/xd002/readme.md
 create mode 100644 keyboards/xd002/rules.mk
 create mode 100644 keyboards/xd002/xd002.c
 create mode 100644 keyboards/xd002/xd002.h

diff --git a/keyboards/xd002/config.h b/keyboards/xd002/config.h
new file mode 100644
index 00000000000..c17d816cfce
--- /dev/null
+++ b/keyboards/xd002/config.h
@@ -0,0 +1,60 @@
+/* Copyright 2020 zvecr<git@zvecr.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/>.
+ */
+#pragma once
+
+#include "config_common.h"
+
+/* USB Device descriptor parameter */
+#define VENDOR_ID  0x7844 // "XD"
+#define PRODUCT_ID 0x0202
+#define DEVICE_VER 0x0001
+#define MANUFACTURER XIUDI
+#define PRODUCT XD002
+
+/* matrix size */
+#define MATRIX_ROWS 1
+#define MATRIX_COLS 2
+
+/*
+ * Keyboard Matrix Assignments
+ *
+ * On this board we have direct connection: no diodes.
+ */
+#define DIRECT_PINS {{ B0, B1 }}
+
+/* Debounce reduces chatter (unintended double-presses) - set 0 if debouncing is not needed */
+#define DEBOUNCE 5
+
+#define RGBLED_NUM 2
+#define RGB_DI_PIN B2
+
+// Save as much space as we can...
+#define LAYER_STATE_8BIT
+#define NO_ACTION_LAYER
+#define NO_ACTION_TAPPING
+#define NO_ACTION_ONESHOT
+#define NO_RESET
+
+// usbconfig.h overrides
+#define USB_CFG_IOPORTNAME B
+#define USB_CFG_DMINUS_BIT 3
+#define USB_CFG_DPLUS_BIT 4
+#define USB_COUNT_SOF 0
+#define USB_INTR_CFG PCMSK
+#define USB_INTR_CFG_SET (1<<USB_CFG_DPLUS_BIT)
+#define USB_INTR_ENABLE_BIT PCIE
+#define USB_INTR_PENDING_BIT PCIF
+#define USB_INTR_VECTOR SIG_PIN_CHANGE
diff --git a/keyboards/xd002/info.json b/keyboards/xd002/info.json
new file mode 100644
index 00000000000..101952ec35c
--- /dev/null
+++ b/keyboards/xd002/info.json
@@ -0,0 +1,16 @@
+{
+  "keyboard_name": "xd002",
+  "url": "https://kprepublic.com/products/xd002-xiudi-2-custom-mechanical-keyboard-2-keys-underglow-and-switch-rgb-pcb-programmed-hot-swappable-macro-key-aluminum-case",
+  "maintainer": "zvecr",
+  "width": 2,
+  "height": 1,
+  "layouts": {
+    "LAYOUT": {
+      "key_count": 2,
+      "layout": [
+        {"x":0, "y":0},
+        {"x":1, "y":0}
+      ]
+    }
+  }
+}
diff --git a/keyboards/xd002/keymaps/default/keymap.c b/keyboards/xd002/keymaps/default/keymap.c
new file mode 100644
index 00000000000..ae53d396d1a
--- /dev/null
+++ b/keyboards/xd002/keymaps/default/keymap.c
@@ -0,0 +1,7 @@
+#include QMK_KEYBOARD_H
+
+const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
+  [0] = LAYOUT(
+    KC_A,  KC_B
+  )
+};
diff --git a/keyboards/xd002/keymaps/rgb/keymap.c b/keyboards/xd002/keymaps/rgb/keymap.c
new file mode 100644
index 00000000000..af047341381
--- /dev/null
+++ b/keyboards/xd002/keymaps/rgb/keymap.c
@@ -0,0 +1,46 @@
+#include QMK_KEYBOARD_H
+
+// Defines the keycodes used by our macros in process_record_user
+enum custom_keycodes {
+  QMKURL = SAFE_RANGE,
+};
+
+const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
+  [0] = LAYOUT(
+    RGB_HUI,  QMKURL
+  )
+};
+
+bool process_record_user(uint16_t keycode, keyrecord_t *record) {
+  if (record->event.pressed) {
+    switch (keycode) {
+      case QMKURL:
+        // Using SEND_STRING here adds 400bytes ...
+        // SEND_STRING("https://qmk.fm/" SS_TAP(X_ENTER));
+        tap_code(KC_H);
+        tap_code(KC_T);
+        tap_code(KC_T);
+        tap_code(KC_P);
+        tap_code(KC_S);
+        tap_code16(KC_COLON);
+        tap_code(KC_SLASH);
+        tap_code(KC_SLASH);
+        tap_code(KC_Q);
+        tap_code(KC_M);
+        tap_code(KC_K);
+        tap_code(KC_DOT);
+        tap_code(KC_F);
+        tap_code(KC_M);
+        tap_code(KC_SLASH);
+        tap_code(KC_ENTER);
+        break;
+    }
+  }
+  return true;
+}
+
+void keyboard_post_init_user(void) {
+  rgblight_enable_noeeprom(); // enables Rgb, without saving settings
+  rgblight_sethsv_noeeprom(180, 255, 255); // sets the color to teal/cyan without saving
+  rgblight_mode_noeeprom(RGBLIGHT_MODE_STATIC_LIGHT); // sets mode to Fast breathing without saving
+}
diff --git a/keyboards/xd002/keymaps/rgb/rules.mk b/keyboards/xd002/keymaps/rgb/rules.mk
new file mode 100644
index 00000000000..1e3cebb1451
--- /dev/null
+++ b/keyboards/xd002/keymaps/rgb/rules.mk
@@ -0,0 +1 @@
+RGBLIGHT_ENABLE = yes
diff --git a/keyboards/xd002/keymaps/rgb_lite/keymap.c b/keyboards/xd002/keymaps/rgb_lite/keymap.c
new file mode 100644
index 00000000000..ed9185e3b52
--- /dev/null
+++ b/keyboards/xd002/keymaps/rgb_lite/keymap.c
@@ -0,0 +1,31 @@
+#include QMK_KEYBOARD_H
+#include "rgblite.h"
+
+// Defines the keycodes used by our macros in process_record_user
+enum custom_keycodes {
+  QMKURL = SAFE_RANGE,
+};
+
+const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
+  [0] = LAYOUT(
+    RGB_HUI,  QMKURL
+  )
+};
+
+bool process_record_user(uint16_t keycode, keyrecord_t *record) {
+  if (record->event.pressed) {
+    switch (keycode) {
+      case RGB_HUI:
+        rgblight_increase_hue();
+        break;
+      case QMKURL:
+        SEND_STRING("https://qmk.fm/" SS_TAP(X_ENTER));
+        break;
+    }
+  }
+  return true;
+}
+
+void keyboard_post_init_user(void) {
+  rgblight_increase_hue();
+}
diff --git a/keyboards/xd002/keymaps/rgb_lite/rgblite.h b/keyboards/xd002/keymaps/rgb_lite/rgblite.h
new file mode 100644
index 00000000000..ca98484b818
--- /dev/null
+++ b/keyboards/xd002/keymaps/rgb_lite/rgblite.h
@@ -0,0 +1,26 @@
+#pragma once
+
+#include "ws2812.h"
+#include "rgblight_list.h"
+
+static inline void rgblight_setrgb(uint8_t _r, uint8_t _g, uint8_t _b) {
+    LED_TYPE leds[RGBLED_NUM] = {{.r = _r, .g = _g, .b = _b}, {.r = _r, .g = _g, .b = _b}};
+    ws2812_setleds(leds, RGBLED_NUM);
+}
+
+static void rgblight_increase_hue(void) {
+    static uint8_t state = 0;
+
+    state = (state + 1) % 3;
+    switch (state) {
+        case 1:
+            rgblight_setrgb_red();
+            break;
+        case 2:
+            rgblight_setrgb_blue();
+            break;
+        default:
+            rgblight_setrgb_green();
+            break;
+    }
+}
diff --git a/keyboards/xd002/keymaps/rgb_lite/rules.mk b/keyboards/xd002/keymaps/rgb_lite/rules.mk
new file mode 100644
index 00000000000..227bbcae32d
--- /dev/null
+++ b/keyboards/xd002/keymaps/rgb_lite/rules.mk
@@ -0,0 +1 @@
+SRC += ws2812.c
diff --git a/keyboards/xd002/keymaps/tap_dance/config.h b/keyboards/xd002/keymaps/tap_dance/config.h
new file mode 100644
index 00000000000..b86e862d30d
--- /dev/null
+++ b/keyboards/xd002/keymaps/tap_dance/config.h
@@ -0,0 +1,3 @@
+#pragma once
+
+#define TAPPING_TERM 500
diff --git a/keyboards/xd002/keymaps/tap_dance/keymap.c b/keyboards/xd002/keymaps/tap_dance/keymap.c
new file mode 100644
index 00000000000..0f384ce99fe
--- /dev/null
+++ b/keyboards/xd002/keymaps/tap_dance/keymap.c
@@ -0,0 +1,19 @@
+#include QMK_KEYBOARD_H
+
+enum layers {
+  _BASE = 0,
+};
+
+enum {
+  TD_BC = 0
+};
+
+qk_tap_dance_action_t tap_dance_actions[] = {
+  [TD_BC]  = ACTION_TAP_DANCE_DOUBLE(KC_B, KC_C)
+};
+
+const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
+  [_BASE] = LAYOUT(
+    KC_A,  TD(TD_BC)
+  )
+};
diff --git a/keyboards/xd002/keymaps/tap_dance/rules.mk b/keyboards/xd002/keymaps/tap_dance/rules.mk
new file mode 100644
index 00000000000..e5ddcae8d92
--- /dev/null
+++ b/keyboards/xd002/keymaps/tap_dance/rules.mk
@@ -0,0 +1 @@
+TAP_DANCE_ENABLE = yes
diff --git a/keyboards/xd002/keymaps/volume/keymap.c b/keyboards/xd002/keymaps/volume/keymap.c
new file mode 100644
index 00000000000..2365d7b245a
--- /dev/null
+++ b/keyboards/xd002/keymaps/volume/keymap.c
@@ -0,0 +1,7 @@
+#include QMK_KEYBOARD_H
+
+const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
+  [0] = LAYOUT(
+    KC_VOLU, KC_VOLD
+  )
+};
diff --git a/keyboards/xd002/keymaps/volume/rules.mk b/keyboards/xd002/keymaps/volume/rules.mk
new file mode 100644
index 00000000000..fcfd2225bcf
--- /dev/null
+++ b/keyboards/xd002/keymaps/volume/rules.mk
@@ -0,0 +1 @@
+EXTRAKEY_ENABLE = yes
diff --git a/keyboards/xd002/readme.md b/keyboards/xd002/readme.md
new file mode 100644
index 00000000000..6358bc754e4
--- /dev/null
+++ b/keyboards/xd002/readme.md
@@ -0,0 +1,49 @@
+# xd002
+
+![xd002](https://cdn.shopify.com/s/files/1/2711/4238/products/xd002_1024x1024.jpg?v=1522134411)
+
+2% Custom mechanical keyboard. ATtiny85 powered, with 2*WS2812 LEDs, and the micronucleus bootloader.
+
+**Note**: Due to limited firmware space, a _**lot**_ of features have to be disabled to get a functioning QMK based keyboard.
+
+* Keyboard Maintainer: [zvecr](https://github.com/zvecr)
+* Hardware Supported: xd002
+* Hardware Availability: [kprepublic](https://kprepublic.com/products/xd002-xiudi-2-custom-mechanical-keyboard-2-keys-underglow-and-switch-rgb-pcb-programmed-hot-swappable-macro-key-aluminum-case)
+
+Make example for this keyboard (after setting up your build environment):
+
+    make xd002: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).
+
+## Flashing
+### Prerequisites
+
+```bash
+git clone https://github.com/micronucleus/micronucleus.git
+cd micronucleus/commandline/
+sudo make install
+```
+
+On Linux, you’ll need proper privileges to access the MCU. You can either use sudo when flashing firmware, or place [these files](https://github.com/micronucleus/micronucleus/blob/master/commandline/49-micronucleus.rules) in /etc/udev/rules.d/. Once added run the following:
+
+```bash
+sudo udevadm control --reload-rules
+sudo udevadm trigger
+```
+
+### Instructions
+
+**Reset Key**: Hold down key nearest to the USB socket while plugging in the keyboard.
+
+```bash
+make xd002:default:flash
+
+# or directly with...
+micronucleus --run <firmware.hex>
+```
+
+### Recovery
+
+* [Original Firmware](https://github.com/xiudi/Attiny85_vusb_pad_test)
+* [Bootloader Repair](https://digistump.com/wiki/digispark/tutorials/proisp)
diff --git a/keyboards/xd002/rules.mk b/keyboards/xd002/rules.mk
new file mode 100644
index 00000000000..1142142bfcb
--- /dev/null
+++ b/keyboards/xd002/rules.mk
@@ -0,0 +1,34 @@
+# MCU name
+MCU = attiny85
+
+# Bootloader selection
+BOOTLOADER = micronucleus
+OPT_DEFS += -DBOOTLOADER_SIZE=1862
+PROGRAM_CMD = micronucleus --run $(BUILD_DIR)/$(TARGET).hex
+
+# Build Options
+#   change yes to no to disable
+#
+BOOTMAGIC_ENABLE = no       # Virtual DIP switch configuration
+MOUSEKEY_ENABLE = no        # Mouse keys
+EXTRAKEY_ENABLE = no        # Audio control and System control
+CONSOLE_ENABLE = no         # Console for debug
+COMMAND_ENABLE = no         # Commands for debug and configuration
+# Do not enable SLEEP_LED_ENABLE. it uses the same timer as BACKLIGHT_ENABLE
+SLEEP_LED_ENABLE = no       # Breathing sleep LED during USB suspend
+# if this doesn't work, see here: https://github.com/tmk/tmk_keyboard/wiki/FAQ#nkro-doesnt-work
+NKRO_ENABLE = no            # USB Nkey Rollover
+BACKLIGHT_ENABLE = no       # Enable keyboard backlight functionality on B7 by default
+RGBLIGHT_ENABLE = no        # Enable keyboard RGB underglow
+MIDI_ENABLE = no            # MIDI support
+UNICODE_ENABLE = no         # Unicode
+BLUETOOTH_ENABLE = no       # Enable Bluetooth with the Adafruit EZ-Key HID
+AUDIO_ENABLE = no           # Audio output on port C6
+FAUXCLICKY_ENABLE = no      # Use buzzer to emulate clicky switches
+HD44780_ENABLE = no         # Enable support for HD44780 based LCDs
+
+# Save as much space as we can...
+LTO_ENABLE = yes
+GRAVE_ESC_ENABLE = no
+MAGIC_ENABLE = no
+SPACE_CADET_ENABLE = no
diff --git a/keyboards/xd002/xd002.c b/keyboards/xd002/xd002.c
new file mode 100644
index 00000000000..e679b97243a
--- /dev/null
+++ b/keyboards/xd002/xd002.c
@@ -0,0 +1,16 @@
+/* Copyright 2020 zvecr<git@zvecr.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/>.
+ */
+#include "xd002.h"
diff --git a/keyboards/xd002/xd002.h b/keyboards/xd002/xd002.h
new file mode 100644
index 00000000000..3d780191bb3
--- /dev/null
+++ b/keyboards/xd002/xd002.h
@@ -0,0 +1,33 @@
+/* Copyright 2020 zvecr<git@zvecr.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/>.
+ */
+#pragma once
+
+#include "quantum.h"
+
+/* This a shortcut to help you visually see your layout.
+ *
+ * The first section contains all of the arguments representing the physical
+ * layout of the board and position of the keys.
+ *
+ * The second converts the arguments into a two-dimensional array which
+ * represents the switch matrix.
+ */
+#define LAYOUT( \
+	K01, K02 \
+) \
+{ \
+  { K01, K02 }, \
+}