You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

182 lines
5.4 KiB

  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. typedef struct {
  18. bool is_press_action;
  19. int state;
  20. } tap;
  21. //tap dance states
  22. enum {
  23. SINGLE_TAP = 1,
  24. SINGLE_HOLD = 2,
  25. DOUBLE_TAP = 3,
  26. TRIPLE_TAP = 4
  27. };
  28. //tap dance keys
  29. enum {
  30. TAPPY_KEY = 0
  31. };
  32. //function to handle all the tap dances
  33. int cur_dance(tap_dance_state_t *state);
  34. //functions for each tap dance
  35. void tk_finished(tap_dance_state_t *state, void *user_data);
  36. void tk_reset(tap_dance_state_t *state, void *user_data);
  37. #define INDICATOR_LED B5
  38. #define TX_LED D5
  39. #define RX_LED B0
  40. #define _FN0 1
  41. #define _ML1 2
  42. #define _CL0 3
  43. const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
  44. [0] = LAYOUT(/* Base */
  45. TD(TAPPY_KEY),KC_HOME, KC_PGUP,
  46. KC_DEL, KC_END, KC_PGDN,
  47. KC_UP,
  48. KC_LEFT, KC_DOWN, KC_RIGHT),
  49. [_FN0] = LAYOUT(/* function layer */
  50. KC_TRNS, KC_PAUS, KC_VOLU,
  51. KC_ENTER, KC_SCRL, KC_VOLD,
  52. KC_TRNS,
  53. KC_TRNS, KC_TRNS, KC_TRNS),
  54. [_ML1] = LAYOUT(/* media function layer on double tap */
  55. KC_TRNS, KC_TRNS, KC_VOLU,
  56. KC_MUTE, KC_TRNS, KC_VOLD,
  57. KC_SPC,
  58. KC_MRWD, KC_MPLY, KC_MFFD),
  59. [_CL0] = LAYOUT(/* control layer on single tap */
  60. KC_TRNS, KC_TRNS, KC_TRNS,
  61. KC_TRNS, KC_TRNS, KC_TRNS,
  62. LCTL(KC_UP),
  63. LCTL(KC_LEFT), LCTL(KC_DOWN), LCTL(KC_RIGHT) ),
  64. };
  65. void matrix_init_user(void) {
  66. //init the Pro Micro on-board LEDs
  67. setPinOutput(TX_LED);
  68. setPinOutput(RX_LED);
  69. //set to off
  70. writePinHigh(TX_LED);
  71. writePinHigh(RX_LED);
  72. }
  73. //determine the current tap dance state
  74. int cur_dance (tap_dance_state_t *state){
  75. if(state->count == 1){
  76. //if a tap was registered
  77. if(!state->pressed){
  78. //if not still pressed, then was a single tap
  79. return SINGLE_TAP;
  80. } else {
  81. //if still pressed/held down, then it's a single hold
  82. return SINGLE_HOLD;
  83. }
  84. } else if(state->count == 2){
  85. //if tapped twice, set to double tap
  86. return DOUBLE_TAP;
  87. } else if(state->count == 3){
  88. //if tapped thrice, set to triple tap
  89. return TRIPLE_TAP;
  90. } else {
  91. return 8;
  92. }
  93. }
  94. //initialize the tap structure for the tap key
  95. static tap tk_tap_state = {
  96. .is_press_action = true,
  97. .state = 0
  98. };
  99. //functions that control what our tap dance key does
  100. void tk_finished(tap_dance_state_t *state, void *user_data){
  101. tk_tap_state.state = cur_dance(state);
  102. switch(tk_tap_state.state){
  103. case SINGLE_TAP:
  104. //toggle desired layer when tapped:
  105. if(layer_state_is(_CL0)){
  106. //if already active, toggle it to off
  107. layer_off(_CL0);
  108. //turn off LEDs
  109. writePinHigh(TX_LED);
  110. writePinHigh(RX_LED);
  111. } else {
  112. //turn on the command layer
  113. layer_on(_CL0);
  114. //turn on the LEDs
  115. writePinLow(TX_LED);
  116. writePinLow(RX_LED);
  117. }
  118. break;
  119. case SINGLE_HOLD:
  120. //set to desired layer when held:
  121. //setting to the function layer
  122. layer_on(_FN0);
  123. break;
  124. case DOUBLE_TAP:
  125. //set to desired layer when double tapped:
  126. //setting to the media layer
  127. if(layer_state_is(_ML1)){
  128. //if already active, toggle it to off
  129. layer_off(_ML1);
  130. //turn off the indicator LED
  131. //set LED HI to turn it off
  132. writePinHigh(INDICATOR_LED);
  133. } else {
  134. //turn on the media layer
  135. layer_on(_ML1);
  136. //turn on the indicator LED
  137. //set LED pin to LOW to turn it on
  138. writePinLow(INDICATOR_LED);
  139. }
  140. break;
  141. case TRIPLE_TAP:
  142. //reset all layers
  143. layer_clear();
  144. //set all LEDs off
  145. writePinHigh(TX_LED);
  146. writePinHigh(RX_LED);
  147. writePinHigh(INDICATOR_LED);
  148. break;
  149. }
  150. }
  151. void tk_reset(tap_dance_state_t *state, void *user_data){
  152. //if held and released, leave the layer
  153. if(tk_tap_state.state == SINGLE_HOLD){
  154. layer_off(_FN0);
  155. }
  156. //reset the state
  157. tk_tap_state.state = 0;
  158. }
  159. //associate the tap dance key with its functionality
  160. tap_dance_action_t tap_dance_actions[] = {
  161. [TAPPY_KEY] = ACTION_TAP_DANCE_FN_ADVANCED(NULL, tk_finished, tk_reset)
  162. };