qmk_firmware

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

keymap.c (13701B)


      1 #include QMK_KEYBOARD_H
      2 #include <string.h>
      3 #include <stdio.h>
      4 #include "taphold.h"
      5 #include "ds1307.h"
      6 
      7 /* Note: don't forget there's some more code in qmk_firmware/users/anderson dir */
      8 
      9 #define _MAIN 0
     10 #define _ALPHA 1
     11 #define _BETA 2
     12 
     13 enum custom_keycodes {
     14     KC_MAIN = SAFE_RANGE,
     15     KC_ALPHA,
     16     KC_BETA,
     17     KC_SET_TIME,
     18 };
     19 
     20 /* TapHold is my own implementation of the `LT` macro. It's processed in `process_record_user()`. */
     21 #define TAPHOLD_CONFIG_SIZE 3
     22 taphold_t taphold_config[TAPHOLD_CONFIG_SIZE] = {
     23     {.key=KC_ALPHA, .mode=TAPHOLD_LAYER, .shortAction=KC_ESC, .longAction=_ALPHA},
     24     {.key=KC_BETA, .mode=TAPHOLD_LAYER, .shortAction=KC_EQL, .longAction=_BETA},
     25     {.key=KC_RCTL, .mode=TAPHOLD_MOD, .shortAction=KC_MINS, .longAction=KC_LCTL},
     26 };
     27 uint16_t taphold_config_size = TAPHOLD_CONFIG_SIZE;
     28 uint32_t taphold_timeout = 90;
     29 
     30 /* Colors */
     31 uint32_t layer_colors[3] = {
     32     [_MAIN] = 0xFF0010,
     33     [_ALPHA] = 0x4020FF,
     34     [_BETA] = 0x20FF00,
     35 };
     36 
     37 const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
     38     /* Main layer
     39                                                        │MUTE │           │L_MOD│
     40        ┏━━━━━┳━━━━━┯━━━━━┯━━━━━┯━━━━━┯━━━━━┳━━━━━┯━━━━━┯━━━━━┯━━━━━┯━━━━━┳━━━━━┓
     41        ┃ TAB ┃  Q  │  W  │  E  │  R  │  T  ┃  Y  │  U  │  I  │  O  │  P  ┃ BSP ┃
     42        ┣━━━━━╉─────┼─────┼─────┼─────┼─────╂─────┼─────┼─────┼─────┼─────╊━━━━━┫
     43        ┃𝛼/ESC┃  A  │  S  │  D  │  F  │  G  ┃  H  │  J  │  K  │  L  │  ;  ┃ RET ┃
     44        ┣━━━━━╉─────┼─────┼─────┼─────┼─────╂─────┼─────┼─────┼─────┼─────╊━━━━━┫
     45        ┃SHIFT┃  Z  │  X  │  C  │  V  │  B  ┃  N  │  M  │  ,  │  .  │  /  ┃CTL/-┃
     46        ┣━━━━━╉─────┼─────┼─────┼─────┼─────╂─────┼─────┼─────┼─────┼─────╊━━━━━┫
     47        ┃LCTRL┃     │     │ ALT │ GUI │SPACE┃SPACE│ 𝛽/= │  '  │     │     ┃  \  ┃
     48        ┗━━━━━┻━━━━━┷━━━━━┷━━━━━┷━━━━━┷━━━━━┻━━━━━┷━━━━━┷━━━━━┷━━━━━┷━━━━━┻━━━━━┛
     49        */
     50     [_MAIN] = LAYOUT(
     51                                                                                 KC_MUTE,                   _______,
     52         KC_TAB,  KC_Q,    KC_W,    KC_E,    KC_R,    KC_T,    KC_Y,    KC_U,    KC_I,    KC_O,    KC_P,    KC_BSPC,
     53         KC_ALPHA,KC_A,    KC_S,    KC_D,    KC_F,    KC_G,    KC_H,    KC_J,    KC_K,    KC_L,    KC_SCLN, KC_ENT,
     54         KC_LSFT, KC_Z,    KC_X,    KC_C,    KC_V,    KC_B,    KC_N,    KC_M,    KC_COMM, KC_DOT,  KC_SLSH, KC_RCTL,
     55         KC_LCTL, _______, _______, KC_LALT, KC_LGUI, KC_SPC,  KC_SPC,  KC_BETA, KC_QUOT, _______, _______, KC_BSLS
     56     ),
     57 
     58     /* Alpha layer (𝛼)
     59                                                        │     │           │     │
     60        ┏━━━━━┳━━━━━┯━━━━━┯━━━━━┯━━━━━┯━━━━━┳━━━━━┯━━━━━┯━━━━━┯━━━━━┯━━━━━┳━━━━━┓
     61        ┃     ┃PREV │PLAY │NEXT │     │NUMLK┃  -  │ ^^^ │  ^  │ vvv │  ~  ┃ DEL ┃
     62        ┣━━━━━╉─────┼─────┼─────┼─────┼─────╂─────┼─────┼─────┼─────┼─────╊━━━━━┫
     63        ┃     ┃     │VOL -│VOL +│     │CPSLK┃HOME │ <-- │  v  │ --> │  `  ┃  \  ┃
     64        ┣━━━━━╉─────┼─────┼─────┼─────┼─────╂─────┼─────┼─────┼─────┼─────╊━━━━━┫
     65        ┃     ┃     │     │     │     │SCRLK┃ END │  =  │  [  │  ]  │  (  ┃  )  ┃
     66        ┣━━━━━╉─────┼─────┼─────┼─────┼─────╂─────┼─────┼─────┼─────┼─────╊━━━━━┫
     67        ┃     ┃     │     │     │     │     ┃     │     │     │     │     ┃     ┃
     68        ┗━━━━━┻━━━━━┷━━━━━┷━━━━━┷━━━━━┷━━━━━┻━━━━━┷━━━━━┷━━━━━┷━━━━━┷━━━━━┻━━━━━┛
     69        */
     70     [_ALPHA] = LAYOUT(
     71                                                                                 _______,                   _______,
     72         _______, KC_MPRV, KC_MPLY, KC_MNXT, _______, KC_NUM,  KC_MINS, KC_PGUP, KC_UP,   KC_PGDN, KC_TILD, KC_DEL,
     73         _______, _______, KC_VOLD, KC_VOLU, _______, KC_CAPS, KC_HOME, KC_LEFT, KC_DOWN, KC_RIGHT,KC_GRV,  KC_BSLS,
     74         _______, _______, _______, _______, _______, KC_SCRL, KC_END,  KC_EQL,  KC_LBRC, KC_RBRC, KC_LPRN ,KC_RPRN,
     75         _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______
     76     ),
     77 
     78     /* Beta layer (𝛽)
     79                                                        │     │           │     │
     80        ┏━━━━━┳━━━━━┯━━━━━┯━━━━━┯━━━━━┯━━━━━┳━━━━━┯━━━━━┯━━━━━┯━━━━━┯━━━━━┳━━━━━┓
     81        ┃ RGB ┃  1  │  2  │  3  │  4  │  5  ┃  6  │  7  │  8  │  9  │  0  ┃ F12 ┃
     82        ┣━━━━━╉─────┼─────┼─────┼─────┼─────╂─────┼─────┼─────┼─────┼─────╊━━━━━┫
     83        ┃L_MOD┃ F1  │ F2  │ F3  │ F4  │ F5  ┃ F6  │ F7  │ F8  │ F9  │ F10 ┃ F11 ┃
     84        ┣━━━━━╉─────┼─────┼─────┼─────┼─────╂─────┼─────┼─────┼─────┼─────╊━━━━━┫
     85        ┃     ┃QK_BOOT│DB_TOGG│     │     │TIME ┃SLEEP│     │  {  │  }  │PTSCR┃     ┃
     86        ┣━━━━━╉─────┼─────┼─────┼─────┼─────╂─────┼─────┼─────┼─────┼─────╊━━━━━┫
     87        ┃     ┃     │     │     │     │     ┃     │     │     │     │     ┃     ┃
     88        ┗━━━━━┻━━━━━┷━━━━━┷━━━━━┷━━━━━┷━━━━━┻━━━━━┷━━━━━┷━━━━━┷━━━━━┷━━━━━┻━━━━━┛
     89        */
     90     [_BETA] = LAYOUT(
     91                                                                                 _______,                   _______,
     92         UG_TOGG, KC_1,    KC_2,    KC_3,    KC_4,    KC_5,     KC_6,    KC_7,   KC_8,    KC_9,    KC_0,    KC_F12,
     93         _______, KC_F1,   KC_F2,   KC_F3,   KC_F4,   KC_F5,    KC_F6,  KC_F7,   KC_F8,   KC_F9,   KC_F10,  KC_F11,
     94         _______, QK_BOOT, DB_TOGG, _______, _______, KC_SET_TIME,KC_SLEP,_______,KC_LCBR,KC_RCBR, KC_PSCR, _______,
     95         _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______
     96     )
     97 };
     98 
     99 static bool alpha_pressed = false;
    100 static bool beta_pressed = false;
    101 
    102 static bool ctrl_pressed = false;
    103 static bool alt_pressed = false;
    104 static bool shift_pressed = false;
    105 static bool gui_pressed = false;
    106 
    107 void keyboard_post_init_user(void) {
    108     /* debug_enable = true; */
    109     /* debug_matrix = true; */
    110 }
    111 
    112 void eeconfig_init_user(void) {
    113     set_unicode_input_mode(UNICODE_MODE_LINUX);
    114 }
    115 
    116 static uint32_t last_update = 0;
    117 static uint8_t hours, minutes, seconds;
    118 
    119 void matrix_scan_user(void) {
    120     uint32_t now = timer_read32();
    121     if (now - last_update > 500) {
    122         ds1307_get_time(&hours, &minutes, &seconds);
    123         last_update = now;
    124     }
    125 }
    126 
    127 static bool is_in_set_time = false;
    128 static char new_time[6];
    129 static uint8_t new_time_index = 0;
    130 
    131 bool process_record_user(uint16_t keycode, keyrecord_t *record) {
    132     if (keycode == KC_SET_TIME && record->event.pressed) {
    133         is_in_set_time = true;
    134         new_time_index = 0;
    135     } else if (is_in_set_time) {
    136         if (!record->event.pressed && keycode >= KC_1 && keycode <= KC_0) {
    137             new_time[new_time_index++] = (keycode == KC_0) ? 0 : keycode - KC_1 + 1;
    138             if (new_time_index == 6) {
    139                 is_in_set_time = false;
    140                 ds1307_set_time(
    141                         (new_time[0]) * 10 + (new_time[1]),
    142                         (new_time[2]) * 10 + (new_time[3]),
    143                         (new_time[4]) * 10 + (new_time[5])
    144                 );
    145                 for (int i = 0; i < 6; i++) {
    146                     tap_code(KC_BACKSPACE);
    147                 }
    148             }
    149         }
    150     }
    151 
    152     if (keycode == KC_LCTL || keycode == KC_RCTL) {
    153         ctrl_pressed = record->event.pressed;
    154     } else if (keycode == KC_LALT) {
    155         alt_pressed = record->event.pressed;
    156     } else if (keycode == KC_LSFT) {
    157         shift_pressed = record->event.pressed;
    158     } else if (keycode == KC_LGUI) {
    159         gui_pressed = record->event.pressed;
    160     } else if (keycode == KC_ALPHA) {
    161         alpha_pressed = record->event.pressed;
    162     } else if (keycode == KC_BETA) {
    163         beta_pressed = record->event.pressed;
    164     }
    165 
    166     if (keycode == QK_BOOT) {
    167         rgblight_setrgb(255, 255, 0);
    168     }
    169     if (keycode == KC_LCTL) {
    170         /* Some Overlay1_Enable fuckery! */
    171         (record->event.pressed ? register_code : unregister_code)(KC_LCTL);
    172         return false;
    173     }
    174     return taphold_process(keycode, record);
    175 }
    176 
    177 bool encoder_update_user(uint8_t index, bool clockwise) {
    178     if (index == 0) {
    179         if (!alpha_pressed) {
    180             tap_code(clockwise ? KC_VOLD : KC_VOLU);
    181         } else {
    182             tap_code(clockwise ? KC_MPRV : KC_MNXT);
    183         }
    184     } else if (index == 1) {
    185         if (!alpha_pressed) {
    186             tap_code(clockwise ? KC_UP : KC_DOWN);
    187         } else {
    188             tap_code(clockwise ? KC_PGUP : KC_PGDN);
    189         }
    190     }
    191     return true;
    192 }
    193 
    194 #ifdef OLED_ENABLE
    195 oled_rotation_t oled_init_user(oled_rotation_t rotation) {
    196     return OLED_ROTATION_0;
    197 }
    198 
    199 bool oled_task_user(void) {
    200     /* Host Keyboard Layer Status */
    201     uint8_t current_layer = get_highest_layer(layer_state);
    202 
    203     /* Layer */
    204     static const char PROGMEM icons[4][3][6] = {
    205         {
    206             { 0x80, 0x81, 0x82, 0x83, 0x84, 0 },
    207             { 0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0 },
    208             { 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0 }
    209         },
    210         {
    211             { 0x85, 0x86, 0x87, 0x88, 0x89, 0 },
    212             { 0xa5, 0xa6, 0xa7, 0xa8, 0xa9, 0 },
    213             { 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0 }
    214         },
    215         {
    216             { 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0 },
    217             { 0xaa, 0xab, 0xac, 0xad, 0xae, 0 },
    218             { 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0 }
    219         },
    220         {
    221             { 0x8f, 0x90, 0x91, 0x92, 0x93, 0 },
    222             { 0xaf, 0xb0, 0xb1, 0xb2, 0xb3, 0 },
    223             { 0xcf, 0xd0, 0xd1, 0xd2, 0xd3, 0 }
    224         }
    225     };
    226 
    227     uint8_t icon_index = current_layer == _MAIN ? 3 : current_layer == _ALPHA ? 1 : 2;
    228     for (int i = 0; i < 3; i++) {
    229         oled_set_cursor(0, i + 1);
    230         oled_write_P(icons[icon_index][i], false);
    231     }
    232 
    233     /* Time */
    234     oled_set_cursor(6, 0);
    235     // oled_write_P(PSTR("-D48 Custom-\n"), false);
    236     char buf[16];
    237     sprintf(
    238             buf,
    239             "%02d:%02d:%02d", hours, minutes, seconds
    240     );
    241     oled_write(buf, false);
    242 
    243     /* Modifiers */
    244     static const char PROGMEM mods[][2] = {
    245         {0x94, 0x95}, // CTL
    246         {0x96, 0x97}, // ALT
    247         {0x98, 0x99}, // GUI
    248         {0x9a, 0x9b},  // SFT
    249         /* {0x9c, 0x9d},  // EMPTY */
    250     };
    251 
    252     char mod_data[13] = "\x9c\x9d\x9c\x9d\x9c\x9d\x9c\x9d \x07\x07\x07\0";
    253     if (ctrl_pressed) strncpy(mod_data, mods[0], 2);
    254     if (alt_pressed) strncpy(mod_data + 2, mods[1], 2);
    255     if (gui_pressed) strncpy(mod_data + 4, mods[2], 2);
    256     if (shift_pressed) strncpy(mod_data + 6, mods[3], 2);
    257     led_t led_usb_state = host_keyboard_led_state();
    258     if (led_usb_state.num_lock) mod_data[9] = 'N';
    259     if (led_usb_state.caps_lock) mod_data[10] = 'C';
    260     if (led_usb_state.scroll_lock) mod_data[11] = 'S';
    261 
    262     oled_set_cursor(6, 1);
    263     oled_write(mod_data, false);
    264 
    265     /* Matrix */
    266     static const char PROGMEM matrix_chars[] = {
    267         0xb4, // None
    268         0xb5, // Upper
    269         0xb6, // Lower
    270         0xb7  // Both
    271     };
    272 
    273     for (uint8_t row = 1; row < MATRIX_ROWS; row += 2) {
    274         // Skip first row because it's used by the encoders.
    275         uint16_t bits1 = matrix_get_row(row);
    276         uint16_t bits2 = matrix_get_row(row + 1);
    277 
    278         for (uint8_t col = 0; col < MATRIX_COLS; col++) {
    279             uint8_t matrix_char = matrix_chars[((bits1 & (1 << col)) ? 1 : 0) | ((bits2 & (1 << col)) ? 2 : 0)];
    280             oled_set_cursor(6 + col, 2 + (row - 1) / 2);
    281             oled_write_char(matrix_char, false);
    282         }
    283     }
    284 
    285     return false;
    286 }
    287 #endif