diff options
Diffstat (limited to 'users/dennytom/chording_engine')
-rw-r--r-- | users/dennytom/chording_engine/README.md | 376 | ||||
-rw-r--r-- | users/dennytom/chording_engine/chord.py | 466 | ||||
-rw-r--r-- | users/dennytom/chording_engine/engine.part.1 | 163 | ||||
-rw-r--r-- | users/dennytom/chording_engine/engine.part.2 | 323 | ||||
-rw-r--r-- | users/dennytom/chording_engine/engine.part.3 | 404 | ||||
-rw-r--r-- | users/dennytom/chording_engine/keymap_def.schema.json | 337 | ||||
-rw-r--r-- | users/dennytom/chording_engine/parser.py | 231 | ||||
-rw-r--r-- | users/dennytom/chording_engine/state_machine.dot | 49 | ||||
-rw-r--r-- | users/dennytom/chording_engine/state_machine.svg | 235 | ||||
-rw-r--r-- | users/dennytom/chording_engine/tests/minunit.h | 288 | ||||
-rw-r--r-- | users/dennytom/chording_engine/tests/test.c | 1259 | ||||
-rw-r--r-- | users/dennytom/chording_engine/tests/test_full.sh | 11 | ||||
-rw-r--r-- | users/dennytom/chording_engine/tests/test_keymap_def.json | 145 | ||||
-rw-r--r-- | users/dennytom/chording_engine/tests/test_quick.sh | 6 |
14 files changed, 4293 insertions, 0 deletions
diff --git a/users/dennytom/chording_engine/README.md b/users/dennytom/chording_engine/README.md new file mode 100644 index 000000000..3610b190b --- /dev/null +++ b/users/dennytom/chording_engine/README.md @@ -0,0 +1,376 @@ +# README + +## About + +This is a custom combo engine. I call it chording engine mostly to differentiate it from QMK's combos. It is useful even if you are not using chording as a main input method to replace combos. + +Why does this exist? Typing on tiny keyboards can be challenging and you will end up relying on dances and / or combos. Pure QMK combos can be insufficient as they do not really support overlapping combos. For example, if you define 3 combos `(KC_Q, KC_W)`, `(KC_Z, KC_X)` and `(KC_Q, KC_W, KC_Z, KC_X)` and press Q, W, Z and X at the same time, all three combos will activate. Steno engines (and g Board Industries' custom steno inspired engine) solve this, however, they don't allow for comfortable typing in the traditional way. The steno chord activates only when *all* keys are lifted and makes it difficult to implement some advanced features. This engine treats each chord independently to allow for more comfortable typing experience. + +## TOC + +- [README](#readme) + - [About](#about) + - [TOC](#toc) + - [Start here](#start-here) + - [Steps](#steps) + - [Features](#features) + - [Chords](#chords) + - [Tap-Dance](#tap-dance) + - [Pseudolayers](#pseudolayers) + - [Control chords](#control-chords) + - [Settings up JSON definition](#settings-up-json-definition) + - [Keyboard and engine parameters](#keyboard-and-engine-parameters) + - [Pseudolayers](#pseudolayers-1) + - [Supported keycodes](#supported-keycodes) + - [Leader Key](#leader-key) + - [Extra code](#extra-code) + - [Further details](#further-details) + - [Implementation](#implementation) + - [Internal keycodes](#internal-keycodes) + - [Chords](#chords-1) + - [Caveats](#caveats) + +## Start here + +This engine therefore uses python parser that translates a JSON definition of keyboard specific information and keymap definition and produces `keymap.c`. Every function on this keymap is a chord (combo). The resulting keymap file is long and I do not encourage you to edit it. All you should have to edit is the JSON file. To produce the keymap file, run + +```sh +./parser.py keymap_def.json keymap.c +``` + +To prepare the keymap JSON definition, you can use on of my keymaps as a starting point. I have on for butterstick and for georgi. There is also a JSON schema that has some examples and sane defaults. All details are explained in the next section. The parser tries to validate some of the things that the JSON schema can not. Finally there is a JSON in the tests folder that has at least one example of every feature. + +Watch out, you can not name your JSON file `keymap.json` if you place in the keymap folder. QMK creates `keymap.json` as a part of compilation process and if you already have one, it gets confused. + +## Steps + +When setting up a new keyboard, follow the steps: + +1. Make a new directory for your keymap as QMK's documentation describes. +2. Write your JSON. Name it anything but `keymap.json`. +3. Depending on the keyboard / keymap, create `rules.mk` (follow QMK's documentation and note that if the keyboard's `rules.mk` include custom source files, this is the place you can remove them). +4. Use my python parser to generate the `keymap.c`. Run it from the `/users/dennytom/chording_engine` directory as it is using relative paths to some extra files. +5. Follow QMK's documentation to compile and flash your firmware. + +## Features + +### Chords + +Once again, *everything* on this keymap is a chord. Even sending `KC_Q` is done by pressing a single key chord. Chord gets activated after all it's keys get pressed. Only the longest chord gets activated. The order of the pressed keys *does not matter*, only the fact they have been pressed within the same time frame. An active chord gets deactivated if *any* of it's keys gets depressed. To activate the same single chord again, *all* it's keys have to be depressed and pressed again. With a few exceptions chords are independent of each other. No matter if some chords are currently active and some not, others can be activated or deactivated without affecting each other's state. *If you press keys to belonging to multiple different, non-overlapping chords, all get activated in the order they are defined in the keymap.* + +### Tap-Dance + +To make it even stranger, all chords are technically tap-dance chords. They are relatively simple state machines that execute a specific function every time they change state. For simplicity and optimization purposes, there are a few prewritten functions that implement common features like "send a single key" or "lock". Any number of chords can be "in dance" at any given moment without affecting each other's state. Custom dances can be easily added. Check out the `state_machine.png` to see all the states any chord can be in. + +### Pseudolayers + +Only one QMK layer is used. Following the butterstick's default keymap's example, the chording engine is using pseudolayers. The main difference to QMK's layers is that only one pseudolayer can be active at each time (meaning you can not use `KC_TRANS`, I actually don't know what will happen if you do). Chords can be activated only if they are on the currently active pseudolayer. Chords that are currently active do not get deactivated if the pseudolayer changes and will deactivate if any of their keys gets depressed even no matter the current pseudolayer. Locked chords (see below) and chords on the `ALWAYS_ON` pseudolayer can be activated anytime. + +### Control chords + +The engine implements a number of ways of changing how chords behave: + +* **Lock**: Similarly to QMK's lock, the next chord activated after the Lock chord will not deactivate on release of any of its keys, it will deactivate when all its keys get pressed again. Any number of chords can be locked at the same time. To make sure a locked chord can be unlocked, it can activate no matter the current pseudolayer. A chord can be locked mid dance. +* **One shots**: Chords that send keycodes and chords that turn on pseudolayers can be one shots. If tapped, they will lock (stay active) until the next keycode gets sent, *not necessarily when the next chord gets activated*. If held, they will deactivate on release *even if no keycode got sent*. +* **Tap-Hold**: Also called key-layer dance and key-key dance. Either sends a defined keycode on tap and temporarily switches pseudolayer on hold *or* sends two different keycodes on tap and hold. +* **Command mode**: After getting activated for the first time, the keyboard switches to command mode. All *keycodes* that would get registered get buffered instead. After activating the Command mode chord for the second time, all buffered keycodes get released at the same time allowing for key combination that would be hard or impossible to press. The Command mode only affects keycodes. It is therefore possible to change pseudolayers or activate / deactivate other chords while in Command mode. While multiple Command mode chords can be defined, they would not be independent. The keyboard either is or is not in command mode and there is only one buffer. +* **Leader key**: Just like pure QMK's Leader key, this allows you to add functions that get executed if the Leader key and a specific sequence of keycodes gets registered in a predefined order in a short timeframe. For example `:wq` can send `Ctrl+S` and `Ctrl+W` in a quick succession. While multiple Leader keys can be defined, they all would access the same list of sequences. +* **Dynamic macro**: A sequence of keycodes can be recorded and stored in the RAM of the keyboard and replayed. + +## Settings up JSON definition + +The JSON definition has 3 main sections. The elements `keys`, `parameters` and `layers` teach the engine about the details of your keyboard and set its parameters. The elements `pseudolayers`, `leader_sequences` and `chord_sets` define your keymap. Finally, the elements `extra_code` and `extra_dependencies` allow you to include more code to extend the capabilities of the engine. + +### Keyboard and engine parameters + +I do not have experience with stenography, so the the steno keycodes are hard for me to remember. That is why the keymap is using new keycodes TOP1, TOP2, ... . + +```c + "keys": ["TOP1", "TOP2", "TOP3", ...] +``` + +You can name these however you like as long as they do not crash with QMK's keycodes. + +*The chording engine in it's current implementation can handle up to 64 keys. If you need to support more, contact me (email or u/DennyTom at Reddit).* + +All timings, maximum lengths for macros, command mode and leader function are defined in `keyboard_parameters` field. Almost all should be pretty self-explanatory. + +My keyboards are small, so I only use the engine, but you might want to use layers that combine chord-able keys and traditional QMK keys or layers with advanced keycodes, for example for stenography. The array `layers` defines all the parser needs to know: + +```json +"layers": [ + { + "type": "auto" + }, + { + "type": "manual", + "keycodes": ["KC_1", "KC_2", "KC_3", "KC_4", "KC_5", "KC_6", "KC_7", "KC_8", "KC_9", "KC_0", + "KC_Q", "KC_W", "KC_E", "KC_R", "KC_T", "KC_Y", "KC_U", "KC_I", "KC_O", "KC_P" + ] + }, + { + "type": "manual", + "keycodes": ["KC_1", "KC_2", "KC_3", "KC_4", "KC_5", "KC_6", "KC_7", "KC_8", "KC_9", "KC_0", + "BOT1", "BOT2", "BOT3", "BOT4", "BOT5", "BOT6", "BOT7", "BOT8", "BOT9", "BOT0"] + } + ] +``` + +This example defines 3 layers, one that is automatically populated with chording engine's internal keycodes, second that is populated with QMK's keycodes and third that uses both internal and QMK's keycodes. The layers do not have names, you have to access them with `TO(1)` and `TO(0)`. + +Some keyboards mangle the order of keycodes when registering them in the layers. For that fill up the `layout_function_name` with the name of function / macro. If your keyboard does not do it, leave that string empty. + +### Pseudolayers + +Array `pseudolayers` defines the keymap per pseudolayer. Each field has to contain the name for the layer and the list of chords. + +```JSON +"pseudolayers": [ + { + "name": "QWERTY", + "chords": [ + { + "type": "simple", + "keycode": "SPACE", + "chord": ["BOT1", "BOT0"] + }, + { + "type": "visual", + "keycode": "CLEAR_KB", + "chord": [ + "X", "", "", "", "", "", "", "", "", "X", + "X", "", "", "", "", "", "", "", "", "X", + ] + }, + { + "type": "visual_array", + "keys": ["TOP1", "TOP2", "TOP3"], + "dictionary": [ + ["X", "X", " ", "ESC"], + [" ", "X", "X", "TAB"], + ["X", "X", "X", "ENTER"] + ] + }, + { + "type": "chord_set", + "set": "rows", + "keycodes": [ + "Q", "W", "E", "R", "T", "Y", "U", "I", "O", "P", + "A", "S", "D", "F", "G", "H", "J", "K", "L", ";", + "Z", "X", "C", "V", "B", "N", "M", ",", ".", "/" + ] + } + ] + } +] +``` + +The array `chord` defines chords. You can either use simple chord and list all the keys that have to pressed at the same time, or use the visual chord and place `"X"` over keys that will be part of the chord. You can also use `visual_array` to define a number of chords in a visual way on a subset of keys defined in the `keys` array. Finally, you can use `chord_set` to define a number of chords following a pattern that was set in the `chord_sets` array in the root object like this: + +```json +"chord_sets": [ + { + "name": "rows", + "chords": [ + ["TOP1"], ["TOP2"], ["TOP3"], ["TOP4"], ["TOP5"], ["TOP6"], ["TOP7"], ["TOP8"], ["TOP9"], ["TOP0"] + ["TOP1", "BOT1"], ["TOP2", "BOT2"], ["TOP3", "BOT3"], ["TOP4", "BOT4"], ["TOP5", "BOT5"], ["TOP6", "BOT6"], ["TOP7", "BOT7"], ["TOP8", "BOT8"], ["TOP9", "BOT9"], ["TOP0", "BOT0"], + ["BOT1"], ["BOT2"], ["BOT3"], ["BOT4"], ["BOT5"], ["BOT6"], ["BOT7"], ["BOT8"], ["BOT9"], ["BOT0"] + ] + }, + { + "name": "cols", + "chords": [ + ["TOP1", "TOP2"], ["TOP2", "TOP3"], ["TOP3", "TOP4"], ["TOP4", "TOP5"], ["TOP5", "TOP6"], ["TOP6", "TOP7"], ["TOP7", "TOP8"], ["TOP8", "TOP9"], ["TOP9", "TOP0"], + ["TOP1", "TOP2", "BOT1", "BOT2"], ["TOP2", "TOP3", "BOT2", "BOT3"], ["TOP3", "TOP4", "BOT3", "BOT4"], ["TOP4", "TOP5", "BOT4", "BOT5"], ["TOP5", "TOP6", "BOT5", "BOT6"], ["TOP6", "TOP7", "BOT6", "BOT7"], ["TOP7", "TOP8", "BOT7", "BOT8"], ["TOP8", "TOP9", "BOT8", "BOT9"], ["TOP9", "TOP0", "BOT9", "BOT0"], + ["BOT1", "BOT2"], ["BOT2", "BOT3"], ["BOT3", "BOT4"], ["BOT4", "BOT5"], ["BOT5", "BOT6"], ["BOT6", "BOT7"], ["BOT7", "BOT8"], ["BOT8", "BOT9"], ["BOT9", "BOT00"], + ] + } +] +``` + + + +You might notice that the code tries to do a few clever things when parsing keycodes: + +* If the keycode would be just a character basic keycode, it tries to allow the use of shortcuts. `Q` will get replaced with `KC_Q`, `,` becomes `KC_COMMA`. This *should* work for all KC_ keycodes unless I missed some. +* `MO()` and `DF()` macros work the same way for pseudolayers as they would for layers in pure QMK. +* `O()` is a shortcut for `OSK()` or `OSL()`. +* `STR('...')` sends a string. Careful with quoting. +* Special chords like Command mode have their own codes like `CMD`. +* The empty strings get ignored. + +### Supported keycodes + +* **`X`** or **`KC_X`**: Send code `KC_X` just like a normal keyboard. + +* **`STR("X")`**: Send string "x" on each activation of the chord. Once again, watch out for quoting and escaping characters. If you want special characters (especially quotes) in your string, look up Python reference for string literals and experiment. Also, because of how the string gets parsed, it is not possible to use `(` in the string. + +* **`MO(X)`**: Temporary switch to pseudolayer `X`. Because only one pseudolayer can be active at any moment, this works by switching back to the pseudolayer the chord lives on on deactivation. If you chain `MO()`s on multiple pseudolayers and deactivate them in a random order, you might end up stranded on a pseudolayer. I recommend adding `CLEAR` somewhere on `ALWAYS_ON` pseudolayer just in case. + +* **`MO(X,Y)`**: Temporary switch to pseudolayer `Y`. Switches to pseudolayer `X` on deactivation. Especially useful when you want to put the `MO()` chord on `ALWAYS_ON`. + +* **`DF(X)`**: Permanent switch to pseudolayer `X`. + +* **`TO(X)`**: Switches the QMK layer to `X`. + +* **`O(X)`**: One-shot key `X` (if `X` starts with `"KC_"`) or one-shot layer `X` (otherwise) . Both have retro tapping enabled. + +* **Tap-holds** + + * **`KK(X, Y)`**: Pulses code `X` on tap and code `Y` on hold. + * **`KL(X, Y)`**: Pulses code `X` on tap and switches to pseudolayer `Y` on hold. If during the hold no key gets registered, the code `X` will get sent instead (similar to QMK's retro tapping). + * **`KM(X, Y)`**: Same as `KK()` but meant for modifiers on hold. Instead of a timer to figure out tap-hold, uses retro tapping like behavior just like `KL()`. This has issues with GUI and ALT as they often have a meaning. + * The chording engine determines if you are holding a chord based on a *global* timer. If you start holding a tap-hold chord and very quickly start tapping other chords, the hold might not activate until a short moment *after the last* chord when the timer expires. If you are running into this, adjust timeouts or wait a brief moment after pressing the chord to make sure it switches into the hold state before pressing other chords. + +* **Autoshift** + + * **`AS(X)`**: Pulses code `X` on tap and Pulses left shift + `X` on hold. + * **`AT`** : Toggles autoshift for all autoshift chords. If off, all `AS` chords act like `KC` chords. + +* **`LOCK`**: The lock key. Since tap-dances of chords are independent, it is possible to lock a chord *anywhere in it's dance if you time it right!*. If that happens, use the `CLEAR` chord or restart your keeb. + +* **`CMD`**: The command mode. The number of keycodes that can be buffered is defined in in `command_max_length`. + +* **`LEAD`**: The leader key. The maximum length of the sequences needs to be defined in `keyboard_params`. You can use `leader_sequences` array to add sequences: + + ```json + "leader_sequences": [ + { + "name": "fn_L1", + "function": "void fn_L1(void) { SEND(KC_LCTL); SEND(KC_LALT); SEND(KC_DEL); }", + "sequence": ["KC_Q", "KC_Z"] + } + ] + ``` + + When the engine notices the sequence, it will call the function defined in the field `name`. You can either define it in the `function` field, in the field `extra_code` or in an external file that you then have to insert manually or using the `extra_dependencies` array. The parser copy-pastes the contents `extra_code` of all files specified in the `extra_dependencies` array in the `keymap.c`. + +* **`M(X, VALUE1, VALUE2)`**: A custom macro. Adds a chord that will use function `X` and with `chord.value1 = VALUE1; chord.value2 = VALUE2;`. The function `X` can be arbitrary C function, go crazy. Just like with the leader sequences, you have to insert the code into the generated `keymap.c` manually or through `extra_code` or `extra_dependencies`. The following example defines a macro that acts exactly like `KC_MEH` (the chording engine *should* support `KC_MEH`, this is just an example): + + ```c + void fn_M1(const struct Chord* self) { + switch (*self->state) { + case ACTIVATED: + key_in(KC_LCTL); + key_in(KC_LSFT); + key_in(KC_LALT); + break; + case DEACTIVATED: + key_out(KC_LCTL); + key_out(KC_LSFT); + key_out(KC_LALT); + break; + case FINISHED: + case FINISHED_FROM_ACTIVE: + break; + case RESTART: + key_out(KC_LCTL); + key_out(KC_LSFT); + key_out(KC_LALT); + break; + default: + break; + } + } + ``` + + Since this feels like it would be the most common way to use this feature, I wrote a macro for this: + +* **`MK(X1, X2, ...)`**: Acts like `KC()` except it registers / unregisters all `X1`, `X2`, ... codes at the same time. + +* **`D(X1, X2, ...)`**: A basic keycode dance. If tapped (or held), registers `X1`. If tapped and then tapped again (or held), registers `X2`, ... It *cannot* be combined with tap-hold, however holding will result in repeat. You can put in as many basic keycodes as you want, but the macro will break if you go beyond 256. It will try to expand shortened keycodes. Advanced keycodes are not supported. + +* **`DM_RECORD`, `DM_NEXT`, `DM_END`, `DM_PLAY`**: Start recording a dynamic macro. Once you start recording, basic keycodes will get stored. When replaying the macro, all keys you press before `DM_NEXT` or `DM_END` will get pressed at the same time. For example the sequence `DM_RECORD`, `KC_CTRL`, `KC_A`, `DM_NEXT`, `KC_BSPC`, `DM_END` will record a macro that when played will execute the sequence Ctrl+a, Backspace. `dynamic_macro_max_length` defines the maximum length of the macro to be recorded. You can increase it for the price of RAM. The example above requires 4 units of length to be saved (Ctrl, A, next, Backspace). + +* **`CLEAR_KB`**: clears keyboard, sets all chords to the default state and switches the pseudolayer to the default one. Basically the emergency stop button. + +* **`RESET`**: Go to the DFU flashing mode. + +**Caveat** of the current implementation is that the tap-hold, `MK` and `D` keycodes can not accept any of the keycodes that have some sort a function like dynamic macro specific chords, `CLEAR_KB`, `RESET`, `LOCK`, `AT`, ... + +### Leader Key + +The sequences are not defined by the *keys* you press but by the *keycodes* that get intercepted. The length of the sequence must be equal or shorter than the maximum (defined in `keyboard.inc`). Currently, the timeout for the leader sequence refreshes after each key pressed. If the sequence is not in the database, nothing will happen. + +### Extra code + +Extra C code needed to define custom chords can be added by quoting in in the `extra_code` element or by saving it in another header file and including it using the `extra_dependencies` element: + +```json +{ + "extra_code": "void double_dance(const struct Chord* self) { ... }\n", + "extra_dependencies": ["my_header.h"] +} +``` + + + +## Further details + +### Implementation + +The source files are split into several files. `engine.part.1`, `engine.part.2` and `engine.part.3` contain C code that defines the Chord structure, implementations for all provided functions and the engine itself. `parser.py` generates keyboard and keymap dependent code. The file `chord.py` contains most of the logic required to properly translate chords from the JSON to the C code. I rarely write in python, if you have improvements, let me know, *please*. + +### Internal keycodes + +When `process_record_user()` gets one of the internal keycodes, it returns `true`, completely bypassing keyboard's and QMK's `process_record` functions. *All other* keycodes get passed down to QMK's standard processing. + +### Chords + +Each chord is defined by a constant structure, a function and two non-constant `int` variables keeping the track of the chord's state: + +```c +struct Chord { + uint32_t keycodes_hash; + uint8_t pseudolayer; + uint8_t* state; + uint8_t* counter; + uint16_t value1; + uint8_t value2; + void (*function) (const struct Chord*); +}; + +uint8_t state_0 = IDLE; +uint8_t counter_0 = 0; +void function_0(struct Chord* self) { + switch (*self->state) { + case ACTIVATED: + register_code(self->value1); + break; + case DEACTIVATED: + unregister_code(self->value1); + break; + case FINISHED: + case PRESS_FROM_ACTIVE: + break; + case RESTART: + unregister_code(self->value1); + break; + default: + break; + } +} +const struct Chord chord_0 PROGMEM = {H_TOP1, QWERTY, &state_0, &counter_0, KC_Q, 0, function_0}; +``` + +All chords have to be added to `list_of_chord` array that gets regularly scanned and processed. The function doesn't actually activate on all state changes, there are a few more like `IDLE` (nothing is currently happening to the chord) or `IN_ONE_SHOT` (the chord is one shot and is currently locked). Those are all necessary for internal use only. The ones you have to worry about are + +* `ACTIVATED`: Analogous to a key being pressed (this includes repeated presses for tap-dance) +* `DEACTIVATED`: Analogous to a key being depressed (also can be repeated) +* `FINISHED`: Happens if the chord got deactivated and then the dance timer expired. +* `PRESS_FROM_ACTIVE`: Happens if the chord was active when the dance timer expired. Meaning you at least once activated the chord and then kept holding it down. Useful to recognize taps and holds. +* `FINISHED_FROM_ACTIVE`: Happens *after* `PRESS_FROM_HOLD` if the chord is still active when the dance timer expires for the second time. Can be combined with the `counter` to recognize even longer presses. Useful if you want to recognize long presses, for example for autoshift functionality. In `keyboard.inc` you can set `LONG_PRESS_MULTIPLIER` to set how many times does dance timer have to expire for the autoshift to trigger. +* `RESTART`: The dance is done. Happens immediately after `FINISHED` or on chord deactivation from `FINISHED_FROM_ACTIVE`. Anything you have to do to get the chord into `IDLE` mode happens here. + +The chords change states based on external and internal events. Anytime a chord's function is activated, it may change it's own state. Also, on certain events, the chording engine will trigger the functions of all chords in a specific state and *if the chords' state hasn't changed* it will then change it appropriately. The default behavior when a chord changes state is described by the following diagram: + +![state machine diagram](state_machine.png) +The colors differentiate in which function the change happens, see `state_machine.dot` for a bit more detail. Black arrows happen in more than one function. Arrows without a label happen immediately. + +You can see that the diagram is not exhaustive. For example nothing leads into `IN_ONE_SHOT`. That is because the chord's function can change the chord's state. This is useful for some advanced chords that break the default behavir (one-shots) and for optimization (chords that just send `KC_X` do not need to ever go into dance). + +## Caveats + +Each chord stores as much as possible in `PROGMEM` and unless it needs it, doesn't allocate `counter`. However it still has to store it's `state` and sometimes the `counter` in RAM. If you keep adding more chords, at one point you will run out. If your firmware fits in the memory and your keyboard crashes, try optimizing your RAM usage. + +Also, the code is not perfect. I keep testing it, but can not guarantee that it is stable. Some functions take (very short but still) time and if you happen to create keypress event when the keyboard can not see it, a chord can get stuck in a funny state. That is especially fun if the pseudolayer changes and you can not immediately press it again. Just restart the keyboard or push the key a few times. diff --git a/users/dennytom/chording_engine/chord.py b/users/dennytom/chording_engine/chord.py new file mode 100644 index 000000000..707f36b82 --- /dev/null +++ b/users/dennytom/chording_engine/chord.py @@ -0,0 +1,466 @@ +from functools import reduce +import re + +strings = [] +number_of_strings = -1 + +def top_level_split(s): + """ + Split `s` by top-level commas only. Commas within parentheses are ignored. + """ + + # Parse the string tracking whether the current character is within + # parentheses. + balance = 0 + parts = [] + part = "" + + for i in range(len(s)): + c = s[i] + part += c + if c == '(': + balance += 1 + elif c == ')': + balance -= 1 + elif c == ',' and balance == 0 and not s[i+1] == ',': + part = part[:-1].strip() + parts.append(part) + part = "" + + # Capture last part + if len(part): + parts.append(part.strip()) + + return parts + +def new_chord(on_pseudolayer, keycodes_hash, has_counter, value1, value2, function, output_buffer, index): + counter_link = "NULL" + output_buffer += "uint8_t state_" + str(index) + " = IDLE;\n" + if has_counter: + output_buffer += "uint8_t counter_" + str(index) + " = 0;\n" + counter_link = "&counter_" + str(index) + output_buffer += "const struct Chord chord_" + str(index) + " PROGMEM = {" + keycodes_hash + ", " + on_pseudolayer + ", &state_" + str(index) + ", " + counter_link + ", " + str(value1) + ", " + str(value2) + ", " + function + "};\n" + index += 1 + return [output_buffer, index] + +def KC(on_pseudolayer, keycodes_hash, keycode, output_buffer, index): + return new_chord(on_pseudolayer, keycodes_hash, False, keycode, 0, "single_dance", output_buffer, index) + +def AS(on_pseudolayer, keycodes_hash, keycode, output_buffer, index): + return new_chord(on_pseudolayer, keycodes_hash, True, keycode, 0, "autoshift_dance", output_buffer, index) + +def AT(on_pseudolayer, keycodes_hash, output_buffer, index): + return new_chord(on_pseudolayer, keycodes_hash, False, 0, 0, "autoshift_toggle", output_buffer, index) + +def KL(on_pseudolayer, keycodes_hash, keycode, to_pseudolayer, output_buffer, index): + return new_chord(on_pseudolayer, keycodes_hash, True, keycode, to_pseudolayer, "key_layer_dance", output_buffer, index) + +def KK(on_pseudolayer, keycodes_hash, keycode1, keycode2, output_buffer, index): + return new_chord(on_pseudolayer, keycodes_hash, True, keycode1, keycode2, "key_key_dance", output_buffer, index) + +def KM(on_pseudolayer, keycodes_hash, keycode, to_pseudolayer, output_buffer, index): + return new_chord(on_pseudolayer, keycodes_hash, False, keycode, to_pseudolayer, "key_mod_dance", output_buffer, index) + +def MO(on_pseudolayer, keycodes_hash, to_pseudolayer, output_buffer, index): + return new_chord(on_pseudolayer, keycodes_hash, False, to_pseudolayer, 0, "temp_pseudolayer", output_buffer, index) + +def MO_alt(on_pseudolayer, keycodes_hash, from_pseudolayer, to_pseudolayer, output_buffer, index): + return new_chord(on_pseudolayer, keycodes_hash, False, to_pseudolayer, from_pseudolayer, "temp_pseudolayer_alt", output_buffer, index) + +def LOCK(on_pseudolayer, keycodes_hash, output_buffer, index): + return new_chord(on_pseudolayer, keycodes_hash, False, 0, 0, "lock", output_buffer, index) + +def DF(on_pseudolayer, keycodes_hash, to_pseudolayer, output_buffer, index): + return new_chord(on_pseudolayer, keycodes_hash, False, to_pseudolayer, 0, "perm_pseudolayer", output_buffer, index) + +def TO(on_pseudolayer, keycodes_hash, to_pseudolayer, output_buffer, index): + return new_chord(on_pseudolayer, keycodes_hash, False, to_pseudolayer, 0, "switch_layer", output_buffer, index) + +def OSK(on_pseudolayer, keycodes_hash, keycode, output_buffer, index): + return new_chord(on_pseudolayer, keycodes_hash, False, keycode, 0, "one_shot_key", output_buffer, index) + +def OSL(on_pseudolayer, keycodes_hash, to_pseudolayer, output_buffer, index): + return new_chord(on_pseudolayer, keycodes_hash, False, to_pseudolayer, 0, "one_shot_layer", output_buffer, index) + +def CMD(on_pseudolayer, keycodes_hash, output_buffer, index): + return new_chord(on_pseudolayer, keycodes_hash, False, 0, 0, "command", output_buffer, index) + +def DM_RECORD(on_pseudolayer, keycodes_hash, output_buffer, index): + return new_chord(on_pseudolayer, keycodes_hash, False, 0, 0, "dynamic_macro_record", output_buffer, index) + +def DM_NEXT(on_pseudolayer, keycodes_hash, output_buffer, index): + return new_chord(on_pseudolayer, keycodes_hash, False, 0, 0, "dynamic_macro_next", output_buffer, index) + +def DM_END(on_pseudolayer, keycodes_hash, output_buffer, index): + return new_chord(on_pseudolayer, keycodes_hash, False, 0, 0, "dynamic_macro_end", output_buffer, index) + +def DM_PLAY(on_pseudolayer, keycodes_hash, output_buffer, index): + return new_chord(on_pseudolayer, keycodes_hash, False, 0, 0, "dynamic_macro_play", output_buffer, index) + +def LEAD(on_pseudolayer, keycodes_hash, output_buffer, index): + return new_chord(on_pseudolayer, keycodes_hash, False, 0, 0, "leader", output_buffer, index) + +def CLEAR(on_pseudolayer, keycodes_hash, output_buffer, index): + return new_chord(on_pseudolayer, keycodes_hash, False, 0, 0, "clear", output_buffer, index) + +def RESET(on_pseudolayer, keycodes_hash, output_buffer, index): + return new_chord(on_pseudolayer, keycodes_hash, False, 0, 0, "reset", output_buffer, index) + +def STR(on_pseudolayer, keycodes_hash, string_input, output_buffer, index, number_of_strings, strings): + [a, b] = new_chord(on_pseudolayer, keycodes_hash, False, number_of_strings, 0, "string_in", output_buffer, index) + return [a, b, number_of_strings + 1, strings + [string_input]] + +def M(on_pseudolayer, keycodes_hash, value1, value2, fnc, output_buffer, index): + return new_chord(on_pseudolayer, keycodes_hash, True, value1, value2, fnc, output_buffer, index) + +def expand_keycode_fnc(DEFINITION): + if DEFINITION == "`": + DEFINITION = "GRAVE" + elif DEFINITION == "-": + DEFINITION = "MINUS" + elif DEFINITION == "=": + DEFINITION = "EQUAL" + elif DEFINITION == "[": + DEFINITION = "LBRACKET" + elif DEFINITION == "]": + DEFINITION = "RBRACKET" + elif DEFINITION == "\\": + DEFINITION = "BSLASH" + elif DEFINITION == ";": + DEFINITION = "SCOLON" + elif DEFINITION == "'": + DEFINITION = "QUOTE" + elif DEFINITION == ",": + DEFINITION = "COMMA" + elif DEFINITION == ".": + DEFINITION = "DOT" + elif DEFINITION == "/": + DEFINITION = "SLASH" + elif DEFINITION == "~": + DEFINITION = "TILDE" + elif DEFINITION == "*": + DEFINITION = "ASTERISK" + elif DEFINITION == "+": + DEFINITION = "PLUS" + elif DEFINITION == "(": + DEFINITION = "LEFT_PAREN" + elif DEFINITION == ")": + DEFINITION = "RIGHT_PAREN" + elif DEFINITION == "<": + DEFINITION = "LEFT_ANGLE_BRACKET" + elif DEFINITION == ">": + DEFINITION = "RIGHT_ANGLE_BRACKET" + elif DEFINITION == "{": + DEFINITION = "LEFT_CURLY_BRACE" + elif DEFINITION == "}": + DEFINITION = "RIGHT_CURLY_BRACE" + elif DEFINITION == "?": + DEFINITION = "QUESTION" + elif DEFINITION == "~": + DEFINITION = "TILDE" + elif DEFINITION == ":": + DEFINITION = "COLON" + elif DEFINITION == "_": + DEFINITION = "UNDERSCORE" + elif DEFINITION == '"': + DEFINITION = "DOUBLE_QUOTE" + elif DEFINITION == "@": + DEFINITION = "AT" + elif DEFINITION == "#": + DEFINITION = "HASH" + elif DEFINITION == "$": + DEFINITION = "DOLLAR" + elif DEFINITION == "!": + DEFINITION = "EXCLAIM" + elif DEFINITION == "%": + DEFINITION = "PERCENT" + elif DEFINITION == "^": + DEFINITION = "CIRCUMFLEX" + elif DEFINITION == "&": + DEFINITION = "AMPERSAND" + elif DEFINITION == "|": + DEFINITION = "PIPE" + + if DEFINITION in [ + "A", "a", "B", "b", "C", "c", "D", "d", "E", "e", + "F", "f", "G", "g", "H", "h", "I", "i", "J", "j", + "K", "k", "L", "l", "M", "m", "N", "n", "O", "o", + "P", "p", "Q", "q", "R", "r", "S", "s", "T", "t", + "U", "u", "V", "v", "W", "w", "X", "x", "Y", "y", + "Z", "z", "1", "2", "3", "4", "5", "6", "7", "8", + "9", "0", "F1", "F2", "F3", "F4", "F5", "F6", "F7", + "F8", "F9", "F10", "F11", "F12", "F13", "F14", "F15", + "F16", "F17", "F18", "F19", "F20", "F21", "F22", + "F23", "F24", "ENTER", "ENT", "ESCAPE", "ESC", + "BSPACE", "BSPC", "TAB", "SPACE", "SPC", "NONUS_HASH", + "NUHS", "NONUS_BSLASH", "NUBS", "COMMA", "COMM", + "DOT", "SLASH", "SLSH", "TILDE", "TILD", "EXCLAIM", + "EXLM", "AT", "HASH", "DOLLAR", "DLR", "PERCENT", + "PERC", "CIRCUMFLEX", "CIRC", "AMPERSAND", "AMPR", + "ASTERISK", "ASTR", "LEFT_PAREN", "LPRN", "RIGHT_PAREN", + "RPRN", "UNDERSCORE", "UNDS", "PLUS", "LEFT_CURLY_BRACE", + "LCBR", "RIGHT_CURLY_BRACE", "RCBR", "PIPE", "COLON", + "COLN", "DOUBLE_QUOTE", "DQUO", "DQT", + "LEFT_ANGLE_BRACKET", "LABK", "LT", "RIGHT_ANGLE_BRACKET", + "RABK", "GT", "QUESTION", "QUES", "SCOLON", "SCLN", + "QUOTE", "QUOT", "LBRACKET", "LBRC", "RBRACKET", "RBRC", + "BSLASH", "BSLS", "MINUS", "MINS", "EQUAL", "EQL", + "GRAVE", "GRV", "ZKHK", "CAPSLOCK", "CLCK", "CAPS", + "SCROLLOCK", "SLCK", "BRMD", "NUMLOCK", "NLCK", + "LOCKING_CAPS", "LCAP", "LOCKING_NUM", "LNUM", + "LOCKING_SCROLL", "LSCR", "LCTRL", "LCTL", "LSHIFT", + "LSFT", "LALT", "LGUI", "LCMD", "LWIN", "RCTRL", + "RCTL", "RSHIFT", "RSFT", "RALT", "RGUI", "RCMD", + "RWIN", "INT1", "RO", "INT2", "KANA", "INT3", "JYEN", + "INT4", "HENK", "INT5", "MHEN", "INT6", "INT7", + "INT8", "INT9", "LANG1", "HAEN", "LANG2", "HANJ", + "LANG3", "LANG4", "LANG5", "LANG6", "LANG7", "LANG8", + "LANG9", "PSCREEN", "PSCR", "PAUSE", "PAUS", "BRK", + "BRMU", "INSERT", "INS", "HOME", "PGUP", "DELETE", + "DEL", "END", "PGDOWN", "PGDN", "RIGHT", "RGHT", + "LEFT", "DOWN", "UP", "APPLICATION", "APP", "POWER", + "EXECUTE", "EXEC", "HELP", "MENU", "SELECT", "SLCT", + "STOP", "AGAIN", "AGIN", "UNDO", "CUT", "COPY", + "PASTE", "PSTE", "FIND", "MUTE", "VOLUP", "VOLDOWN", + "ALT_ERASE", "ERAS", "SYSREQ", "CANCEL", "CLEAR", + "CLR", "PRIOR", "RETURN", "SEPARATOR", "OUT", "OPER", + "CLEAR_AGAIN", "CRSEL", "EXSEL", "SYSTEM_POWER", + "PWR", "SYSTEM_SLEEP", "SLEP", "SYSTEM_WAKE", "WAKE", + "AUDIO_MUTE", "MUTE", "AUDIO_VOL_UP", "VOLU", + "AUDIO_VOL_DOWN", "VOLD", "MEDIA_NEXT_TRACK", "MNXT", + "MEDIA_PREV_TRACK", "MPRV", "CPRV", "MEDIA_STOP", "MSTP", + "MEDIA_PLAY_PAUSE", "MPLY", "MEDIA_SELECT", "MSEL", + "MEDIA_EJECT", "EJCT", "MAIL", "CALCULATOR", "CALC", + "MY_COMPUTER", "MYCM", "WWW_SEARCH", "WSCH", "WWW_HOME", + "WHOM", "WWW_BACK", "WBAK", "WWW_FORWARD", "WFWD", + "WWW_STOP", "WSTP", "WWW_REFRESH", "WREF", + "WWW_FAVORITES", "WFAV", "MEDIA_FAST_FORWARD", "MFFD", + "MEDIA_REWIND", "MRWD", "BRIGHTNESS_UP", "BRIU", + "BRIGHTNESS_DOWN", "BRID", "KP_SLASH", "PSLS", + "KP_ASTERISK", "PAST", "KP_MINUS", "PMNS", "KP_PLUS", + "PPLS", "KP_ENTER", "PENT", "KP_1", "P1", "KP_2", "P2", + "KP_3", "P3", "KP_4", "P4", "KP_5", "P5", "KP_6", "P6", + "KP_7", "P7", "KP_8", "P8", "KP_9", "P9", "KP_0", "P0", + "KP_DOT", "PDOT", "KP_EQUAL", "PEQL", "KP_COMMA", "PCMM", + "MS_BTN1", "BTN1", "MS_BTN2", "BTN2", "MS_BTN3", "BTN3", + "MS_BTN4", "BTN4", "MS_BTN5", "BTN5", "MS_BTN6", "BTN6", + "MS_LEFT", "MS_L", "MS_DOWN", "MS_D", "MS_UP", "MS_U", + "MS_RIGHT", "MS_R", "MS_WH_UP", "WH_U", "MS_WH_DOWN", + "WH_D", "MS_WH_LEFT", "MS_WH_L", "MS_WH_RIGHT", "MS_WH_R", + "KC_MS_ACCEL0", "ACL0", "KC_MS_ACCEL1", "ACL1", + "KC_MS_ACCEL2", "ACL2" + ]: + return "KC_" + DEFINITION + else: + return DEFINITION + +def MK(on_pseudolayer, keycodes_hash, definition, output_buffer, index): + l = len(definition.split(', ')) + output_buffer += "void function_" + str(index) + "(const struct Chord* self) {\n" + output_buffer += " switch (*self->state) {\n" + output_buffer += " case ACTIVATED:\n" + for i in range(0, l): + val = definition.split(',')[i].strip() + code = expand_keycode_fnc(val) + output_buffer += " key_in(" + code + ");\n" + output_buffer += " break;\n" + output_buffer += " case DEACTIVATED:\n" + for i in range(0, l): + val = definition.split(',')[i].strip() + code = expand_keycode_fnc(val) + output_buffer += " key_out(" + code + ");\n" + output_buffer += " *self->state = IDLE;\n" + output_buffer += " break;\n" + output_buffer += " case RESTART:\n" + for i in range(0, l): + val = definition.split(',')[i].strip() + code = expand_keycode_fnc(val) + output_buffer += " key_out(" + code + ");\n" + output_buffer += " break;\n" + output_buffer += " default:\n" + output_buffer += " break;\n" + output_buffer += " };\n" + output_buffer += "}\n" + return new_chord(on_pseudolayer, keycodes_hash, True, 0, 0, "function_" + str(index), output_buffer, index) + +def D(on_pseudolayer, keycodes_hash, DEFINITION, output_buffer, index): + l = len(DEFINITION.split(',')) + output_buffer += "void function_" + str(index) + "(const struct Chord* self) {\n" + output_buffer += " switch (*self->state) {\n" + output_buffer += " case ACTIVATED:\n" + output_buffer += " *self->counter = *self->counter + 1;\n" + output_buffer += " break;\n" + output_buffer += " case PRESS_FROM_ACTIVE:\n" + output_buffer += " switch (*self->counter) {\n" + for i in range(0, l): + val = DEFINITION.split(',')[i].strip() + code = expand_keycode_fnc(val) + output_buffer += " case " + str(i + 1) + ":\n" + output_buffer += " key_in( " + code + ");\n" + output_buffer += " break;\n" + output_buffer += " default:\n" + output_buffer += " break;\n" + output_buffer += " }\n" + output_buffer += " *self->state = FINISHED_FROM_ACTIVE;\n" + output_buffer += " break;\n" + output_buffer += " case FINISHED:\n" + output_buffer += " switch (*self->counter) {\n" + for i in range(0, l): + val = DEFINITION.split(',')[i].strip() + code = expand_keycode_fnc(val) + output_buffer += " case " + str(i + 1) + ":\n" + output_buffer += " tap_key( " + code + ");\n" + output_buffer += " break;\n" + output_buffer += " default:\n" + output_buffer += " break;\n" + output_buffer += " }\n" + output_buffer += " *self->counter = 0;\n" + output_buffer += " *self->state = IDLE;\n" + output_buffer += " break;\n" + output_buffer += " case RESTART:\n" + output_buffer += " switch (*self->counter) {\n" + for i in range(0, l): + val = DEFINITION.split(',')[i].strip() + code = expand_keycode_fnc(val) + output_buffer += " case " + str(i + 1) + ":\n" + output_buffer += " key_out( " + code + ");\n" + output_buffer += " break;\n" + output_buffer += " default:\n" + output_buffer += " break;\n" + output_buffer += " }\n" + output_buffer += " *self->counter = 0;\n" + output_buffer += " break;\n" + output_buffer += " default:\n" + output_buffer += " break;\n" + output_buffer += " }\n" + output_buffer += "}\n" + return new_chord(on_pseudolayer, keycodes_hash, True, 0, 0, "function_" + str(index), output_buffer, index) + +def O(on_pseudolayer, keycodes_hash, DEFINITION, output_buffer, index): + if DEFINITION[0:3] == "KC_": + return OSK(on_pseudolayer, keycodes_hash, DEFINITION, output_buffer, index) + else: + return OSL(on_pseudolayer, keycodes_hash, DEFINITION, output_buffer, index) + +def add_key(PSEUDOLAYER, KEYCODES_HASH, DEFINITION, output_buffer, index, number_of_strings, strings): + # if "= {" + KEYCODES_HASH + ", " + PSEUDOLAYER in output_buffer: + # KEYCODES_HASH = re.sub('H_', '', KEYCODES_HASH) + # raise Exception("You are trying to register a chord that you already registered (" + KEYCODES_HASH + ", " + PSEUDOLAYER + ")") + + if DEFINITION == "": + return [output_buffer, index, number_of_strings, strings] + else: + split = DEFINITION.split("(") + type = split[0].strip() + if len(split) == 1: + if type == "LOCK": + [output_buffer, index] = LOCK(PSEUDOLAYER, KEYCODES_HASH, output_buffer, index) + elif type == "AT": + [output_buffer, index] = AT(PSEUDOLAYER, KEYCODES_HASH, output_buffer, index) + elif type == "CMD": + [output_buffer, index] = CMD(PSEUDOLAYER, KEYCODES_HASH, output_buffer, index) + elif type == "LEAD": + [output_buffer, index] = LEAD(PSEUDOLAYER, KEYCODES_HASH, output_buffer, index) + elif type == "DM_RECORD": + [output_buffer, index] = DM_RECORD(PSEUDOLAYER, KEYCODES_HASH, output_buffer, index) + elif type == "DM_NEXT": + [output_buffer, index] = DM_NEXT(PSEUDOLAYER, KEYCODES_HASH, output_buffer, index) + elif type == "DM_END": + [output_buffer, index] = DM_END(PSEUDOLAYER, KEYCODES_HASH, output_buffer, index) + elif type == "DM_PLAY": + [output_buffer, index] = DM_PLAY(PSEUDOLAYER, KEYCODES_HASH, output_buffer, index) + elif type == "CLEAR_KB": + [output_buffer, index] = CLEAR(PSEUDOLAYER, KEYCODES_HASH, output_buffer, index) + elif type == "RESET": + [output_buffer, index] = RESET(PSEUDOLAYER, KEYCODES_HASH, output_buffer, index) + else: + code = expand_keycode_fnc(type) + [output_buffer, index] = KC(PSEUDOLAYER, KEYCODES_HASH, code, output_buffer, index) + else: + val = split[1][:-1].strip() + if type == "O": + code = expand_keycode_fnc(val) + [output_buffer, index] = O(PSEUDOLAYER, KEYCODES_HASH, code, output_buffer, index) + elif type == "D": + [output_buffer, index] = D(PSEUDOLAYER, KEYCODES_HASH, val, output_buffer, index) + elif type == "MK": + [output_buffer, index] = MK(PSEUDOLAYER, KEYCODES_HASH, val, output_buffer, index) + elif type == "M": + fnc = val.split(',')[0].strip() + val1 = val.split(',')[1].strip() + val2 = val.split(',')[2].strip() + [output_buffer, index] = M(PSEUDOLAYER, KEYCODES_HASH, val1, val2, fnc, output_buffer, index) + elif type == "KK": + val1 = val.split(',')[0].strip() + code1 = expand_keycode_fnc(val1) + val2 = val.split(',')[1].strip() + code2 = expand_keycode_fnc(val2) + [output_buffer, index] = KK(PSEUDOLAYER, KEYCODES_HASH, code1, code2, output_buffer, index) + elif type == "KL": + val1 = val.split(',')[0].strip() + code1 = expand_keycode_fnc(val1) + val2 = val.split(',')[1].strip() + [output_buffer, index] = KL(PSEUDOLAYER, KEYCODES_HASH, code1, val2, output_buffer, index) + elif type == "KM": + val1 = val.split(',')[0].strip() + code1 = expand_keycode_fnc(val1) + val2 = val.split(',')[1].strip() + code2 = expand_keycode_fnc(val2) + [output_buffer, index] = KM(PSEUDOLAYER, KEYCODES_HASH, code1, code2, output_buffer, index) + elif type == "AS": + code = expand_keycode_fnc(val) + [output_buffer, index] = AS(PSEUDOLAYER, KEYCODES_HASH, code, output_buffer, index) + elif type == "MO": + if not ',' in val: + [output_buffer, index] = MO(PSEUDOLAYER, KEYCODES_HASH, val, output_buffer, index) + else: + val1 = val.split(',')[0].strip() + val2 = val.split(',')[1].strip() + [output_buffer, index] = MO_alt(PSEUDOLAYER, KEYCODES_HASH, val1, val2, output_buffer, index) + elif type == "DF": + [output_buffer, index] = DF(PSEUDOLAYER, KEYCODES_HASH, val, output_buffer, index) + elif type == "TO": + [output_buffer, index] = TO(PSEUDOLAYER, KEYCODES_HASH, val, output_buffer, index) + elif type == "STR": + [output_buffer, index, number_of_strings, strings] = STR(PSEUDOLAYER, KEYCODES_HASH, val, output_buffer, index, number_of_strings, strings) + return [output_buffer, index, number_of_strings, strings] + +def add_leader_combo(DEFINITION, FUNCTION): + return list_of_leader_combos.append([DEFINITION, FUNCTION]) + +def add_chord_set(PSEUDOLAYER, INPUT_STRING, TYPE, data, output_buffer, index, number_of_strings, strings): + chord_set = {} + for set in data["chord_sets"]: + if set["name"] == TYPE: + chord_set = set["chords"] + break + + separated_string = top_level_split(INPUT_STRING) + for word, chord in zip(separated_string, chord_set): + chord_hash = reduce((lambda x, y: str(x) + " + " + str(y)), ["H_" + key for key in chord]) + [output_buffer, index, number_of_strings, strings] = add_key(PSEUDOLAYER, chord_hash, word, output_buffer, index, number_of_strings, strings) + + return [output_buffer, index, number_of_strings, strings] + +def add_dictionary(PSEUDOLAYER, keycodes, array, output_buffer, index, number_of_strings, strings): + for chord in array: + hash = "" + for word, key in zip(chord[:-1], keycodes): + if word == "X": + hash = hash + " + H_" + key + hash = hash[3:] + if hash != "": + [output_buffer, index, number_of_strings, strings] = add_key(PSEUDOLAYER, hash, chord[-1], output_buffer, index, number_of_strings, strings) + + return [output_buffer, index, number_of_strings, strings] + +def secret_chord(PSEUDOLAYER, ACTION, INPUT_STRING, data, output_buffer, index, number_of_strings, strings): + separated_string = top_level_split(INPUT_STRING) + hash = "" + for word, key in zip(separated_string, data["keys"]): + if word == "X": + hash = hash + " + H_" + key + + hash = hash[3:] + if hash != "": + return add_key(PSEUDOLAYER, hash, ACTION, output_buffer, index, number_of_strings, strings)
\ No newline at end of file diff --git a/users/dennytom/chording_engine/engine.part.1 b/users/dennytom/chording_engine/engine.part.1 new file mode 100644 index 000000000..73df4cdea --- /dev/null +++ b/users/dennytom/chording_engine/engine.part.1 @@ -0,0 +1,163 @@ +enum chord_states { + IDLE, + READY, + ACTIVATED, + DEACTIVATED, + PRESS_FROM_ACTIVE, + FINISHED_FROM_ACTIVE, + IDLE_IN_DANCE, + READY_IN_DANCE, + FINISHED, + LOCKED, + READY_LOCKED, + RESTART, + IN_ONE_SHOT +}; + +struct Chord { + uint32_t keycodes_hash; + uint8_t pseudolayer; + uint8_t* state; + uint8_t* counter; + uint16_t value1; + uint8_t value2; + void (*function) (const struct Chord*); +}; + +uint8_t current_pseudolayer = DEFAULT_PSEUDOLAYER; +bool lock_next = false; +uint16_t chord_timer = 0; +uint16_t dance_timer = 0; +bool autoshift_mode = true; +uint8_t keycode_index = 0; +uint8_t command_mode = 0; +uint8_t command_ind = 0; +bool in_leader_mode = false; +uint8_t leader_ind = 0; +uint16_t leader_timer = 0; +uint8_t dynamic_macro_mode = false; +uint8_t dynamic_macro_ind = 0; +bool a_key_went_through = false; +struct Chord* last_chord = NULL; + +bool handle_US_ANSI_shifted_keys(int16_t keycode, bool in) { + bool is_US_ANSI_shifted = true; + + int16_t regular_keycode = KC_NO; + switch (keycode) { + case KC_TILDE: + regular_keycode = KC_GRAVE; + break; + case KC_EXCLAIM: + regular_keycode = KC_1; + break; + case KC_AT: + regular_keycode = KC_2; + break; + case KC_HASH: + regular_keycode = KC_3; + break; + case KC_DOLLAR: + regular_keycode = KC_4; + break; + case KC_PERCENT: + regular_keycode = KC_5; + break; + case KC_CIRCUMFLEX: + regular_keycode = KC_6; + break; + case KC_AMPERSAND: + regular_keycode = KC_7; + break; + case KC_ASTERISK: + regular_keycode = KC_8; + break; + case KC_LEFT_PAREN: + regular_keycode = KC_9; + break; + case KC_RIGHT_PAREN: + regular_keycode = KC_0; + break; + case KC_UNDERSCORE: + regular_keycode = KC_MINUS; + break; + case KC_PLUS: + regular_keycode = KC_EQUAL; + break; + case KC_LEFT_CURLY_BRACE: + regular_keycode = KC_LBRACKET; + break; + case KC_RIGHT_CURLY_BRACE: + regular_keycode = KC_RBRACKET; + break; + case KC_PIPE: + regular_keycode = KC_BSLASH; + break; + case KC_COLON: + regular_keycode = KC_SCOLON; + break; + case KC_DOUBLE_QUOTE: + regular_keycode = KC_QUOTE; + break; + case KC_LEFT_ANGLE_BRACKET: + regular_keycode = KC_COMMA; + break; + case KC_RIGHT_ANGLE_BRACKET: + regular_keycode = KC_DOT; + break; + case KC_QUESTION: + regular_keycode = KC_SLASH; + break; + default: + is_US_ANSI_shifted = false; + } + if (is_US_ANSI_shifted) { + if (in) { + register_code(KC_LSFT); + register_code(regular_keycode); + } else { + unregister_code(regular_keycode); + unregister_code(KC_LSFT); + } + } + return is_US_ANSI_shifted; +} + +void key_in(int16_t keycode) { + if (command_mode == 1 && command_ind < COMMAND_MAX_LENGTH) { + command_buffer[command_ind] = keycode; + command_ind++; + a_key_went_through = true; + } else if (in_leader_mode && leader_ind < LEADER_MAX_LENGTH) { + leader_buffer[leader_ind] = keycode; + leader_ind++; + a_key_went_through = true; + } else if (dynamic_macro_mode && dynamic_macro_ind < DYNAMIC_MACRO_MAX_LENGTH) { + dynamic_macro_buffer[dynamic_macro_ind] = keycode; + dynamic_macro_ind++; + a_key_went_through = true; + } else { + if (!handle_US_ANSI_shifted_keys(keycode, true)) { + register_code(keycode); + } + send_keyboard_report(); + a_key_went_through = true; + } +} + +void key_out(int16_t keycode) { + if (command_mode == 0) { + if (!handle_US_ANSI_shifted_keys(keycode, false)) { + if (command_mode == 0 && in_leader_mode == false && dynamic_macro_mode == false) { + unregister_code(keycode); + } + } + send_keyboard_report(); + } +} + +void tap_key(int16_t keycode) { + key_in(keycode); + wait_ms(TAP_TIMEOUT); + key_out(keycode); +}
\ No newline at end of file diff --git a/users/dennytom/chording_engine/engine.part.2 b/users/dennytom/chording_engine/engine.part.2 new file mode 100644 index 000000000..91dcbb750 --- /dev/null +++ b/users/dennytom/chording_engine/engine.part.2 @@ -0,0 +1,323 @@ +void single_dance(const struct Chord* self) { + switch (*self->state) { + case ACTIVATED: + key_in(self->value1); + break; + case DEACTIVATED: + key_out(self->value1); + *self->state = IDLE; + break; + case RESTART: + key_out(self->value1); + break; + default: + break; + } +} + +void key_layer_dance(const struct Chord* self) { + switch (*self->state) { + case ACTIVATED: + current_pseudolayer = self->value2; + a_key_went_through = false; + break; + case DEACTIVATED: + case RESTART: + if (!a_key_went_through) { + tap_key(self->value1); + } + current_pseudolayer = self->pseudolayer; + *self->state = IDLE; // does not have effect if the state was RESTART + break; + default: + break; + } +} + +void key_mod_dance(const struct Chord* self) { + switch (*self->state) { + case ACTIVATED: + key_in(self->value2); + a_key_went_through = false; + break; + case DEACTIVATED: + case RESTART: + key_out(self->value2); + if (!a_key_went_through) { + tap_key(self->value1); + } + *self->state = IDLE; // does not have effect if the state was RESTART + break; + default: + break; + } +} + +void key_key_dance(const struct Chord* self) { + switch (*self->state) { + case ACTIVATED: + break; + case DEACTIVATED: + tap_key(self->value1); + *self->state = IDLE; + break; + case FINISHED: + case PRESS_FROM_ACTIVE: + key_in(self->value2); + break; + case RESTART: + key_out(self->value2); + break; + default: + break; + } +} + +void autoshift_dance_impl(const struct Chord* self) { + switch (*self->state) { + case ACTIVATED: + *self->counter = 0; + break; + case DEACTIVATED: + case RESTART: + tap_key(self->value1); + *self->state = IDLE; + break; + case FINISHED_FROM_ACTIVE: + if (*self->counter == (LONG_PRESS_MULTIPLIER - 2)) { + key_in(KC_LSFT); + tap_key(self->value1); + key_out(KC_LSFT); + *self->state = IDLE; + // the skip to IDLE is usually just a lag optimization, + // in this case it has a logic function, on a short + // press (still longer than a tap) the key does not get shifted + } else { + *self->counter += 1; + *self->state = PRESS_FROM_ACTIVE; + dance_timer = timer_read(); + } + break; + default: + break; + } +} + +void autoshift_dance(const struct Chord* self) { + if (autoshift_mode) { + autoshift_dance_impl(self); + } else { + single_dance(self); + } +} + +void autoshift_toggle(const struct Chord* self){ + if (*self->state == ACTIVATED) { + autoshift_mode = !autoshift_mode; + *self->state = IDLE; + } +} + +void temp_pseudolayer(const struct Chord* self) { + switch (*self->state) { + case ACTIVATED: + current_pseudolayer = self->value1; + break; + case DEACTIVATED: + current_pseudolayer = self->pseudolayer; + *self->state = IDLE; + break; + case RESTART: + current_pseudolayer = self->pseudolayer; + break; + default: + break; + } +} + +void temp_pseudolayer_alt(const struct Chord* self) { + switch (*self->state) { + case ACTIVATED: + current_pseudolayer = self->value1; + break; + case DEACTIVATED: + current_pseudolayer = self->value2; + *self->state = IDLE; + break; + case RESTART: + current_pseudolayer = self->value2; + break; + default: + break; + } +} + +void perm_pseudolayer(const struct Chord* self) { + if (*self->state == ACTIVATED) { + current_pseudolayer = self->value1; + *self->state = IDLE; + } +} + +void switch_layer(const struct Chord* self) { + if (*self->state == ACTIVATED) { + layer_move(self->value1); + *self->state = IDLE; + } +} + +void lock(const struct Chord* self) { + if (*self->state == ACTIVATED) { + lock_next = true; + *self->state = IDLE; + } +} + +void one_shot_key(const struct Chord* self) { + switch (*self->state) { + case ACTIVATED: + break; + case DEACTIVATED: + key_in(self->value1); + *self->state = IN_ONE_SHOT; + break; + case FINISHED: + case PRESS_FROM_ACTIVE: + key_in(self->value1); + a_key_went_through = false; + break; + case RESTART: + if (a_key_went_through) { + key_out(self->value1); + } else { + *self->state = IN_ONE_SHOT; + } + default: + break; + } +} + +void one_shot_layer(const struct Chord* self) { + switch (*self->state) { + case ACTIVATED: + break; + case DEACTIVATED: + current_pseudolayer = self->value1; + *self->state = IN_ONE_SHOT; + break; + case FINISHED: + case PRESS_FROM_ACTIVE: + current_pseudolayer = self->value1; + a_key_went_through = false; + break; + case RESTART: + if (a_key_went_through) { + current_pseudolayer = self->pseudolayer; + } else { + *self->state = IN_ONE_SHOT; + } + default: + break; + } +} + +void command(const struct Chord* self) { + if (*self->state == ACTIVATED) { + command_mode++; + *self->state = IDLE; + } +} + +bool identical(uint16_t* buffer1, uint16_t* buffer2) { + bool same = true; + for (int i = 0; i < LEADER_MAX_LENGTH; i++) { + same = same && (buffer1[i] == buffer2[i]); + } + return same; +} + +void leader(const struct Chord* self) { + if (*self->state == ACTIVATED) { + in_leader_mode = true; + *self->state = IDLE; + } +} + +void dynamic_macro_record(const struct Chord* self) { + if (*self->state == ACTIVATED) { + for (int i = 0; i < DYNAMIC_MACRO_MAX_LENGTH; i++) { + dynamic_macro_buffer[i] = 0; + } + dynamic_macro_mode = true; + *self->state = IDLE; + } +} + +void dynamic_macro_next(const struct Chord* self) { + if (*self->state == ACTIVATED) { + if (dynamic_macro_mode && dynamic_macro_ind < DYNAMIC_MACRO_MAX_LENGTH) { + dynamic_macro_buffer[dynamic_macro_ind] = 0; + dynamic_macro_ind++; + } + *self->state = IDLE; + } +} + +void dynamic_macro_end(const struct Chord* self) { + if (*self->state == ACTIVATED) { + if (dynamic_macro_mode) { + dynamic_macro_mode = false; + } + *self->state = IDLE; + } +} + +void dynamic_macro_play(const struct Chord* self) { + if (*self->state == ACTIVATED) { + int ind_start = 0; + while (ind_start < DYNAMIC_MACRO_MAX_LENGTH) { + for (int i = ind_start; i < DYNAMIC_MACRO_MAX_LENGTH; i++) { + if (dynamic_macro_buffer[i] == 0) { + break; + } + register_code(dynamic_macro_buffer[i]); + } + send_keyboard_report(); + wait_ms(TAP_TIMEOUT); + for (int i = ind_start; i < DYNAMIC_MACRO_MAX_LENGTH; i++) { + if (dynamic_macro_buffer[i] == 0) { + ind_start = i + 1; + break; + } + unregister_code(dynamic_macro_buffer[i]); + } + send_keyboard_report(); + } + *self->state = IDLE; + } +} + +void string_in(const struct Chord* self) { + if (*self->state == ACTIVATED) { + char buffer[STRING_MAX_LENGTH]; + strcpy_P(buffer, (char*)pgm_read_word(&(strings[self->value1]))); + send_string(buffer); + } +} + +void clear(const struct Chord* self); + +void reset_keyboard_kb(void){ +#ifdef WATCHDOG_ENABLE + MCUSR = 0; + wdt_disable(); + wdt_reset(); +#endif + reset_keyboard(); +} + +void reset(const struct Chord* self) { + if (*self->state == ACTIVATED) { + reset_keyboard_kb(); + } +} diff --git a/users/dennytom/chording_engine/engine.part.3 b/users/dennytom/chording_engine/engine.part.3 new file mode 100644 index 000000000..cf19008ab --- /dev/null +++ b/users/dennytom/chording_engine/engine.part.3 @@ -0,0 +1,404 @@ +bool are_hashed_keycodes_in_sound(HASH_TYPE keycodes_hash, HASH_TYPE sound) { + return (keycodes_hash & sound) == keycodes_hash; +} + +uint8_t keycode_to_index(uint16_t keycode) { + return keycode - FIRST_INTERNAL_KEYCODE; +} + +void sound_keycode_array(uint16_t keycode) { + uint8_t index = keycode_to_index(keycode); + keycode_index++; + keycodes_buffer_array[index] = keycode_index; +} + +void silence_keycode_hash_array(HASH_TYPE keycode_hash) { + for (int i = 0; i < NUMBER_OF_KEYS; i++) { + bool index_in_hash = ((HASH_TYPE) 1 << i) & keycode_hash; + if (index_in_hash) { + uint8_t current_val = keycodes_buffer_array[i]; + keycodes_buffer_array[i] = 0; + for (int j = 0; j < NUMBER_OF_KEYS; j++) { + if (keycodes_buffer_array[j] > current_val) { + keycodes_buffer_array[j]--; + } + } + keycode_index--; + } + } +} + +bool are_hashed_keycodes_in_array(HASH_TYPE keycode_hash) { + for (int i = 0; i < NUMBER_OF_KEYS; i++) { + bool index_in_hash = ((HASH_TYPE) 1 << i) & keycode_hash; + bool index_in_array = (bool) keycodes_buffer_array[i]; + if (index_in_hash && !index_in_array) { + return false; + } + } + return true; +} + +void kill_one_shots(void) { + struct Chord chord_storage; + struct Chord* chord_ptr; + struct Chord* chord; + + for (int i = 0; i < NUMBER_OF_CHORDS; i++) { + chord_ptr = (struct Chord*) pgm_read_word (&list_of_chords[i]); + memcpy_P(&chord_storage, chord_ptr, sizeof(struct Chord)); + chord = &chord_storage; + + if (*chord->state == IN_ONE_SHOT) { + *chord->state = RESTART; + chord->function(chord); + if (*chord->state == RESTART) { + *chord->state = IDLE; + } + } + } +} + +void process_finished_dances(void) { + struct Chord chord_storage; + struct Chord* chord_ptr; + struct Chord* chord; + + for (int i = 0; i < NUMBER_OF_CHORDS; i++) { + chord_ptr = (struct Chord*) pgm_read_word (&list_of_chords[i]); + memcpy_P(&chord_storage, chord_ptr, sizeof(struct Chord)); + chord = &chord_storage; + + if (*chord->state == ACTIVATED) { + *chord->state = PRESS_FROM_ACTIVE; + chord->function(chord); + if (a_key_went_through) { + kill_one_shots(); + } + dance_timer = timer_read(); + } else if (*chord->state == IDLE_IN_DANCE) { + *chord->state = FINISHED; + chord->function(chord); + if (*chord->state == FINISHED) { + *chord->state = RESTART; + if (*chord->state == RESTART) { + *chord->state = IDLE; + } + } + } else if (*chord->state == PRESS_FROM_ACTIVE) { + *chord->state = FINISHED_FROM_ACTIVE; + chord->function(chord); + if (a_key_went_through) { + kill_one_shots(); + } + dance_timer = timer_read(); + } + } +} + +uint8_t keycodes_buffer_array_min(uint8_t* first_keycode_index) { + for (int i = 0; i < NUMBER_OF_KEYS; i++) { + if (keycodes_buffer_array[i] == 1) { + if (first_keycode_index != NULL) { + *first_keycode_index = (uint8_t) i; + } + return 1; + } + } + return 0; +} + +void remove_subchords(void) { + struct Chord chord_storage; + struct Chord* chord_ptr; + struct Chord* chord; + + for (int i = 0; i < NUMBER_OF_CHORDS; i++) { + chord_ptr = (struct Chord*) pgm_read_word (&list_of_chords[i]); + memcpy_P(&chord_storage, chord_ptr, sizeof(struct Chord)); + chord = &chord_storage; + + if (!(*chord->state == READY || *chord->state == READY_IN_DANCE || *chord->state == READY_LOCKED)) { + continue; + } + + struct Chord chord_storage_2; + struct Chord* chord_ptr_2; + struct Chord* chord_2; + for (int j = 0; j < NUMBER_OF_CHORDS; j++) { + if (i == j) {continue;} + + chord_ptr_2 = (struct Chord*) pgm_read_word (&list_of_chords[j]); + memcpy_P(&chord_storage_2, chord_ptr_2, sizeof(struct Chord)); + chord_2 = &chord_storage_2; + + if (are_hashed_keycodes_in_sound(chord_2->keycodes_hash, chord->keycodes_hash)) { + if (*chord_2->state == READY) { + *chord_2->state = IDLE; + } + if (*chord_2->state == READY_IN_DANCE) { + *chord_2->state = IDLE_IN_DANCE; + } + if (*chord_2->state == READY_LOCKED) { + *chord_2->state = LOCKED; + } + } + } + } +} + +void process_ready_chords(void) { + uint8_t first_keycode_index = 0; + while (keycodes_buffer_array_min(&first_keycode_index)) { + // find ready chords + struct Chord chord_storage; + struct Chord* chord_ptr; + struct Chord* chord; + + for (int i = 0; i < NUMBER_OF_CHORDS; i++) { + chord_ptr = (struct Chord*) pgm_read_word (&list_of_chords[i]); + memcpy_P(&chord_storage, chord_ptr, sizeof(struct Chord)); + chord = &chord_storage; + + // if the chord does not contain the first keycode + bool contains_first_keycode = ((uint32_t) 1 << first_keycode_index) & chord->keycodes_hash; + if (!contains_first_keycode) { + continue; + } + + if (!are_hashed_keycodes_in_array(chord->keycodes_hash)){ + continue; + } + + if (*chord->state == LOCKED) { + *chord->state = READY_LOCKED; + continue; + } + + if (!(chord->pseudolayer == current_pseudolayer || chord->pseudolayer == ALWAYS_ON)) { + continue; + } + + if (*chord->state == IDLE) { + *chord->state = READY; + continue; + } + + if (*chord->state == IDLE_IN_DANCE) { + *chord->state = READY_IN_DANCE; + } + } + + // remove subchords + remove_subchords(); + + // execute logic + // this should be only one chord + for (int i = 0; i < NUMBER_OF_CHORDS; i++) { + chord_ptr = (struct Chord*) pgm_read_word (&list_of_chords[i]); + memcpy_P(&chord_storage, chord_ptr, sizeof(struct Chord)); + chord = &chord_storage; + + if (*chord->state == READY_LOCKED) { + *chord->state = RESTART; + chord->function(chord); + if (*chord->state == RESTART) { + *chord->state = IDLE; + } + break; + } + + if (*chord->state == READY || *chord->state == READY_IN_DANCE) { + if (last_chord && last_chord != chord) { + process_finished_dances(); + } + + bool lock_next_prev_state = lock_next; + + *chord->state = ACTIVATED; + chord->function(chord); + dance_timer = timer_read(); + + if (lock_next && lock_next == lock_next_prev_state) { + lock_next = false; + *chord->state = PRESS_FROM_ACTIVE; + chord->function(chord); + if (*chord->state == PRESS_FROM_ACTIVE) { + *chord->state = LOCKED; + } + if (a_key_went_through) { + kill_one_shots(); + } + } + break; + } + } + + // silence notes + silence_keycode_hash_array(chord->keycodes_hash); + } +} + +void deactivate_active_chords(uint16_t keycode) { + HASH_TYPE hash = (HASH_TYPE)1 << (keycode - SAFE_RANGE); + bool broken; + struct Chord chord_storage; + struct Chord* chord_ptr; + struct Chord* chord; + + for (int i = 0; i < NUMBER_OF_CHORDS; i++) { + chord_ptr = (struct Chord*) pgm_read_word (&list_of_chords[i]); + memcpy_P(&chord_storage, chord_ptr, sizeof(struct Chord)); + chord = &chord_storage; + + broken = are_hashed_keycodes_in_sound(hash, chord->keycodes_hash); + if (!broken) { + continue; + } + + switch (*chord->state) { + case ACTIVATED: + *chord->state = DEACTIVATED; + chord->function(chord); + + if (*chord->state == DEACTIVATED) { + dance_timer = timer_read(); + *chord->state = IDLE_IN_DANCE; + } + if (*chord->state != IN_ONE_SHOT) { + kill_one_shots(); + } + break; + case PRESS_FROM_ACTIVE: + case FINISHED_FROM_ACTIVE: + *chord->state = RESTART; + chord->function(chord); + if (*chord->state == RESTART) { + *chord->state = IDLE; + } + kill_one_shots(); + break; + default: + break; + } + } + +} + +void process_command(void) { + command_mode = 0; + for (int i = 0; i < COMMAND_MAX_LENGTH; i++) { + if (command_buffer[i]) { + register_code(command_buffer[i]); + } + send_keyboard_report(); + } + wait_ms(TAP_TIMEOUT); + for (int i = 0; i < COMMAND_MAX_LENGTH; i++) { + if (command_buffer[i]) { + unregister_code(command_buffer[i]); + } + send_keyboard_report(); + } + for (int i = 0; i < COMMAND_MAX_LENGTH; i++) { + command_buffer[i] = 0; + } + command_ind = 0; +} + +void process_leader(void) { + in_leader_mode = false; + for (int i = 0; i < NUMBER_OF_LEADER_COMBOS; i++) { + uint16_t trigger[LEADER_MAX_LENGTH]; + memcpy_P(trigger, leader_triggers[i], LEADER_MAX_LENGTH * sizeof(uint16_t)); + + if (identical(leader_buffer, trigger)) { + (*leader_functions[i])(); + break; + } + } + for (int i = 0; i < LEADER_MAX_LENGTH; i++) { + leader_buffer[i] = 0; + } +} + +bool process_record_user(uint16_t keycode, keyrecord_t *record) { + if (keycode < FIRST_INTERNAL_KEYCODE || keycode > LAST_INTERNAL_KEYCODE) { + return true; + } + + if (record->event.pressed) { + sound_keycode_array(keycode); + } else { + process_ready_chords(); + deactivate_active_chords(keycode); + } + chord_timer = timer_read(); + leader_timer = timer_read(); + + return false; +} + +void matrix_scan_user(void) { + bool chord_timer_expired = timer_elapsed(chord_timer) > CHORD_TIMEOUT; + if (chord_timer_expired && keycodes_buffer_array_min(NULL)) { + process_ready_chords(); + } + + bool dance_timer_expired = timer_elapsed(dance_timer) > DANCE_TIMEOUT; + if (dance_timer_expired) { // would love to have && in_dance but not sure how + process_finished_dances(); + } + + bool in_command_mode = command_mode == 2; + if (in_command_mode) { + process_command(); + } + + bool leader_timer_expired = timer_elapsed(leader_timer) > LEADER_TIMEOUT; + if (leader_timer_expired && in_leader_mode) { + process_leader(); + } + +} + +void clear(const struct Chord* self) { + if (*self->state == ACTIVATED) { + // kill all chords + struct Chord chord_storage; + struct Chord* chord_ptr; + struct Chord* chord; + + for (int i = 0; i < NUMBER_OF_CHORDS; i++) { + chord_ptr = (struct Chord*) pgm_read_word (&list_of_chords[i]); + memcpy_P(&chord_storage, chord_ptr, sizeof(struct Chord)); + chord = &chord_storage; + + *chord->state = IDLE; + + if (chord->counter) { + *chord->counter = 0; + } + } + + // clear keyboard + clear_keyboard(); + send_keyboard_report(); + + // switch to default pseudolayer + current_pseudolayer = DEFAULT_PSEUDOLAYER; + + // clear all keyboard states + lock_next = false; + autoshift_mode = true; + command_mode = 0; + in_leader_mode = false; + leader_ind = 0; + dynamic_macro_mode = false; + a_key_went_through = false; + + for (int i = 0; i < DYNAMIC_MACRO_MAX_LENGTH; i++) { + dynamic_macro_buffer[i] = 0; + } + } +}
\ No newline at end of file diff --git a/users/dennytom/chording_engine/keymap_def.schema.json b/users/dennytom/chording_engine/keymap_def.schema.json new file mode 100644 index 000000000..9f9a8c5cb --- /dev/null +++ b/users/dennytom/chording_engine/keymap_def.schema.json @@ -0,0 +1,337 @@ +{ + "definitions": {}, + "$schema": "http://json-schema.org/draft-07/schema#", + "type": "object", + "title": "The Keymap definition", + "required": [ + "keys", + "parameters", + "layers", + "chord_sets", + "pseudolayers", + "leader_sequences", + "extra_code", + "extra_dependencies" + ], + "properties": { + "keys": { + "type": "array", + "title": "The Internal Keycodes", + "description": "Name Keycodes for the Chording Engine. These can be any string except already valid QMK keycodes.", + "items": { + "type": "string", + "title": "Individual Keycode", + "examples": [ + "L1", + "R1", + "TOP1", + "THUMB_3", + "Bottom_6" + ] + } + }, + "parameters": { + "type": "object", + "title": "Keyboard Parameters", + "description": "Keyboard, user and layout specific parameters like timers, max length for buffers and default pseudolayer.", + "required": [ + "layout_function_name", + "chord_timeout", + "dance_timeout", + "leader_timeout", + "tap_timeout", + "command_max_length", + "leader_max_length", + "dynamic_macro_max_length", + "string_max_length", + "long_press_multiplier", + "default_pseudolayer" + ], + "properties": { + "layout_function_name": { + "type": "string", + "examples": [ + "LAYOUT_ginny", + "" + ] + }, + "chord_timeout": { + "type": "integer", + "title": "The Chord Timeout", + "description": "The time in ms you have to press additional keys before the engine assumes you finished pressing keys that are part of a chord.", + "default": 100 + }, + "dance_timeout": { + "type": "integer", + "title": "The Dance Timeout", + "description": "The time in ms you have to repeatedly activate a chord before the engine assumes you finished a dance.", + "default": 200 + }, + "leader_timeout": { + "type": "integer", + "title": "The Leader Timeout", + "description": "The time in ms you have to activate additional chords before the engine assumes you finished adding chords to a leader sequence.", + "default": 750 + }, + "tap_timeout": { + "type": "integer", + "title": "The Tap Timeout", + "description": "The time in ms you have to finish pressing a chord before the engine assumes that you didn't just tap it but are holding it down.", + "default": 50 + }, + "command_max_length": { + "type": "integer", + "title": "Command Max Length", + "description": "The maximum length for chords buffered in command mode.", + "default": 5 + }, + "leader_max_length": { + "type": "integer", + "title": "Leader Max Length", + "description": "The maximum length of leader sequences you can define in your keymap", + "default": 5 + }, + "dynamic_macro_max_length": { + "type": "integer", + "title": "Dynamic Macro Max Length", + "description": "The maximum number of chords (including Dynamic Macro Next) you can record to a Dynamic Macro", + "default": 20 + }, + "string_max_length": { + "type": "integer", + "title": "String Max Length", + "description": "The maximum length of a string that the STR() chord can accept.", + "default": 16 + }, + "long_press_multiplier": { + "type": "integer", + "title": "Long Press Multiplier", + "description": "How many times does the chord timer have expire before a chord is registered as not only held but in a long press (for example for Autoshift). Has to be integer.", + "default": 3 + }, + "default_pseudolayer": { + "type": "string", + "title": "Default Pseudolayer", + "description": "Which pseudolayer should be active when the keyboard starts / restarts.", + "examples": [ + "BASE", + "QWERTY" + ] + } + } + }, + "layers": { + "type": "array", + "title": "QMK Layers", + "description": "The layers QMK needs to know about. Can contain chording engine's internal keycodes or QMK's keycodes. Do not define chords here, those belong in pseudolayers.", + "minItems": 1, + "uniqueItems": false, + "items": { + "type": "object", + "title": "Individual Layers", + "required": [ + "type" + ], + "properties": { + "type": { + "type": "string", + "title": "Type of Individual Layers", + "description": "Auto layer fills all keycodes to be chording engine's internal keycodes, manual let's you place internal and QMK's keycodes however you wish.", + "examples": [ + "auto", + "manual" + ] + }, + "keycodes": { + "type": "array", + "title": "Individual Chord", + "description": "A list of of keys that need to be pressed to activate this chord", + "items": { + "type": "string", + "title": "Individual Keycodes", + "description": "A keycode that is a part of the individual chord. Has to be an internal keycode." + } + } + } + } + }, + "chord_sets": { + "type": "array", + "title": "Chord Sets Definitions", + "description": "Describes predefined sets of chords to ease defining a number of chords in a pseudolayer.", + "items": { + "type": "object", + "required": [ + "name", + "chords" + ], + "properties": { + "name": { + "type": "string", + "title": "Name of the Set", + "examples": [ + "rows", + "asetniop" + ] + }, + "chords": { + "type": "array", + "title": "Chords", + "description": "List of all chords in this set", + "minItems": 1, + "uniqueItems": true, + "items": { + "type": "array", + "title": "Individual Chord", + "description": "A list of of keys that need to be pressed to activate this chord", + "items": { + "type": "string", + "title": "Individual Keycodes", + "description": "A keycode that is a part of the individual chord. Has to be an internal keycode." + } + } + } + } + } + }, + "pseudolayers": { + "type": "array", + "title": "Pseudolayers", + "description": "The pseudolayers holding the chords to be processed by the chording engine.", + "minItems": 1, + "uniqueItems": true, + "items": { + "type": "object", + "required": [ + "name" + ], + "properties": { + "name": { + "type": "string", + "title": "Name of the Pseudolayer", + "default": null, + "examples": [ + "ALWAYS_ON", + "QWERTY" + ] + }, + "chords": { + "type": "array", + "title": "Chords", + "description": "List of chords belonging on the pseudolayer.", + "items": { + "type": "object", + "required": [ + "type" + ], + "properties": { + "type": { + "type": "string", + "title": "Type of the chord array", + "description": "Defines how this objects describes one or more chords.", + "examples": [ + "visual_array", + "visual", + "simple", + "chord_set" + ] + }, + "keys": { + "type": "array", + "title": "Subset of keys", + "description": "Subset of internal keycodes that will be used when defining the chords. For visual_array type only.", + "examples": [ + "[\"L1\", \"L2\", \"L3\", \"L4\", \"R1\", \"R2\", \"R3\", \"R4\"]" + ], + "items": { + "type": "string" + } + }, + "dictionary": { + "type": "array", + "title": "Dictionary", + "description": "A table. Each row defines in a visual way which keys have to be pressed and what is the desired outcome. For visual_array type only.", + "items": { + "type": "array", + "items": { + "type": "string", + "examples": [ + "[\"X"\, \" "\, \" "\, \"X"\, \"X"\, \" "\, \" "\, \"X"\, \"MO(BASE, NUM)\"] + ] + } + } + }, + "keycode": { + "type": "string", + "title": "Keycode", + "description": "A keycode to be assigned to the chord when it is registered. For simple and visual types only." + }, + "set": { + "type": "string", + "title": "Chord set", + "description": "Name of the chord set to be used. Has to be one already defined in the chord_sets array. For chord_set type only." + }, + "keycodes": { + "type": "array", + "title": "Keycodes", + "description": "List of keycodes to be assigned to each chord when it is registered. For set type only.", + "items": { + "type": "string" + } + }, + "chord": { + "type": "array", + "title": "Chord", + "description": "Array of \"X\"'s and \" \"'s that shows which keys have to be pressed to activate the chord. For visual type only ", + "items": { + "type": "string" + } + } + } + } + } + } + } + }, + "leader_sequences": { + "type": "array", + "items": { + "type": "object", + "properties": { + "name": { + "type": "string", + "examples": ["fn_L1"] + }, + "function": { + "type": "string", + "description": "C code the sequence should run. Instead of here, can be defined in extra_dependencies or extra_code", + "examples": ["void fn_L1(void) { SEND(KC_LCTL); SEND(KC_LALT); SEND(KC_DEL); }"] + }, + "sequence": { + "type": "array", + "items": { + "type": "string" + }, + "examples": [["KC_Q", "KC_Z"]] + } + } + } + }, + "extra_code": { + "type": "string", + "label": "Extra Code", + "description": "C code to be inserted into the generated keymap", + "examples": ["void fn_L1(void) {\n SEND(KC_LCTL);\n SEND(KC_LALT);\n SEND(KC_DEL);\n}\n"], + "default": "" + }, + "extra_dependencies": { + "type": "array", + "label": "Extra Dependencies", + "description": "List of files to be #include'd in the generated keymap", + "examples": [ + "[\"user_functions.c\"]" + ], + "default": "" + } + } +}
\ No newline at end of file diff --git a/users/dennytom/chording_engine/parser.py b/users/dennytom/chording_engine/parser.py new file mode 100644 index 000000000..b62cf007e --- /dev/null +++ b/users/dennytom/chording_engine/parser.py @@ -0,0 +1,231 @@ +#!/usr/bin/env python3 + +import json +from functools import reduce +from chord import * +import sys + +comma_separator = (lambda x, y: str(x) + ", " + str(y)) +string_sum = (lambda x, y: str(x) + " + " + str(y)) +newline_separator = (lambda x, y: str(x) + "\n" + str(y)) + +def add_includes(data): + output_buffer = "" + if not ("do_not_include_QMK" in data["parameters"] and data["parameters"]["do_not_include_QMK"] == True): + output_buffer += "#include QMK_KEYBOARD_H\n" + if len(data["extra_dependencies"]) > 0: + for dependecy in data["extra_dependencies"]: + output_buffer += '#include "' + dependecy + '"\n' + + return output_buffer + "\n" + +def add_parameters(data): + output_buffer = "" + + number_of_keys = len(data["keys"]) + if number_of_keys <= 8: + hash_type = "uint8_t" + elif number_of_keys <= 16: + hash_type = "uint16_t" + elif number_of_keys <= 32: + hash_type = "uint32_t" + elif number_of_keys <= 64: + hash_type = "uint64_t" + else: + raise Exception("The engine currently supports only up to 64 keys.") + + output_buffer += "#define CHORD_TIMEOUT " + str(data["parameters"]["chord_timeout"]) + "\n" + output_buffer += "#define DANCE_TIMEOUT " + str(data["parameters"]["dance_timeout"]) + "\n" + output_buffer += "#define LEADER_TIMEOUT " + str(data["parameters"]["leader_timeout"]) + "\n" + output_buffer += "#define TAP_TIMEOUT " + str(data["parameters"]["tap_timeout"]) + "\n" + output_buffer += "#define LONG_PRESS_MULTIPLIER " + str(data["parameters"]["long_press_multiplier"]) + "\n" + output_buffer += "#define DYNAMIC_MACRO_MAX_LENGTH " + str(data["parameters"]["dynamic_macro_max_length"]) + "\n" + output_buffer += "#define COMMAND_MAX_LENGTH " + str(data["parameters"]["command_max_length"]) + "\n" + output_buffer += "#define STRING_MAX_LENGTH " + str(data["parameters"]["string_max_length"]) + "\n" + output_buffer += "#define LEADER_MAX_LENGTH " + str(data["parameters"]["leader_max_length"]) + "\n" + output_buffer += "#define HASH_TYPE " + hash_type + "\n" + output_buffer += "#define NUMBER_OF_KEYS " + str(len(data["keys"])) + "\n" + output_buffer += "#define DEFAULT_PSEUDOLAYER " + data["parameters"]["default_pseudolayer"] + "\n" + + return output_buffer + "\n" + +def add_keycodes(data): + output_buffer = "" + + if not len(data["keys"]) == len(set(data["keys"])): + raise Exception("The keys must have unique names") + + for key, counter in zip(data["keys"], range(0, len(data["keys"]))): + output_buffer += "#define H_" + key + " ((HASH_TYPE) 1 << " + str(counter) + ")\n" + output_buffer += "\n" + + output_buffer += "enum internal_keycodes {\n" + output_buffer += " " + data["keys"][0] + " = SAFE_RANGE,\n" + output_buffer += " " + reduce(comma_separator, [key for key in data["keys"][1:]]) + ",\n" + output_buffer += " FIRST_INTERNAL_KEYCODE = " + data["keys"][0] + ",\n" + output_buffer += " LAST_INTERNAL_KEYCODE = " + data["keys"][-1] + "\n" + output_buffer += "};\n" + + return output_buffer + "\n" + +def add_pseudolayers(data): + output_buffer = "" + + if len(data["pseudolayers"]) == 0: + raise Exception("You didn't define any pseudolayers") + + if not len([pseudolayer["name"] for pseudolayer in data["pseudolayers"]]) == len(set([pseudolayer["name"] for pseudolayer in data["pseudolayers"]])): + raise Exception("The pseudolayers must have unique names") + + pseudolayers = data["pseudolayers"] + if not "ALWAYS_ON" in [layer["name"] for layer in pseudolayers]: + pseudolayers += [{"name": "ALWAYS_ON", "chords": []}] # the engine expects ALWAYS_ON to exist + + output_buffer += "enum pseudolayers {\n" + output_buffer += " " + reduce(comma_separator, [layer["name"] for layer in pseudolayers]) + "\n" + output_buffer += "};\n" + + return output_buffer + "\n" + +def add_layers(data): + output_buffer = "" + + output_buffer += "const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {\n" + for layer, counter in zip(data["layers"], range(0,len(data["layers"]))): + if layer["type"] == "auto": + output_buffer += " [" + str(counter) + "] = " + data["parameters"]["layout_function_name"] + "(" + reduce(comma_separator, [key for key in data["keys"]]) + "),\n" + else: + output_buffer += " [" + str(counter) + "] = " + data["parameters"]["layout_function_name"] + "(" + reduce(comma_separator, [key for key in layer["keycodes"]]) + "),\n" + output_buffer += "};\n" + output_buffer += "size_t keymapsCount = " + str(len(data["layers"])) + ";\n" + + return output_buffer + "\n" + +def prep_buffers(data): + output_buffer = "" + + output_buffer += "uint8_t keycodes_buffer_array[] = {\n" + output_buffer += " " + reduce(comma_separator, ["0"] * len(data["keys"])) + "\n" + output_buffer += "};\n" + output_buffer += "\n" + + output_buffer += "uint8_t command_buffer[] = {\n" + output_buffer += " " + reduce(comma_separator, ["0"] * data["parameters"]["command_max_length"]) + "\n" + output_buffer += "};\n" + output_buffer += "\n" + + output_buffer += "uint16_t leader_buffer[] = {\n" + output_buffer += " " + reduce(comma_separator, ["0"] * data["parameters"]["leader_max_length"]) + "\n" + output_buffer += "};\n" + output_buffer += "\n" + + output_buffer += "uint8_t dynamic_macro_buffer[] = {\n" + output_buffer += " " + reduce(comma_separator, ["0"] * data["parameters"]["dynamic_macro_max_length"]) + "\n" + output_buffer += "};" + + return output_buffer + "\n" + +def parse_keyboard_specifics(data): + keyboard_part_0 = add_includes(data) + keyboard_part_0 += add_keycodes(data) + keyboard_part_0 += add_pseudolayers(data) + keyboard_part_0 += add_parameters(data) + keyboard_part_0 += add_layers(data) + keyboard_part_0 += prep_buffers(data) + + return keyboard_part_0 + '\n' + +def parse_chords(data): + keyboard_part_2 = "" + strings = [] + number_of_strings = 0 + number_of_chords = 0 + + for pseudolayer in data["pseudolayers"]: + name = pseudolayer["name"] + for chord in pseudolayer["chords"]: + if chord["type"] == "chord_set": + keycodes = reduce(comma_separator, [word for word in chord["keycodes"]]) + [keyboard_part_2, number_of_chords, number_of_strings, strings] = add_chord_set(name, keycodes, chord["set"], data, keyboard_part_2, number_of_chords, number_of_strings, strings) + if chord["type"] == "visual_array": + [keyboard_part_2, number_of_chords, number_of_strings, strings] = add_dictionary(name, chord["keys"], chord["dictionary"], keyboard_part_2, number_of_chords, number_of_strings, strings) + if chord["type"] == "visual": + keycodes = reduce(comma_separator, [word for word in chord["chord"]]) + [keyboard_part_2, number_of_chords, number_of_strings, strings] = secret_chord(name, chord["keycode"], keycodes, data, keyboard_part_2, number_of_chords, number_of_strings, strings) + elif chord["type"] == "simple": + keycodes = reduce(string_sum, ["H_" + word for word in chord["chord"]]) + [keyboard_part_2, number_of_chords, number_of_strings, strings] = add_key(name, keycodes, chord["keycode"], keyboard_part_2, number_of_chords, number_of_strings, strings) + keyboard_part_2 += "\n" + + keyboard_part_2 += "const struct Chord* const list_of_chords[] PROGMEM = {\n" + keyboard_part_2 += " " + reduce(comma_separator, ["&chord_" + str(i) for i in range(0, number_of_chords)]) + "\n" + keyboard_part_2 += "};\n" + keyboard_part_2 += "\n" + + if len(data["leader_sequences"]) > 0: + keyboard_part_2 += reduce(newline_separator, [sequence["function"] for sequence in data["leader_sequences"]]) + "\n\n" + keyboard_part_2 += "const uint16_t leader_triggers[][LEADER_MAX_LENGTH] PROGMEM = {\n" + for sequence in data["leader_sequences"]: + keyboard_part_2 += " {" + reduce(comma_separator, sequence["sequence"] + ["0"] * (data["parameters"]["leader_max_length"] - len(sequence["sequence"]))) + "},\n" + keyboard_part_2 += "};\n\n" + keyboard_part_2 += "void (*leader_functions[]) (void) = {\n" + keyboard_part_2 += " " + reduce(comma_separator, ["&" + sequence["name"] for sequence in data["leader_sequences"]]) + "\n" + keyboard_part_2 += "};\n" + else: + keyboard_part_2 += "const uint16_t** const leader_triggers PROGMEM = NULL;\n" + keyboard_part_2 += "void (*leader_functions[]) (void) = {};\n" + keyboard_part_2 += "\n" + + keyboard_part_2 += "#define NUMBER_OF_CHORDS " + str(number_of_chords) + "\n" + keyboard_part_2 += "#define NUMBER_OF_LEADER_COMBOS " + str(len(data["leader_sequences"])) + + return keyboard_part_2 + "\n\n" + +def parse_strings_for_chords(data): + keyboard_part_1 = "" + + for string, i in zip(strings, range(0, len(strings))): + keyboard_part_1 += "const char string_" + str(i) + " [] PROGMEM = \"" + string + "\";\n" + + keyboard_part_1 += "\n" + keyboard_part_1 += "const char * const strings[] PROGMEM = {\n" + if len(strings) > 0: + keyboard_part_1 += " " + reduce(comma_separator, ["string_" + str(i) for i in range(0, len(strings))]) + keyboard_part_1 += "\n};\n" + + return keyboard_part_1 + +def main(): + if len(sys.argv) != 3: + raise Exception("Wrong number of arguments.\n\nUsage: python parser.py keymap.json keymap.c") + + input_filepath = sys.argv[1] + output_filepath = sys.argv[2] + + with open(input_filepath, "r") as read_file: + data = json.load(read_file) + + keyboard_part_0 = parse_keyboard_specifics(data) + keyboard_part_1 = parse_strings_for_chords(data) + keyboard_part_2 = parse_chords(data) + + engine_part_1 = open("engine.part.1", "r").read() + engine_part_2 = open("engine.part.2", "r").read() + "\n" + engine_part_3 = open("engine.part.3", "r").read() + + output_buffer = keyboard_part_0 + output_buffer += engine_part_1 + + if len(data["extra_code"]) > 0: + output_buffer += data["extra_code"] + "\n" + + output_buffer += keyboard_part_1 + output_buffer += engine_part_2 + output_buffer += keyboard_part_2 + output_buffer += engine_part_3 + + with open(output_filepath, "w") as write_file: + write_file.write(output_buffer) + +if __name__ == "__main__": + main()
\ No newline at end of file diff --git a/users/dennytom/chording_engine/state_machine.dot b/users/dennytom/chording_engine/state_machine.dot new file mode 100644 index 000000000..431e6f69b --- /dev/null +++ b/users/dennytom/chording_engine/state_machine.dot @@ -0,0 +1,49 @@ +digraph { + IDLE + READY + ACTIVATED + DEACTIVATED + PRESS_FROM_ACTIVE + FINISHED_FROM_ACTIVE + IDLE_IN_DANCE + READY_IN_DANCE + FINISHED + LOCKED + READY_LOCKED + RESTART + IN_ONE_SHOT + + // common + FINISHED -> RESTART; + RESTART -> IDLE; + DEACTIVATED -> IDLE_IN_DANCE; + + // kill_one_shots() + IN_ONE_SHOT -> RESTART [label="non-one-shot key went through", color="blue"]; + + // process_finished_dances() + ACTIVATED -> PRESS_FROM_ACTIVE [label="dance timer", color="green"]; + IDLE_IN_DANCE -> FINISHED [label="dance timer", color="green"]; + PRESS_FROM_ACTIVE -> FINISHED_FROM_ACTIVE [label="dance timer", color="green"]; + + // remove_subchords() + READY -> IDLE [label="superchord active", color="red"]; + READY_IN_DANCE -> IDLE_IN_DANCE [label="superchord active", color="red"]; + READY_LOCKED -> LOCKED [label="superchord active", color="red"]; + + // process_ready_chords() + LOCKED -> READY_LOCKED [label="all keys pressed", color="orange"]; + IDLE -> READY [label="all keys pressed", color="orange"]; + IDLE_IN_DANCE -> READY_IN_DANCE [label="all keys pressed", color="orange"]; + + READY_LOCKED -> RESTART [label="chord timer", color="orange"]; + READY -> ACTIVATED [label="chord timer", color="orange"]; + READY_IN_DANCE -> ACTIVATED [label="chord timer", color="orange"]; + ACTIVATED -> PRESS_FROM_ACTIVE [label="lock next", color="orange"]; + PRESS_FROM_ACTIVE -> LOCKED [label="lock next", color="orange"]; + + // deactivate_active_chords() + ACTIVATED -> DEACTIVATED [label="a key lifted", color="purple"]; + PRESS_FROM_ACTIVE -> RESTART [label="a key lifted", color="orange"]; + FINISHED_FROM_ACTIVE -> DEACTIVATED [label="a key lifted", color="orange"]; +}
\ No newline at end of file diff --git a/users/dennytom/chording_engine/state_machine.svg b/users/dennytom/chording_engine/state_machine.svg new file mode 100644 index 000000000..773168988 --- /dev/null +++ b/users/dennytom/chording_engine/state_machine.svg @@ -0,0 +1,235 @@ +<?xml version="1.0" encoding="UTF-8" standalone="no"?> +<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" + "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"> +<!-- Generated by graphviz version 2.40.1 (20161225.0304) + --> +<!-- Title: %0 Pages: 1 --> +<svg width="829pt" height="754pt" + viewBox="0.00 0.00 829.35 754.40" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"> +<g id="graph0" class="graph" transform="scale(1 1) rotate(0) translate(4 750.4)"> +<title>%0</title> +<polygon fill="#ffffff" stroke="transparent" points="-4,4 -4,-750.4 825.3486,-750.4 825.3486,4 -4,4"/> +<!-- IDLE --> +<g id="node1" class="node"> +<title>IDLE</title> +<ellipse fill="none" stroke="#000000" cx="492" cy="-728.4" rx="33.043" ry="18"/> +<text text-anchor="middle" x="492" y="-724.2" font-family="Times,serif" font-size="14.00" fill="#000000">IDLE</text> +</g> +<!-- READY --> +<g id="node2" class="node"> +<title>READY</title> +<ellipse fill="none" stroke="#000000" cx="404" cy="-639.6" rx="44.0814" ry="18"/> +<text text-anchor="middle" x="404" y="-635.4" font-family="Times,serif" font-size="14.00" fill="#000000">READY</text> +</g> +<!-- IDLE->READY --> +<g id="edge12" class="edge"> +<title>IDLE->READY</title> +<path fill="none" stroke="#ffa500" d="M487.6897,-710.0931C484.347,-699.0742 478.772,-685.3407 470,-675.6 463.1872,-668.0349 454.3989,-661.7377 445.4714,-656.6382"/> +<polygon fill="#ffa500" stroke="#ffa500" points="447.0426,-653.5099 436.5654,-651.9352 443.7738,-659.6998 447.0426,-653.5099"/> +<text text-anchor="middle" x="524.317" y="-679.8" font-family="Times,serif" font-size="14.00" fill="#000000">all keys pressed</text> +</g> +<!-- READY->IDLE --> +<g id="edge8" class="edge"> +<title>READY->IDLE</title> +<path fill="none" stroke="#ff0000" d="M383.1899,-655.7528C371.9777,-666.4401 362.0803,-680.6044 370.88,-692.4 380.5143,-705.3144 418.9714,-715.2612 450.0017,-721.3939"/> +<polygon fill="#ff0000" stroke="#ff0000" points="449.7109,-724.9011 460.188,-723.3263 451.0156,-718.0238 449.7109,-724.9011"/> +<text text-anchor="middle" x="420.56" y="-679.8" font-family="Times,serif" font-size="14.00" fill="#000000">superchord active</text> +</g> +<!-- ACTIVATED --> +<g id="node3" class="node"> +<title>ACTIVATED</title> +<ellipse fill="none" stroke="#000000" cx="404" cy="-550.8" rx="66.0512" ry="18"/> +<text text-anchor="middle" x="404" y="-546.6" font-family="Times,serif" font-size="14.00" fill="#000000">ACTIVATED</text> +</g> +<!-- READY->ACTIVATED --> +<g id="edge15" class="edge"> +<title>READY->ACTIVATED</title> +<path fill="none" stroke="#ffa500" d="M404,-621.2006C404,-609.0949 404,-593.0076 404,-579.2674"/> +<polygon fill="#ffa500" stroke="#ffa500" points="407.5001,-578.872 404,-568.872 400.5001,-578.8721 407.5001,-578.872"/> +<text text-anchor="middle" x="436.4611" y="-591" font-family="Times,serif" font-size="14.00" fill="#000000">chord timer</text> +</g> +<!-- DEACTIVATED --> +<g id="node4" class="node"> +<title>DEACTIVATED</title> +<ellipse fill="none" stroke="#000000" cx="301" cy="-284.4" rx="79.2922" ry="18"/> +<text text-anchor="middle" x="301" y="-280.2" font-family="Times,serif" font-size="14.00" fill="#000000">DEACTIVATED</text> +</g> +<!-- ACTIVATED->DEACTIVATED --> +<g id="edge19" class="edge"> +<title>ACTIVATED->DEACTIVATED</title> +<path fill="none" stroke="#a020f0" d="M414.457,-532.6765C417.1337,-527.1214 419.6389,-520.8699 421,-514.8 426.1765,-491.7151 456.5634,-425.2497 416,-355.2 402.3607,-331.646 377.4429,-314.8951 354.2805,-303.6207"/> +<polygon fill="#a020f0" stroke="#a020f0" points="355.7259,-300.433 345.1798,-299.4173 352.7907,-306.7879 355.7259,-300.433"/> +<text text-anchor="middle" x="468.4881" y="-413.4" font-family="Times,serif" font-size="14.00" fill="#000000">a key lifted</text> +</g> +<!-- PRESS_FROM_ACTIVE --> +<g id="node5" class="node"> +<title>PRESS_FROM_ACTIVE</title> +<ellipse fill="none" stroke="#000000" cx="279" cy="-462" rx="111.797" ry="18"/> +<text text-anchor="middle" x="279" y="-457.8" font-family="Times,serif" font-size="14.00" fill="#000000">PRESS_FROM_ACTIVE</text> +</g> +<!-- ACTIVATED->PRESS_FROM_ACTIVE --> +<g id="edge5" class="edge"> +<title>ACTIVATED->PRESS_FROM_ACTIVE</title> +<path fill="none" stroke="#00ff00" d="M381.0189,-533.8085C363.6663,-521.0049 341.3076,-504.5706 332,-498 326.1999,-493.9054 319.9973,-489.6147 313.9535,-485.4802"/> +<polygon fill="#00ff00" stroke="#00ff00" points="315.5139,-482.3085 305.2776,-479.5755 311.5753,-488.0954 315.5139,-482.3085"/> +<text text-anchor="middle" x="387.8454" y="-502.2" font-family="Times,serif" font-size="14.00" fill="#000000">dance timer</text> +</g> +<!-- ACTIVATED->PRESS_FROM_ACTIVE --> +<g id="edge17" class="edge"> +<title>ACTIVATED->PRESS_FROM_ACTIVE</title> +<path fill="none" stroke="#ffa500" d="M346.8191,-541.6158C317.1162,-535.5544 285.1886,-526.5544 276.2896,-514.8 271.0903,-507.9325 270.0671,-499.0047 270.857,-490.45"/> +<polygon fill="#ffa500" stroke="#ffa500" points="274.3462,-490.8071 272.5608,-480.3638 267.444,-489.6411 274.3462,-490.8071"/> +<text text-anchor="middle" x="302.8552" y="-502.2" font-family="Times,serif" font-size="14.00" fill="#000000">lock next</text> +</g> +<!-- IDLE_IN_DANCE --> +<g id="node7" class="node"> +<title>IDLE_IN_DANCE</title> +<ellipse fill="none" stroke="#000000" cx="293" cy="-195.6" rx="86.2367" ry="18"/> +<text text-anchor="middle" x="293" y="-191.4" font-family="Times,serif" font-size="14.00" fill="#000000">IDLE_IN_DANCE</text> +</g> +<!-- DEACTIVATED->IDLE_IN_DANCE --> +<g id="edge3" class="edge"> +<title>DEACTIVATED->IDLE_IN_DANCE</title> +<path fill="none" stroke="#000000" d="M299.3424,-266.0006C298.241,-253.775 296.7738,-237.4887 295.5279,-223.6599"/> +<polygon fill="#000000" stroke="#000000" points="299.0114,-223.3176 294.6281,-213.672 292.0396,-223.9458 299.0114,-223.3176"/> +</g> +<!-- FINISHED_FROM_ACTIVE --> +<g id="node6" class="node"> +<title>FINISHED_FROM_ACTIVE</title> +<ellipse fill="none" stroke="#000000" cx="280" cy="-373.2" rx="127.3672" ry="18"/> +<text text-anchor="middle" x="280" y="-369" font-family="Times,serif" font-size="14.00" fill="#000000">FINISHED_FROM_ACTIVE</text> +</g> +<!-- PRESS_FROM_ACTIVE->FINISHED_FROM_ACTIVE --> +<g id="edge7" class="edge"> +<title>PRESS_FROM_ACTIVE->FINISHED_FROM_ACTIVE</title> +<path fill="none" stroke="#00ff00" d="M279.2072,-443.6006C279.3435,-431.4949 279.5247,-415.4076 279.6794,-401.6674"/> +<polygon fill="#00ff00" stroke="#00ff00" points="283.1836,-401.3108 279.7965,-391.272 276.184,-401.2319 283.1836,-401.3108"/> +<text text-anchor="middle" x="312.8454" y="-413.4" font-family="Times,serif" font-size="14.00" fill="#000000">dance timer</text> +</g> +<!-- LOCKED --> +<g id="node10" class="node"> +<title>LOCKED</title> +<ellipse fill="none" stroke="#000000" cx="84" cy="-373.2" rx="50.38" ry="18"/> +<text text-anchor="middle" x="84" y="-369" font-family="Times,serif" font-size="14.00" fill="#000000">LOCKED</text> +</g> +<!-- PRESS_FROM_ACTIVE->LOCKED --> +<g id="edge18" class="edge"> +<title>PRESS_FROM_ACTIVE->LOCKED</title> +<path fill="none" stroke="#ffa500" d="M241.402,-444.8785C207.8161,-429.584 158.7472,-407.2387 124.3482,-391.574"/> +<polygon fill="#ffa500" stroke="#ffa500" points="125.6113,-388.3034 115.06,-387.3442 122.7102,-394.6739 125.6113,-388.3034"/> +<text text-anchor="middle" x="224.8552" y="-413.4" font-family="Times,serif" font-size="14.00" fill="#000000">lock next</text> +</g> +<!-- RESTART --> +<g id="node12" class="node"> +<title>RESTART</title> +<ellipse fill="none" stroke="#000000" cx="201" cy="-18" rx="53.9098" ry="18"/> +<text text-anchor="middle" x="201" y="-13.8" font-family="Times,serif" font-size="14.00" fill="#000000">RESTART</text> +</g> +<!-- PRESS_FROM_ACTIVE->RESTART --> +<g id="edge20" class="edge"> +<title>PRESS_FROM_ACTIVE->RESTART</title> +<path fill="none" stroke="#ffa500" d="M190.3393,-450.9993C108.6441,-438.3099 0,-413.8018 0,-373.2 0,-373.2 0,-373.2 0,-106.8 0,-74.7037 86.8374,-46.4364 146.3579,-30.8311"/> +<polygon fill="#ffa500" stroke="#ffa500" points="147.578,-34.1313 156.3916,-28.2515 145.835,-27.3517 147.578,-34.1313"/> +<text text-anchor="middle" x="31.4881" y="-235.8" font-family="Times,serif" font-size="14.00" fill="#000000">a key lifted</text> +</g> +<!-- FINISHED_FROM_ACTIVE->DEACTIVATED --> +<g id="edge21" class="edge"> +<title>FINISHED_FROM_ACTIVE->DEACTIVATED</title> +<path fill="none" stroke="#ffa500" d="M284.3512,-354.8006C287.2424,-342.575 291.0939,-326.2887 294.3642,-312.4599"/> +<polygon fill="#ffa500" stroke="#ffa500" points="297.8308,-313.0091 296.7262,-302.472 291.0187,-311.3981 297.8308,-313.0091"/> +<text text-anchor="middle" x="324.4881" y="-324.6" font-family="Times,serif" font-size="14.00" fill="#000000">a key lifted</text> +</g> +<!-- READY_IN_DANCE --> +<g id="node8" class="node"> +<title>READY_IN_DANCE</title> +<ellipse fill="none" stroke="#000000" cx="402" cy="-106.8" rx="97.2741" ry="18"/> +<text text-anchor="middle" x="402" y="-102.6" font-family="Times,serif" font-size="14.00" fill="#000000">READY_IN_DANCE</text> +</g> +<!-- IDLE_IN_DANCE->READY_IN_DANCE --> +<g id="edge13" class="edge"> +<title>IDLE_IN_DANCE->READY_IN_DANCE</title> +<path fill="none" stroke="#ffa500" d="M285.2465,-177.3414C281.8879,-166.3406 280.1649,-152.6093 287.366,-142.8 292.9374,-135.2107 308.8319,-128.4976 327.077,-122.9864"/> +<polygon fill="#ffa500" stroke="#ffa500" points="328.1338,-126.3246 336.79,-120.2157 326.2135,-119.5932 328.1338,-126.3246"/> +<text text-anchor="middle" x="332.317" y="-147" font-family="Times,serif" font-size="14.00" fill="#000000">all keys pressed</text> +</g> +<!-- FINISHED --> +<g id="node9" class="node"> +<title>FINISHED</title> +<ellipse fill="none" stroke="#000000" cx="201" cy="-106.8" rx="55.0323" ry="18"/> +<text text-anchor="middle" x="201" y="-102.6" font-family="Times,serif" font-size="14.00" fill="#000000">FINISHED</text> +</g> +<!-- IDLE_IN_DANCE->FINISHED --> +<g id="edge6" class="edge"> +<title>IDLE_IN_DANCE->FINISHED</title> +<path fill="none" stroke="#00ff00" d="M231.9976,-182.6889C220.3684,-177.4272 209.5756,-170.022 202.3092,-159.6 197.3878,-152.5413 195.8986,-143.5625 195.9866,-135.0199"/> +<polygon fill="#00ff00" stroke="#00ff00" points="199.4821,-135.2114 196.7515,-124.9745 192.5023,-134.6799 199.4821,-135.2114"/> +<text text-anchor="middle" x="235.8454" y="-147" font-family="Times,serif" font-size="14.00" fill="#000000">dance timer</text> +</g> +<!-- READY_IN_DANCE->ACTIVATED --> +<g id="edge16" class="edge"> +<title>READY_IN_DANCE->ACTIVATED</title> +<path fill="none" stroke="#ffa500" d="M460.9306,-121.2262C473.3432,-126.4214 485.5301,-133.4169 495,-142.8 512.9398,-160.5753 516,-170.3454 516,-195.6 516,-462 516,-462 516,-462 516,-495.4749 484.8997,-518.3284 455.476,-532.4591"/> +<polygon fill="#ffa500" stroke="#ffa500" points="453.6869,-529.4272 446.0233,-536.7429 456.5764,-535.803 453.6869,-529.4272"/> +<text text-anchor="middle" x="548.4611" y="-324.6" font-family="Times,serif" font-size="14.00" fill="#000000">chord timer</text> +</g> +<!-- READY_IN_DANCE->IDLE_IN_DANCE --> +<g id="edge9" class="edge"> +<title>READY_IN_DANCE->IDLE_IN_DANCE</title> +<path fill="none" stroke="#ff0000" d="M398.1917,-124.942C395.0071,-136.1761 389.421,-150.2021 380,-159.6 372.9696,-166.6132 364.3766,-172.3251 355.3586,-176.9585"/> +<polygon fill="#ff0000" stroke="#ff0000" points="353.8046,-173.8215 346.241,-181.2406 356.7803,-180.1575 353.8046,-173.8215"/> +<text text-anchor="middle" x="441.56" y="-147" font-family="Times,serif" font-size="14.00" fill="#000000">superchord active</text> +</g> +<!-- FINISHED->RESTART --> +<g id="edge1" class="edge"> +<title>FINISHED->RESTART</title> +<path fill="none" stroke="#000000" d="M201,-88.4006C201,-76.2949 201,-60.2076 201,-46.4674"/> +<polygon fill="#000000" stroke="#000000" points="204.5001,-46.072 201,-36.072 197.5001,-46.0721 204.5001,-46.072"/> +</g> +<!-- READY_LOCKED --> +<g id="node11" class="node"> +<title>READY_LOCKED</title> +<ellipse fill="none" stroke="#000000" cx="116" cy="-284.4" rx="88.0442" ry="18"/> +<text text-anchor="middle" x="116" y="-280.2" font-family="Times,serif" font-size="14.00" fill="#000000">READY_LOCKED</text> +</g> +<!-- LOCKED->READY_LOCKED --> +<g id="edge11" class="edge"> +<title>LOCKED->READY_LOCKED</title> +<path fill="none" stroke="#ffa500" d="M62.8266,-356.7262C52.2277,-346.3972 43.0571,-332.745 50.366,-320.4 53.6683,-314.8223 58.175,-310.0516 63.2916,-305.985"/> +<polygon fill="#ffa500" stroke="#ffa500" points="65.4987,-308.7148 71.7485,-300.1596 61.5278,-302.9501 65.4987,-308.7148"/> +<text text-anchor="middle" x="95.317" y="-324.6" font-family="Times,serif" font-size="14.00" fill="#000000">all keys pressed</text> +</g> +<!-- READY_LOCKED->LOCKED --> +<g id="edge10" class="edge"> +<title>READY_LOCKED->LOCKED</title> +<path fill="none" stroke="#ff0000" d="M134.3377,-302.181C142.7003,-312.508 149.4503,-325.708 143,-337.2 139.1182,-344.1159 133.3081,-349.8603 126.8241,-354.5772"/> +<polygon fill="#ff0000" stroke="#ff0000" points="124.8745,-351.6695 118.3451,-360.0132 128.6526,-357.5624 124.8745,-351.6695"/> +<text text-anchor="middle" x="195.56" y="-324.6" font-family="Times,serif" font-size="14.00" fill="#000000">superchord active</text> +</g> +<!-- READY_LOCKED->RESTART --> +<g id="edge14" class="edge"> +<title>READY_LOCKED->RESTART</title> +<path fill="none" stroke="#ffa500" d="M114.0574,-266.3175C111.0132,-230.457 108.1435,-148.6335 137,-88.8 145.6041,-70.9596 160.1275,-54.343 173.1661,-41.6972"/> +<polygon fill="#ffa500" stroke="#ffa500" points="175.702,-44.1178 180.6155,-34.7312 170.9209,-39.0049 175.702,-44.1178"/> +<text text-anchor="middle" x="152.4611" y="-147" font-family="Times,serif" font-size="14.00" fill="#000000">chord timer</text> +</g> +<!-- RESTART->IDLE --> +<g id="edge2" class="edge"> +<title>RESTART->IDLE</title> +<path fill="none" stroke="#000000" d="M251.8307,-24.1482C358.5139,-37.7418 596,-71.9196 596,-106.8 596,-639.6 596,-639.6 596,-639.6 596,-665.1965 591.0323,-674.2337 573,-692.4 561.7252,-703.7586 546.243,-711.8298 531.8327,-717.4099"/> +<polygon fill="#000000" stroke="#000000" points="530.3349,-714.2281 522.0925,-720.8849 532.6871,-720.8211 530.3349,-714.2281"/> +</g> +<!-- IN_ONE_SHOT --> +<g id="node13" class="node"> +<title>IN_ONE_SHOT</title> +<ellipse fill="none" stroke="#000000" cx="700" cy="-106.8" rx="75.8643" ry="18"/> +<text text-anchor="middle" x="700" y="-102.6" font-family="Times,serif" font-size="14.00" fill="#000000">IN_ONE_SHOT</text> +</g> +<!-- IN_ONE_SHOT->RESTART --> +<g id="edge4" class="edge"> +<title>IN_ONE_SHOT->RESTART</title> +<path fill="none" stroke="#0000ff" d="M676.9312,-89.3704C659.5151,-77.2164 634.5042,-61.8271 610,-54 547.5343,-34.0473 363.5933,-24.1795 265.053,-20.2067"/> +<polygon fill="#0000ff" stroke="#0000ff" points="265.0515,-16.704 254.9213,-19.8069 264.7755,-23.6986 265.0515,-16.704"/> +<text text-anchor="middle" x="733.6743" y="-58.2" font-family="Times,serif" font-size="14.00" fill="#000000">non-one-shot key went through</text> +</g> +</g> +</svg> diff --git a/users/dennytom/chording_engine/tests/minunit.h b/users/dennytom/chording_engine/tests/minunit.h new file mode 100644 index 000000000..ed71b6253 --- /dev/null +++ b/users/dennytom/chording_engine/tests/minunit.h @@ -0,0 +1,288 @@ +#define mu_assert(message, test) \ + do { \ + if (!(test)) { \ + return message; \ + } \ + } while (0) + +#define RED "\033[0;31m" +#define GREEN "\033[0;32m" +#define NC "\033[0m" + +enum ASSERT_TYPES { + UINT, + INT +}; + +#define BUFF_SIZE 1024 +char buffer[BUFF_SIZE]; + +#define ASSERT_EQ(type, actual, expected) \ + do { \ + if (actual != expected) { \ + switch (type) { \ + case UINT: \ + snprintf(buffer, BUFF_SIZE, "\nline %d\nvar %s\nactual = %u\nexpected = %u\n", __LINE__, #actual, actual, expected); \ + break; \ + case INT: \ + snprintf(buffer, BUFF_SIZE, "\nline %d\nvar %s\nactual = %d\nexpected = %d\n", __LINE__, #actual, actual, expected); \ + break; \ + default: \ + snprintf(buffer, BUFF_SIZE, "\nline %d\nunsupported ASSERT_EQ type\n", __LINE__); \ + break; \ + } \ + printf("%s\n", buffer); \ + passed = false; \ + all_passed = false; \ + } \ + } while (0) + +#include <stdio.h> +#include <stdint.h> +#include <stddef.h> +#include <stdbool.h> +#include <string.h> + +#define MATRIX_ROWS 2 +#define MATRIX_COLS 10 +#define LAYOUT_test( \ + k09, k08, k07, k06, k05, k04, k03, k02, k01, k00, \ + k19, k18, k17, k16, k15, k14, k13, k12, k11, k10 \ +) { \ + { k00, k01, k02, k03, k04, k05, k06, k07, k08, k09}, \ + { k10, k11, k12, k13, k14, k15, k16, k17, k18, k19}, \ +} + +#define PROGMEM +#define memcpy_P memcpy +const struct Chord* pgm_read_word(const struct Chord* const* chord) {return *chord;} + +typedef struct { + uint8_t col; + uint8_t row; +} keypos_t; + +typedef struct { + keypos_t key; + bool pressed; + uint16_t time; +} keyevent_t; + +typedef struct { + bool interrupted :1; + bool reserved2 :1; + bool reserved1 :1; + bool reserved0 :1; + uint8_t count :4; +} tap_t; + +typedef struct { + keyevent_t event; + tap_t tap; +} keyrecord_t; + +keyrecord_t pressed = {{{0,0},true,0}, {0,0,0,0,0}}; +keyrecord_t depressed = {{{0,0},false,0}, {0,0,0,0,0}}; + +enum keycodes { + KC_NO, + KC_TILDE, + KC_GRAVE, + KC_EXCLAIM, + KC_1, + KC_AT, + KC_2, + KC_HASH, + KC_3, + KC_DOLLAR, + KC_4, + KC_PERCENT, + KC_5, + KC_CIRCUMFLEX, + KC_6, + KC_AMPERSAND, + KC_7, + KC_ASTERISK, + KC_8, + KC_LEFT_PAREN, + KC_9, + KC_RIGHT_PAREN, + KC_0, + KC_UNDERSCORE, + KC_MINUS, + KC_PLUS, + KC_EQUAL, + KC_LEFT_CURLY_BRACE, + KC_LBRACKET, + KC_RIGHT_CURLY_BRACE, + KC_RBRACKET, + KC_PIPE, + KC_BSLASH, + KC_COLON, + KC_SCOLON, + KC_DOUBLE_QUOTE, + KC_QUOTE, + KC_LEFT_ANGLE_BRACKET, + KC_COMMA, + KC_RIGHT_ANGLE_BRACKET, + KC_DOT, + KC_QUESTION, + KC_SLASH, + KC_Q, + KC_W, + KC_E, + KC_R, + KC_T, + KC_Y, + KC_U, + KC_I, + KC_O, + KC_P, + KC_A, + KC_S, + KC_D, + KC_F, + KC_G, + KC_H, + KC_J, + KC_K, + KC_L, + KC_Z, + KC_X, + KC_C, + KC_V, + KC_B, + KC_N, + KC_M, + KC_ESC, + KC_LSFT, + KC_LCTL, + KC_LGUI, + KC_LALT, + KC_RALT, + KC_RCTL, + KC_RGUI, + KC_RSFT, + KC_TAB, + KC_DEL, + KC_INS, + KC_BSPC, + KC_ENTER, + KC_SPACE, + KC_F1, + KC_F2, + KC_F3, + KC_F4, + KC_F5, + KC_F6, + KC_F7, + KC_F8, + KC_F9, + KC_F10, + KC_F11, + KC_F12, + KC_LEFT, + KC_DOWN, + KC_UP, + KC_RIGHT, + + SAFE_RANGE +}; + +#define HISTORY 20 + +int16_t current_time; +uint8_t keyboard_history[HISTORY][SAFE_RANGE-1]; +int16_t time_history[HISTORY]; +uint8_t history_index; + +void register_code(int16_t keycode) { + history_index++; + for (int j = 0; j < SAFE_RANGE-1; j++) { + keyboard_history[history_index][j] = keyboard_history[history_index-1][j]; + } + keyboard_history[history_index][keycode] = 1; + time_history[history_index] = current_time; +}; +void unregister_code(int16_t keycode) { + history_index++; + for (int j = 0; j < SAFE_RANGE-1; j++) { + keyboard_history[history_index][j] = keyboard_history[history_index-1][j]; + } + keyboard_history[history_index][keycode] = 0; + time_history[history_index] = current_time; +}; +void send_keyboard_report(void) { /*still don't know what this does*/ }; +void matrix_scan_user (void); +void wait_ms(uint16_t ms) { + current_time += ms; +}; +uint16_t timer_read(void) { + uint16_t result = current_time; + return result; +}; +uint16_t timer_elapsed(uint16_t timer) { + uint16_t result = current_time - timer; + return result; +}; +void layer_move(int16_t layer) { /*ignoring for now*/ }; +void clear_keyboard(void) { + history_index++; + for (int j = 0; j < SAFE_RANGE-1; j++) { + keyboard_history[history_index][j] = 0; + } + time_history[history_index] = current_time; +}; +void reset_keyboard(void) { /*ignoring for now*/ }; + +void pause_ms(uint16_t ms) { + for (int i = 0; i < ms; i++) { + current_time++; + matrix_scan_user(); + } +}; + +#define TEST(name) \ + do { \ + printf("%s\n", name); \ + passed = true; \ + do { \ + uint8_t clear_state = ACTIVATED; \ + struct Chord clear_chord PROGMEM = {0, QWERTY, &clear_state, NULL, 0, 0, clear}; \ + clear_chord.function(&clear_chord); \ + } while (0); \ + current_time = 0; \ + history_index = 0; \ + for (int j = 0; j < SAFE_RANGE-1; j++) { \ + keyboard_history[0][j] = 0; \ + } \ + time_history[0] = 0; \ + for (int i = 1; i < HISTORY; i++) { \ + for (int j = 0; j < SAFE_RANGE-1; j++) { \ + keyboard_history[i][j] = -1; \ + } \ + time_history[i] = -1; \ + } + +#define END_TEST \ + if (passed) { \ + printf(GREEN"PASSED"NC"\n"); \ + } else { \ + printf(RED"FAILED"NC"\n"); \ + } \ + } while(0); + +#define MAIN \ +int main(int argc, char **argv) { \ + bool passed = true; \ + bool all_passed = true; + +#define END \ + printf("\n"); \ + if (all_passed) { \ + printf(GREEN"ALL TESTS PASSED"NC"\n"); \ + } else { \ + printf(RED"TESTS FAILED"NC"\n"); \ + } \ + return 1 - all_passed; \ +}
\ No newline at end of file diff --git a/users/dennytom/chording_engine/tests/test.c b/users/dennytom/chording_engine/tests/test.c new file mode 100644 index 000000000..0cc172f0a --- /dev/null +++ b/users/dennytom/chording_engine/tests/test.c @@ -0,0 +1,1259 @@ +#include "minunit.h" +#include "test_keymap.c" + +MAIN + +// CLEAR_KB +TEST("clear") + for (int i = 0; i < NUMBER_OF_CHORDS; i++) { + struct Chord* chord_ptr = (struct Chord*) pgm_read_word (&list_of_chords[i]); + struct Chord chord_storage; + memcpy_P(&chord_storage, chord_ptr, sizeof(struct Chord)); + struct Chord* chord = &chord_storage; + + *chord->state = READY; + + if (chord->counter) { + *chord->counter = 1; + } + } + + history_index++; + for (int j = 0; j < SAFE_RANGE-1; j++) { + keyboard_history[history_index][j] = 1; + } + + current_pseudolayer = 5; + lock_next = true; + autoshift_mode = false; + command_mode = 1; + in_leader_mode = true; + dynamic_macro_mode = true; + a_key_went_through = true; + + for (int i = 0; i < DYNAMIC_MACRO_MAX_LENGTH; i++) { + dynamic_macro_buffer[i] = 1; + } + + uint8_t clear_state = ACTIVATED; + struct Chord clear_chord PROGMEM = {0, QWERTY, &clear_state, NULL, 0, 0, clear}; + clear_chord.function(&clear_chord); + + for (int i = 0; i < NUMBER_OF_CHORDS; i++) { + struct Chord* chord_ptr = (struct Chord*) pgm_read_word (&list_of_chords[i]); + struct Chord chord_storage; + memcpy_P(&chord_storage, chord_ptr, sizeof(struct Chord)); + struct Chord* chord = &chord_storage; + + ASSERT_EQ(UINT, *chord->state, IDLE); + + if (chord->counter) { + ASSERT_EQ(UINT, *chord->counter, 0); + } + } + + for (int j = 0; j < SAFE_RANGE-1; j++) { + ASSERT_EQ(UINT, keyboard_history[history_index][j], 0); + } + + ASSERT_EQ(UINT, current_pseudolayer, 1); + ASSERT_EQ(UINT, lock_next, false); + ASSERT_EQ(UINT, autoshift_mode, true); + ASSERT_EQ(UINT, command_mode, 0); + ASSERT_EQ(UINT, in_leader_mode, false); + ASSERT_EQ(UINT, leader_ind, 0); + ASSERT_EQ(UINT, dynamic_macro_mode, false); + ASSERT_EQ(UINT, a_key_went_through, false); + + for (int i = 0; i < DYNAMIC_MACRO_MAX_LENGTH; i++) { + ASSERT_EQ(UINT, dynamic_macro_buffer[i], 0); + } +END_TEST + +TEST("pause_ms") + pause_ms(500); + ASSERT_EQ(UINT, current_time, 500); +END_TEST + +// KC +TEST("single_dance_held_states") + ASSERT_EQ(UINT, state_1, IDLE); + process_record_user(TOP1, &pressed); + pause_ms(CHORD_TIMEOUT); + ASSERT_EQ(UINT, state_1, IDLE); + pause_ms(1); + ASSERT_EQ(UINT, state_1, ACTIVATED); + pause_ms(DANCE_TIMEOUT); + ASSERT_EQ(UINT, state_1, ACTIVATED); + pause_ms(1); + ASSERT_EQ(UINT, state_1, PRESS_FROM_ACTIVE); + pause_ms(DANCE_TIMEOUT); + ASSERT_EQ(UINT, state_1, PRESS_FROM_ACTIVE); + pause_ms(1); + ASSERT_EQ(UINT, state_1, FINISHED_FROM_ACTIVE); + process_record_user(TOP1, &depressed); + ASSERT_EQ(UINT, state_1, IDLE); +END_TEST + +TEST("single_dance_held_codes") + ASSERT_EQ(UINT, keyboard_history[history_index][KC_Q], 0); + process_record_user(TOP1, &pressed); + pause_ms(CHORD_TIMEOUT); + ASSERT_EQ(UINT, keyboard_history[history_index][KC_Q], 0); + pause_ms(1); + ASSERT_EQ(UINT, keyboard_history[history_index][KC_Q], 1); + pause_ms(DANCE_TIMEOUT); + ASSERT_EQ(UINT, keyboard_history[history_index][KC_Q], 1); + pause_ms(1); + ASSERT_EQ(UINT, keyboard_history[history_index][KC_Q], 1); + pause_ms(DANCE_TIMEOUT); + ASSERT_EQ(UINT, keyboard_history[history_index][KC_Q], 1); + pause_ms(1); + ASSERT_EQ(UINT, keyboard_history[history_index][KC_Q], 1); + process_record_user(TOP1, &depressed); + ASSERT_EQ(UINT, keyboard_history[history_index][KC_Q], 0); +END_TEST + +TEST("single_dance_tapped_states") + ASSERT_EQ(UINT, state_1, IDLE); + process_record_user(TOP1, &pressed); + pause_ms(CHORD_TIMEOUT); + ASSERT_EQ(UINT, state_1, IDLE); + pause_ms(1); + ASSERT_EQ(UINT, state_1, ACTIVATED); + process_record_user(TOP1, &depressed); + ASSERT_EQ(UINT, state_1, IDLE); +END_TEST + +TEST("single_dance_tapped_codes") + ASSERT_EQ(UINT, keyboard_history[history_index][KC_Q], 0); + process_record_user(TOP1, &pressed); + pause_ms(CHORD_TIMEOUT); + ASSERT_EQ(UINT, keyboard_history[history_index][KC_Q], 0); + pause_ms(1); + ASSERT_EQ(UINT, keyboard_history[history_index][KC_Q], 1); + process_record_user(TOP1, &depressed); + ASSERT_EQ(UINT, keyboard_history[history_index][KC_Q], 0); +END_TEST + +// I can not actually track the states if the tap is faster than chord timeout + +TEST("single_dance_tapped_fast_codes") + ASSERT_EQ(UINT, state_0, IDLE); + process_record_user(TOP1, &pressed); + pause_ms(1); + process_record_user(TOP1, &depressed); + ASSERT_EQ(UINT, keyboard_history[0][KC_Q], 0); + ASSERT_EQ(UINT, keyboard_history[1][KC_Q], 1); + ASSERT_EQ(UINT, keyboard_history[2][KC_Q], 0); +END_TEST + +TEST("subchords_are_ignored") + ASSERT_EQ(UINT, state_0, IDLE); + process_record_user(TOP1, &pressed); + pause_ms(1); + process_record_user(TOP2, &pressed); + pause_ms(CHORD_TIMEOUT + 1); + ASSERT_EQ(UINT, keyboard_history[history_index][KC_Q], 0); + ASSERT_EQ(UINT, keyboard_history[history_index][KC_W], 0); + ASSERT_EQ(UINT, keyboard_history[history_index][KC_ESC], 1); +END_TEST + +TEST("multiple_chords_at_once") + ASSERT_EQ(UINT, state_0, IDLE); + process_record_user(TOP1, &pressed); + pause_ms(1); + process_record_user(TOP3, &pressed); + pause_ms(CHORD_TIMEOUT + 1); + ASSERT_EQ(UINT, keyboard_history[history_index][KC_Q], 1); + ASSERT_EQ(UINT, keyboard_history[history_index][KC_E], 1); +END_TEST + +// MO +TEST("momentary_layer") + ASSERT_EQ(UINT, current_pseudolayer, QWERTY); + process_record_user(BOT7, &pressed); + pause_ms(1); + process_record_user(BOT8, &pressed); + pause_ms(CHORD_TIMEOUT + 1); + ASSERT_EQ(UINT, current_pseudolayer, NUM); + process_record_user(BOT7, &depressed); + pause_ms(1); + process_record_user(BOT8, &depressed); + ASSERT_EQ(UINT, current_pseudolayer, QWERTY); +END_TEST + +TEST("momentary_layer_reset") + ASSERT_EQ(UINT, current_pseudolayer, QWERTY); + process_record_user(BOT7, &pressed); + pause_ms(1); + process_record_user(BOT8, &pressed); + pause_ms(CHORD_TIMEOUT + 1); + ASSERT_EQ(UINT, current_pseudolayer, NUM); + pause_ms(DANCE_TIMEOUT + 1); + ASSERT_EQ(UINT, current_pseudolayer, NUM); + process_record_user(BOT7, &depressed); + pause_ms(1); + process_record_user(BOT8, &depressed); + ASSERT_EQ(UINT, current_pseudolayer, QWERTY); +END_TEST + +TEST("momentary_layer_alt") + ASSERT_EQ(UINT, current_pseudolayer, QWERTY); + + process_record_user(TOP8, &pressed); + pause_ms(1); + process_record_user(TOP9, &pressed); + pause_ms(1); + process_record_user(TOP0, &pressed); + pause_ms(1); + process_record_user(BOT8, &pressed); + pause_ms(1); + process_record_user(BOT9, &pressed); + pause_ms(1); + process_record_user(BOT0, &pressed); + pause_ms(CHORD_TIMEOUT + 1); + + ASSERT_EQ(UINT, current_pseudolayer, NUM); + + process_record_user(TOP8, &depressed); + pause_ms(1); + process_record_user(TOP9, &depressed); + pause_ms(1); + process_record_user(TOP0, &depressed); + pause_ms(1); + process_record_user(BOT8, &depressed); + pause_ms(1); + process_record_user(BOT9, &depressed); + pause_ms(1); + process_record_user(BOT0, &depressed); + + ASSERT_EQ(UINT, current_pseudolayer, FNC); +END_TEST + +// DF +TEST("permanent_layer") + ASSERT_EQ(UINT, current_pseudolayer, QWERTY); + process_record_user(BOT9, &pressed); + pause_ms(1); + process_record_user(BOT0, &pressed); + pause_ms(CHORD_TIMEOUT + 1); + ASSERT_EQ(UINT, current_pseudolayer, NUM); + process_record_user(BOT9, &depressed); + pause_ms(1); + process_record_user(BOT0, &depressed); + ASSERT_EQ(UINT, current_pseudolayer, NUM); + pause_ms(1000); + ASSERT_EQ(UINT, current_pseudolayer, NUM); +END_TEST + +// AT +TEST("autoshift_toggle") + ASSERT_EQ(UINT, autoshift_mode, 1); + uint8_t state = ACTIVATED; + struct Chord chord PROGMEM = {0, QWERTY, &state, NULL, 0, 0, autoshift_toggle}; + chord.function(&chord); + ASSERT_EQ(UINT, autoshift_mode, 0); + state = ACTIVATED; + chord.function(&chord); + ASSERT_EQ(UINT, autoshift_mode, 1); +END_TEST + +// AS +TEST("autoshift_tap") + process_record_user(BOT1, &pressed); + pause_ms(CHORD_TIMEOUT + 1); + process_record_user(BOT1, &depressed); + + ASSERT_EQ(UINT, keyboard_history[0][KC_Z], 0); + ASSERT_EQ(UINT, keyboard_history[0][KC_LSFT], 0); + + ASSERT_EQ(UINT, keyboard_history[1][KC_Z], 1); + ASSERT_EQ(UINT, keyboard_history[1][KC_LSFT], 0); + + ASSERT_EQ(UINT, keyboard_history[2][KC_Z], 0); + ASSERT_EQ(UINT, keyboard_history[2][KC_LSFT], 0); +END_TEST + +TEST("autoshift_hold") + process_record_user(BOT1, &pressed); + pause_ms(CHORD_TIMEOUT + 1); + pause_ms(LONG_PRESS_MULTIPLIER * (DANCE_TIMEOUT + 1)); + process_record_user(BOT1, &depressed); + + ASSERT_EQ(UINT, keyboard_history[0][KC_Z], 0); + ASSERT_EQ(UINT, keyboard_history[0][KC_LSFT], 0); + + ASSERT_EQ(UINT, keyboard_history[1][KC_Z], 0); + ASSERT_EQ(UINT, keyboard_history[1][KC_LSFT], 1); + + ASSERT_EQ(UINT, keyboard_history[2][KC_Z], 1); + ASSERT_EQ(UINT, keyboard_history[2][KC_LSFT], 1); + + ASSERT_EQ(UINT, keyboard_history[3][KC_Z], 0); + ASSERT_EQ(UINT, keyboard_history[3][KC_LSFT], 1); + + ASSERT_EQ(UINT, keyboard_history[4][KC_Z], 0); + ASSERT_EQ(UINT, keyboard_history[4][KC_LSFT], 0); +END_TEST + +TEST("autoshift_hold_off") + autoshift_mode = 0; + process_record_user(BOT1, &pressed); + pause_ms(CHORD_TIMEOUT + 1); + pause_ms(LONG_PRESS_MULTIPLIER * (DANCE_TIMEOUT + 1)); + process_record_user(BOT1, &depressed); + + ASSERT_EQ(UINT, keyboard_history[0][KC_Z], 0); + ASSERT_EQ(UINT, keyboard_history[0][KC_LSFT], 0); + + ASSERT_EQ(UINT, keyboard_history[1][KC_Z], 1); + ASSERT_EQ(UINT, keyboard_history[1][KC_LSFT], 0); + + ASSERT_EQ(UINT, keyboard_history[2][KC_Z], 0); + ASSERT_EQ(UINT, keyboard_history[2][KC_LSFT], 0); +END_TEST + +// LOCK +TEST("lock") + ASSERT_EQ(UINT, keyboard_history[history_index][KC_LSFT], 0); + process_record_user(BOT1, &pressed); + process_record_user(BOT2, &pressed); + pause_ms(CHORD_TIMEOUT + 1); + ASSERT_EQ(UINT, keyboard_history[history_index][KC_LSFT], 1); + process_record_user(BOT1, &depressed); + ASSERT_EQ(UINT, keyboard_history[history_index][KC_LSFT], 0); + pause_ms(1); + process_record_user(BOT2, &depressed); + pause_ms(1); + process_record_user(TOP1, &pressed); + process_record_user(TOP2, &pressed); + process_record_user(BOT1, &pressed); + process_record_user(BOT2, &pressed); + pause_ms(CHORD_TIMEOUT + 1); + process_record_user(TOP1, &depressed); + pause_ms(1); + process_record_user(TOP2, &depressed); + process_record_user(BOT1, &depressed); + process_record_user(BOT2, &depressed); + pause_ms(1); + process_record_user(BOT1, &pressed); + process_record_user(BOT2, &pressed); + pause_ms(CHORD_TIMEOUT + 1); + ASSERT_EQ(UINT, keyboard_history[history_index][KC_LSFT], 1); + process_record_user(BOT1, &depressed); + ASSERT_EQ(UINT, keyboard_history[history_index][KC_LSFT], 1); + pause_ms(1); + process_record_user(BOT2, &depressed); + pause_ms(1000); + process_record_user(BOT1, &pressed); + process_record_user(BOT2, &pressed); + pause_ms(CHORD_TIMEOUT + 1); + ASSERT_EQ(UINT, keyboard_history[history_index][KC_LSFT], 0); + process_record_user(BOT1, &depressed); + pause_ms(1); + process_record_user(BOT2, &depressed); + pause_ms(1000); + ASSERT_EQ(UINT, keyboard_history[history_index][KC_LSFT], 0); +END_TEST + +// OSK +TEST("one_shot_key_tap") + ASSERT_EQ(UINT, keyboard_history[history_index][KC_LSFT], 0); + process_record_user(BOT2, &pressed); + process_record_user(BOT3, &pressed); + pause_ms(CHORD_TIMEOUT + 1); + ASSERT_EQ(UINT, keyboard_history[history_index][KC_LSFT], 0); + process_record_user(BOT2, &depressed); + pause_ms(1); + process_record_user(BOT3, &depressed); + ASSERT_EQ(UINT, keyboard_history[history_index][KC_LSFT], 1); + pause_ms(1000); + ASSERT_EQ(UINT, keyboard_history[history_index][KC_LSFT], 1); + + process_record_user(TOP1, &pressed); + pause_ms(CHORD_TIMEOUT + 1); + process_record_user(TOP1, &depressed); + ASSERT_EQ(UINT, keyboard_history[history_index][KC_LSFT], 0); +END_TEST + +TEST("one_shot_key_hold") + ASSERT_EQ(UINT, keyboard_history[history_index][KC_LSFT], 0); + process_record_user(BOT2, &pressed); + process_record_user(BOT3, &pressed); + pause_ms(CHORD_TIMEOUT + 1); + pause_ms(DANCE_TIMEOUT + 1); + ASSERT_EQ(UINT, keyboard_history[history_index][KC_LSFT], 1); + + process_record_user(TOP1, &pressed); + pause_ms(CHORD_TIMEOUT + 1); + process_record_user(TOP1, &depressed); + ASSERT_EQ(UINT, keyboard_history[history_index][KC_LSFT], 1); + + process_record_user(BOT2, &depressed); + ASSERT_EQ(UINT, keyboard_history[history_index][KC_LSFT], 0); +END_TEST + +TEST("one_shot_key_retrotapping") + ASSERT_EQ(UINT, keyboard_history[history_index][KC_LSFT], 0); + process_record_user(BOT2, &pressed); + process_record_user(BOT3, &pressed); + pause_ms(CHORD_TIMEOUT + 1); + pause_ms(DANCE_TIMEOUT + 1); + ASSERT_EQ(UINT, keyboard_history[history_index][KC_LSFT], 1); + + pause_ms(1000); + + process_record_user(BOT2, &depressed); + ASSERT_EQ(UINT, keyboard_history[history_index][KC_LSFT], 1); +END_TEST + +// OSL +TEST("one_shot_layer_tap") + ASSERT_EQ(UINT, current_pseudolayer, QWERTY); + process_record_user(BOT6, &pressed); + process_record_user(BOT7, &pressed); + pause_ms(CHORD_TIMEOUT + 1); + ASSERT_EQ(UINT, current_pseudolayer, QWERTY); + process_record_user(BOT6, &depressed); + pause_ms(1); + process_record_user(BOT7, &depressed); + ASSERT_EQ(UINT, current_pseudolayer, NUM); + pause_ms(1000); + ASSERT_EQ(UINT, current_pseudolayer, NUM); + + process_record_user(TOP1, &pressed); + pause_ms(CHORD_TIMEOUT + 1); + process_record_user(TOP1, &depressed); + ASSERT_EQ(UINT, current_pseudolayer, QWERTY); +END_TEST + +TEST("one_shot_layer_hold") + ASSERT_EQ(UINT, current_pseudolayer, QWERTY); + process_record_user(BOT6, &pressed); + process_record_user(BOT7, &pressed); + pause_ms(CHORD_TIMEOUT + 1); + pause_ms(DANCE_TIMEOUT + 1); + ASSERT_EQ(UINT, current_pseudolayer, NUM); + + process_record_user(TOP1, &pressed); + pause_ms(CHORD_TIMEOUT + 1); + process_record_user(TOP1, &depressed); + ASSERT_EQ(UINT, current_pseudolayer, NUM); + + process_record_user(BOT6, &depressed); + ASSERT_EQ(UINT, current_pseudolayer, QWERTY); +END_TEST + +TEST("one_shot_layer_retrotapping") + ASSERT_EQ(UINT, current_pseudolayer, QWERTY); + process_record_user(BOT6, &pressed); + process_record_user(BOT7, &pressed); + pause_ms(CHORD_TIMEOUT + 1); + pause_ms(DANCE_TIMEOUT + 1); + ASSERT_EQ(UINT, current_pseudolayer, NUM); + + pause_ms(1000); + + process_record_user(BOT6, &depressed); + ASSERT_EQ(UINT, current_pseudolayer, NUM); +END_TEST + +// CMD +TEST("command_mode") + // start recording + process_record_user(TOP5, &pressed); + process_record_user(TOP6, &pressed); + process_record_user(BOT5, &pressed); + process_record_user(BOT6, &pressed); + pause_ms(1); + process_record_user(TOP5, &depressed); + process_record_user(TOP6, &depressed); + process_record_user(BOT5, &depressed); + process_record_user(BOT6, &depressed); + + ASSERT_EQ(UINT, command_mode, 1); + + // record shift+q + process_record_user(BOT1, &pressed); + process_record_user(BOT2, &pressed); + pause_ms(CHORD_TIMEOUT + 1); + ASSERT_EQ(UINT, keyboard_history[history_index][KC_Q], 0); + ASSERT_EQ(UINT, keyboard_history[history_index][KC_LSFT], 0); + process_record_user(BOT1, &depressed); + process_record_user(BOT2, &depressed); + pause_ms(1000); + + process_record_user(TOP1, &pressed); + pause_ms(CHORD_TIMEOUT + 1); + ASSERT_EQ(UINT, keyboard_history[history_index][KC_Q], 0); + ASSERT_EQ(UINT, keyboard_history[history_index][KC_LSFT], 0); + process_record_user(TOP1, &depressed); + pause_ms(1000); + + + ASSERT_EQ(UINT, keyboard_history[history_index][KC_Q], 0); + ASSERT_EQ(UINT, keyboard_history[history_index][KC_LSFT], 0); + // execute + process_record_user(TOP5, &pressed); + process_record_user(TOP6, &pressed); + process_record_user(BOT5, &pressed); + process_record_user(BOT6, &pressed); + pause_ms(CHORD_TIMEOUT + 1); + + ASSERT_EQ(UINT, command_mode, 0); + + // test history + ASSERT_EQ(UINT, keyboard_history[0][KC_Q], 0); + ASSERT_EQ(UINT, keyboard_history[0][KC_LSFT], 0); + + ASSERT_EQ(UINT, keyboard_history[1][KC_Q], 0); + ASSERT_EQ(UINT, keyboard_history[1][KC_LSFT], 1); + + ASSERT_EQ(UINT, keyboard_history[2][KC_Q], 1); + ASSERT_EQ(UINT, keyboard_history[2][KC_LSFT], 1); + + ASSERT_EQ(UINT, keyboard_history[3][KC_Q], 1); + ASSERT_EQ(UINT, keyboard_history[3][KC_LSFT], 0); + + ASSERT_EQ(UINT, keyboard_history[4][KC_Q], 0); + ASSERT_EQ(UINT, keyboard_history[4][KC_LSFT], 0); + + ASSERT_EQ(UINT, keyboard_history[5][KC_Q], 255); + ASSERT_EQ(UINT, keyboard_history[5][KC_LSFT], 255); +END_TEST + +// KK +TEST("key_key_dance_tap") + process_record_user(BOT2, &pressed); + pause_ms(CHORD_TIMEOUT + 1); + process_record_user(BOT2, &depressed); + + ASSERT_EQ(UINT, keyboard_history[0][KC_X], 0); + ASSERT_EQ(UINT, keyboard_history[0][KC_LCTL], 0); + + ASSERT_EQ(UINT, keyboard_history[1][KC_X], 1); + ASSERT_EQ(UINT, keyboard_history[1][KC_LCTL], 0); + + ASSERT_EQ(UINT, keyboard_history[2][KC_X], 0); + ASSERT_EQ(UINT, keyboard_history[2][KC_LCTL], 0); +END_TEST + +TEST("key_key_dance_hold") + process_record_user(BOT2, &pressed); + pause_ms(CHORD_TIMEOUT + 1); + pause_ms(DANCE_TIMEOUT + 1); + process_record_user(BOT2, &depressed); + + ASSERT_EQ(UINT, keyboard_history[0][KC_X], 0); + ASSERT_EQ(UINT, keyboard_history[0][KC_LCTL], 0); + + ASSERT_EQ(UINT, keyboard_history[1][KC_X], 0); + ASSERT_EQ(UINT, keyboard_history[1][KC_LCTL], 1); + + ASSERT_EQ(UINT, keyboard_history[2][KC_X], 0); + ASSERT_EQ(UINT, keyboard_history[2][KC_LCTL], 0); +END_TEST + +// KL +TEST("key_layer_tap") + ASSERT_EQ(UINT, current_pseudolayer, QWERTY); + process_record_user(BOT3, &pressed); + pause_ms(CHORD_TIMEOUT + 1); + ASSERT_EQ(UINT, current_pseudolayer, NUM); + process_record_user(BOT3, &depressed); + ASSERT_EQ(UINT, current_pseudolayer, QWERTY); + pause_ms(1000); + + ASSERT_EQ(UINT, keyboard_history[0][KC_C], 0); + ASSERT_EQ(UINT, keyboard_history[1][KC_C], 1); + ASSERT_EQ(UINT, keyboard_history[2][KC_C], 0); + ASSERT_EQ(UINT, keyboard_history[3][KC_C], 255); +END_TEST + +TEST("key_layer_retrotapping") + ASSERT_EQ(UINT, current_pseudolayer, QWERTY); + process_record_user(BOT3, &pressed); + pause_ms(1000); + ASSERT_EQ(UINT, current_pseudolayer, NUM); + process_record_user(BOT3, &depressed); + ASSERT_EQ(UINT, current_pseudolayer, QWERTY); + pause_ms(1000); + + ASSERT_EQ(UINT, keyboard_history[0][KC_C], 0); + ASSERT_EQ(UINT, keyboard_history[1][KC_C], 1); + ASSERT_EQ(UINT, keyboard_history[2][KC_C], 0); + ASSERT_EQ(UINT, keyboard_history[3][KC_C], 255); +END_TEST + +TEST("key_layer_hold_quick_typist") + ASSERT_EQ(UINT, current_pseudolayer, QWERTY); + process_record_user(BOT3, &pressed); + pause_ms(CHORD_TIMEOUT + 1); + ASSERT_EQ(UINT, current_pseudolayer, NUM); + + pause_ms(1); + process_record_user(TOP1, &pressed); + pause_ms(1); + process_record_user(TOP1, &depressed); + pause_ms(1); + process_record_user(TOP1, &pressed); + pause_ms(1); + process_record_user(TOP1, &depressed); + pause_ms(1); + process_record_user(TOP1, &pressed); + pause_ms(1); + process_record_user(TOP1, &depressed); + ASSERT_EQ(UINT, current_pseudolayer, NUM); + pause_ms(1); + + process_record_user(BOT3, &depressed); + ASSERT_EQ(UINT, current_pseudolayer, QWERTY); + + ASSERT_EQ(UINT, keyboard_history[0][KC_1], 0); + ASSERT_EQ(UINT, keyboard_history[1][KC_1], 1); + ASSERT_EQ(UINT, keyboard_history[2][KC_1], 0); + ASSERT_EQ(UINT, keyboard_history[3][KC_1], 1); + ASSERT_EQ(UINT, keyboard_history[4][KC_1], 0); + ASSERT_EQ(UINT, keyboard_history[5][KC_1], 1); + ASSERT_EQ(UINT, keyboard_history[6][KC_1], 0); + ASSERT_EQ(UINT, keyboard_history[7][KC_1], 255); +END_TEST + +TEST("key_layer_hold_slow_typist") + ASSERT_EQ(UINT, current_pseudolayer, QWERTY); + process_record_user(BOT3, &pressed); + pause_ms(CHORD_TIMEOUT + 1); + ASSERT_EQ(UINT, current_pseudolayer, NUM); + + pause_ms(1000); + process_record_user(TOP1, &pressed); + pause_ms(1000); + process_record_user(TOP1, &depressed); + pause_ms(1000); + process_record_user(TOP1, &pressed); + pause_ms(1000); + process_record_user(TOP1, &depressed); + pause_ms(1000); + process_record_user(TOP1, &pressed); + pause_ms(1000); + process_record_user(TOP1, &depressed); + ASSERT_EQ(UINT, current_pseudolayer, NUM); + pause_ms(1); + + process_record_user(BOT3, &depressed); + ASSERT_EQ(UINT, current_pseudolayer, QWERTY); + + ASSERT_EQ(UINT, keyboard_history[0][KC_1], 0); + ASSERT_EQ(UINT, keyboard_history[1][KC_1], 1); + ASSERT_EQ(UINT, keyboard_history[2][KC_1], 0); + ASSERT_EQ(UINT, keyboard_history[3][KC_1], 1); + ASSERT_EQ(UINT, keyboard_history[4][KC_1], 0); + ASSERT_EQ(UINT, keyboard_history[5][KC_1], 1); + ASSERT_EQ(UINT, keyboard_history[6][KC_1], 0); + ASSERT_EQ(UINT, keyboard_history[7][KC_1], 255); +END_TEST + +// KM +TEST("key_mod_tap") + ASSERT_EQ(UINT, keyboard_history[0][KC_LALT], 0); + ASSERT_EQ(UINT, keyboard_history[0][KC_V], 0); + process_record_user(BOT4, &pressed); + pause_ms(CHORD_TIMEOUT + 1); + ASSERT_EQ(UINT, keyboard_history[1][KC_LALT], 1); + ASSERT_EQ(UINT, keyboard_history[1][KC_V], 0); + process_record_user(BOT4, &depressed); + ASSERT_EQ(UINT, keyboard_history[2][KC_LALT], 0); + ASSERT_EQ(UINT, keyboard_history[2][KC_V], 0); + pause_ms(1000); + + ASSERT_EQ(UINT, keyboard_history[3][KC_LALT], 0); + ASSERT_EQ(UINT, keyboard_history[3][KC_V], 1); + ASSERT_EQ(UINT, keyboard_history[4][KC_LALT], 0); + ASSERT_EQ(UINT, keyboard_history[4][KC_V], 0); + ASSERT_EQ(UINT, keyboard_history[5][KC_LALT], 255); + ASSERT_EQ(UINT, keyboard_history[5][KC_V], 255); +END_TEST + +TEST("key_mod_retrotapping") + ASSERT_EQ(UINT, keyboard_history[0][KC_LALT], 0); + ASSERT_EQ(UINT, keyboard_history[0][KC_V], 0); + process_record_user(BOT4, &pressed); + pause_ms(1000); + ASSERT_EQ(UINT, keyboard_history[1][KC_LALT], 1); + ASSERT_EQ(UINT, keyboard_history[1][KC_V], 0); + process_record_user(BOT4, &depressed); + ASSERT_EQ(UINT, keyboard_history[2][KC_LALT], 0); + ASSERT_EQ(UINT, keyboard_history[2][KC_V], 0); + pause_ms(1000); + + ASSERT_EQ(UINT, keyboard_history[3][KC_LALT], 0); + ASSERT_EQ(UINT, keyboard_history[3][KC_V], 1); + ASSERT_EQ(UINT, keyboard_history[4][KC_LALT], 0); + ASSERT_EQ(UINT, keyboard_history[4][KC_V], 0); + ASSERT_EQ(UINT, keyboard_history[5][KC_LALT], 255); + ASSERT_EQ(UINT, keyboard_history[5][KC_V], 255); +END_TEST + +TEST("key_mod_hold_quick_typist") + process_record_user(BOT4, &pressed); + pause_ms(CHORD_TIMEOUT + 1); + + pause_ms(1); + process_record_user(TOP1, &pressed); + pause_ms(1); + process_record_user(TOP1, &depressed); + pause_ms(1); + process_record_user(TOP1, &pressed); + pause_ms(1); + process_record_user(TOP1, &depressed); + pause_ms(1); + process_record_user(TOP1, &pressed); + pause_ms(1); + process_record_user(TOP1, &depressed); + pause_ms(1); + + process_record_user(BOT4, &depressed); + ASSERT_EQ(UINT, current_pseudolayer, QWERTY); + + ASSERT_EQ(UINT, keyboard_history[0][KC_LALT], 0); + ASSERT_EQ(UINT, keyboard_history[0][KC_V], 0); + ASSERT_EQ(UINT, keyboard_history[1][KC_LALT], 1); + ASSERT_EQ(UINT, keyboard_history[1][KC_V], 0); + ASSERT_EQ(UINT, keyboard_history[2][KC_LALT], 1); + ASSERT_EQ(UINT, keyboard_history[2][KC_V], 0); + ASSERT_EQ(UINT, keyboard_history[3][KC_LALT], 1); + ASSERT_EQ(UINT, keyboard_history[3][KC_V], 0); + ASSERT_EQ(UINT, keyboard_history[4][KC_LALT], 1); + ASSERT_EQ(UINT, keyboard_history[4][KC_V], 0); + ASSERT_EQ(UINT, keyboard_history[5][KC_LALT], 1); + ASSERT_EQ(UINT, keyboard_history[5][KC_V], 0); + ASSERT_EQ(UINT, keyboard_history[6][KC_LALT], 1); + ASSERT_EQ(UINT, keyboard_history[6][KC_V], 0); + ASSERT_EQ(UINT, keyboard_history[7][KC_LALT], 1); + ASSERT_EQ(UINT, keyboard_history[7][KC_V], 0); + ASSERT_EQ(UINT, keyboard_history[8][KC_LALT], 0); + ASSERT_EQ(UINT, keyboard_history[8][KC_V], 0); + ASSERT_EQ(UINT, keyboard_history[9][KC_LALT], 255); + ASSERT_EQ(UINT, keyboard_history[9][KC_V], 255); +END_TEST + +TEST("key_mod_hold_slow_typist") + process_record_user(BOT4, &pressed); + pause_ms(CHORD_TIMEOUT + 1); + + pause_ms(1000); + process_record_user(TOP1, &pressed); + pause_ms(1000); + process_record_user(TOP1, &depressed); + pause_ms(1000); + process_record_user(TOP1, &pressed); + pause_ms(1000); + process_record_user(TOP1, &depressed); + pause_ms(1000); + process_record_user(TOP1, &pressed); + pause_ms(1000); + process_record_user(TOP1, &depressed); + pause_ms(1000); + + process_record_user(BOT4, &depressed); + ASSERT_EQ(UINT, current_pseudolayer, QWERTY); + + ASSERT_EQ(UINT, keyboard_history[0][KC_LALT], 0); + ASSERT_EQ(UINT, keyboard_history[0][KC_V], 0); + ASSERT_EQ(UINT, keyboard_history[0][KC_Q], 0); + ASSERT_EQ(UINT, keyboard_history[1][KC_LALT], 1); + ASSERT_EQ(UINT, keyboard_history[1][KC_V], 0); + ASSERT_EQ(UINT, keyboard_history[1][KC_Q], 0); + ASSERT_EQ(UINT, keyboard_history[2][KC_LALT], 1); + ASSERT_EQ(UINT, keyboard_history[2][KC_V], 0); + ASSERT_EQ(UINT, keyboard_history[2][KC_Q], 1); + ASSERT_EQ(UINT, keyboard_history[3][KC_LALT], 1); + ASSERT_EQ(UINT, keyboard_history[3][KC_V], 0); + ASSERT_EQ(UINT, keyboard_history[3][KC_Q], 0); + ASSERT_EQ(UINT, keyboard_history[4][KC_LALT], 1); + ASSERT_EQ(UINT, keyboard_history[4][KC_V], 0); + ASSERT_EQ(UINT, keyboard_history[4][KC_Q], 1); + ASSERT_EQ(UINT, keyboard_history[5][KC_LALT], 1); + ASSERT_EQ(UINT, keyboard_history[5][KC_V], 0); + ASSERT_EQ(UINT, keyboard_history[5][KC_Q], 0); + ASSERT_EQ(UINT, keyboard_history[6][KC_LALT], 1); + ASSERT_EQ(UINT, keyboard_history[6][KC_V], 0); + ASSERT_EQ(UINT, keyboard_history[6][KC_Q], 1); + ASSERT_EQ(UINT, keyboard_history[7][KC_LALT], 1); + ASSERT_EQ(UINT, keyboard_history[7][KC_V], 0); + ASSERT_EQ(UINT, keyboard_history[7][KC_Q], 0); + ASSERT_EQ(UINT, keyboard_history[8][KC_LALT], 0); + ASSERT_EQ(UINT, keyboard_history[8][KC_V], 0); + ASSERT_EQ(UINT, keyboard_history[8][KC_Q], 0); + ASSERT_EQ(UINT, keyboard_history[9][KC_LALT], 255); + ASSERT_EQ(UINT, keyboard_history[9][KC_V], 255); + ASSERT_EQ(UINT, keyboard_history[9][KC_Q], 255); +END_TEST + +// LEADER +TEST("leader_triggers_global") + uint8_t state = ACTIVATED; + struct Chord chord PROGMEM = {0, QWERTY, &state, NULL, 0, 0, leader}; + chord.function(&chord); + + ASSERT_EQ(UINT, in_leader_mode, 1); +END_TEST + +TEST("leader_no_follow") + uint8_t state = ACTIVATED; + struct Chord chord PROGMEM = {0, QWERTY, &state, NULL, 0, 0, leader}; + chord.function(&chord); + + ASSERT_EQ(UINT, in_leader_mode, 1); + + pause_ms(1000); + + ASSERT_EQ(UINT, in_leader_mode, 0); + ASSERT_EQ(UINT, keyboard_history[1][KC_O], 255); +END_TEST + +TEST("leader_wrong_follow") + process_record_user(TOP2, &pressed); + process_record_user(TOP3, &pressed); + process_record_user(BOT2, &pressed); + process_record_user(BOT3, &pressed); + pause_ms(1); + process_record_user(TOP2, &depressed); + process_record_user(TOP3, &depressed); + process_record_user(BOT2, &depressed); + process_record_user(BOT3, &depressed); + + ASSERT_EQ(UINT, in_leader_mode, 1); + + pause_ms(1); + process_record_user(TOP1, &pressed); + pause_ms(1); + process_record_user(TOP1, &depressed); + pause_ms(1); + process_record_user(TOP2, &pressed); + pause_ms(1); + process_record_user(TOP2, &depressed); + + pause_ms(LEADER_TIMEOUT); + pause_ms(1); + + ASSERT_EQ(UINT, in_leader_mode, 0); + ASSERT_EQ(UINT, keyboard_history[1][KC_Q], 255); +END_TEST + +TEST("leader_correct_follow") + process_record_user(TOP2, &pressed); + process_record_user(TOP3, &pressed); + process_record_user(BOT2, &pressed); + process_record_user(BOT3, &pressed); + pause_ms(1); + process_record_user(TOP2, &depressed); + process_record_user(TOP3, &depressed); + process_record_user(BOT2, &depressed); + process_record_user(BOT3, &depressed); + + ASSERT_EQ(UINT, in_leader_mode, 1); + + pause_ms(1); + process_record_user(TOP0, &pressed); + pause_ms(1); + process_record_user(TOP0, &depressed); + pause_ms(1); + process_record_user(TOP9, &pressed); + pause_ms(1); + process_record_user(TOP9, &depressed); + + pause_ms(LEADER_TIMEOUT); + ASSERT_EQ(UINT, in_leader_mode, 1); + + pause_ms(1); + ASSERT_EQ(UINT, in_leader_mode, 0); + + ASSERT_EQ(UINT, keyboard_history[1][KC_O], 0); + ASSERT_EQ(UINT, keyboard_history[1][KC_P], 0); + ASSERT_EQ(UINT, keyboard_history[1][KC_A], 0); + ASSERT_EQ(UINT, keyboard_history[1][KC_S], 1); + + ASSERT_EQ(UINT, keyboard_history[2][KC_O], 0); + ASSERT_EQ(UINT, keyboard_history[2][KC_P], 0); + ASSERT_EQ(UINT, keyboard_history[2][KC_A], 0); + ASSERT_EQ(UINT, keyboard_history[2][KC_S], 0); + + ASSERT_EQ(UINT, keyboard_history[3][KC_O], 255); + ASSERT_EQ(UINT, keyboard_history[3][KC_P], 255); + ASSERT_EQ(UINT, keyboard_history[3][KC_A], 255); + ASSERT_EQ(UINT, keyboard_history[3][KC_S], 255); + + ASSERT_EQ(UINT, keyboard_history[4][KC_O], 255); + ASSERT_EQ(UINT, keyboard_history[4][KC_P], 255); + ASSERT_EQ(UINT, keyboard_history[4][KC_A], 255); + ASSERT_EQ(UINT, keyboard_history[4][KC_S], 255); + + ASSERT_EQ(UINT, keyboard_history[5][KC_Q], 255); +END_TEST + +// DYNAMIC MACRO +TEST("dynamic_macro_record_mode") + current_pseudolayer = NUM; + + // record + ASSERT_EQ(UINT, dynamic_macro_mode, 0); + process_record_user(BOT7, &pressed); + process_record_user(BOT7, &depressed); + ASSERT_EQ(UINT, dynamic_macro_mode, 1); + pause_ms(1000); + ASSERT_EQ(UINT, dynamic_macro_mode, 1); +END_TEST + +TEST("dynamic_macro_record_mode_off") + current_pseudolayer = NUM; + + process_record_user(BOT7, &pressed); + process_record_user(BOT7, &depressed); + ASSERT_EQ(UINT, dynamic_macro_mode, 1); + + process_record_user(BOT9, &pressed); + process_record_user(BOT9, &depressed); + ASSERT_EQ(UINT, dynamic_macro_mode, 0); +END_TEST + +TEST("dynamic_macro_record_one") + current_pseudolayer = NUM; + + process_record_user(BOT7, &pressed); + process_record_user(BOT7, &depressed); + ASSERT_EQ(UINT, dynamic_macro_mode, 1); + + process_record_user(TOP1, &pressed); + process_record_user(TOP1, &depressed); + + ASSERT_EQ(UINT, keyboard_history[1][KC_1], 255); + + process_record_user(BOT9, &pressed); + process_record_user(BOT9, &depressed); + + pause_ms(1000); + + process_record_user(BOT0, &pressed); + process_record_user(BOT0, &depressed); + + ASSERT_EQ(UINT, keyboard_history[1][KC_1], 1); + ASSERT_EQ(UINT, keyboard_history[2][KC_1], 0); + ASSERT_EQ(UINT, keyboard_history[3][KC_1], 255); + + pause_ms(1000); + + process_record_user(BOT0, &pressed); + process_record_user(BOT0, &depressed); + + ASSERT_EQ(UINT, keyboard_history[3][KC_1], 1); + ASSERT_EQ(UINT, keyboard_history[4][KC_1], 0); + ASSERT_EQ(UINT, keyboard_history[5][KC_1], 255); +END_TEST + +TEST("dynamic_macro_record_two") + current_pseudolayer = NUM; + + process_record_user(BOT7, &pressed); + process_record_user(BOT7, &depressed); + ASSERT_EQ(UINT, dynamic_macro_mode, 1); + + process_record_user(TOP1, &pressed); + process_record_user(TOP1, &depressed); + + process_record_user(TOP2, &pressed); + process_record_user(TOP2, &depressed); + + ASSERT_EQ(UINT, keyboard_history[1][KC_1], 255); + + process_record_user(BOT9, &pressed); + process_record_user(BOT9, &depressed); + + pause_ms(1000); + + process_record_user(BOT0, &pressed); + process_record_user(BOT0, &depressed); + + ASSERT_EQ(UINT, keyboard_history[1][KC_1], 1); + ASSERT_EQ(UINT, keyboard_history[2][KC_2], 1); + ASSERT_EQ(UINT, keyboard_history[3][KC_1], 0); + ASSERT_EQ(UINT, keyboard_history[4][KC_2], 0); + ASSERT_EQ(UINT, keyboard_history[5][KC_1], 255); +END_TEST + +TEST("dynamic_macro_record_two_parts") + current_pseudolayer = NUM; + + process_record_user(BOT7, &pressed); + process_record_user(BOT7, &depressed); + ASSERT_EQ(UINT, dynamic_macro_mode, 1); + + process_record_user(TOP1, &pressed); + process_record_user(TOP1, &depressed); + + process_record_user(TOP2, &pressed); + process_record_user(TOP2, &depressed); + + ASSERT_EQ(UINT, keyboard_history[1][KC_1], 255); + + process_record_user(BOT8, &pressed); + process_record_user(BOT8, &depressed); + + process_record_user(TOP3, &pressed); + process_record_user(TOP3, &depressed); + + process_record_user(BOT9, &pressed); + process_record_user(BOT9, &depressed); + + pause_ms(1000); + + process_record_user(BOT0, &pressed); + process_record_user(BOT0, &depressed); + + ASSERT_EQ(UINT, keyboard_history[1][KC_1], 1); + ASSERT_EQ(UINT, keyboard_history[1][KC_2], 0); + ASSERT_EQ(UINT, keyboard_history[1][KC_3], 0); + + ASSERT_EQ(UINT, keyboard_history[2][KC_1], 1); + ASSERT_EQ(UINT, keyboard_history[2][KC_2], 1); + ASSERT_EQ(UINT, keyboard_history[2][KC_3], 0); + + ASSERT_EQ(UINT, keyboard_history[3][KC_1], 0); + ASSERT_EQ(UINT, keyboard_history[3][KC_2], 1); + ASSERT_EQ(UINT, keyboard_history[3][KC_3], 0); + + ASSERT_EQ(UINT, keyboard_history[4][KC_1], 0); + ASSERT_EQ(UINT, keyboard_history[4][KC_2], 0); + ASSERT_EQ(UINT, keyboard_history[4][KC_3], 0); + + ASSERT_EQ(UINT, keyboard_history[5][KC_1], 0); + ASSERT_EQ(UINT, keyboard_history[5][KC_2], 0); + ASSERT_EQ(UINT, keyboard_history[5][KC_3], 1); + + ASSERT_EQ(UINT, keyboard_history[6][KC_1], 0); + ASSERT_EQ(UINT, keyboard_history[6][KC_2], 0); + ASSERT_EQ(UINT, keyboard_history[6][KC_3], 0); + + ASSERT_EQ(UINT, keyboard_history[7][KC_1], 255); +END_TEST + +// dance + M() + +TEST("dance_tap") + process_record_user(BOT0, &pressed); + process_record_user(BOT0, &depressed); + + pause_ms(1000); + + ASSERT_EQ(UINT, keyboard_history[1][KC_9], 1); + ASSERT_EQ(UINT, keyboard_history[1][KC_0], 0); + ASSERT_EQ(UINT, keyboard_history[2][KC_9], 0); + ASSERT_EQ(UINT, keyboard_history[2][KC_0], 0); + ASSERT_EQ(UINT, keyboard_history[3][KC_9], 255); +END_TEST + +TEST("dance_hold") + process_record_user(BOT0, &pressed); + pause_ms(1000); + + ASSERT_EQ(UINT, keyboard_history[1][KC_9], 1); + + process_record_user(BOT0, &depressed); + ASSERT_EQ(UINT, keyboard_history[2][KC_9], 0); +END_TEST + +TEST("dance_tap_tap") + process_record_user(BOT0, &pressed); + process_record_user(BOT0, &depressed); + process_record_user(BOT0, &pressed); + process_record_user(BOT0, &depressed); + + pause_ms(1000); + + ASSERT_EQ(UINT, keyboard_history[1][KC_9], 0); + ASSERT_EQ(UINT, keyboard_history[1][KC_0], 1); + ASSERT_EQ(UINT, keyboard_history[2][KC_9], 0); + ASSERT_EQ(UINT, keyboard_history[2][KC_0], 0); + ASSERT_EQ(UINT, keyboard_history[3][KC_9], 255); +END_TEST + +TEST("dance_tap_hold") + process_record_user(BOT0, &pressed); + process_record_user(BOT0, &depressed); + pause_ms(1); + process_record_user(BOT0, &pressed); + pause_ms(1000); + + ASSERT_EQ(UINT, keyboard_history[1][KC_0], 1); + + process_record_user(BOT0, &depressed); + ASSERT_EQ(UINT, keyboard_history[2][KC_0], 0); +END_TEST + +// MK +TEST("multiple_keys") + current_pseudolayer = NUM; + + process_record_user(BOT1, &pressed); + process_record_user(BOT1, &depressed); + + ASSERT_EQ(UINT, keyboard_history[1][KC_LCTL], 1); + ASSERT_EQ(UINT, keyboard_history[1][KC_LSFT], 0); + + ASSERT_EQ(UINT, keyboard_history[2][KC_LCTL], 1); + ASSERT_EQ(UINT, keyboard_history[2][KC_LSFT], 1); + + ASSERT_EQ(UINT, keyboard_history[3][KC_LCTL], 0); + ASSERT_EQ(UINT, keyboard_history[3][KC_LSFT], 1); + + ASSERT_EQ(UINT, keyboard_history[4][KC_LCTL], 0); + ASSERT_EQ(UINT, keyboard_history[4][KC_LSFT], 0); + + ASSERT_EQ(UINT, keyboard_history[5][KC_LCTL], 255); +END_TEST + +TEST("multiple_keys_interleaved") + current_pseudolayer = NUM; + + process_record_user(BOT1, &pressed); + pause_ms(CHORD_TIMEOUT+1); + + process_record_user(TOP1, &pressed); + process_record_user(TOP1, &depressed); + process_record_user(TOP1, &pressed); + process_record_user(TOP1, &depressed); + process_record_user(TOP1, &pressed); + process_record_user(TOP1, &depressed); + + process_record_user(BOT1, &depressed); + + ASSERT_EQ(UINT, keyboard_history[1][KC_LCTL], 1); + ASSERT_EQ(UINT, keyboard_history[1][KC_LSFT], 0); + + ASSERT_EQ(UINT, keyboard_history[2][KC_LCTL], 1); + ASSERT_EQ(UINT, keyboard_history[2][KC_LSFT], 1); + + ASSERT_EQ(UINT, keyboard_history[3][KC_1], 1); + ASSERT_EQ(UINT, keyboard_history[4][KC_1], 0); + ASSERT_EQ(UINT, keyboard_history[5][KC_1], 1); + ASSERT_EQ(UINT, keyboard_history[6][KC_1], 0); + ASSERT_EQ(UINT, keyboard_history[7][KC_1], 1); + ASSERT_EQ(UINT, keyboard_history[8][KC_1], 0); + + ASSERT_EQ(UINT, keyboard_history[9][KC_LCTL], 0); + ASSERT_EQ(UINT, keyboard_history[9][KC_LSFT], 1); + + ASSERT_EQ(UINT, keyboard_history[10][KC_LCTL], 0); + ASSERT_EQ(UINT, keyboard_history[10][KC_LSFT], 0); + + ASSERT_EQ(UINT, keyboard_history[11][KC_LCTL], 255); +END_TEST + +// D +TEST("dance_one") + current_pseudolayer = NUM; + + process_record_user(BOT3, &pressed); + process_record_user(BOT3, &depressed); + + pause_ms(CHORD_TIMEOUT+DANCE_TIMEOUT+2); + + ASSERT_EQ(UINT, keyboard_history[1][KC_1], 1); + ASSERT_EQ(UINT, keyboard_history[2][KC_1], 0); + ASSERT_EQ(UINT, keyboard_history[3][KC_1], 255); + + process_record_user(BOT3, &pressed); + process_record_user(BOT3, &depressed); + + pause_ms(CHORD_TIMEOUT+DANCE_TIMEOUT+2); + + ASSERT_EQ(UINT, keyboard_history[1][KC_1], 1); + ASSERT_EQ(UINT, keyboard_history[2][KC_1], 0); + ASSERT_EQ(UINT, keyboard_history[3][KC_1], 1); + ASSERT_EQ(UINT, keyboard_history[4][KC_1], 0); + ASSERT_EQ(UINT, keyboard_history[5][KC_1], 255); +END_TEST + +TEST("dance_two") + current_pseudolayer = NUM; + + process_record_user(BOT3, &pressed); + process_record_user(BOT3, &depressed); + process_record_user(BOT3, &pressed); + process_record_user(BOT3, &depressed); + + pause_ms(CHORD_TIMEOUT+DANCE_TIMEOUT+2); + + ASSERT_EQ(UINT, keyboard_history[1][KC_2], 1); + ASSERT_EQ(UINT, keyboard_history[2][KC_2], 0); + ASSERT_EQ(UINT, keyboard_history[3][KC_2], 255); + + process_record_user(BOT3, &pressed); + process_record_user(BOT3, &depressed); + process_record_user(BOT3, &pressed); + process_record_user(BOT3, &depressed); + + pause_ms(CHORD_TIMEOUT+DANCE_TIMEOUT+2); + + ASSERT_EQ(UINT, keyboard_history[1][KC_2], 1); + ASSERT_EQ(UINT, keyboard_history[2][KC_2], 0); + ASSERT_EQ(UINT, keyboard_history[3][KC_2], 1); + ASSERT_EQ(UINT, keyboard_history[4][KC_2], 0); + ASSERT_EQ(UINT, keyboard_history[5][KC_2], 255); +END_TEST + +TEST("dance_three") + current_pseudolayer = NUM; + + process_record_user(BOT3, &pressed); + process_record_user(BOT3, &depressed); + process_record_user(BOT3, &pressed); + process_record_user(BOT3, &depressed); + process_record_user(BOT3, &pressed); + process_record_user(BOT3, &depressed); + + pause_ms(CHORD_TIMEOUT+DANCE_TIMEOUT+2); + + ASSERT_EQ(UINT, keyboard_history[1][KC_3], 1); + ASSERT_EQ(UINT, keyboard_history[2][KC_3], 0); + ASSERT_EQ(UINT, keyboard_history[3][KC_3], 255); + + process_record_user(BOT3, &pressed); + process_record_user(BOT3, &depressed); + process_record_user(BOT3, &pressed); + process_record_user(BOT3, &depressed); + process_record_user(BOT3, &pressed); + process_record_user(BOT3, &depressed); + + pause_ms(CHORD_TIMEOUT+DANCE_TIMEOUT+2); + + ASSERT_EQ(UINT, keyboard_history[1][KC_3], 1); + ASSERT_EQ(UINT, keyboard_history[2][KC_3], 0); + ASSERT_EQ(UINT, keyboard_history[3][KC_3], 1); + ASSERT_EQ(UINT, keyboard_history[4][KC_3], 0); + ASSERT_EQ(UINT, keyboard_history[5][KC_3], 255); +END_TEST + +TEST("dance_two_held") + current_pseudolayer = NUM; + + process_record_user(BOT3, &pressed); + process_record_user(BOT3, &depressed); + process_record_user(BOT3, &pressed); + + pause_ms(CHORD_TIMEOUT+DANCE_TIMEOUT+2); + + ASSERT_EQ(UINT, keyboard_history[1][KC_2], 1); + ASSERT_EQ(UINT, keyboard_history[2][KC_2], 255); + + process_record_user(BOT3, &depressed); + ASSERT_EQ(UINT, keyboard_history[2][KC_2], 0); + ASSERT_EQ(UINT, keyboard_history[3][KC_2], 255); +END_TEST + +// These two are leaving the chording engine, they kinda have to be tested manually +// TO +// RESET + +END diff --git a/users/dennytom/chording_engine/tests/test_full.sh b/users/dennytom/chording_engine/tests/test_full.sh new file mode 100644 index 000000000..ea93aec8f --- /dev/null +++ b/users/dennytom/chording_engine/tests/test_full.sh @@ -0,0 +1,11 @@ +#!/bin/sh +cd .. +python3 parser.py tests/test_keymap_def.json tests/test_keymap.c && \ +gcc -g tests/test.c -o tests/test && \ +tests/test && \ +python3 parser.py ../../../keyboards/butterstick/keymaps/tomas/keymap_def.json ../../../keyboards/butterstick/keymaps/tomas/keymap.c && \ +python3 parser.py ../../../keyboards/georgi/keymaps/tomas/keymap_def.json ../../../keyboards/georgi/keymaps/tomas/keymap.c && \ +python3 parser.py ../../../keyboards/georgi/keymaps/buttery/keymap_def.json ../../../keyboards/georgi/keymaps/buttery/keymap.c && \ +cd ../../../ && \ +make butterstick && \ +make georgi
\ No newline at end of file diff --git a/users/dennytom/chording_engine/tests/test_keymap_def.json b/users/dennytom/chording_engine/tests/test_keymap_def.json new file mode 100644 index 000000000..eae2ed37c --- /dev/null +++ b/users/dennytom/chording_engine/tests/test_keymap_def.json @@ -0,0 +1,145 @@ +{ + "keys": [ + "TOP1", "TOP2", "TOP3", "TOP4", "TOP5", "TOP6", "TOP7", "TOP8", "TOP9", "TOP0", + "BOT1", "BOT2", "BOT3", "BOT4", "BOT5", "BOT6", "BOT7", "BOT8", "BOT9", "BOT0" + ], + "parameters": { + "do_not_include_QMK": true, + "layout_function_name": "LAYOUT_test", + "chord_timeout": 100, + "dance_timeout": 200, + "leader_timeout": 750, + "tap_timeout": 50, + "command_max_length": 5, + "leader_max_length": 5, + "dynamic_macro_max_length": 20, + "string_max_length": 16, + "long_press_multiplier": 3, + "default_pseudolayer": "QWERTY" + }, + "layers": [ + { + "type": "auto" + }, + { + "type": "manual", + "keycodes": ["KC_1", "KC_2", "KC_3", "KC_4", "KC_5", "KC_6", "KC_7", "KC_8", "KC_9", "KC_0", + "KC_Q", "KC_W", "KC_E", "KC_R", "KC_T", "KC_Y", "KC_U", "KC_I", "KC_O", "KC_P" + ] + } + ], + "chord_sets": [ + { + "name": "rows", + "chords": [ + ["TOP1"], ["TOP2"], ["TOP3"], ["TOP4"], ["TOP5"], ["TOP6"], ["TOP7"], ["TOP8"], ["TOP9"], ["TOP0"], + ["TOP1", "BOT1"], ["TOP2", "BOT2"], ["TOP3", "BOT3"], ["TOP4", "BOT4"], ["TOP5", "BOT5"], ["TOP6", "BOT6"], ["TOP7", "BOT7"], ["TOP8", "BOT8"], ["TOP9", "BOT9"], ["TOP0", "BOT0"], + ["BOT1"], ["BOT2"], ["BOT3"], ["BOT4"], ["BOT5"], ["BOT6"], ["BOT7"], ["BOT8"], ["BOT9"], ["BOT0"] + ] + }, + { + "name": "cols", + "chords": [ + ["TOP1", "TOP2"], ["TOP2", "TOP3"], ["TOP3", "TOP4"], ["TOP4", "TOP5"], ["TOP5", "TOP6"], ["TOP6", "TOP7"], ["TOP7", "TOP8"], ["TOP8", "TOP9"], ["TOP9", "TOP0"], + ["TOP1", "TOP2", "BOT1", "BOT2"], ["TOP2", "TOP3", "BOT2", "BOT3"], ["TOP3", "TOP4", "BOT3", "BOT4"], ["TOP4", "TOP5", "BOT4", "BOT5"], ["TOP5", "TOP6", "BOT5", "BOT6"], ["TOP6", "TOP7", "BOT6", "BOT7"], ["TOP7", "TOP8", "BOT7", "BOT8"], ["TOP8", "TOP9", "BOT8", "BOT9"], ["TOP9", "TOP0", "BOT9", "BOT0"], + ["BOT1", "BOT2"], ["BOT2", "BOT3"], ["BOT3", "BOT4"], ["BOT4", "BOT5"], ["BOT5", "BOT6"], ["BOT6", "BOT7"], ["BOT7", "BOT8"], ["BOT8", "BOT9"], ["BOT9", "BOT0"] + ] + }, + { + "name": "asetniop", + "chords": [ + ["TOP1"], ["TOP2"], ["TOP3"], ["TOP4"], ["TOP7"], ["TOP8"], ["TOP9"], ["TOP0"], + ["TOP1", "TOP2"], ["TOP2", "TOP3"], ["TOP3", "TOP4"], ["TOP4", "TOP7"], ["TOP7", "TOP8"], ["TOP8", "TOP9"], ["TOP9", "TOP0"], + ["TOP1", "TOP3"], ["TOP2", "TOP4"], ["TOP3", "TOP7"], ["TOP4", "TOP8"], ["TOP7", "TOP9"], ["TOP8", "TOP0"], + ["TOP1", "TOP4"], ["TOP2", "TOP7"], ["TOP3", "TOP8"], ["TOP4", "TOP9"], ["TOP7", "TOP0"], + ["TOP1", "TOP7"], ["TOP2", "TOP8"], ["TOP3", "TOP9"], ["TOP4", "TOP0"], + ["TOP1", "TOP8"], ["TOP2", "TOP9"], ["TOP3", "TOP0"], + ["TOP1", "TOP9"], ["TOP2", "TOP0"], + ["TOP1", "TOP0"] + ] + } + ], + "pseudolayers": [ + { + "name": "ALWAYS_ON", + "chords": [ + { + "type": "visual", + "chord": [ + " ", " ", " ", " ", " ", " ", " ", "X", "X", "X", + " ", " ", " ", " ", " ", " ", " ", "X", "X", "X" + ], + "keycode": "MO(FNC, NUM)" + } + ] + }, + { + "name": "QWERTY", + "chords": [ + { + "type": "chord_set", + "set": "rows", + "keycodes": [ + "Q", "W", "E", "R", "T", "Y", "U", "I", "O", "P", + "A", "S", "D", "F", "G", "H", "J", "K", "L", ";", + "AS(Z)", "KK(X, LCTL)", "KL(C, NUM)", "KM(V, LALT)", "B", "N", "M", "COMMA", ".", "M(double_dance, KC_9, KC_0)" + ] + }, + { + "type": "chord_set", + "set": "cols", + "keycodes": [ + "ESC", "", "", "", "", "", "", "", "", + "", "LEAD", "", "", "CMD", "", "", "", "", + "LSFT", "O(LSFT)", "", "", "", "O(NUM)", "MO(NUM)", "", "DF(NUM)" + ] + }, + { + "type": "visual", + "chord": [ + "X", "X", " ", " ", " ", " ", " ", " ", " ", " ", + "X", "X", " ", " ", " ", " ", " ", " ", " ", " " + ], + "keycode": "LOCK" + } + ] + }, + { + "name": "NUM", + "chords": [ + { + "type": "chord_set", + "set": "rows", + "keycodes": [ + "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", + "", "", "", "", "", "", "", "", "", "", + "", "KC_LEFT", "D(KC_1, KC_2, KC_3)", "", "", "", "DM_RECORD", "DM_NEXT", "DM_END", "DM_PLAY" + ] + }, + { + "type": "simple", + "chord": ["BOT1"], + "keycode": "MK(KC_LCTL, KC_LSFT)" + } + ] + }, + { + "name": "FNC", + "chords": [] + } + ], + "leader_sequences": [ + { + "name": "fnc_L1", + "function": "void fnc_L1(void) { key_in(KC_A); clear_keyboard(); }", + "sequence": ["KC_O", "KC_P"] + }, + { + "name": "fnc_L2", + "function": "void fnc_L2(void) { key_in(KC_S); clear_keyboard(); }", + "sequence": ["KC_P", "KC_O"] + } + ], + "extra_code": "void double_dance(const struct Chord* self) {\n switch (*self->state) {\n case ACTIVATED:\n *self->counter = (*self->counter + 1) % 2;\n break;\n case PRESS_FROM_ACTIVE:\n if (*self->counter == 1) {\n key_in(self->value1);\n } else {\n key_in(self->value2);\n }\n *self->state = FINISHED_FROM_ACTIVE;\n break;\n case FINISHED:\n if (*self->counter == 1) {\n tap_key(self->value1);\n } else {\n tap_key(self->value2);\n }\n *self->counter = 0;\n *self->state = IDLE;\n break;\n case RESTART:\n if (*self->counter == 1) {\n key_out(self->value1);\n } else {\n key_out(self->value2);\n }\n *self->counter = 0;\n break;\n default:\n break;\n }\n}\n", + "extra_dependencies": [] +}
\ No newline at end of file diff --git a/users/dennytom/chording_engine/tests/test_quick.sh b/users/dennytom/chording_engine/tests/test_quick.sh new file mode 100644 index 000000000..3740122e2 --- /dev/null +++ b/users/dennytom/chording_engine/tests/test_quick.sh @@ -0,0 +1,6 @@ +#!/bin/sh + +cd .. +python3 parser.py tests/test_keymap_def.json tests/test_keymap.c && \ +gcc -g tests/test.c -o tests/test && \ +tests/test
\ No newline at end of file |