logo

qmk_firmware

custom branch of QMK firmware git clone https://anongit.hacktivis.me/git/qmk_firmware.git

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