tap_dances.c 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. // Copyright 2020 Christopher Courtney, aka Drashna Jael're (@drashna) <drashna@live.com>
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #include "tap_dances.h"
  4. #define NUM_OF_DIABLO_KEYS 4
  5. // define diablo macro timer variables
  6. diablo_timer_t diablo_timer[NUM_OF_DIABLO_KEYS];
  7. // Set the default intervals. Always start with 0 so that it will disable on first hit.
  8. // Otherwise, you will need to hit a bunch of times, or hit the "clear" command
  9. uint8_t diablo_times[] = {0, 1, 3, 5, 10, 30};
  10. /**
  11. * @brief Main function for handling diable related tap dances.
  12. *
  13. * @param state Main data struction contining information about events
  14. * @param user_data Local data for the dance. Allows customization to be passed on to function
  15. */
  16. void diablo_tapdance_master(qk_tap_dance_state_t *state, void *user_data) {
  17. diable_keys_t *diablo_keys = (diable_keys_t *)user_data;
  18. // Sets the keycode based on the index
  19. diablo_timer[diablo_keys->index].keycode = diablo_keys->keycode;
  20. // if the tapdance is hit more than the number of elemints in the array, reset
  21. if (state->count >= ARRAY_SIZE(diablo_times)) {
  22. diablo_timer[diablo_keys->index].key_interval = 0;
  23. reset_tap_dance(state);
  24. } else { // else set the interval (tapdance count starts at 1, array starts at 0, so offset by one)
  25. diablo_timer[diablo_keys->index].key_interval = diablo_times[state->count - 1];
  26. }
  27. }
  28. // clang-format off
  29. // One function to rule them all!! Where the Magic Sauce lies
  30. #define ACTION_TAP_DANCE_DIABLO(index, keycode) { \
  31. .fn = { NULL, (void *)diablo_tapdance_master, NULL }, \
  32. .user_data = (void *)&((diable_keys_t) { index, keycode }), \
  33. }
  34. // clang-format on
  35. // Tap Dance Definitions, sets the index and the keycode.
  36. qk_tap_dance_action_t tap_dance_actions[] = {
  37. // tap once to disable, and more to enable timed micros
  38. [TD_D3_1] = ACTION_TAP_DANCE_DIABLO(0, KC_1),
  39. [TD_D3_2] = ACTION_TAP_DANCE_DIABLO(1, KC_2),
  40. [TD_D3_3] = ACTION_TAP_DANCE_DIABLO(2, KC_3),
  41. [TD_D3_4] = ACTION_TAP_DANCE_DIABLO(3, KC_4),
  42. };
  43. /**
  44. * @brief Runs check to see if timer has elapsed for each dance, and sends keycodes, if it has.
  45. *
  46. */
  47. void run_diablo_macro_check(void) {
  48. for (uint8_t index = 0; index < NUM_OF_DIABLO_KEYS; index++) {
  49. // if key_interval is 0, it's disabled, so only run if it's set. If it's set, check the timer.
  50. if (diablo_timer[index].key_interval && timer_elapsed(diablo_timer[index].timer) > (diablo_timer[index].key_interval * 1000)) {
  51. // reset the timer, since enough time has passed
  52. diablo_timer[index].timer = timer_read();
  53. // send keycode ONLY if we're on the diablo layer.
  54. if (IS_LAYER_ON(_DIABLO)) {
  55. tap_code(diablo_timer[index].keycode);
  56. }
  57. }
  58. }
  59. }