qmk_firmware

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

keymap.c (4006B)


      1 /* Copyright 2019 Ethan Durrant (emdarcher)
      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 
     18 //create the tap type
     19 typedef struct {
     20     bool is_press_action;
     21     int state;
     22 } tap;
     23 
     24 //tap dance states
     25 enum {
     26     SINGLE_TAP = 1,
     27     SINGLE_HOLD = 2,
     28 };
     29 
     30 //tap dance keys
     31 enum {
     32     TAPPY_KEY = 0
     33 };
     34 
     35 //function to handle all the tap dances
     36 int cur_dance(tap_dance_state_t *state);
     37 
     38 //functions for each tap dance
     39 void tk_finished(tap_dance_state_t *state, void *user_data);
     40 void tk_reset(tap_dance_state_t *state, void *user_data);
     41 
     42 #define INDICATOR_LED   B5
     43 
     44 #define _FN0    1
     45 #define _ML1    2
     46 
     47 const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
     48     [0] = LAYOUT(/* Base */
     49                  TD(TAPPY_KEY),KC_HOME, KC_PGUP,
     50                  KC_DEL,    KC_END,     KC_PGDN,
     51                  
     52                             KC_UP,
     53                  KC_LEFT,   KC_DOWN,    KC_RIGHT),
     54     [_FN0] = LAYOUT(/* function layer */
     55                  KC_TRNS,   KC_PAUS,    KC_VOLU,
     56                  KC_ENTER,  KC_SCRL,    KC_VOLD,
     57                  
     58                             KC_TRNS,
     59                  KC_TRNS,   KC_TRNS,    KC_TRNS),
     60     [_ML1] = LAYOUT(/* media function layer, toggled on a single tap */
     61                  KC_TRNS,   KC_TRNS,    KC_VOLU, 
     62                  KC_MUTE,   KC_TRNS,    KC_VOLD,
     63                  
     64                             KC_SPC,
     65                  KC_MRWD,   KC_MPLY,    KC_MFFD),
     66 };
     67 
     68 //determine the current tap dance state
     69 int cur_dance (tap_dance_state_t *state){
     70     if(state->count == 1){
     71         //if a tap was registered
     72         if(!state->pressed){
     73             //if not still pressed, then was a single tap
     74             return SINGLE_TAP;
     75         } else {
     76             //if still pressed/held down, then it's a single hold
     77             return SINGLE_HOLD;
     78         }
     79     } else {
     80         return 8;
     81     }
     82 }
     83 
     84 //initialize the tap structure for the tap key
     85 static tap tk_tap_state = {
     86     .is_press_action = true,
     87     .state = 0
     88 };
     89 
     90 //functions that control what our tap dance key does
     91 void tk_finished(tap_dance_state_t *state, void *user_data){
     92     tk_tap_state.state = cur_dance(state);
     93     switch(tk_tap_state.state){
     94         case SINGLE_TAP:
     95             //send desired key when tapped:
     96             //setting to the media layer
     97             if(layer_state_is(_ML1)){
     98                 //if already active, toggle it to off
     99                 layer_off(_ML1);
    100                 //turn off the indicator LED
    101                 //set LED HI to turn it off
    102                 gpio_write_pin_high(INDICATOR_LED);
    103             } else {
    104                 //turn on the media layer
    105                 layer_on(_ML1);
    106                 //turn on the indicator LED
    107                 //set LED pin to LOW to turn it on
    108                 gpio_write_pin_low(INDICATOR_LED);
    109             }
    110             break;
    111         case SINGLE_HOLD:
    112             //set to desired layer when held:
    113             //setting to the function layer
    114             layer_on(_FN0);
    115             break;
    116     }
    117 }
    118 
    119 void tk_reset(tap_dance_state_t *state, void *user_data){
    120     //if held and released, leave the layer
    121     if(tk_tap_state.state == SINGLE_HOLD){
    122         layer_off(_FN0);
    123     }
    124     //reset the state
    125     tk_tap_state.state = 0; 
    126 }
    127 
    128 //associate the tap dance key with its functionality
    129 tap_dance_action_t tap_dance_actions[] = {
    130     [TAPPY_KEY] = ACTION_TAP_DANCE_FN_ADVANCED(NULL, tk_finished, tk_reset)
    131 };