qmk_firmware

QMK firmware for my keyboards (Corne, Sweep Ferris) and trackball (Ploopy Adept)
Log | Files | Refs | Submodules | LICENSE

keymap.c (9424B)


      1 /* Copyright 2020 marksard
      2  *
      3  * This program is free software: you can redistribute it and/or modify
      4  * it under the terms of the GNU General Public License as published by
      5  * the Free Software Foundation, either version 2 of the License, or
      6  * (at your option) any later version.
      7  *
      8  * This program is distributed in the hope that it will be useful,
      9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
     10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     11  * GNU General Public License for more details.
     12  *
     13  * You should have received a copy of the GNU General Public License
     14  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
     15  */
     16 #include QMK_KEYBOARD_H
     17 #include "../common/oled_helper.h"
     18 
     19 // Each layer gets a name for readability, which is then used in the keymap matrix below.
     20 // The underscores don't mean anything - you can have a layer called STUFF or any other name.
     21 // Layer names don't all need to be of the same length, obviously, and you can also skip them
     22 // entirely and just use numbers.
     23 enum layer_number {
     24   _BASE = 0,
     25   _LOWER,
     26   _RAISE,
     27   _ADJUST,
     28 };
     29 
     30 enum custom_keycodes {
     31   LOWER = SAFE_RANGE,
     32   RAISE,
     33   ADJUST,
     34   KANJI,
     35   RGBRST
     36 };
     37 
     38 #define KC_TBSF  LSFT_T(KC_TAB)
     39 #define KC_ALAP  LALT_T(KC_APP)
     40 #define KC_GRSF  RSFT_T(KC_GRV)
     41 
     42 const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
     43   [_BASE] = LAYOUT_base(
     44   //,--------------------------------------------------------------------------------------------------------------------.
     45        KC_ESC,    KC_Q,    KC_W,    KC_E,    KC_R,    KC_T,    KC_Y,    KC_U,    KC_I,    KC_O,    KC_P,          KC_BSPC,
     46   //|--------+--------+--------+--------+--------+--------|--------+--------+--------+--------+--------+-----------------|
     47       KC_TBSF,    KC_A,    KC_S,    KC_D,    KC_F,    KC_G,    KC_H,    KC_J,    KC_K,    KC_L, KC_SCLN,           KC_ENT,
     48   //|--------+--------+--------+--------+--------+--------|--------+--------+--------+--------+--------+--------+--------|
     49       KC_LSFT,    KC_Z,    KC_X,    KC_C,    KC_V,    KC_B,    KC_N,    KC_M, KC_COMM,  KC_DOT, KC_SLSH,   KC_UP,
     50   //|--------+--------+--------+--------+--------+--------|--------+--------+--------+--------+--------+--------+--------|
     51       KC_LCTL, KC_LALT, KC_LGUI,   LOWER,          KC_BSPC,           KC_SPC,   RAISE, KC_ALAP, KC_LEFT, KC_DOWN, KC_RGHT,
     52   //`-------------------------------------------------------------------------------------------------------------------'
     53       KC_GRSF
     54   // ExtraKey: This key is an extra key. REV1 is a split back space. REV2 is to the right of the arrow-up key.
     55   ),
     56 
     57   [_LOWER] = LAYOUT_base(
     58   //,--------------------------------------------------------------------------------------------------------------------.
     59       _______,   KC_F1,   KC_F2,   KC_F3,   KC_F4,   KC_F5, KC_MINS,  KC_EQL, KC_LBRC, KC_RBRC, KC_BSLS,           KC_DEL,
     60   //|--------+--------+--------+--------+--------+--------|--------+--------+--------+--------+--------+-----------------|
     61       _______,   KC_F6,   KC_F7,   KC_F8,   KC_F9,  KC_F10, XXXXXXX, XXXXXXX, XXXXXXX, KC_SCLN, KC_QUOT,          _______,
     62   //|--------+--------+--------+--------+--------+--------|--------+--------+--------+--------+--------+--------+--------|
     63       _______,  KC_F11,  KC_F12, XXXXXXX, XXXXXXX,   KANJI, XXXXXXX, XXXXXXX, KC_COMM,  KC_DOT,  KC_GRV, KC_PGUP,
     64   //|--------+--------+--------+--------+--------+--------|--------+--------+--------+--------+--------+--------+--------|
     65       _______, _______, _______, _______,           KC_DEL,          _______, _______, XXXXXXX, KC_HOME, KC_PGDN,  KC_END,
     66   //`-------------------------------------------------------------------------------------------------------------------'
     67       _______
     68   // ExtraKey: This key is an extra key. REV1 is a split back space. REV2 is to the right of the arrow-up key.
     69   ),
     70 
     71   [_RAISE] = LAYOUT_base(
     72   //,--------------------------------------------------------------------------------------------------------------------.
     73       _______,    KC_1,    KC_2,    KC_3,    KC_4,    KC_5,    KC_6,    KC_7,    KC_8,    KC_9,    KC_0,          XXXXXXX,
     74   //|--------+--------+--------+--------+--------+--------|--------+--------+--------+--------+--------+-----------------|
     75       _______, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX,    KC_4,    KC_5,    KC_6, KC_QUOT,          _______,
     76   //|--------+--------+--------+--------+--------+--------|--------+--------+--------+--------+--------+--------+--------|
     77       _______, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX,    KC_1,    KC_2,    KC_3, KC_INT1, XXXXXXX,
     78   //|--------+--------+--------+--------+--------+--------|--------+--------+--------+--------+--------+--------+--------|
     79       _______, _______, _______, _______,          _______,          _______, _______,    KC_0,  KC_DOT, KC_COMM, KC_SLSH,
     80   //`-------------------------------------------------------------------------------------------------------------------'
     81       _______
     82   // ExtraKey: This key is an extra key. REV1 is a split back space. REV2 is to the right of the arrow-up key.
     83   ),
     84 
     85   [_ADJUST] = LAYOUT_base(
     86   //,--------------------------------------------------------------------------------------------------------------------.
     87       XXXXXXX, QK_BOOT,  RGBRST, AG_NORM, AG_SWAP, XXXXXXX, XXXXXXX, MS_WHLL, MS_WHLU, KC_HOME, KC_PGUP,          XXXXXXX,
     88   //|--------+--------+--------+--------+--------+--------|--------+--------+--------+--------+--------+-----------------|
     89       XXXXXXX, UG_TOGG, UG_HUEU, UG_SATU, UG_VALU, XXXXXXX, XXXXXXX, MS_WHLR, MS_WHLD,  KC_END, KC_PGDN,          XXXXXXX,
     90   //|--------+--------+--------+--------+--------+--------|--------+--------+--------+--------+--------+--------+--------|
     91       _______, UG_NEXT, UG_HUED, UG_SATD, UG_VALD, XXXXXXX, XXXXXXX, XXXXXXX, MS_BTN1, MS_BTN2, XXXXXXX, MS_UP,
     92   //|--------+--------+--------+--------+--------+--------|--------+--------+--------+--------+--------+--------+--------|
     93       _______, _______, _______, _______,          XXXXXXX,          XXXXXXX, _______, XXXXXXX, MS_LEFT, MS_DOWN, MS_RGHT,
     94   //`-------------------------------------------------------------------------------------------------------------------'
     95       _______
     96   // ExtraKey: This key is an extra key. REV1 is a split back space. REV2 is to the right of the arrow-up key.
     97   )
     98 };
     99 
    100 #define L_BASE _BASE
    101 #define L_LOWER (1<<_LOWER)
    102 #define L_RAISE (1<<_RAISE)
    103 #define L_ADJUST (1<<_ADJUST)
    104 #define L_ADJUST_TRI (L_ADJUST|L_RAISE|L_LOWER)
    105 
    106 #ifdef OLED_ENABLE
    107 #include <stdio.h>
    108 #include <string.h>
    109 
    110 typedef struct {
    111   uint8_t state;
    112   char name[8];
    113 }LAYER_DISPLAY_NAME;
    114 
    115 #define LAYER_DISPLAY_MAX 5
    116 const LAYER_DISPLAY_NAME layer_display_name[LAYER_DISPLAY_MAX] = {
    117   {L_BASE, "Base"},
    118   {L_BASE + 1, "Base"},
    119   {L_LOWER, "Lower"},
    120   {L_RAISE, "Raise"},
    121   {L_ADJUST_TRI, "Adjust"}
    122 };
    123 
    124 static inline const char* get_leyer_status(void) {
    125 
    126   for (uint8_t i = 0; i < LAYER_DISPLAY_MAX; ++i) {
    127     if (layer_state == 0 && layer_display_name[i].state == default_layer_state) {
    128 
    129       return layer_display_name[i].name;
    130     } else if (layer_state != 0 && layer_display_name[i].state == layer_state) {
    131 
    132       return layer_display_name[i].name;
    133     }
    134   }
    135 
    136   return "?";
    137 }
    138 
    139 static char layer_status_buf[24] = "Layer state ready.\n";
    140 static inline void update_keymap_status(void) {
    141 
    142   snprintf(layer_status_buf, sizeof(layer_status_buf) - 1, "OS:%s Layer:%s\n",
    143     keymap_config.swap_lalt_lgui? "win" : "mac", get_leyer_status());
    144 }
    145 
    146 static inline void render_keymap_status(void) {
    147 
    148   oled_write(layer_status_buf, false);
    149 }
    150 
    151 #define UPDATE_KEYMAP_STATUS() update_keymap_status()
    152 
    153 static inline void render_status(void) {
    154 
    155   UPDATE_LED_STATUS();
    156   RENDER_LED_STATUS();
    157   render_keymap_status();
    158   UPDATE_LOCK_STATUS();
    159   RENDER_LOCK_STATUS();
    160   RENDER_KEY_STATUS();
    161 }
    162 
    163 oled_rotation_t oled_init_user(oled_rotation_t rotation) {
    164 
    165 //   if (is_keyboard_master())
    166 //     return OLED_ROTATION_180;  // flips the display 180 degrees if offhand
    167   return rotation;
    168 }
    169 
    170 bool oled_task_user(void) {
    171 
    172   if (is_keyboard_master()) {
    173     render_status();
    174   } else {
    175     render_logo();
    176   }
    177     return false;
    178 }
    179 
    180 #else
    181 
    182 #define UPDATE_KEYMAP_STATUS()
    183 
    184 #endif
    185 
    186 static inline void update_change_layer(bool pressed, uint8_t layer1, uint8_t layer2, uint8_t layer3) {
    187 
    188   pressed ? layer_on(layer1) : layer_off(layer1);
    189   IS_LAYER_ON(layer1) && IS_LAYER_ON(layer2) ? layer_on(layer3) : layer_off(layer3);
    190 }
    191 
    192 int RGB_current_mode;
    193 bool process_record_user(uint16_t keycode, keyrecord_t *record) {
    194 
    195   UPDATE_KEY_STATUS(keycode, record);
    196 
    197   bool result = false;
    198   switch (keycode) {
    199     case LOWER:
    200       update_change_layer(record->event.pressed, _LOWER, _RAISE, _ADJUST);
    201       break;
    202     case RAISE:
    203       update_change_layer(record->event.pressed, _RAISE, _LOWER, _ADJUST);
    204         break;
    205     case KANJI:
    206       if (record->event.pressed) {
    207         if (keymap_config.swap_lalt_lgui == false) {
    208           register_code(KC_LNG2);
    209         } else {
    210           SEND_STRING(SS_LALT("`"));
    211         }
    212       } else {
    213         unregister_code(KC_LNG2);
    214       }
    215       break;
    216     #ifdef RGBLIGHT_ENABLE
    217       case RGBRST:
    218           if (record->event.pressed) {
    219             eeconfig_update_rgblight_default();
    220             rgblight_enable();
    221           }
    222         break;
    223     #endif
    224     default:
    225       result = true;
    226       break;
    227   }
    228 
    229   UPDATE_KEYMAP_STATUS();
    230   return result;
    231 }