qmk_firmware/users/drashna/keyrecords/tap_dances.c
Jeff Epler 9632360caa
Use a macro to compute the size of arrays at compile time (#18044)
* Add ARRAY_SIZE and CEILING utility macros

* Apply a coccinelle patch to use ARRAY_SIZE

* fix up some straggling items

* Fix 'make test:secure'

* Enhance ARRAY_SIZE macro to reject acting on pointers

The previous definition would not produce a diagnostic for
```
int *p;
size_t num_elem = ARRAY_SIZE(p)
```
but the new one will.

* explicitly get definition of ARRAY_SIZE

* Convert to ARRAY_SIZE when const is involved

The following spatch finds additional instances where the array is
const and the division is by the size of the type, not the size of
the first element:
```
@ rule5a using "empty.iso" @
type T;
const T[] E;
@@

- (sizeof(E)/sizeof(T))
+ ARRAY_SIZE(E)

@ rule6a using "empty.iso" @
type T;
const T[] E;
@@

- sizeof(E)/sizeof(T)
+ ARRAY_SIZE(E)
```

* New instances of ARRAY_SIZE added since initial spatch run

* Use `ARRAY_SIZE` in docs (found by grep)

* Manually use ARRAY_SIZE

hs_set is expected to be the same size as uint16_t, though it's made
of two 8-bit integers

* Just like char, sizeof(uint8_t) is guaranteed to be 1

This is at least true on any plausible system where qmk is actually used.

Per my understanding it's universally true, assuming that uint8_t exists:
https://stackoverflow.com/questions/48655310/can-i-assume-that-sizeofuint8-t-1

* Run qmk-format on core C files touched in this branch

Co-authored-by: Stefan Kerkmann <karlk90@pm.me>
2022-08-30 10:20:04 +02:00

67 lines
2.8 KiB
C

// Copyright 2020 Christopher Courtney, aka Drashna Jael're (@drashna) <drashna@live.com>
// SPDX-License-Identifier: GPL-2.0-or-later
#include "tap_dances.h"
#define NUM_OF_DIABLO_KEYS 4
// define diablo macro timer variables
diablo_timer_t diablo_timer[NUM_OF_DIABLO_KEYS];
// Set the default intervals. Always start with 0 so that it will disable on first hit.
// Otherwise, you will need to hit a bunch of times, or hit the "clear" command
uint8_t diablo_times[] = {0, 1, 3, 5, 10, 30};
/**
* @brief Main function for handling diable related tap dances.
*
* @param state Main data struction contining information about events
* @param user_data Local data for the dance. Allows customization to be passed on to function
*/
void diablo_tapdance_master(qk_tap_dance_state_t *state, void *user_data) {
diable_keys_t *diablo_keys = (diable_keys_t *)user_data;
// Sets the keycode based on the index
diablo_timer[diablo_keys->index].keycode = diablo_keys->keycode;
// if the tapdance is hit more than the number of elemints in the array, reset
if (state->count >= ARRAY_SIZE(diablo_times)) {
diablo_timer[diablo_keys->index].key_interval = 0;
reset_tap_dance(state);
} else { // else set the interval (tapdance count starts at 1, array starts at 0, so offset by one)
diablo_timer[diablo_keys->index].key_interval = diablo_times[state->count - 1];
}
}
// clang-format off
// One function to rule them all!! Where the Magic Sauce lies
#define ACTION_TAP_DANCE_DIABLO(index, keycode) { \
.fn = { NULL, (void *)diablo_tapdance_master, NULL }, \
.user_data = (void *)&((diable_keys_t) { index, keycode }), \
}
// clang-format on
// Tap Dance Definitions, sets the index and the keycode.
qk_tap_dance_action_t tap_dance_actions[] = {
// tap once to disable, and more to enable timed micros
[TD_D3_1] = ACTION_TAP_DANCE_DIABLO(0, KC_1),
[TD_D3_2] = ACTION_TAP_DANCE_DIABLO(1, KC_2),
[TD_D3_3] = ACTION_TAP_DANCE_DIABLO(2, KC_3),
[TD_D3_4] = ACTION_TAP_DANCE_DIABLO(3, KC_4),
};
/**
* @brief Runs check to see if timer has elapsed for each dance, and sends keycodes, if it has.
*
*/
void run_diablo_macro_check(void) {
for (uint8_t index = 0; index < NUM_OF_DIABLO_KEYS; index++) {
// if key_interval is 0, it's disabled, so only run if it's set. If it's set, check the timer.
if (diablo_timer[index].key_interval && timer_elapsed(diablo_timer[index].timer) > (diablo_timer[index].key_interval * 1000)) {
// reset the timer, since enough time has passed
diablo_timer[index].timer = timer_read();
// send keycode ONLY if we're on the diablo layer.
if (IS_LAYER_ON(_DIABLO)) {
tap_code(diablo_timer[index].keycode);
}
}
}
}