From 26dacd51fce49d83b2dff0cda45126c327e743fa Mon Sep 17 00:00:00 2001
From: Njiall <mbeilles@student.42.fr>
Date: Sat, 25 Aug 2018 19:35:24 +0200
Subject: [PATCH] Keyboard: Added gamepad layout to cospad (#3714)

---
 keyboards/cospad/cospad.h                |  36 ++-
 keyboards/cospad/keymaps/detrus/keymap.c | 369 +++++++++++++++++++++++
 2 files changed, 403 insertions(+), 2 deletions(-)
 create mode 100644 keyboards/cospad/keymaps/detrus/keymap.c

diff --git a/keyboards/cospad/cospad.h b/keyboards/cospad/cospad.h
index 36c7f3367a..df42b0df1e 100644
--- a/keyboards/cospad/cospad.h
+++ b/keyboards/cospad/cospad.h
@@ -22,6 +22,21 @@
    * `-------------------'
  */
 
+/* COSPAD gamepad matrix layout
+   * ,-------------------.
+   * | 00 | 01 | 02 | 03 |
+   * |----|----|----|----|
+   * | 10 | 11 | 12 | 13 |
+   * |----|----|----|----|
+   * | 20 | 21 | 22 |    |
+   * |----|----|----| 23 |
+   * | 30 | 31 | 32 |    |
+   * |----|----|----|----|
+   * | 40 | 41 | 42 | 43 |
+   * |----|----|----|----|
+   * | 50 | 51 | 52 | 53 |
+   * `-------------------'
+ */
 
 /* COSPAD numpad matrix layout
    * ,-------------------.
@@ -41,7 +56,7 @@
 // The first section contains all of the arguments
 // The second converts the arguments into a two-dimensional array
 #define LAYOUT_ortho_6x4( \
-  k00, k01, k02, k03, \
+	k00, k01, k02, k03, \
 	k10, k11, k12, k13, \
 	k20, k21, k22, k23, \
 	k30, k31, k32, k33, \
@@ -57,8 +72,25 @@
 	{k50, k51, k52, k53}  \
 }
 
+#define LAYOUT_gamepad_6x4( \
+	k00, k01, k02, k03, \
+	k10, k11, k12, k13, \
+	k20, k21, k22,      \
+	k30, k31, k32, k23, \
+	k40, k41, k42, k43, \
+	k50, k51, k52, k53  \
+) \
+{ \
+	{k00, k01, k02, k03},   \
+	{k10, k11, k12, k13},   \
+	{k20, k21, k22, k23},   \
+	{k30, k31, k32, KC_NO}, \
+	{k40, k41, k42, k43},   \
+	{k50, k51, k52, k53}    \
+}
+
 #define LAYOUT_numpad_6x4( \
-  k00, k01, k02, k03,  \
+	k00, k01, k02, k03,  \
 	k10, k11, k12, k13,  \
 	k20, k21, k22,       \
 	k30, k31, k32, k23,  \
diff --git a/keyboards/cospad/keymaps/detrus/keymap.c b/keyboards/cospad/keymaps/detrus/keymap.c
new file mode 100644
index 0000000000..f9eddc2a87
--- /dev/null
+++ b/keyboards/cospad/keymaps/detrus/keymap.c
@@ -0,0 +1,369 @@
+#include QMK_KEYBOARD_H
+#include "led.h"
+#include <print.h>
+
+#ifdef RGBLIGHT_ENABLE
+#include "rgblight.h"
+#endif
+
+#define _______ KC_TRNS
+
+// Each layer gets a name for readability, which is then used in the keymap matrix below.
+enum cospad_layers {
+	_QWERTY_LAYER,
+	_QWERTZ_LAYER,
+	_COLEMA_LAYER,
+	_DVORAK_LAYER,
+	_QWERTY_LOWER_LAYER,
+	_QWERTZ_LOWER_LAYER,
+	_COLEMA_LOWER_LAYER,
+	_DVORAK_LOWER_LAYER,
+	_RAISE_LAYER,
+	_ALTER_LAYER,
+};
+
+// To switch the default layer used for the layout, there are special keycodes.
+// Which onces detected below serve to switch it.
+enum cospad_keycodes {
+	QWERTY = SAFE_RANGE,
+	QWERTZ,
+	COLEMAK,
+	DVORAK
+};
+
+const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
+
+	/* Keymap _QWERTY_LAYER: Default layer
+	 * ,-----------------------.
+	 * |  T  |  G  |  B  | Alt |
+	 * |-----|-----|-----|-----|
+	 * |  R  |  F  |  V  | LOW |
+	 * |-----|-----|-----|-----|
+	 * |  E  |  D  |  C  |     |
+	 * |-----|-----|-----| Spc |
+	 * |  W  |  S  |  X  |     |
+	 * |-----|-----|-----|-----|
+	 * |  Q  |  A  |  Z  | RAI |
+	 * |-----|-----|-----|-----|
+	 * | Esc | Tab | Sft | Ctl |
+	 * `-----------------------'
+	 *
+	 *   And it's LOWER layer
+	 * ,-----------------------.
+	 * |  P  |  ;  |  /  | Alt |
+	 * |-----|-----|-----|-----|
+	 * |  O  |  L  |  .  | LOW |
+	 * |-----|-----|-----|-----|
+	 * |  I  |  K  |  ,  |     |
+	 * |-----|-----|-----| Ent |
+	 * |  U  |  J  |  M  |     |
+	 * |-----|-----|-----|-----|
+	 * |  Y  |  H  |  N  | RAI |
+	 * |-----|-----|-----|-----|
+	 * | Esc | Tab | Sft | Ctl |
+	 * `-----------------------'
+	 */
+	[_QWERTY_LAYER] = LAYOUT_gamepad_6x4(
+			KC_T,    KC_G,    KC_B,    KC_LALT,  \
+			KC_R,    KC_F,    KC_V,    MO(_QWERTY_LOWER_LAYER),\
+			KC_E,    KC_D,    KC_C,              \
+			KC_W,    KC_S,    KC_X,    KC_SPACE, \
+			KC_Q,    KC_A,    KC_Z,    MO(_RAISE_LAYER),\
+			KC_GESC, KC_TAB,  KC_LSFT, KC_LCTRL),
+	[_QWERTY_LOWER_LAYER] = LAYOUT_gamepad_6x4(
+			KC_P,    KC_SCLN, KC_SLSH, KC_LALT,  \
+			KC_O,    KC_L,    KC_DOT,  _______,  \
+			KC_I,    KC_K,    KC_COMM,           \
+			KC_U,    KC_J,    KC_M,    KC_ENTER, \
+			KC_Y,    KC_H,    KC_N,    _______,  \
+			_______, _______,  _______, _______),
+
+
+
+	/* Keymap _QWERTZ_LAYER: Alternate default layer
+	 * ,-----------------------.
+	 * |  T  |  G  |  B  | Alt |
+	 * |-----|-----|-----|-----|
+	 * |  R  |  F  |  V  | LOW |
+	 * |-----|-----|-----|-----|
+	 * |  E  |  D  |  C  |     |
+	 * |-----|-----|-----| Spc |
+	 * |  W  |  S  |  X  |     |
+	 * |-----|-----|-----|-----|
+	 * |  Q  |  A  |  Y  | RAI |
+	 * |-----|-----|-----|-----|
+	 * | Esc | Tab | Sft | Ctl |
+	 * `-----------------------'
+	 *
+	 *   And it's LOWER layer
+	 * ,-----------------------.
+	 * |  P  |  ;  |  /  | Alt |
+	 * |-----|-----|-----|-----|
+	 * |  O  |  L  |  >  | LOW |
+	 * |-----|-----|-----|-----|
+	 * |  I  |  K  |  <  |     |
+	 * |-----|-----|-----| Ent |
+	 * |  U  |  J  |  M  |     |
+	 * |-----|-----|-----|-----|
+	 * |  Z  |  H  |  N  | RAI |
+	 * |-----|-----|-----|-----|
+	 * | Esc | Tab | Sft | Ctl |
+	 * `-----------------------'
+	 */
+	[_QWERTZ_LAYER] = LAYOUT_gamepad_6x4(
+			KC_T,    KC_G,    KC_B,    KC_LALT,  \
+			KC_R,    KC_F,    KC_V,    MO(_QWERTZ_LOWER_LAYER),\
+			KC_E,    KC_D,    KC_C,              \
+			KC_W,    KC_S,    KC_X,    KC_SPACE, \
+			KC_Q,    KC_A,    KC_Y,    MO(_RAISE_LAYER),\
+			KC_GESC, KC_TAB,  KC_LSFT, KC_LCTRL),
+	[_QWERTZ_LOWER_LAYER] = LAYOUT_gamepad_6x4(
+			KC_P,    KC_SCLN, KC_SLSH, KC_LALT,  \
+			KC_O,    KC_L,    KC_DOT,  _______,  \
+			KC_I,    KC_K,    KC_COMM,           \
+			KC_U,    KC_J,    KC_M,    KC_ENTER, \
+			KC_Z,    KC_H,    KC_N,    _______,  \
+			_______, _______, _______, _______),
+
+
+
+	/* Keymap _COLEMA_LAYER: Alternate default layer
+	 * ,-----------------------.
+	 * |  G  |  D  |  B  | Alt |
+	 * |-----|-----|-----|-----|
+	 * |  P  |  T  |  V  | LOW |
+	 * |-----|-----|-----|-----|
+	 * |  F  |  S  |  C  |     |
+	 * |-----|-----|-----| Spc |
+	 * |  W  |  R  |  X  |     |
+	 * |-----|-----|-----|-----|
+	 * |  Q  |  A  |  Z  | RAI |
+	 * |-----|-----|-----|-----|
+	 * | Esc | Tab | Sft | Ctl |
+	 * `-----------------------'
+	 *
+	 *   And it's LOWER layer
+	 * ,-----------------------.
+	 * |  ;  |  O  |  /  | Alt |
+	 * |-----|-----|-----|-----|
+	 * |  Y  |  I  |  .  | LOW |
+	 * |-----|-----|-----|-----|
+	 * |  U  |  E  |  ,  |     |
+	 * |-----|-----|-----| Ent |
+	 * |  L  |  N  |  M  |     |
+	 * |-----|-----|-----|-----|
+	 * |  J  |  H  |  K  | RAI |
+	 * |-----|-----|-----|-----|
+	 * | Esc | Tab | Sft | Ctl |
+	 * `-----------------------'
+	 */
+	[_COLEMA_LAYER] = LAYOUT_gamepad_6x4(
+			KC_T,    KC_D,     KC_B,    KC_LALT,  \
+			KC_R,    KC_T,     KC_V,    MO(_COLEMA_LOWER_LAYER),\
+			KC_E,    KC_S,     KC_C,              \
+			KC_W,    KC_R,     KC_X,    KC_SPACE, \
+			KC_Q,    KC_A,     KC_Z,    MO(_RAISE_LAYER),\
+			KC_GESC, KC_TAB,   KC_LSFT, KC_LCTRL),
+	[_COLEMA_LOWER_LAYER] = LAYOUT_gamepad_6x4(
+			KC_SCLN, KC_O,     KC_SLSH, _______,  \
+			KC_Y,    KC_I,     KC_DOT,  _______,  \
+			KC_U,    KC_E,     KC_COMM,           \
+			KC_L,    KC_N,     KC_M,    KC_ENTER, \
+			KC_J,    KC_H,     KC_K,    _______,  \
+			KC_F,    KC_G,     _______, _______),
+
+
+
+	/* Keymap _DVORAK_LAYER: Alternate default layer
+	 * ,-----------------------.
+	 * |  Y  |  I  |  X  | Alt |
+	 * |-----|-----|-----|-----|
+	 * |  P  |  U  |  K  | LOW |
+	 * |-----|-----|-----|-----|
+	 * |  .  |  E  |  J  |     |
+	 * |-----|-----|-----| Spc |
+	 * |  ,  |  O  |  Q  |     |
+	 * |-----|-----|-----|-----|
+	 * |  '  |  A  |  ;  | RAI |
+	 * |-----|-----|-----|-----|
+	 * | Esc | Tab | Sft | Ctl |
+	 * `-----------------------'
+	 *
+	 *   And it's LOWER layer
+	 * ,-----------------------.
+	 * |  L  |  S  |  Z  | Alt |
+	 * |-----|-----|-----|-----|
+	 * |  R  |  N  |  V  | LOW |
+	 * |-----|-----|-----|-----|
+	 * |  C  |  T  |  W  |     |
+	 * |-----|-----|-----| Ent |
+	 * |  G  |  H  |  M  |     |
+	 * |-----|-----|-----|-----|
+	 * |  F  |  D  |  B  | RAI |
+	 * |-----|-----|-----|-----|
+	 * | Esc | Tab | Sft | Ctl |
+	 * `-----------------------'
+	 */
+	[_DVORAK_LAYER] = LAYOUT_gamepad_6x4(
+			KC_Y,    KC_I,    KC_X,    KC_LALT,   \
+			KC_P,    KC_U,    KC_K,    MO(_DVORAK_LOWER_LAYER),\
+			KC_DOT,  KC_E,    KC_J,               \
+			KC_COMM, KC_O,    KC_A,    KC_SPACE,  \
+			KC_QUOT, KC_A,    KC_SCLN, MO(_RAISE_LAYER),\
+			KC_GESC, KC_TAB,  KC_LSFT, KC_LCTRL),
+	[_DVORAK_LOWER_LAYER] = LAYOUT_gamepad_6x4(
+			KC_L,    KC_S,    KC_Z,    KC_LALT, \
+			KC_R,    KC_N,    KC_V,    _______, \
+			KC_C,    KC_T,    KC_W,             \
+			KC_G,    KC_H,    KC_M,    KC_ENTER,\
+			KC_F,    KC_D,    KC_B,    _______, \
+			_______, _______, _______, _______),
+
+
+
+	/* Keymap _RAISE_LAYER: Additional layer to access more
+	 * ,-----------------------.
+	 * |  5  |  0  | del | Alt |
+	 * |-----|-----|-----|-----|
+	 * |  4  |  9  | ->  | LOW |
+	 * |-----|-----|-----|-----|
+	 * |  3  |  8  |  <- |     |
+	 * |-----|-----|-----| Spc |
+	 * |  2  |  7  | ->  |     |
+	 * |-----|-----|-----|-----|
+	 * |  1  |  6  |  <- | RAI |
+	 * |-----|-----|-----|-----|
+	 * | Esc | Tab | Sft | Ctl |
+	 * `-----------------------'
+	 */
+	[_RAISE_LAYER] = LAYOUT_gamepad_6x4(
+			KC_5,    KC_0,   KC_BSPC,  _______, \
+			KC_4,    KC_9,   KC_RIGHT, _______, \
+			KC_3,    KC_8,   KC_UP,             \
+			KC_2,    KC_7,   KC_DOWN,  _______, \
+			KC_1,    KC_6,   KC_LEFT,  _______, \
+			_______, _______, _______, _______),
+
+
+
+	/* Keymap _ALTER_LAYER: Function layer used to control the Leds
+	 *                     and use media buttons
+	 * ,----------------------------------------.
+	 * | Val Dec | Bl Toggle  | Qwertz  | Super |
+	 * |---------|------------|---------|-------|
+	 * | Val Inc |   Bl Off   | Qwerty  |       |
+	 * |---------|------------|---------|-------|
+	 * | Sat Dec |   Bl On    | Colemak |       |
+	 * |---------|------------|---------|       |
+	 * | Sat Inc | RGB Toggle | Dvorak  |       |
+	 * |---------|------------|---------|-------|
+	 * | Hue Dec |  RGB Next  | Vol Dwn |       |
+	 * |---------|------------|---------|-------|
+	 * | Hue Inc |  RGB Prev  | Vol Up  | Reset |
+	 * `----------------------------------------'
+	 */
+	[_ALTER_LAYER] = LAYOUT_gamepad_6x4(
+			RGB_VAD, BL_TOGG,  QWERTZ,  KC_LGUI, \
+			RGB_VAI, BL_OFF,   QWERTY,  _______, \
+			RGB_SAD, BL_ON,    COLEMAK,          \
+			RGB_SAI, RGB_TOG,  DVORAK,  _______, \
+			RGB_HUD, RGB_MOD,  KC_VOLD, _______, \
+			RGB_HUI, RGB_RMOD, KC_VOLU, RESET),
+};
+
+// Makes sure to update the good tri-layer if a layer changes
+uint32_t layer_state_set_user(uint32_t state) {
+	switch (biton32(default_layer_state)) {
+		case _QWERTY_LAYER:
+			state = update_tri_layer_state(state, _RAISE_LAYER, _QWERTY_LOWER_LAYER, _ALTER_LAYER);
+			break;
+		case _QWERTZ_LAYER:
+			state = update_tri_layer_state(state, _RAISE_LAYER, _QWERTZ_LOWER_LAYER, _ALTER_LAYER);
+			break;
+		case _COLEMA_LAYER:
+			state = update_tri_layer_state(state, _RAISE_LAYER, _COLEMA_LOWER_LAYER, _ALTER_LAYER);
+			break;
+		case _DVORAK_LAYER:
+			state = update_tri_layer_state(state, _RAISE_LAYER, _DVORAK_LOWER_LAYER, _ALTER_LAYER);
+			break;
+	}
+	return state;
+}
+
+// Makes the tri-layer
+uint32_t default_layer_state_set_kb(uint32_t state) {
+	switch (biton32(state)) {
+		case _QWERTY_LAYER:
+			state = update_tri_layer_state(state, _RAISE_LAYER, _QWERTZ_LOWER_LAYER, _ALTER_LAYER);
+			state = update_tri_layer_state(state, _RAISE_LAYER, _COLEMA_LOWER_LAYER, _ALTER_LAYER);
+			state = update_tri_layer_state(state, _RAISE_LAYER, _DVORAK_LOWER_LAYER, _ALTER_LAYER);
+			layer_move(_QWERTY_LAYER);
+			break;
+		case _QWERTZ_LAYER:
+			state = update_tri_layer_state(state, _RAISE_LAYER, _QWERTY_LOWER_LAYER, _ALTER_LAYER);
+			state = update_tri_layer_state(state, _RAISE_LAYER, _COLEMA_LOWER_LAYER, _ALTER_LAYER);
+			state = update_tri_layer_state(state, _RAISE_LAYER, _DVORAK_LOWER_LAYER, _ALTER_LAYER);
+			layer_move(_QWERTZ_LAYER);
+			break;
+		case _COLEMA_LAYER:
+			state = update_tri_layer_state(state, _RAISE_LAYER, _QWERTY_LOWER_LAYER, _ALTER_LAYER);
+			state = update_tri_layer_state(state, _RAISE_LAYER, _QWERTZ_LOWER_LAYER, _ALTER_LAYER);
+			state = update_tri_layer_state(state, _RAISE_LAYER, _DVORAK_LOWER_LAYER, _ALTER_LAYER);
+			layer_move(_COLEMA_LAYER);
+			break;
+		case _DVORAK_LAYER:
+			state = update_tri_layer_state(state, _RAISE_LAYER, _QWERTY_LOWER_LAYER, _ALTER_LAYER);
+			state = update_tri_layer_state(state, _RAISE_LAYER, _QWERTZ_LOWER_LAYER, _ALTER_LAYER);
+			state = update_tri_layer_state(state, _RAISE_LAYER, _COLEMA_LOWER_LAYER, _ALTER_LAYER);
+			layer_move(_DVORAK_LAYER);
+			break;
+	}
+	return state;
+}
+
+bool process_record_user(uint16_t keycode, keyrecord_t *record) {
+	switch (keycode) {
+		case BL_TOGG:
+			if (record->event.pressed) {
+				cospad_bl_led_togg();
+			}
+			return false;
+		case BL_ON:
+			if (record->event.pressed) {
+				cospad_bl_led_on();
+			}
+			return false;
+		case BL_OFF:
+			if (record->event.pressed) {
+				cospad_bl_led_off();
+			}
+			return false;
+		case QWERTY:
+			if (record->event.pressed) {
+				set_single_persistent_default_layer(_QWERTY_LAYER);
+				print("switched to QWERTY layout\n");
+			}
+			return false;
+			break;
+		case QWERTZ:
+			if (record->event.pressed) {
+				set_single_persistent_default_layer(_QWERTZ_LAYER);
+			}
+			return false;
+			break;
+		case COLEMAK:
+			if (record->event.pressed) {
+				set_single_persistent_default_layer(_COLEMA_LAYER);
+			}
+			return false;
+			break;
+		case DVORAK:
+			if (record->event.pressed) {
+				set_single_persistent_default_layer(_DVORAK_LAYER);
+			}
+			return false;
+			break;
+		default:
+			return true;
+	}
+}