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

1625 lines
56 KiB

Merge ChibiOS and LUFA descriptor support (#2362) * Move lufa descriptor to protocol/usb_descriptor * Try to compile usb_descriptor on ChibiOS * Add lufa_utils for ChibiOS Lufa USB descriptors for ChibiOS * More lufa_util compatibility fixes * First compiling version of shared USB descriptor * Send the usb descriptors * Fix the CONSOLE output on ChibiOS * Add errors for unsupported interfaces * Enable support for vitual serial port USB descriptors * Implement virtual serial port for ChibiOS * Cleanup the lufa_utils Use the default lufa header files * Add raw hid support for ChibiOS This is completely untested * Enable midi compilation on ChibiOS * Move midi functionality out of lufa.c * Don't register sysex callback when not needed * ChibiOS compilation fixes * Update ChibiOS submodule * Fix the Midi USB descriptor It didn't work properly when both Midi and Virtual serial port was enabled. * Add MIDI support for ChibiOS * Fix USB descriptor strings on ChibiOS * Use serial usb driver for raw hid * Generalize the ChibiOS stream like drivers This makes the initialization much more simple and eliminates a lot of the code duplication. * Convert console output to chibios stream driver * Fixes for ChibiOS update * Update the ChibiOS contrib submodule To include the usb data toggle synchronization fixes * Fix duplicate reset enumeration on ChibiOS * Add missing include * Add number of endpoints check for ChibiOS * Enable serial USB driver on all keyboards * Add missing includes when API is enabled withot midi * Add another missing inlcude
6 years ago
Merge ChibiOS and LUFA descriptor support (#2362) * Move lufa descriptor to protocol/usb_descriptor * Try to compile usb_descriptor on ChibiOS * Add lufa_utils for ChibiOS Lufa USB descriptors for ChibiOS * More lufa_util compatibility fixes * First compiling version of shared USB descriptor * Send the usb descriptors * Fix the CONSOLE output on ChibiOS * Add errors for unsupported interfaces * Enable support for vitual serial port USB descriptors * Implement virtual serial port for ChibiOS * Cleanup the lufa_utils Use the default lufa header files * Add raw hid support for ChibiOS This is completely untested * Enable midi compilation on ChibiOS * Move midi functionality out of lufa.c * Don't register sysex callback when not needed * ChibiOS compilation fixes * Update ChibiOS submodule * Fix the Midi USB descriptor It didn't work properly when both Midi and Virtual serial port was enabled. * Add MIDI support for ChibiOS * Fix USB descriptor strings on ChibiOS * Use serial usb driver for raw hid * Generalize the ChibiOS stream like drivers This makes the initialization much more simple and eliminates a lot of the code duplication. * Convert console output to chibios stream driver * Fixes for ChibiOS update * Update the ChibiOS contrib submodule To include the usb data toggle synchronization fixes * Fix duplicate reset enumeration on ChibiOS * Add missing include * Add number of endpoints check for ChibiOS * Enable serial USB driver on all keyboards * Add missing includes when API is enabled withot midi * Add another missing inlcude
6 years ago
Merge ChibiOS and LUFA descriptor support (#2362) * Move lufa descriptor to protocol/usb_descriptor * Try to compile usb_descriptor on ChibiOS * Add lufa_utils for ChibiOS Lufa USB descriptors for ChibiOS * More lufa_util compatibility fixes * First compiling version of shared USB descriptor * Send the usb descriptors * Fix the CONSOLE output on ChibiOS * Add errors for unsupported interfaces * Enable support for vitual serial port USB descriptors * Implement virtual serial port for ChibiOS * Cleanup the lufa_utils Use the default lufa header files * Add raw hid support for ChibiOS This is completely untested * Enable midi compilation on ChibiOS * Move midi functionality out of lufa.c * Don't register sysex callback when not needed * ChibiOS compilation fixes * Update ChibiOS submodule * Fix the Midi USB descriptor It didn't work properly when both Midi and Virtual serial port was enabled. * Add MIDI support for ChibiOS * Fix USB descriptor strings on ChibiOS * Use serial usb driver for raw hid * Generalize the ChibiOS stream like drivers This makes the initialization much more simple and eliminates a lot of the code duplication. * Convert console output to chibios stream driver * Fixes for ChibiOS update * Update the ChibiOS contrib submodule To include the usb data toggle synchronization fixes * Fix duplicate reset enumeration on ChibiOS * Add missing include * Add number of endpoints check for ChibiOS * Enable serial USB driver on all keyboards * Add missing includes when API is enabled withot midi * Add another missing inlcude
6 years ago
Velocikey: Match RGB animation speed to typing speed (#3754) * Draft commit of typing speed RGB control * More information in the readme * Support all RGB animation modes (Fixes #1) * Added support for all RGB light modes to use typing speed Except christmas lights because that is seizure-inducing at high speeds! * Introduced a value range specific to each RGB mode Because some modes are a little too much when running at full speed! * Update readme.md * Update readme.md * Re-arrange typing_speed definitions (Fixes #5) (#6) * Re-arrange variable definitions to avoid including quantum.h from rgblight.c * Fix a compilation error when trying to run make test:all * Tweaks to the typing speed decay rate * Renamed to momentum; moved implementation into dedicated files * Groundwork for toggling momentum on/off (currently always on) * Add EEPROM toggle for momentum-matching * Moved momentum out of RGBLIGHT_ENABLE toggles so it's more generic * Move momentum decay task out of rgblight_task() * Fix missing momentum.h in lufa.c * Experimental LED support (untested) * Draft commit of typing speed RGB control * More information in the readme * Support all RGB animation modes (Fixes #1) * Added support for all RGB light modes to use typing speed Except christmas lights because that is seizure-inducing at high speeds! * Introduced a value range specific to each RGB mode Because some modes are a little too much when running at full speed! * Update readme.md * Update readme.md * Re-arrange typing_speed definitions (Fixes #5) (#6) * Re-arrange variable definitions to avoid including quantum.h from rgblight.c * Fix a compilation error when trying to run make test:all * Tweaks to the typing speed decay rate * Renamed to momentum; moved implementation into dedicated files * Groundwork for toggling momentum on/off (currently always on) * Add EEPROM toggle for momentum-matching * Moved momentum out of RGBLIGHT_ENABLE toggles so it's more generic * Move momentum decay task out of rgblight_task() * Fix missing momentum.h in lufa.c * Added documentation * Renamed feature to velocikey * Reverted readme to original state * Correct the readme title * Updated feature name in the docs * Update EECONFIG name * Add compile-time toggles for velocikey * Update feature documentation * Revert "Merge branch 'led-support' into master" This reverts commit e123ff5febf61639b9a9020748e1c2e2313460ff, reversing changes made to df111a55b9d4929182e16108b1c0ead15b16df97. * Move velocikey EECONFIG definition to depend on VELOCIKEY_ENABLE * Rename decay_task function to decelerate * Apply suggestions from code review Co-Authored-By: chrislewisdev <chris@chrislewisdev.com> * Re-order eeconfig definitions * Apply coding conventions * Apply #ifdef check in lufa.c * Refactored interval time checks into one functionc * Small rename * Fix unused function error for layouts not using all rgb effects * Only update EEPROM if Velocikey is enabled * Incorporate code review feedback * Small adjustment to top-end decay rate * Add Velocikey documentation to table of contents * Bring tetris:default keymap size down by disabling audio
5 years ago
Velocikey: Match RGB animation speed to typing speed (#3754) * Draft commit of typing speed RGB control * More information in the readme * Support all RGB animation modes (Fixes #1) * Added support for all RGB light modes to use typing speed Except christmas lights because that is seizure-inducing at high speeds! * Introduced a value range specific to each RGB mode Because some modes are a little too much when running at full speed! * Update readme.md * Update readme.md * Re-arrange typing_speed definitions (Fixes #5) (#6) * Re-arrange variable definitions to avoid including quantum.h from rgblight.c * Fix a compilation error when trying to run make test:all * Tweaks to the typing speed decay rate * Renamed to momentum; moved implementation into dedicated files * Groundwork for toggling momentum on/off (currently always on) * Add EEPROM toggle for momentum-matching * Moved momentum out of RGBLIGHT_ENABLE toggles so it's more generic * Move momentum decay task out of rgblight_task() * Fix missing momentum.h in lufa.c * Experimental LED support (untested) * Draft commit of typing speed RGB control * More information in the readme * Support all RGB animation modes (Fixes #1) * Added support for all RGB light modes to use typing speed Except christmas lights because that is seizure-inducing at high speeds! * Introduced a value range specific to each RGB mode Because some modes are a little too much when running at full speed! * Update readme.md * Update readme.md * Re-arrange typing_speed definitions (Fixes #5) (#6) * Re-arrange variable definitions to avoid including quantum.h from rgblight.c * Fix a compilation error when trying to run make test:all * Tweaks to the typing speed decay rate * Renamed to momentum; moved implementation into dedicated files * Groundwork for toggling momentum on/off (currently always on) * Add EEPROM toggle for momentum-matching * Moved momentum out of RGBLIGHT_ENABLE toggles so it's more generic * Move momentum decay task out of rgblight_task() * Fix missing momentum.h in lufa.c * Added documentation * Renamed feature to velocikey * Reverted readme to original state * Correct the readme title * Updated feature name in the docs * Update EECONFIG name * Add compile-time toggles for velocikey * Update feature documentation * Revert "Merge branch 'led-support' into master" This reverts commit e123ff5febf61639b9a9020748e1c2e2313460ff, reversing changes made to df111a55b9d4929182e16108b1c0ead15b16df97. * Move velocikey EECONFIG definition to depend on VELOCIKEY_ENABLE * Rename decay_task function to decelerate * Apply suggestions from code review Co-Authored-By: chrislewisdev <chris@chrislewisdev.com> * Re-order eeconfig definitions * Apply coding conventions * Apply #ifdef check in lufa.c * Refactored interval time checks into one functionc * Small rename * Fix unused function error for layouts not using all rgb effects * Only update EEPROM if Velocikey is enabled * Incorporate code review feedback * Small adjustment to top-end decay rate * Add Velocikey documentation to table of contents * Bring tetris:default keymap size down by disabling audio
5 years ago
Backlighting for JJ40 and underglow initialisation code (#2260) * Cleanup Mechmini keymap. Once the custom RGB function is defined, there is no need to manually handle RGB code. * Change default to KEYMAP_MIT, not KEYMAP_OFFSET * Add custom RGB code for JJ40 * Reset Mechmini advertised power draw to 500. Will have to test actual maximum power draw later. * RGB working on JJ40. * Fix: saturation increase/decrease flipped * Add new directory for my custom keymap with RGB keycodes * Swap LAlt and LGUI * Update JJ40 max power draw with measured value * Update: fun40 rules.mk to enable underglow; earlier failed Travis CI * Fix: init RGB LEDs on boot. Also added HHKB-like keymap for XD60. * Super rudimentary backlight test, init RGB LEDs on boot * Backlighting works - stays on for now * Toggling working * Now can override backlight.c functions. Problem was functions in backlight.c weren't called before due to a lack of matrix_scan_quantum() in matrix.c * Timers not working * Delete global.h * Cleanup * Compiles * Good sign: LEDs stop working again * Handle timer1 overflow * Progress: fix: forgot to init * Backlighting fully working now except breathing. * Revert keymap to original keycodes * Update XD60 keymap README * Update JJ40 keymap with backlight toggles * Breathing working just fine. * Update references * Add backlight_set() call * Cleanup code to disable backlight * Fix: does not compile * Fix: missing call to rgblight_task. * Testing with BACKLIGHT_BREATHING * Cleanup * Cleanup comments * More commenting cleanup. * Do not enable BACKLIGHT_BREATHING by default
6 years ago
Velocikey: Match RGB animation speed to typing speed (#3754) * Draft commit of typing speed RGB control * More information in the readme * Support all RGB animation modes (Fixes #1) * Added support for all RGB light modes to use typing speed Except christmas lights because that is seizure-inducing at high speeds! * Introduced a value range specific to each RGB mode Because some modes are a little too much when running at full speed! * Update readme.md * Update readme.md * Re-arrange typing_speed definitions (Fixes #5) (#6) * Re-arrange variable definitions to avoid including quantum.h from rgblight.c * Fix a compilation error when trying to run make test:all * Tweaks to the typing speed decay rate * Renamed to momentum; moved implementation into dedicated files * Groundwork for toggling momentum on/off (currently always on) * Add EEPROM toggle for momentum-matching * Moved momentum out of RGBLIGHT_ENABLE toggles so it's more generic * Move momentum decay task out of rgblight_task() * Fix missing momentum.h in lufa.c * Experimental LED support (untested) * Draft commit of typing speed RGB control * More information in the readme * Support all RGB animation modes (Fixes #1) * Added support for all RGB light modes to use typing speed Except christmas lights because that is seizure-inducing at high speeds! * Introduced a value range specific to each RGB mode Because some modes are a little too much when running at full speed! * Update readme.md * Update readme.md * Re-arrange typing_speed definitions (Fixes #5) (#6) * Re-arrange variable definitions to avoid including quantum.h from rgblight.c * Fix a compilation error when trying to run make test:all * Tweaks to the typing speed decay rate * Renamed to momentum; moved implementation into dedicated files * Groundwork for toggling momentum on/off (currently always on) * Add EEPROM toggle for momentum-matching * Moved momentum out of RGBLIGHT_ENABLE toggles so it's more generic * Move momentum decay task out of rgblight_task() * Fix missing momentum.h in lufa.c * Added documentation * Renamed feature to velocikey * Reverted readme to original state * Correct the readme title * Updated feature name in the docs * Update EECONFIG name * Add compile-time toggles for velocikey * Update feature documentation * Revert "Merge branch 'led-support' into master" This reverts commit e123ff5febf61639b9a9020748e1c2e2313460ff, reversing changes made to df111a55b9d4929182e16108b1c0ead15b16df97. * Move velocikey EECONFIG definition to depend on VELOCIKEY_ENABLE * Rename decay_task function to decelerate * Apply suggestions from code review Co-Authored-By: chrislewisdev <chris@chrislewisdev.com> * Re-order eeconfig definitions * Apply coding conventions * Apply #ifdef check in lufa.c * Refactored interval time checks into one functionc * Small rename * Fix unused function error for layouts not using all rgb effects * Only update EEPROM if Velocikey is enabled * Incorporate code review feedback * Small adjustment to top-end decay rate * Add Velocikey documentation to table of contents * Bring tetris:default keymap size down by disabling audio
5 years ago
Overhaul Unicode Common functionality (#4325) * Standardize the Unicode EEPROM code * Remove unicode init from process_record_* functions * Add unicode init to where it belongs: matrix_init_quantum * Move Unicode proccessing to unicode common * Add audio feedback to input mode keys to drive konstantin up a wall * Tap_code cleanup * Update keycodes * Update unicode documentation * Update unicode keycodes for consistency/easier merge * Add Audio Feedback section * Remove Functions from feature page And link to the file instead. Link to specific lines later on. * Fix spacing Co-Authored-By: drashna <drashna@live.com> * Because I missed it! Co-Authored-By: drashna <drashna@live.com> * Fix spacing Co-Authored-By: drashna <drashna@live.com> * SPAAAAAAAAAACing Co-Authored-By: drashna <drashna@live.com> * Add BSD for future compatibility * Thought I fixed that! Co-Authored-By: drashna <drashna@live.com> * non-breaking Co-Authored-By: drashna <drashna@live.com> * Considered that Co-Authored-By: drashna <drashna@live.com> * Yuuup Co-Authored-By: drashna <drashna@live.com> * consistency Co-Authored-By: drashna <drashna@live.com> * white spaces .... copied from elsewhere Co-Authored-By: drashna <drashna@live.com> * white spaces Co-Authored-By: drashna <drashna@live.com> * white spaces Co-Authored-By: drashna <drashna@live.com> * Update keycode defines * Fix Linux Song * Update all of the songs * Cleanup * Move and update check to ensure only one unicode method is enabled * Update quantum/quantum_keycodes.h * Update documentation * Wordsmithing and cleanup * Format unicode_common (#13) * case alignment * process_record_unicode_common → process_unicode_common * Move song arrays into function where they're used, align preprocessor directives * Swap the order of UC_WIN and UC_BSD * Update Unicode docs * Reorder Unicode mode stuff to match the order of input mode constants * Fix capitalization in doc subtitle * Readd BSD and OSX_RALT songs * Reword BSD note in docs * Readd BSD keycode description * Reword explanation of input on different platforms * Steal vomindoraan's input mode documentation Co-Authored-By: vomindoraan (vomindoraan@gmail.com) * Willingly give Drashna the rest of my Unicode doc improvements * Wordsmithing Co-Authored-By: drashna <drashna@live.com> * remove merge artifacts * Unicode common cleanup (#17) * Standardize the Unicode EEPROM code * Remove unicode init from process_record_* functions * Add unicode init to where it belongs: matrix_init_quantum * Move Unicode proccessing to unicode common * Add audio feedback to input mode keys to drive konstantin up a wall * Tap_code cleanup * Update keycodes * Update unicode documentation * Update unicode keycodes for consistency/easier merge * Add Audio Feedback section * Remove Functions from feature page And link to the file instead. Link to specific lines later on. * Fix white spaces Co-Authored-By: drashna <drashna@live.com> * Fix spacing Co-Authored-By: drashna <drashna@live.com> * Because I missed it! Co-Authored-By: drashna <drashna@live.com> * Fix spacing Co-Authored-By: drashna <drashna@live.com> * SPAAAAAAAAAACing Co-Authored-By: drashna <drashna@live.com> * white spaces Co-Authored-By: drashna <drashna@live.com> * Add BSD for future compatibility * Thought I fixed that! Co-Authored-By: drashna <drashna@live.com> * non-breaking Co-Authored-By: drashna <drashna@live.com> * Considered that Co-Authored-By: drashna <drashna@live.com> * Yuuup Co-Authored-By: drashna <drashna@live.com> * consistency Co-Authored-By: drashna <drashna@live.com> * white spaces .... copied from elsewhere Co-Authored-By: drashna <drashna@live.com> * white spaces Co-Authored-By: drashna <drashna@live.com> * white spaces Co-Authored-By: drashna <drashna@live.com> * Update keycode defines * Fix Linux Song * Update all of the songs * Cleanup * Move and update check to ensure only one unicode method is enabled * Update quantum/quantum_keycodes.h * Update documentation * Wordsmithing and cleanup * Format unicode_common (#13) * case alignment * process_record_unicode_common → process_unicode_common * Move song arrays into function where they're used, align preprocessor directives * Swap the order of UC_WIN and UC_BSD * Update Unicode docs * Reorder Unicode mode stuff to match the order of input mode constants * Fix capitalization in doc subtitle * Readd BSD and OSX_RALT songs * Reword BSD note in docs * Readd BSD keycode description * Reword explanation of input on different platforms * Steal vomindoraan's input mode documentation Co-Authored-By: vomindoraan (vomindoraan@gmail.com) * Willingly give Drashna the rest of my Unicode doc improvements * Wordsmithing Co-Authored-By: drashna <drashna@live.com> * Rearrange process_unicode_common functions * Make Unicode input mode constants (UC_*) an enum * Simplify unicode_input_start/finish code * Make the key used for WinCompose configurable * Remove UC_OSX_RALT in favor of setting the key with UNICODE_OSX_KEY * Update Unicode input mode doc * Add descriptions and rearrange definitions in process_unicode_common.h * Add registry command to Unicode docs + misc updates * Reword an explanation in Unicode docs * Add TODO comment * Remove trailing whitespace * Improve Cycling documentation * Add Unicode Input method Cycling support (#19) * Standardize the Unicode EEPROM code * Remove unicode init from process_record_* functions * Add unicode init to where it belongs: matrix_init_quantum * Move Unicode proccessing to unicode common * Add audio feedback to input mode keys to drive konstantin up a wall * Tap_code cleanup * Update keycodes * Update unicode documentation * Update unicode keycodes for consistency/easier merge * Add Audio Feedback section * Remove Functions from feature page And link to the file instead. Link to specific lines later on. * Fix white spaces Co-Authored-By: drashna <drashna@live.com> * Fix spacing Co-Authored-By: drashna <drashna@live.com> * Because I missed it! Co-Authored-By: drashna <drashna@live.com> * Fix spacing Co-Authored-By: drashna <drashna@live.com> * SPAAAAAAAAAACing Co-Authored-By: drashna <drashna@live.com> * white spaces Co-Authored-By: drashna <drashna@live.com> * Add BSD for future compatibility * Thought I fixed that! Co-Authored-By: drashna <drashna@live.com> * non-breaking Co-Authored-By: drashna <drashna@live.com> * Considered that Co-Authored-By: drashna <drashna@live.com> * Yuuup Co-Authored-By: drashna <drashna@live.com> * consistency Co-Authored-By: drashna <drashna@live.com> * white spaces .... copied from elsewhere Co-Authored-By: drashna <drashna@live.com> * white spaces Co-Authored-By: drashna <drashna@live.com> * white spaces Co-Authored-By: drashna <drashna@live.com> * Update keycode defines * Fix Linux Song * Update all of the songs * Cleanup * Move and update check to ensure only one unicode method is enabled * Update quantum/quantum_keycodes.h * Update documentation * Wordsmithing and cleanup * Format unicode_common (#13) * case alignment * process_record_unicode_common → process_unicode_common * Move song arrays into function where they're used, align preprocessor directives * Swap the order of UC_WIN and UC_BSD * Update Unicode docs * Reorder Unicode mode stuff to match the order of input mode constants * Fix capitalization in doc subtitle * Readd BSD and OSX_RALT songs * Reword BSD note in docs * Readd BSD keycode description * Reword explanation of input on different platforms * Steal vomindoraan's input mode documentation Co-Authored-By: vomindoraan (vomindoraan@gmail.com) * Willingly give Drashna the rest of my Unicode doc improvements * Wordsmithing Co-Authored-By: drashna <drashna@live.com> * Rearrange process_unicode_common functions * Make Unicode input mode constants (UC_*) an enum * Simplify unicode_input_start/finish code * Make the key used for WinCompose configurable * Remove UC_OSX_RALT in favor of setting the key with UNICODE_OSX_KEY * Update Unicode input mode doc * Add descriptions and rearrange definitions in process_unicode_common.h * Add registry command to Unicode docs + misc updates * Reword an explanation in Unicode docs * Add TODO comment * Add cycle_unicode_input_mode and UNICODE_SELECTED_MODES macro * Add an option for making cycle changes persistent * Add debug prints to functions that change input_mode * Use cycle_unicode_input_mode in whitefox/konstantin * Add persist_unicode_input_mode function * Add offset to cycle to allow stepping in reverse * Add keycodes: UNICODE_MODE_FORWARD, UNICODE_MODE_REVERSE Aliases: UC_MOD, UC_RMOD (respectively) * REVERT ME: Undo changes to whitefox/konstantin to avoid conflicts * Fix wrong constant name * Revert "REVERT ME: Undo changes to whitefox/konstantin to avoid conflicts" This reverts commit 42676bf251fc8e3823f5e614dad6e510ba47a2f3. * Change dprintf text * Give selected modes priority over EEPROM when !UNICODE_CYCLE_PERSIST * Remove trailing whitespace * Cleanup of RALT code and unicode compilation stuff * Remove else for unicode handling
5 years ago
Moves features to their own files (process_*), adds tap dance feature (#460) * non-working commit * working * subprojects implemented for planck * pass a subproject variable through to c * consolidates clueboard revisions * thanks for letting me know about conflicts.. * turn off audio for yang's * corrects starting paths for subprojects * messing around with travis * semicolon * travis script * travis script * script for travis * correct directory (probably), amend files to commit * remove origin before adding * git pull, correct syntax * git checkout * git pull origin branch * where are we? * where are we? * merging * force things to happen * adds commit message, adds add * rebase, no commit message * rebase branch * idk! * try just pull * fetch - merge * specify repo branch * checkout * goddammit * merge? idk * pls * after all * don't split up keyboards * syntax * adds quick for all-keyboards * trying out new script * script update * lowercase * all keyboards * stop replacing compiled.hex automatically * adds if statement * skip automated build branches * forces push to automated build branch * throw an add in there * upstream? * adds AUTOGEN * ignore all .hex files again * testing out new repo * global ident * generate script, keyboard_keymap.hex * skip generation for now, print pandoc info, submodule update * try trusty * and sudo * try generate * updates subprojects to keyboards * no idea * updates to keyboards * cleans up clueboard stuff * setup to use local readme * updates cluepad, planck experimental * remove extra led.c [ci skip] * audio and midi moved over to separate files * chording, leader, unicode separated * consolidate each [skip ci] * correct include * quantum: Add a tap dance feature (#451) * quantum: Add a tap dance feature With this feature one can specify keys that behave differently, based on the amount of times they have been tapped, and when interrupted, they get handled before the interrupter. To make it clear how this is different from `ACTION_FUNCTION_TAP`, lets explore a certain setup! We want one key to send `Space` on single tap, but `Enter` on double-tap. With `ACTION_FUNCTION_TAP`, it is quite a rain-dance to set this up, and has the problem that when the sequence is interrupted, the interrupting key will be send first. Thus, `SPC a` will result in `a SPC` being sent, if they are typed within `TAPPING_TERM`. With the tap dance feature, that'll come out as `SPC a`, correctly. The implementation hooks into two parts of the system, to achieve this: into `process_record_quantum()`, and the matrix scan. We need the latter to be able to time out a tap sequence even when a key is not being pressed, so `SPC` alone will time out and register after `TAPPING_TERM` time. But lets start with how to use it, first! First, you will need `TAP_DANCE_ENABLE=yes` in your `Makefile`, because the feature is disabled by default. This adds a little less than 1k to the firmware size. Next, you will want to define some tap-dance keys, which is easiest to do with the `TD()` macro, that - similar to `F()`, takes a number, which will later be used as an index into the `tap_dance_actions` array. This array specifies what actions shall be taken when a tap-dance key is in action. Currently, there are two possible options: * `ACTION_TAP_DANCE_DOUBLE(kc1, kc2)`: Sends the `kc1` keycode when tapped once, `kc2` otherwise. * `ACTION_TAP_DANCE_FN(fn)`: Calls the specified function - defined in the user keymap - with the current state of the tap-dance action. The first option is enough for a lot of cases, that just want dual roles. For example, `ACTION_TAP_DANCE(KC_SPC, KC_ENT)` will result in `Space` being sent on single-tap, `Enter` otherwise. And that's the bulk of it! Do note, however, that this implementation does have some consequences: keys do not register until either they reach the tapping ceiling, or they time out. This means that if you hold the key, nothing happens, no repeat, no nothing. It is possible to detect held state, and register an action then too, but that's not implemented yet. Keys also unregister immediately after being registered, so you can't even hold the second tap. This is intentional, to be consistent. And now, on to the explanation of how it works! The main entry point is `process_tap_dance()`, called from `process_record_quantum()`, which is run for every keypress, and our handler gets to run early. This function checks whether the key pressed is a tap-dance key. If it is not, and a tap-dance was in action, we handle that first, and enqueue the newly pressed key. If it is a tap-dance key, then we check if it is the same as the already active one (if there's one active, that is). If it is not, we fire off the old one first, then register the new one. If it was the same, we increment the counter and the timer. This means that you have `TAPPING_TERM` time to tap the key again, you do not have to input all the taps within that timeframe. This allows for longer tap counts, with minimal impact on responsiveness. Our next stop is `matrix_scan_tap_dance()`. This handles the timeout of tap-dance keys. For the sake of flexibility, tap-dance actions can be either a pair of keycodes, or a user function. The latter allows one to handle higher tap counts, or do extra things, like blink the LEDs, fiddle with the backlighting, and so on. This is accomplished by using an union, and some clever macros. In the end, lets see a full example! ```c enum { CT_SE = 0, CT_CLN, CT_EGG }; /* Have the above three on the keymap, TD(CT_SE), etc... */ void dance_cln (qk_tap_dance_state_t *state) { if (state->count == 1) { register_code (KC_RSFT); register_code (KC_SCLN); unregister_code (KC_SCLN); unregister_code (KC_RSFT); } else { register_code (KC_SCLN); unregister_code (KC_SCLN); reset_tap_dance (state); } } void dance_egg (qk_tap_dance_state_t *state) { if (state->count >= 100) { SEND_STRING ("Safety dance!"); reset_tap_dance (state); } } const qk_tap_dance_action_t tap_dance_actions[] = { [CT_SE] = ACTION_TAP_DANCE_DOUBLE (KC_SPC, KC_ENT) ,[CT_CLN] = ACTION_TAP_DANCE_FN (dance_cln) ,[CT_EGG] = ACTION_TAP_DANCE_FN (dance_egg) }; ``` This addresses #426. Signed-off-by: Gergely Nagy <algernon@madhouse-project.org> * hhkb: Fix the build with the new tap-dance feature Signed-off-by: Gergely Nagy <algernon@madhouse-project.org> * tap_dance: Move process_tap_dance further down Process the tap dance stuff after midi and audio, because those don't process keycodes, but row/col positions. Signed-off-by: Gergely Nagy <algernon@madhouse-project.org> * tap_dance: Use conditionals instead of dummy functions To be consistent with how the rest of the quantum features are implemented, use ifdefs instead of dummy functions. Signed-off-by: Gergely Nagy <algernon@madhouse-project.org> * Merge branch 'master' into quantum-keypress-process # Conflicts: # Makefile # keyboards/planck/rev3/config.h # keyboards/planck/rev4/config.h * update build script
8 years ago
Moves features to their own files (process_*), adds tap dance feature (#460) * non-working commit * working * subprojects implemented for planck * pass a subproject variable through to c * consolidates clueboard revisions * thanks for letting me know about conflicts.. * turn off audio for yang's * corrects starting paths for subprojects * messing around with travis * semicolon * travis script * travis script * script for travis * correct directory (probably), amend files to commit * remove origin before adding * git pull, correct syntax * git checkout * git pull origin branch * where are we? * where are we? * merging * force things to happen * adds commit message, adds add * rebase, no commit message * rebase branch * idk! * try just pull * fetch - merge * specify repo branch * checkout * goddammit * merge? idk * pls * after all * don't split up keyboards * syntax * adds quick for all-keyboards * trying out new script * script update * lowercase * all keyboards * stop replacing compiled.hex automatically * adds if statement * skip automated build branches * forces push to automated build branch * throw an add in there * upstream? * adds AUTOGEN * ignore all .hex files again * testing out new repo * global ident * generate script, keyboard_keymap.hex * skip generation for now, print pandoc info, submodule update * try trusty * and sudo * try generate * updates subprojects to keyboards * no idea * updates to keyboards * cleans up clueboard stuff * setup to use local readme * updates cluepad, planck experimental * remove extra led.c [ci skip] * audio and midi moved over to separate files * chording, leader, unicode separated * consolidate each [skip ci] * correct include * quantum: Add a tap dance feature (#451) * quantum: Add a tap dance feature With this feature one can specify keys that behave differently, based on the amount of times they have been tapped, and when interrupted, they get handled before the interrupter. To make it clear how this is different from `ACTION_FUNCTION_TAP`, lets explore a certain setup! We want one key to send `Space` on single tap, but `Enter` on double-tap. With `ACTION_FUNCTION_TAP`, it is quite a rain-dance to set this up, and has the problem that when the sequence is interrupted, the interrupting key will be send first. Thus, `SPC a` will result in `a SPC` being sent, if they are typed within `TAPPING_TERM`. With the tap dance feature, that'll come out as `SPC a`, correctly. The implementation hooks into two parts of the system, to achieve this: into `process_record_quantum()`, and the matrix scan. We need the latter to be able to time out a tap sequence even when a key is not being pressed, so `SPC` alone will time out and register after `TAPPING_TERM` time. But lets start with how to use it, first! First, you will need `TAP_DANCE_ENABLE=yes` in your `Makefile`, because the feature is disabled by default. This adds a little less than 1k to the firmware size. Next, you will want to define some tap-dance keys, which is easiest to do with the `TD()` macro, that - similar to `F()`, takes a number, which will later be used as an index into the `tap_dance_actions` array. This array specifies what actions shall be taken when a tap-dance key is in action. Currently, there are two possible options: * `ACTION_TAP_DANCE_DOUBLE(kc1, kc2)`: Sends the `kc1` keycode when tapped once, `kc2` otherwise. * `ACTION_TAP_DANCE_FN(fn)`: Calls the specified function - defined in the user keymap - with the current state of the tap-dance action. The first option is enough for a lot of cases, that just want dual roles. For example, `ACTION_TAP_DANCE(KC_SPC, KC_ENT)` will result in `Space` being sent on single-tap, `Enter` otherwise. And that's the bulk of it! Do note, however, that this implementation does have some consequences: keys do not register until either they reach the tapping ceiling, or they time out. This means that if you hold the key, nothing happens, no repeat, no nothing. It is possible to detect held state, and register an action then too, but that's not implemented yet. Keys also unregister immediately after being registered, so you can't even hold the second tap. This is intentional, to be consistent. And now, on to the explanation of how it works! The main entry point is `process_tap_dance()`, called from `process_record_quantum()`, which is run for every keypress, and our handler gets to run early. This function checks whether the key pressed is a tap-dance key. If it is not, and a tap-dance was in action, we handle that first, and enqueue the newly pressed key. If it is a tap-dance key, then we check if it is the same as the already active one (if there's one active, that is). If it is not, we fire off the old one first, then register the new one. If it was the same, we increment the counter and the timer. This means that you have `TAPPING_TERM` time to tap the key again, you do not have to input all the taps within that timeframe. This allows for longer tap counts, with minimal impact on responsiveness. Our next stop is `matrix_scan_tap_dance()`. This handles the timeout of tap-dance keys. For the sake of flexibility, tap-dance actions can be either a pair of keycodes, or a user function. The latter allows one to handle higher tap counts, or do extra things, like blink the LEDs, fiddle with the backlighting, and so on. This is accomplished by using an union, and some clever macros. In the end, lets see a full example! ```c enum { CT_SE = 0, CT_CLN, CT_EGG }; /* Have the above three on the keymap, TD(CT_SE), etc... */ void dance_cln (qk_tap_dance_state_t *state) { if (state->count == 1) { register_code (KC_RSFT); register_code (KC_SCLN); unregister_code (KC_SCLN); unregister_code (KC_RSFT); } else { register_code (KC_SCLN); unregister_code (KC_SCLN); reset_tap_dance (state); } } void dance_egg (qk_tap_dance_state_t *state) { if (state->count >= 100) { SEND_STRING ("Safety dance!"); reset_tap_dance (state); } } const qk_tap_dance_action_t tap_dance_actions[] = { [CT_SE] = ACTION_TAP_DANCE_DOUBLE (KC_SPC, KC_ENT) ,[CT_CLN] = ACTION_TAP_DANCE_FN (dance_cln) ,[CT_EGG] = ACTION_TAP_DANCE_FN (dance_egg) }; ``` This addresses #426. Signed-off-by: Gergely Nagy <algernon@madhouse-project.org> * hhkb: Fix the build with the new tap-dance feature Signed-off-by: Gergely Nagy <algernon@madhouse-project.org> * tap_dance: Move process_tap_dance further down Process the tap dance stuff after midi and audio, because those don't process keycodes, but row/col positions. Signed-off-by: Gergely Nagy <algernon@madhouse-project.org> * tap_dance: Use conditionals instead of dummy functions To be consistent with how the rest of the quantum features are implemented, use ifdefs instead of dummy functions. Signed-off-by: Gergely Nagy <algernon@madhouse-project.org> * Merge branch 'master' into quantum-keypress-process # Conflicts: # Makefile # keyboards/planck/rev3/config.h # keyboards/planck/rev4/config.h * update build script
8 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
7 years ago
7 years ago
  1. /* Copyright 2016-2017 Jack Humbert
  2. *
  3. * This program is free software: you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation, either version 2 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include "quantum.h"
  17. #if !defined(RGBLIGHT_ENABLE) && !defined(RGB_MATRIX_ENABLE)
  18. # include "rgb.h"
  19. #endif
  20. #ifdef PROTOCOL_LUFA
  21. # include "outputselect.h"
  22. #endif
  23. #ifndef BREATHING_PERIOD
  24. # define BREATHING_PERIOD 6
  25. #endif
  26. #include "backlight.h"
  27. extern backlight_config_t backlight_config;
  28. #ifdef FAUXCLICKY_ENABLE
  29. # include "fauxclicky.h"
  30. #endif
  31. #ifdef API_ENABLE
  32. # include "api.h"
  33. #endif
  34. #ifdef MIDI_ENABLE
  35. # include "process_midi.h"
  36. #endif
  37. #ifdef VELOCIKEY_ENABLE
  38. # include "velocikey.h"
  39. #endif
  40. #ifdef HAPTIC_ENABLE
  41. # include "haptic.h"
  42. #endif
  43. #ifdef ENCODER_ENABLE
  44. # include "encoder.h"
  45. #endif
  46. #ifdef AUDIO_ENABLE
  47. # ifndef GOODBYE_SONG
  48. # define GOODBYE_SONG SONG(GOODBYE_SOUND)
  49. # endif
  50. # ifndef AG_NORM_SONG
  51. # define AG_NORM_SONG SONG(AG_NORM_SOUND)
  52. # endif
  53. # ifndef AG_SWAP_SONG
  54. # define AG_SWAP_SONG SONG(AG_SWAP_SOUND)
  55. # endif
  56. # ifndef CG_NORM_SONG
  57. # define CG_NORM_SONG SONG(AG_NORM_SOUND)
  58. # endif
  59. # ifndef CG_SWAP_SONG
  60. # define CG_SWAP_SONG SONG(AG_SWAP_SOUND)
  61. # endif
  62. float goodbye_song[][2] = GOODBYE_SONG;
  63. float ag_norm_song[][2] = AG_NORM_SONG;
  64. float ag_swap_song[][2] = AG_SWAP_SONG;
  65. float cg_norm_song[][2] = CG_NORM_SONG;
  66. float cg_swap_song[][2] = CG_SWAP_SONG;
  67. # ifdef DEFAULT_LAYER_SONGS
  68. float default_layer_songs[][16][2] = DEFAULT_LAYER_SONGS;
  69. # endif
  70. #endif
  71. static void do_code16(uint16_t code, void (*f)(uint8_t)) {
  72. switch (code) {
  73. case QK_MODS ... QK_MODS_MAX:
  74. break;
  75. default:
  76. return;
  77. }
  78. if (code & QK_LCTL) f(KC_LCTL);
  79. if (code & QK_LSFT) f(KC_LSFT);
  80. if (code & QK_LALT) f(KC_LALT);
  81. if (code & QK_LGUI) f(KC_LGUI);
  82. if (code < QK_RMODS_MIN) return;
  83. if (code & QK_RCTL) f(KC_RCTL);
  84. if (code & QK_RSFT) f(KC_RSFT);
  85. if (code & QK_RALT) f(KC_RALT);
  86. if (code & QK_RGUI) f(KC_RGUI);
  87. }
  88. static inline void qk_register_weak_mods(uint8_t kc) {
  89. add_weak_mods(MOD_BIT(kc));
  90. send_keyboard_report();
  91. }
  92. static inline void qk_unregister_weak_mods(uint8_t kc) {
  93. del_weak_mods(MOD_BIT(kc));
  94. send_keyboard_report();
  95. }
  96. static inline void qk_register_mods(uint8_t kc) {
  97. add_weak_mods(MOD_BIT(kc));
  98. send_keyboard_report();
  99. }
  100. static inline void qk_unregister_mods(uint8_t kc) {
  101. del_weak_mods(MOD_BIT(kc));
  102. send_keyboard_report();
  103. }
  104. void register_code16(uint16_t code) {
  105. if (IS_MOD(code) || code == KC_NO) {
  106. do_code16(code, qk_register_mods);
  107. } else {
  108. do_code16(code, qk_register_weak_mods);
  109. }
  110. register_code(code);
  111. }
  112. void unregister_code16(uint16_t code) {
  113. unregister_code(code);
  114. if (IS_MOD(code) || code == KC_NO) {
  115. do_code16(code, qk_unregister_mods);
  116. } else {
  117. do_code16(code, qk_unregister_weak_mods);
  118. }
  119. }
  120. void tap_code16(uint16_t code) {
  121. register_code16(code);
  122. #if TAP_CODE_DELAY > 0
  123. wait_ms(TAP_CODE_DELAY);
  124. #endif
  125. unregister_code16(code);
  126. }
  127. __attribute__((weak)) bool process_action_kb(keyrecord_t *record) { return true; }
  128. __attribute__((weak)) bool process_record_kb(uint16_t keycode, keyrecord_t *record) { return process_record_user(keycode, record); }
  129. __attribute__((weak)) bool process_record_user(uint16_t keycode, keyrecord_t *record) { return true; }
  130. void reset_keyboard(void) {
  131. clear_keyboard();
  132. #if defined(MIDI_ENABLE) && defined(MIDI_BASIC)
  133. process_midi_all_notes_off();
  134. #endif
  135. #ifdef AUDIO_ENABLE
  136. # ifndef NO_MUSIC_MODE
  137. music_all_notes_off();
  138. # endif
  139. uint16_t timer_start = timer_read();
  140. PLAY_SONG(goodbye_song);
  141. shutdown_user();
  142. while (timer_elapsed(timer_start) < 250) wait_ms(1);
  143. stop_all_notes();
  144. #else
  145. shutdown_user();
  146. wait_ms(250);
  147. #endif
  148. #ifdef HAPTIC_ENABLE
  149. haptic_shutdown();
  150. #endif
  151. // this is also done later in bootloader.c - not sure if it's neccesary here
  152. #ifdef BOOTLOADER_CATERINA
  153. *(uint16_t *)0x0800 = 0x7777; // these two are a-star-specific
  154. #endif
  155. bootloader_jump();
  156. }
  157. /* true if the last press of GRAVE_ESC was shifted (i.e. GUI or SHIFT were pressed), false otherwise.
  158. * Used to ensure that the correct keycode is released if the key is released.
  159. */
  160. static bool grave_esc_was_shifted = false;
  161. /* Convert record into usable keycode via the contained event. */
  162. uint16_t get_record_keycode(keyrecord_t *record) { return get_event_keycode(record->event); }
  163. /* Convert event into usable keycode. Checks the layer cache to ensure that it
  164. * retains the correct keycode after a layer change, if the key is still pressed.
  165. */
  166. uint16_t get_event_keycode(keyevent_t event) {
  167. #if !defined(NO_ACTION_LAYER) && !defined(STRICT_LAYER_RELEASE)
  168. /* TODO: Use store_or_get_action() or a similar function. */
  169. if (!disable_action_cache) {
  170. uint8_t layer;
  171. if (event.pressed) {
  172. layer = layer_switch_get_layer(event.key);
  173. update_source_layers_cache(event.key, layer);
  174. } else {
  175. layer = read_source_layers_cache(event.key);
  176. }
  177. return keymap_key_to_keycode(layer, event.key);
  178. } else
  179. #endif
  180. return keymap_key_to_keycode(layer_switch_get_layer(event.key), event.key);
  181. }
  182. /* Main keycode processing function. Hands off handling to other functions,
  183. * then processes internal Quantum keycodes, then processes ACTIONs.
  184. */
  185. bool process_record_quantum(keyrecord_t *record) {
  186. uint16_t keycode = get_record_keycode(record);
  187. // This is how you use actions here
  188. // if (keycode == KC_LEAD) {
  189. // action_t action;
  190. // action.code = ACTION_DEFAULT_LAYER_SET(0);
  191. // process_action(record, action);
  192. // return false;
  193. // }
  194. #ifdef VELOCIKEY_ENABLE
  195. if (velocikey_enabled() && record->event.pressed) {
  196. velocikey_accelerate();
  197. }
  198. #endif
  199. #ifdef TAP_DANCE_ENABLE
  200. preprocess_tap_dance(keycode, record);
  201. #endif
  202. if (!(
  203. #if defined(KEY_LOCK_ENABLE)
  204. // Must run first to be able to mask key_up events.
  205. process_key_lock(&keycode, record) &&
  206. #endif
  207. #if defined(AUDIO_ENABLE) && defined(AUDIO_CLICKY)
  208. process_clicky(keycode, record) &&
  209. #endif // AUDIO_CLICKY
  210. #ifdef HAPTIC_ENABLE
  211. process_haptic(keycode, record) &&
  212. #endif // HAPTIC_ENABLE
  213. #if defined(RGB_MATRIX_ENABLE)
  214. process_rgb_matrix(keycode, record) &&
  215. #endif
  216. process_record_kb(keycode, record) &&
  217. #if defined(MIDI_ENABLE) && defined(MIDI_ADVANCED)
  218. process_midi(keycode, record) &&
  219. #endif
  220. #ifdef AUDIO_ENABLE
  221. process_audio(keycode, record) &&
  222. #endif
  223. #ifdef STENO_ENABLE
  224. process_steno(keycode, record) &&
  225. #endif
  226. #if (defined(AUDIO_ENABLE) || (defined(MIDI_ENABLE) && defined(MIDI_BASIC))) && !defined(NO_MUSIC_MODE)
  227. process_music(keycode, record) &&
  228. #endif
  229. #ifdef TAP_DANCE_ENABLE
  230. process_tap_dance(keycode, record) &&
  231. #endif
  232. #if defined(UNICODE_ENABLE) || defined(UNICODEMAP_ENABLE) || defined(UCIS_ENABLE)
  233. process_unicode_common(keycode, record) &&
  234. #endif
  235. #ifdef LEADER_ENABLE
  236. process_leader(keycode, record) &&
  237. #endif
  238. #ifdef COMBO_ENABLE
  239. process_combo(keycode, record) &&
  240. #endif
  241. #ifdef PRINTING_ENABLE
  242. process_printer(keycode, record) &&
  243. #endif
  244. #ifdef AUTO_SHIFT_ENABLE
  245. process_auto_shift(keycode, record) &&
  246. #endif
  247. #ifdef TERMINAL_ENABLE
  248. process_terminal(keycode, record) &&
  249. #endif
  250. #ifdef SPACE_CADET_ENABLE
  251. process_space_cadet(keycode, record) &&
  252. #endif
  253. true)) {
  254. return false;
  255. }
  256. // Shift / paren setup
  257. switch (keycode) {
  258. case RESET:
  259. if (record->event.pressed) {
  260. reset_keyboard();
  261. }
  262. return false;
  263. case DEBUG:
  264. if (record->event.pressed) {
  265. debug_enable ^= 1;
  266. if (debug_enable) {
  267. print("DEBUG: enabled.\n");
  268. } else {
  269. print("DEBUG: disabled.\n");
  270. }
  271. }
  272. return false;
  273. case EEPROM_RESET:
  274. if (record->event.pressed) {
  275. eeconfig_init();
  276. }
  277. return false;
  278. #ifdef FAUXCLICKY_ENABLE
  279. case FC_TOG:
  280. if (record->event.pressed) {
  281. FAUXCLICKY_TOGGLE;
  282. }
  283. return false;
  284. case FC_ON:
  285. if (record->event.pressed) {
  286. FAUXCLICKY_ON;
  287. }
  288. return false;
  289. case FC_OFF:
  290. if (record->event.pressed) {
  291. FAUXCLICKY_OFF;
  292. }
  293. return false;
  294. #endif
  295. #if defined(RGBLIGHT_ENABLE) || defined(RGB_MATRIX_ENABLE)
  296. case RGB_TOG:
  297. // Split keyboards need to trigger on key-up for edge-case issue
  298. # ifndef SPLIT_KEYBOARD
  299. if (record->event.pressed) {
  300. # else
  301. if (!record->event.pressed) {
  302. # endif
  303. rgblight_toggle();
  304. }
  305. return false;
  306. case RGB_MODE_FORWARD:
  307. if (record->event.pressed) {
  308. uint8_t shifted = get_mods() & (MOD_BIT(KC_LSHIFT) | MOD_BIT(KC_RSHIFT));
  309. if (shifted) {
  310. rgblight_step_reverse();
  311. } else {
  312. rgblight_step();
  313. }
  314. }
  315. return false;
  316. case RGB_MODE_REVERSE:
  317. if (record->event.pressed) {
  318. uint8_t shifted = get_mods() & (MOD_BIT(KC_LSHIFT) | MOD_BIT(KC_RSHIFT));
  319. if (shifted) {
  320. rgblight_step();
  321. } else {
  322. rgblight_step_reverse();
  323. }
  324. }
  325. return false;
  326. case RGB_HUI:
  327. // Split keyboards need to trigger on key-up for edge-case issue
  328. # ifndef SPLIT_KEYBOARD
  329. if (record->event.pressed) {
  330. # else
  331. if (!record->event.pressed) {
  332. # endif
  333. rgblight_increase_hue();
  334. }
  335. return false;
  336. case RGB_HUD:
  337. // Split keyboards need to trigger on key-up for edge-case issue
  338. # ifndef SPLIT_KEYBOARD
  339. if (record->event.pressed) {
  340. # else
  341. if (!record->event.pressed) {
  342. # endif
  343. rgblight_decrease_hue();
  344. }
  345. return false;
  346. case RGB_SAI:
  347. // Split keyboards need to trigger on key-up for edge-case issue
  348. # ifndef SPLIT_KEYBOARD
  349. if (record->event.pressed) {
  350. # else
  351. if (!record->event.pressed) {
  352. # endif
  353. rgblight_increase_sat();
  354. }
  355. return false;
  356. case RGB_SAD:
  357. // Split keyboards need to trigger on key-up for edge-case issue
  358. # ifndef SPLIT_KEYBOARD
  359. if (record->event.pressed) {
  360. # else
  361. if (!record->event.pressed) {
  362. # endif
  363. rgblight_decrease_sat();
  364. }
  365. return false;
  366. case RGB_VAI:
  367. // Split keyboards need to trigger on key-up for edge-case issue
  368. # ifndef SPLIT_KEYBOARD
  369. if (record->event.pressed) {
  370. # else
  371. if (!record->event.pressed) {
  372. # endif
  373. rgblight_increase_val();
  374. }
  375. return false;
  376. case RGB_VAD:
  377. // Split keyboards need to trigger on key-up for edge-case issue
  378. # ifndef SPLIT_KEYBOARD
  379. if (record->event.pressed) {
  380. # else
  381. if (!record->event.pressed) {
  382. # endif
  383. rgblight_decrease_val();
  384. }
  385. return false;
  386. case RGB_SPI:
  387. if (record->event.pressed) {
  388. rgblight_increase_speed();
  389. }
  390. return false;
  391. case RGB_SPD:
  392. if (record->event.pressed) {
  393. rgblight_decrease_speed();
  394. }
  395. return false;
  396. case RGB_MODE_PLAIN:
  397. if (record->event.pressed) {
  398. rgblight_mode(RGBLIGHT_MODE_STATIC_LIGHT);
  399. }
  400. return false;
  401. case RGB_MODE_BREATHE:
  402. # ifdef RGBLIGHT_EFFECT_BREATHING
  403. if (record->event.pressed) {
  404. if ((RGBLIGHT_MODE_BREATHING <= rgblight_get_mode()) && (rgblight_get_mode() < RGBLIGHT_MODE_BREATHING_end)) {
  405. rgblight_step();
  406. } else {
  407. rgblight_mode(RGBLIGHT_MODE_BREATHING);
  408. }
  409. }
  410. # endif
  411. return false;
  412. case RGB_MODE_RAINBOW:
  413. # ifdef RGBLIGHT_EFFECT_RAINBOW_MOOD
  414. if (record->event.pressed) {
  415. if ((RGBLIGHT_MODE_RAINBOW_MOOD <= rgblight_get_mode()) && (rgblight_get_mode() < RGBLIGHT_MODE_RAINBOW_MOOD_end)) {
  416. rgblight_step();
  417. } else {
  418. rgblight_mode(RGBLIGHT_MODE_RAINBOW_MOOD);
  419. }
  420. }
  421. # endif
  422. return false;
  423. case RGB_MODE_SWIRL:
  424. # ifdef RGBLIGHT_EFFECT_RAINBOW_SWIRL
  425. if (record->event.pressed) {
  426. if ((RGBLIGHT_MODE_RAINBOW_SWIRL <= rgblight_get_mode()) && (rgblight_get_mode() < RGBLIGHT_MODE_RAINBOW_SWIRL_end)) {
  427. rgblight_step();
  428. } else {
  429. rgblight_mode(RGBLIGHT_MODE_RAINBOW_SWIRL);
  430. }
  431. }
  432. # endif
  433. return false;
  434. case RGB_MODE_SNAKE:
  435. # ifdef RGBLIGHT_EFFECT_SNAKE
  436. if (record->event.pressed) {
  437. if ((RGBLIGHT_MODE_SNAKE <= rgblight_get_mode()) && (rgblight_get_mode() < RGBLIGHT_MODE_SNAKE_end)) {
  438. rgblight_step();
  439. } else {
  440. rgblight_mode(RGBLIGHT_MODE_SNAKE);
  441. }
  442. }
  443. # endif
  444. return false;
  445. case RGB_MODE_KNIGHT:
  446. # ifdef RGBLIGHT_EFFECT_KNIGHT
  447. if (record->event.pressed) {
  448. if ((RGBLIGHT_MODE_KNIGHT <= rgblight_get_mode()) && (rgblight_get_mode() < RGBLIGHT_MODE_KNIGHT_end)) {
  449. rgblight_step();
  450. } else {
  451. rgblight_mode(RGBLIGHT_MODE_KNIGHT);
  452. }
  453. }
  454. # endif
  455. return false;
  456. case RGB_MODE_XMAS:
  457. # ifdef RGBLIGHT_EFFECT_CHRISTMAS
  458. if (record->event.pressed) {
  459. rgblight_mode(RGBLIGHT_MODE_CHRISTMAS);
  460. }
  461. # endif
  462. return false;
  463. case RGB_MODE_GRADIENT:
  464. # ifdef RGBLIGHT_EFFECT_STATIC_GRADIENT
  465. if (record->event.pressed) {
  466. if ((RGBLIGHT_MODE_STATIC_GRADIENT <= rgblight_get_mode()) && (rgblight_get_mode() < RGBLIGHT_MODE_STATIC_GRADIENT_end)) {
  467. rgblight_step();
  468. } else {
  469. rgblight_mode(RGBLIGHT_MODE_STATIC_GRADIENT);
  470. }
  471. }
  472. # endif
  473. return false;
  474. case RGB_MODE_RGBTEST:
  475. # ifdef RGBLIGHT_EFFECT_RGB_TEST
  476. if (record->event.pressed) {
  477. rgblight_mode(RGBLIGHT_MODE_RGB_TEST);
  478. }
  479. # endif
  480. return false;
  481. #endif // defined(RGBLIGHT_ENABLE) || defined(RGB_MATRIX_ENABLE)
  482. #ifdef VELOCIKEY_ENABLE
  483. case VLK_TOG:
  484. if (record->event.pressed) {
  485. velocikey_toggle();
  486. }
  487. return false;
  488. #endif
  489. #ifdef PROTOCOL_LUFA
  490. case OUT_AUTO:
  491. if (record->event.pressed) {
  492. set_output(OUTPUT_AUTO);
  493. }
  494. return false;
  495. case OUT_USB:
  496. if (record->event.pressed) {
  497. set_output(OUTPUT_USB);
  498. }
  499. return false;
  500. # ifdef BLUETOOTH_ENABLE
  501. case OUT_BT:
  502. if (record->event.pressed) {
  503. set_output(OUTPUT_BLUETOOTH);
  504. }
  505. return false;
  506. # endif
  507. #endif
  508. case MAGIC_SWAP_CONTROL_CAPSLOCK ... MAGIC_TOGGLE_ALT_GUI:
  509. case MAGIC_SWAP_LCTL_LGUI ... MAGIC_EE_HANDS_RIGHT:
  510. if (record->event.pressed) {
  511. // MAGIC actions (BOOTMAGIC without the boot)
  512. if (!eeconfig_is_enabled()) {
  513. eeconfig_init();
  514. }
  515. /* keymap config */
  516. keymap_config.raw = eeconfig_read_keymap();
  517. switch (keycode) {
  518. case MAGIC_SWAP_CONTROL_CAPSLOCK:
  519. keymap_config.swap_control_capslock = true;
  520. break;
  521. case MAGIC_CAPSLOCK_TO_CONTROL:
  522. keymap_config.capslock_to_control = true;
  523. break;
  524. case MAGIC_SWAP_LALT_LGUI:
  525. keymap_config.swap_lalt_lgui = true;
  526. break;
  527. case MAGIC_SWAP_RALT_RGUI:
  528. keymap_config.swap_ralt_rgui = true;
  529. break;
  530. case MAGIC_SWAP_LCTL_LGUI:
  531. keymap_config.swap_lctl_lgui = true;
  532. break;
  533. case MAGIC_SWAP_RCTL_RGUI:
  534. keymap_config.swap_rctl_rgui = true;
  535. break;
  536. case MAGIC_NO_GUI:
  537. keymap_config.no_gui = true;
  538. break;
  539. case MAGIC_SWAP_GRAVE_ESC:
  540. keymap_config.swap_grave_esc = true;
  541. break;
  542. case MAGIC_SWAP_BACKSLASH_BACKSPACE:
  543. keymap_config.swap_backslash_backspace = true;
  544. break;
  545. case MAGIC_HOST_NKRO:
  546. keymap_config.nkro = true;
  547. break;
  548. case MAGIC_SWAP_ALT_GUI:
  549. keymap_config.swap_lalt_lgui = keymap_config.swap_ralt_rgui = true;
  550. #ifdef AUDIO_ENABLE
  551. PLAY_SONG(ag_swap_song);
  552. #endif
  553. break;
  554. case MAGIC_SWAP_CTL_GUI:
  555. keymap_config.swap_lctl_lgui = keymap_config.swap_rctl_rgui = true;
  556. #ifdef AUDIO_ENABLE
  557. PLAY_SONG(cg_swap_song);
  558. #endif
  559. break;
  560. case MAGIC_UNSWAP_CONTROL_CAPSLOCK:
  561. keymap_config.swap_control_capslock = false;
  562. break;
  563. case MAGIC_UNCAPSLOCK_TO_CONTROL:
  564. keymap_config.capslock_to_control = false;
  565. break;
  566. case MAGIC_UNSWAP_LALT_LGUI:
  567. keymap_config.swap_lalt_lgui = false;
  568. break;
  569. case MAGIC_UNSWAP_RALT_RGUI:
  570. keymap_config.swap_ralt_rgui = false;
  571. break;
  572. case MAGIC_UNSWAP_LCTL_LGUI:
  573. keymap_config.swap_lctl_lgui = false;
  574. break;
  575. case MAGIC_UNSWAP_RCTL_RGUI:
  576. keymap_config.swap_rctl_rgui = false;
  577. break;
  578. case MAGIC_UNNO_GUI:
  579. keymap_config.no_gui = false;
  580. break;
  581. case MAGIC_UNSWAP_GRAVE_ESC:
  582. keymap_config.swap_grave_esc = false;
  583. break;
  584. case MAGIC_UNSWAP_BACKSLASH_BACKSPACE:
  585. keymap_config.swap_backslash_backspace = false;
  586. break;
  587. case MAGIC_UNHOST_NKRO:
  588. keymap_config.nkro = false;
  589. break;
  590. case MAGIC_UNSWAP_ALT_GUI:
  591. keymap_config.swap_lalt_lgui = keymap_config.swap_ralt_rgui = false;
  592. #ifdef AUDIO_ENABLE
  593. PLAY_SONG(ag_norm_song);
  594. #endif
  595. break;
  596. case MAGIC_UNSWAP_CTL_GUI:
  597. keymap_config.swap_lctl_lgui = keymap_config.swap_rctl_rgui = false;
  598. #ifdef AUDIO_ENABLE
  599. PLAY_SONG(cg_norm_song);
  600. #endif
  601. break;
  602. case MAGIC_TOGGLE_ALT_GUI:
  603. keymap_config.swap_lalt_lgui = !keymap_config.swap_lalt_lgui;
  604. keymap_config.swap_ralt_rgui = keymap_config.swap_lalt_lgui;
  605. #ifdef AUDIO_ENABLE
  606. if (keymap_config.swap_ralt_rgui) {
  607. PLAY_SONG(ag_swap_song);
  608. } else {
  609. PLAY_SONG(ag_norm_song);
  610. }
  611. #endif
  612. break;
  613. case MAGIC_TOGGLE_CTL_GUI:
  614. keymap_config.swap_lctl_lgui = !keymap_config.swap_lctl_lgui;
  615. keymap_config.swap_rctl_rgui = keymap_config.swap_lctl_lgui;
  616. #ifdef AUDIO_ENABLE
  617. if (keymap_config.swap_rctl_rgui) {
  618. PLAY_SONG(cg_swap_song);
  619. } else {
  620. PLAY_SONG(cg_norm_song);
  621. }
  622. #endif
  623. break;
  624. case MAGIC_TOGGLE_NKRO:
  625. keymap_config.nkro = !keymap_config.nkro;
  626. break;
  627. case MAGIC_EE_HANDS_LEFT:
  628. eeconfig_update_handedness(true);
  629. break;
  630. case MAGIC_EE_HANDS_RIGHT:
  631. eeconfig_update_handedness(false);
  632. break;
  633. default:
  634. break;
  635. }
  636. eeconfig_update_keymap(keymap_config.raw);
  637. clear_keyboard(); // clear to prevent stuck keys
  638. return false;
  639. }
  640. break;
  641. case GRAVE_ESC: {
  642. uint8_t shifted = get_mods() & ((MOD_BIT(KC_LSHIFT) | MOD_BIT(KC_RSHIFT) | MOD_BIT(KC_LGUI) | MOD_BIT(KC_RGUI)));
  643. #ifdef GRAVE_ESC_ALT_OVERRIDE
  644. // if ALT is pressed, ESC is always sent
  645. // this is handy for the cmd+opt+esc shortcut on macOS, among other things.
  646. if (get_mods() & (MOD_BIT(KC_LALT) | MOD_BIT(KC_RALT))) {
  647. shifted = 0;
  648. }
  649. #endif
  650. #ifdef GRAVE_ESC_CTRL_OVERRIDE
  651. // if CTRL is pressed, ESC is always sent
  652. // this is handy for the ctrl+shift+esc shortcut on windows, among other things.
  653. if (get_mods() & (MOD_BIT(KC_LCTL) | MOD_BIT(KC_RCTL))) {
  654. shifted = 0;
  655. }
  656. #endif
  657. #ifdef GRAVE_ESC_GUI_OVERRIDE
  658. // if GUI is pressed, ESC is always sent
  659. if (get_mods() & (MOD_BIT(KC_LGUI) | MOD_BIT(KC_RGUI))) {
  660. shifted = 0;
  661. }
  662. #endif
  663. #ifdef GRAVE_ESC_SHIFT_OVERRIDE
  664. // if SHIFT is pressed, ESC is always sent
  665. if (get_mods() & (MOD_BIT(KC_LSHIFT) | MOD_BIT(KC_RSHIFT))) {
  666. shifted = 0;
  667. }
  668. #endif
  669. if (record->event.pressed) {
  670. grave_esc_was_shifted = shifted;
  671. add_key(shifted ? KC_GRAVE : KC_ESCAPE);
  672. } else {
  673. del_key(grave_esc_was_shifted ? KC_GRAVE : KC_ESCAPE);
  674. }
  675. send_keyboard_report();
  676. return false;
  677. }
  678. #if defined(BACKLIGHT_ENABLE) && defined(BACKLIGHT_BREATHING)
  679. case BL_BRTG: {
  680. if (record->event.pressed) {
  681. backlight_toggle_breathing();
  682. }
  683. return false;
  684. }
  685. #endif
  686. }
  687. return process_action_kb(record);
  688. }
  689. __attribute__((weak)) const bool ascii_to_shift_lut[128] PROGMEM = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  690. 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0};
  691. __attribute__((weak)) const bool ascii_to_altgr_lut[128] PROGMEM = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  692. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
  693. __attribute__((weak)) const uint8_t ascii_to_keycode_lut[128] PROGMEM = {// NUL SOH STX ETX EOT ENQ ACK BEL
  694. XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX,
  695. // BS TAB LF VT FF CR SO SI
  696. KC_BSPC, KC_TAB, KC_ENT, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX,
  697. // DLE DC1 DC2 DC3 DC4 NAK SYN ETB
  698. XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX,
  699. // CAN EM SUB ESC FS GS RS US
  700. XXXXXXX, XXXXXXX, XXXXXXX, KC_ESC, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX,
  701. // ! " # $ % & '
  702. KC_SPC, KC_1, KC_QUOT, KC_3, KC_4, KC_5, KC_7, KC_QUOT,
  703. // ( ) * + , - . /
  704. KC_9, KC_0, KC_8, KC_EQL, KC_COMM, KC_MINS, KC_DOT, KC_SLSH,
  705. // 0 1 2 3 4 5 6 7
  706. KC_0, KC_1, KC_2, KC_3, KC_4, KC_5, KC_6, KC_7,
  707. // 8 9 : ; < = > ?
  708. KC_8, KC_9, KC_SCLN, KC_SCLN, KC_COMM, KC_EQL, KC_DOT, KC_SLSH,
  709. // @ A B C D E F G
  710. KC_2, KC_A, KC_B, KC_C, KC_D, KC_E, KC_F, KC_G,
  711. // H I J K L M N O
  712. KC_H, KC_I, KC_J, KC_K, KC_L, KC_M, KC_N, KC_O,
  713. // P Q R S T U V W
  714. KC_P, KC_Q, KC_R, KC_S, KC_T, KC_U, KC_V, KC_W,
  715. // X Y Z [ \ ] ^ _
  716. KC_X, KC_Y, KC_Z, KC_LBRC, KC_BSLS, KC_RBRC, KC_6, KC_MINS,
  717. // ` a b c d e f g
  718. KC_GRV, KC_A, KC_B, KC_C, KC_D, KC_E, KC_F, KC_G,
  719. // h i j k l m n o
  720. KC_H, KC_I, KC_J, KC_K, KC_L, KC_M, KC_N, KC_O,
  721. // p q r s t u v w
  722. KC_P, KC_Q, KC_R, KC_S, KC_T, KC_U, KC_V, KC_W,
  723. // x y z { | } ~ DEL
  724. KC_X, KC_Y, KC_Z, KC_LBRC, KC_BSLS, KC_RBRC, KC_GRV, KC_DEL};
  725. void send_string(const char *str) { send_string_with_delay(str, 0); }
  726. void send_string_P(const char *str) { send_string_with_delay_P(str, 0); }
  727. void send_string_with_delay(const char *str, uint8_t interval) {
  728. while (1) {
  729. char ascii_code = *str;
  730. if (!ascii_code) break;
  731. if (ascii_code == SS_TAP_CODE) {
  732. // tap
  733. uint8_t keycode = *(++str);
  734. register_code(keycode);
  735. unregister_code(keycode);
  736. } else if (ascii_code == SS_DOWN_CODE) {
  737. // down
  738. uint8_t keycode = *(++str);
  739. register_code(keycode);
  740. } else if (ascii_code == SS_UP_CODE) {
  741. // up
  742. uint8_t keycode = *(++str);
  743. unregister_code(keycode);
  744. } else {
  745. send_char(ascii_code);
  746. }
  747. ++str;
  748. // interval
  749. {
  750. uint8_t ms = interval;
  751. while (ms--) wait_ms(1);
  752. }
  753. }
  754. }
  755. void send_string_with_delay_P(const char *str, uint8_t interval) {
  756. while (1) {
  757. char ascii_code = pgm_read_byte(str);
  758. if (!ascii_code) break;
  759. if (ascii_code == SS_TAP_CODE) {
  760. // tap
  761. uint8_t keycode = pgm_read_byte(++str);
  762. register_code(keycode);
  763. unregister_code(keycode);
  764. } else if (ascii_code == SS_DOWN_CODE) {
  765. // down
  766. uint8_t keycode = pgm_read_byte(++str);
  767. register_code(keycode);
  768. } else if (ascii_code == SS_UP_CODE) {
  769. // up
  770. uint8_t keycode = pgm_read_byte(++str);
  771. unregister_code(keycode);
  772. } else {
  773. send_char(ascii_code);
  774. }
  775. ++str;
  776. // interval
  777. {
  778. uint8_t ms = interval;
  779. while (ms--) wait_ms(1);
  780. }
  781. }
  782. }
  783. void send_char(char ascii_code) {
  784. uint8_t keycode = pgm_read_byte(&ascii_to_keycode_lut[(uint8_t)ascii_code]);
  785. bool is_shifted = pgm_read_byte(&ascii_to_shift_lut[(uint8_t)ascii_code]);
  786. bool is_altgred = pgm_read_byte(&ascii_to_altgr_lut[(uint8_t)ascii_code]);
  787. if (is_shifted) {
  788. register_code(KC_LSFT);
  789. }
  790. if (is_altgred) {
  791. register_code(KC_RALT);
  792. }
  793. tap_code(keycode);
  794. if (is_altgred) {
  795. unregister_code(KC_RALT);
  796. }
  797. if (is_shifted) {
  798. unregister_code(KC_LSFT);
  799. }
  800. }
  801. void set_single_persistent_default_layer(uint8_t default_layer) {
  802. #if defined(AUDIO_ENABLE) && defined(DEFAULT_LAYER_SONGS)
  803. PLAY_SONG(default_layer_songs[default_layer]);
  804. #endif
  805. eeconfig_update_default_layer(1U << default_layer);
  806. default_layer_set(1U << default_layer);
  807. }
  808. layer_state_t update_tri_layer_state(layer_state_t state, uint8_t layer1, uint8_t layer2, uint8_t layer3) {
  809. layer_state_t mask12 = (1UL << layer1) | (1UL << layer2);
  810. layer_state_t mask3 = 1UL << layer3;
  811. return (state & mask12) == mask12 ? (state | mask3) : (state & ~mask3);
  812. }
  813. void update_tri_layer(uint8_t layer1, uint8_t layer2, uint8_t layer3) { layer_state_set(update_tri_layer_state(layer_state, layer1, layer2, layer3)); }
  814. void tap_random_base64(void) {
  815. #if defined(__AVR_ATmega32U4__)
  816. uint8_t key = (TCNT0 + TCNT1 + TCNT3 + TCNT4) % 64;
  817. #else
  818. uint8_t key = rand() % 64;
  819. #endif
  820. switch (key) {
  821. case 0 ... 25:
  822. register_code(KC_LSFT);
  823. register_code(key + KC_A);
  824. unregister_code(key + KC_A);
  825. unregister_code(KC_LSFT);
  826. break;
  827. case 26 ... 51:
  828. register_code(key - 26 + KC_A);
  829. unregister_code(key - 26 + KC_A);
  830. break;
  831. case 52:
  832. register_code(KC_0);
  833. unregister_code(KC_0);
  834. break;
  835. case 53 ... 61:
  836. register_code(key - 53 + KC_1);
  837. unregister_code(key - 53 + KC_1);
  838. break;
  839. case 62:
  840. register_code(KC_LSFT);
  841. register_code(KC_EQL);
  842. unregister_code(KC_EQL);
  843. unregister_code(KC_LSFT);
  844. break;
  845. case 63:
  846. register_code(KC_SLSH);
  847. unregister_code(KC_SLSH);
  848. break;
  849. }
  850. }
  851. __attribute__((weak)) void bootmagic_lite(void) {
  852. // The lite version of TMK's bootmagic based on Wilba.
  853. // 100% less potential for accidentally making the
  854. // keyboard do stupid things.
  855. // We need multiple scans because debouncing can't be turned off.
  856. matrix_scan();
  857. #if defined(DEBOUNCING_DELAY) && DEBOUNCING_DELAY > 0
  858. wait_ms(DEBOUNCING_DELAY * 2);
  859. #elif defined(DEBOUNCE) && DEBOUNCE > 0
  860. wait_ms(DEBOUNCE * 2);
  861. #else
  862. wait_ms(30);
  863. #endif
  864. matrix_scan();
  865. // If the Esc and space bar are held down on power up,
  866. // reset the EEPROM valid state and jump to bootloader.
  867. // Assumes Esc is at [0,0].
  868. // This isn't very generalized, but we need something that doesn't
  869. // rely on user's keymaps in firmware or EEPROM.
  870. if (matrix_get_row(BOOTMAGIC_LITE_ROW) & (1 << BOOTMAGIC_LITE_COLUMN)) {
  871. eeconfig_disable();
  872. // Jump to bootloader.
  873. bootloader_jump();
  874. }
  875. }
  876. void matrix_init_quantum() {
  877. #ifdef BOOTMAGIC_LITE
  878. bootmagic_lite();
  879. #endif
  880. if (!eeconfig_is_enabled()) {
  881. eeconfig_init();
  882. }
  883. #ifdef BACKLIGHT_ENABLE
  884. # ifdef LED_MATRIX_ENABLE
  885. led_matrix_init();
  886. # else
  887. backlight_init_ports();
  888. # endif
  889. #endif
  890. #ifdef AUDIO_ENABLE
  891. audio_init();
  892. #endif
  893. #ifdef RGB_MATRIX_ENABLE
  894. rgb_matrix_init();
  895. #endif
  896. #ifdef ENCODER_ENABLE
  897. encoder_init();
  898. #endif
  899. #if defined(UNICODE_ENABLE) || defined(UNICODEMAP_ENABLE) || defined(UCIS_ENABLE)
  900. unicode_input_mode_init();
  901. #endif
  902. #ifdef HAPTIC_ENABLE
  903. haptic_init();
  904. #endif
  905. #ifdef OUTPUT_AUTO_ENABLE
  906. set_output(OUTPUT_AUTO);
  907. #endif
  908. #ifdef DIP_SWITCH_ENABLE
  909. dip_switch_init();
  910. #endif
  911. matrix_init_kb();
  912. }
  913. void matrix_scan_quantum() {
  914. #if defined(AUDIO_ENABLE) && !defined(NO_MUSIC_MODE)
  915. matrix_scan_music();
  916. #endif
  917. #ifdef TAP_DANCE_ENABLE
  918. matrix_scan_tap_dance();
  919. #endif
  920. #ifdef COMBO_ENABLE
  921. matrix_scan_combo();
  922. #endif
  923. #if defined(BACKLIGHT_ENABLE)
  924. # if defined(LED_MATRIX_ENABLE)
  925. led_matrix_task();
  926. # elif defined(BACKLIGHT_PIN)
  927. backlight_task();
  928. # endif
  929. #endif
  930. #ifdef RGB_MATRIX_ENABLE
  931. rgb_matrix_task();
  932. #endif
  933. #ifdef ENCODER_ENABLE
  934. encoder_read();
  935. #endif
  936. #ifdef HAPTIC_ENABLE
  937. haptic_task();
  938. #endif
  939. #ifdef DIP_SWITCH_ENABLE
  940. dip_switch_read(false);
  941. #endif
  942. matrix_scan_kb();
  943. }
  944. #if defined(BACKLIGHT_ENABLE) && (defined(BACKLIGHT_PIN) || defined(BACKLIGHT_PINS))
  945. // This logic is a bit complex, we support 3 setups:
  946. //
  947. // 1. Hardware PWM when backlight is wired to a PWM pin.
  948. // Depending on this pin, we use a different output compare unit.
  949. // 2. Software PWM with hardware timers, but the used timer
  950. // depends on the Audio setup (Audio wins over Backlight).
  951. // 3. Full software PWM, driven by the matrix scan, if both timers are used by Audio.
  952. # if (defined(__AVR_AT90USB646__) || defined(__AVR_AT90USB647__) || defined(__AVR_AT90USB1286__) || defined(__AVR_AT90USB1287__) || defined(__AVR_ATmega16U4__) || defined(__AVR_ATmega32U4__)) && (BACKLIGHT_PIN == B5 || BACKLIGHT_PIN == B6 || BACKLIGHT_PIN == B7)
  953. # define HARDWARE_PWM
  954. # define ICRx ICR1
  955. # define TCCRxA TCCR1A
  956. # define TCCRxB TCCR1B
  957. # define TIMERx_OVF_vect TIMER1_OVF_vect
  958. # define TIMSKx TIMSK1
  959. # define TOIEx TOIE1
  960. # if BACKLIGHT_PIN == B5
  961. # define COMxx1 COM1A1
  962. # define OCRxx OCR1A
  963. # elif BACKLIGHT_PIN == B6
  964. # define COMxx1 COM1B1
  965. # define OCRxx OCR1B
  966. # elif BACKLIGHT_PIN == B7
  967. # define COMxx1 COM1C1
  968. # define OCRxx OCR1C
  969. # endif
  970. # elif (defined(__AVR_AT90USB646__) || defined(__AVR_AT90USB647__) || defined(__AVR_AT90USB1286__) || defined(__AVR_AT90USB1287__) || defined(__AVR_ATmega16U4__) || defined(__AVR_ATmega32U4__)) && (BACKLIGHT_PIN == C4 || BACKLIGHT_PIN == C5 || BACKLIGHT_PIN == C6)
  971. # define HARDWARE_PWM
  972. # define ICRx ICR3
  973. # define TCCRxA TCCR3A
  974. # define TCCRxB TCCR3B
  975. # define TIMERx_OVF_vect TIMER3_OVF_vect
  976. # define TIMSKx TIMSK3
  977. # define TOIEx TOIE3
  978. # if BACKLIGHT_PIN == C4
  979. # if (defined(__AVR_ATmega16U4__) || defined(__AVR_ATmega32U4__))
  980. # error This MCU has no C4 pin!
  981. # else
  982. # define COMxx1 COM3C1
  983. # define OCRxx OCR3C
  984. # endif
  985. # elif BACKLIGHT_PIN == C5
  986. # if (defined(__AVR_ATmega16U4__) || defined(__AVR_ATmega32U4__))
  987. # error This MCU has no C5 pin!
  988. # else
  989. # define COMxx1 COM3B1
  990. # define OCRxx OCR3B
  991. # endif
  992. # elif BACKLIGHT_PIN == C6
  993. # define COMxx1 COM3A1
  994. # define OCRxx OCR3A
  995. # endif
  996. # elif (defined(__AVR_ATmega16U2__) || defined(__AVR_ATmega32U2__)) && (BACKLIGHT_PIN == B7 || BACKLIGHT_PIN == C5 || BACKLIGHT_PIN == C6)
  997. # define HARDWARE_PWM
  998. # define ICRx ICR1
  999. # define TCCRxA TCCR1A
  1000. # define TCCRxB TCCR1B
  1001. # define TIMERx_OVF_vect TIMER1_OVF_vect
  1002. # define TIMSKx TIMSK1
  1003. # define TOIEx TOIE1
  1004. # if BACKLIGHT_PIN == B7
  1005. # define COMxx1 COM1C1
  1006. # define OCRxx OCR1C
  1007. # elif BACKLIGHT_PIN == C5
  1008. # define COMxx1 COM1B1
  1009. # define OCRxx OCR1B
  1010. # elif BACKLIGHT_PIN == C6
  1011. # define COMxx1 COM1A1
  1012. # define OCRxx OCR1A
  1013. # endif
  1014. # elif defined(__AVR_ATmega32A__) && (BACKLIGHT_PIN == D4 || BACKLIGHT_PIN == D5)
  1015. # define HARDWARE_PWM
  1016. # define ICRx ICR1
  1017. # define TCCRxA TCCR1A
  1018. # define TCCRxB TCCR1B
  1019. # define TIMERx_OVF_vect TIMER1_OVF_vect
  1020. # define TIMSKx TIMSK
  1021. # define TOIEx TOIE1
  1022. # if BACKLIGHT_PIN == D4
  1023. # define COMxx1 COM1B1
  1024. # define OCRxx OCR1B
  1025. # elif BACKLIGHT_PIN == D5
  1026. # define COMxx1 COM1A1
  1027. # define OCRxx OCR1A
  1028. # endif
  1029. # elif defined(__AVR_ATmega328P__) && (BACKLIGHT_PIN == B1 || BACKLIGHT_PIN == B2)
  1030. # define HARDWARE_PWM
  1031. # define ICRx ICR1
  1032. # define TCCRxA TCCR1A
  1033. # define TCCRxB TCCR1B
  1034. # define TIMERx_OVF_vect TIMER1_OVF_vect
  1035. # define TIMSKx TIMSK1
  1036. # define TOIEx TOIE1
  1037. # if BACKLIGHT_PIN == B1
  1038. # define COMxx1 COM1A1
  1039. # define OCRxx OCR1A
  1040. # elif BACKLIGHT_PIN == B2
  1041. # define COMxx1 COM1B1
  1042. # define OCRxx OCR1B
  1043. # endif
  1044. # else
  1045. # if !defined(BACKLIGHT_CUSTOM_DRIVER)
  1046. # if !defined(B5_AUDIO) && !defined(B6_AUDIO) && !defined(B7_AUDIO)
  1047. // Timer 1 is not in use by Audio feature, Backlight can use it
  1048. # pragma message "Using hardware timer 1 with software PWM"
  1049. # define HARDWARE_PWM
  1050. # define BACKLIGHT_PWM_TIMER
  1051. # define ICRx ICR1
  1052. # define TCCRxA TCCR1A
  1053. # define TCCRxB TCCR1B
  1054. # define TIMERx_COMPA_vect TIMER1_COMPA_vect
  1055. # define TIMERx_OVF_vect TIMER1_OVF_vect
  1056. # if defined(__AVR_ATmega32A__) // This MCU has only one TIMSK register
  1057. # define TIMSKx TIMSK
  1058. # else
  1059. # define TIMSKx TIMSK1
  1060. # endif
  1061. # define TOIEx TOIE1
  1062. # define OCIExA OCIE1A
  1063. # define OCRxx OCR1A
  1064. # elif !defined(C6_AUDIO) && !defined(C5_AUDIO) && !defined(C4_AUDIO)
  1065. # pragma message "Using hardware timer 3 with software PWM"
  1066. // Timer 3 is not in use by Audio feature, Backlight can use it
  1067. # define HARDWARE_PWM
  1068. # define BACKLIGHT_PWM_TIMER
  1069. # define ICRx ICR1
  1070. # define TCCRxA TCCR3A
  1071. # define TCCRxB TCCR3B
  1072. # define TIMERx_COMPA_vect TIMER3_COMPA_vect
  1073. # define TIMERx_OVF_vect TIMER3_OVF_vect
  1074. # define TIMSKx TIMSK3
  1075. # define TOIEx TOIE3
  1076. # define OCIExA OCIE3A
  1077. # define OCRxx OCR3A
  1078. # else
  1079. # pragma message "Audio in use - using pure software PWM"
  1080. # define NO_HARDWARE_PWM
  1081. # endif
  1082. # else
  1083. # pragma message "Custom driver defined - using pure software PWM"
  1084. # define NO_HARDWARE_PWM
  1085. # endif
  1086. # endif
  1087. # ifndef BACKLIGHT_ON_STATE
  1088. # define BACKLIGHT_ON_STATE 0
  1089. # endif
  1090. void backlight_on(uint8_t backlight_pin) {
  1091. # if BACKLIGHT_ON_STATE == 0
  1092. writePinLow(backlight_pin);
  1093. # else
  1094. writePinHigh(backlight_pin);
  1095. # endif
  1096. }
  1097. void backlight_off(uint8_t backlight_pin) {
  1098. # if BACKLIGHT_ON_STATE == 0
  1099. writePinHigh(backlight_pin);
  1100. # else
  1101. writePinLow(backlight_pin);
  1102. # endif
  1103. }
  1104. # if defined(NO_HARDWARE_PWM) || defined(BACKLIGHT_PWM_TIMER) // pwm through software
  1105. // we support multiple backlight pins
  1106. # ifndef BACKLIGHT_LED_COUNT
  1107. # define BACKLIGHT_LED_COUNT 1
  1108. # endif
  1109. # if BACKLIGHT_LED_COUNT == 1
  1110. # define BACKLIGHT_PIN_INIT \
  1111. { BACKLIGHT_PIN }
  1112. # else
  1113. # define BACKLIGHT_PIN_INIT BACKLIGHT_PINS
  1114. # endif
  1115. # define FOR_EACH_LED(x) \
  1116. for (uint8_t i = 0; i < BACKLIGHT_LED_COUNT; i++) { \
  1117. uint8_t backlight_pin = backlight_pins[i]; \
  1118. { x } \
  1119. }
  1120. static const uint8_t backlight_pins[BACKLIGHT_LED_COUNT] = BACKLIGHT_PIN_INIT;
  1121. # else // full hardware PWM
  1122. // we support only one backlight pin
  1123. static const uint8_t backlight_pin = BACKLIGHT_PIN;
  1124. # define FOR_EACH_LED(x) x
  1125. # endif
  1126. # ifdef NO_HARDWARE_PWM
  1127. __attribute__((weak)) void backlight_init_ports(void) {
  1128. // Setup backlight pin as output and output to on state.
  1129. FOR_EACH_LED(setPinOutput(backlight_pin); backlight_on(backlight_pin);)
  1130. # ifdef BACKLIGHT_BREATHING
  1131. if (is_backlight_breathing()) {
  1132. breathing_enable();
  1133. }
  1134. # endif
  1135. }
  1136. __attribute__((weak)) void backlight_set(uint8_t level) {}
  1137. uint8_t backlight_tick = 0;
  1138. # ifndef BACKLIGHT_CUSTOM_DRIVER
  1139. void backlight_task(void) {
  1140. if ((0xFFFF >> ((BACKLIGHT_LEVELS - get_backlight_level()) * ((BACKLIGHT_LEVELS + 1) / 2))) & (1 << backlight_tick)) {
  1141. FOR_EACH_LED(backlight_on(backlight_pin);)
  1142. } else {
  1143. FOR_EACH_LED(backlight_off(backlight_pin);)
  1144. }
  1145. backlight_tick = (backlight_tick + 1) % 16;
  1146. }
  1147. # endif
  1148. # ifdef BACKLIGHT_BREATHING
  1149. # ifndef BACKLIGHT_CUSTOM_DRIVER
  1150. # error "Backlight breathing only available with hardware PWM. Please disable."
  1151. # endif
  1152. # endif
  1153. # else // hardware pwm through timer
  1154. # ifdef BACKLIGHT_PWM_TIMER
  1155. // The idea of software PWM assisted by hardware timers is the following
  1156. // we use the hardware timer in fast PWM mode like for hardware PWM, but
  1157. // instead of letting the Output Match Comparator control the led pin
  1158. // (which is not possible since the backlight is not wired to PWM pins on the
  1159. // CPU), we do the LED on/off by oursleves.
  1160. // The timer is setup to count up to 0xFFFF, and we set the Output Compare
  1161. // register to the current 16bits backlight level (after CIE correction).
  1162. // This means the CPU will trigger a compare match interrupt when the counter
  1163. // reaches the backlight level, where we turn off the LEDs,
  1164. // but also an overflow interrupt when the counter rolls back to 0,
  1165. // in which we're going to turn on the LEDs.
  1166. // The LED will then be on for OCRxx/0xFFFF time, adjusted every 244Hz.
  1167. // Triggered when the counter reaches the OCRx value
  1168. ISR(TIMERx_COMPA_vect) { FOR_EACH_LED(backlight_off(backlight_pin);) }
  1169. // Triggered when the counter reaches the TOP value
  1170. // this one triggers at F_CPU/65536 =~ 244 Hz
  1171. ISR(TIMERx_OVF_vect) {
  1172. # ifdef BACKLIGHT_BREATHING
  1173. if (is_breathing()) {
  1174. breathing_task();
  1175. }
  1176. # endif
  1177. // for very small values of OCRxx (or backlight level)
  1178. // we can't guarantee this whole code won't execute
  1179. // at the same time as the compare match interrupt
  1180. // which means that we might turn on the leds while
  1181. // trying to turn them off, leading to flickering
  1182. // artifacts (especially while breathing, because breathing_task
  1183. // takes many computation cycles).
  1184. // so better not turn them on while the counter TOP is very low.
  1185. if (OCRxx > 256) {
  1186. FOR_EACH_LED(backlight_on(backlight_pin);)
  1187. }
  1188. }
  1189. # endif
  1190. # define TIMER_TOP 0xFFFFU
  1191. // See http://jared.geek.nz/2013/feb/linear-led-pwm
  1192. static uint16_t cie_lightness(uint16_t v) {
  1193. if (v <= 5243) // if below 8% of max
  1194. return v / 9; // same as dividing by 900%
  1195. else {
  1196. uint32_t y = (((uint32_t)v + 10486) << 8) / (10486 + 0xFFFFUL); // add 16% of max and compare
  1197. // to get a useful result with integer division, we shift left in the expression above
  1198. // and revert what we've done again after squaring.
  1199. y = y * y * y >> 8;
  1200. if (y > 0xFFFFUL) // prevent overflow
  1201. return 0xFFFFU;
  1202. else
  1203. return (uint16_t)y;
  1204. }
  1205. }
  1206. // range for val is [0..TIMER_TOP]. PWM pin is high while the timer count is below val.
  1207. static inline void set_pwm(uint16_t val) { OCRxx = val; }
  1208. # ifndef BACKLIGHT_CUSTOM_DRIVER
  1209. __attribute__((weak)) void backlight_set(uint8_t level) {
  1210. if (level > BACKLIGHT_LEVELS) level = BACKLIGHT_LEVELS;
  1211. if (level == 0) {
  1212. # ifdef BACKLIGHT_PWM_TIMER
  1213. if (OCRxx) {
  1214. TIMSKx &= ~(_BV(OCIExA));
  1215. TIMSKx &= ~(_BV(TOIEx));
  1216. FOR_EACH_LED(backlight_off(backlight_pin);)
  1217. }
  1218. # else
  1219. // Turn off PWM control on backlight pin
  1220. TCCRxA &= ~(_BV(COMxx1));
  1221. # endif
  1222. } else {
  1223. # ifdef BACKLIGHT_PWM_TIMER
  1224. if (!OCRxx) {
  1225. TIMSKx |= _BV(OCIExA);
  1226. TIMSKx |= _BV(TOIEx);
  1227. }
  1228. # else
  1229. // Turn on PWM control of backlight pin
  1230. TCCRxA |= _BV(COMxx1);
  1231. # endif
  1232. }
  1233. // Set the brightness
  1234. set_pwm(cie_lightness(TIMER_TOP * (uint32_t)level / BACKLIGHT_LEVELS));
  1235. }
  1236. void backlight_task(void) {}
  1237. # endif // BACKLIGHT_CUSTOM_DRIVER
  1238. # ifdef BACKLIGHT_BREATHING
  1239. # define BREATHING_NO_HALT 0
  1240. # define BREATHING_HALT_OFF 1
  1241. # define BREATHING_HALT_ON 2
  1242. # define BREATHING_STEPS 128
  1243. static uint8_t breathing_period = BREATHING_PERIOD;
  1244. static uint8_t breathing_halt = BREATHING_NO_HALT;
  1245. static uint16_t breathing_counter = 0;
  1246. # ifdef BACKLIGHT_PWM_TIMER
  1247. static bool breathing = false;
  1248. bool is_breathing(void) { return breathing; }
  1249. # define breathing_interrupt_enable() \
  1250. do { \
  1251. breathing = true; \
  1252. } while (0)
  1253. # define breathing_interrupt_disable() \
  1254. do { \
  1255. breathing = false; \
  1256. } while (0)
  1257. # else
  1258. bool is_breathing(void) { return !!(TIMSKx & _BV(TOIEx)); }
  1259. # define breathing_interrupt_enable() \
  1260. do { \
  1261. TIMSKx |= _BV(TOIEx); \
  1262. } while (0)
  1263. # define breathing_interrupt_disable() \
  1264. do { \
  1265. TIMSKx &= ~_BV(TOIEx); \
  1266. } while (0)
  1267. # endif
  1268. # define breathing_min() \
  1269. do { \
  1270. breathing_counter = 0; \
  1271. } while (0)
  1272. # define breathing_max() \
  1273. do { \
  1274. breathing_counter = breathing_period * 244 / 2; \
  1275. } while (0)
  1276. void breathing_enable(void) {
  1277. breathing_counter = 0;
  1278. breathing_halt = BREATHING_NO_HALT;
  1279. breathing_interrupt_enable();
  1280. }
  1281. void breathing_pulse(void) {
  1282. if (get_backlight_level() == 0)
  1283. breathing_min();
  1284. else
  1285. breathing_max();
  1286. breathing_halt = BREATHING_HALT_ON;
  1287. breathing_interrupt_enable();
  1288. }
  1289. void breathing_disable(void) {
  1290. breathing_interrupt_disable();
  1291. // Restore backlight level
  1292. backlight_set(get_backlight_level());
  1293. }
  1294. void breathing_self_disable(void) {
  1295. if (get_backlight_level() == 0)
  1296. breathing_halt = BREATHING_HALT_OFF;
  1297. else
  1298. breathing_halt = BREATHING_HALT_ON;
  1299. }
  1300. void breathing_toggle(void) {
  1301. if (is_breathing())
  1302. breathing_disable();
  1303. else
  1304. breathing_enable();
  1305. }
  1306. void breathing_period_set(uint8_t value) {
  1307. if (!value) value = 1;
  1308. breathing_period = value;
  1309. }
  1310. void breathing_period_default(void) { breathing_period_set(BREATHING_PERIOD); }
  1311. void breathing_period_inc(void) { breathing_period_set(breathing_period + 1); }
  1312. void breathing_period_dec(void) { breathing_period_set(breathing_period - 1); }
  1313. /* To generate breathing curve in python:
  1314. * from math import sin, pi; [int(sin(x/128.0*pi)**4*255) for x in range(128)]
  1315. */
  1316. static const uint8_t breathing_table[BREATHING_STEPS] PROGMEM = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 2, 3, 4, 5, 6, 8, 10, 12, 15, 17, 20, 24, 28, 32, 36, 41, 46, 51, 57, 63, 70, 76, 83, 91, 98, 106, 113, 121, 129, 138, 146, 154, 162, 170, 178, 185, 193, 200, 207, 213, 220, 225, 231, 235, 240, 244, 247, 250, 252, 253, 254, 255, 254, 253, 252, 250, 247, 244, 240, 235, 231, 225, 220, 213, 207, 200, 193, 185, 178, 170, 162, 154, 146, 138, 129, 121, 113, 106, 98, 91, 83, 76, 70, 63, 57, 51, 46, 41, 36, 32, 28, 24, 20, 17, 15, 12, 10, 8, 6, 5, 4, 3, 2, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
  1317. // Use this before the cie_lightness function.
  1318. static inline uint16_t scale_backlight(uint16_t v) { return v / BACKLIGHT_LEVELS * get_backlight_level(); }
  1319. # ifdef BACKLIGHT_PWM_TIMER
  1320. void breathing_task(void)
  1321. # else
  1322. /* Assuming a 16MHz CPU clock and a timer that resets at 64k (ICR1), the following interrupt handler will run
  1323. * about 244 times per second.
  1324. */
  1325. ISR(TIMERx_OVF_vect)
  1326. # endif
  1327. {
  1328. uint16_t interval = (uint16_t)breathing_period * 244 / BREATHING_STEPS;
  1329. // resetting after one period to prevent ugly reset at overflow.
  1330. breathing_counter = (breathing_counter + 1) % (breathing_period * 244);
  1331. uint8_t index = breathing_counter / interval % BREATHING_STEPS;
  1332. if (((breathing_halt == BREATHING_HALT_ON) && (index == BREATHING_STEPS / 2)) || ((breathing_halt == BREATHING_HALT_OFF) && (index == BREATHING_STEPS - 1))) {
  1333. breathing_interrupt_disable();
  1334. }
  1335. set_pwm(cie_lightness(scale_backlight((uint16_t)pgm_read_byte(&breathing_table[index]) * 0x0101U)));
  1336. }
  1337. # endif // BACKLIGHT_BREATHING
  1338. __attribute__((weak)) void backlight_init_ports(void) {
  1339. // Setup backlight pin as output and output to on state.
  1340. FOR_EACH_LED(setPinOutput(backlight_pin); backlight_on(backlight_pin);)
  1341. // I could write a wall of text here to explain... but TL;DW
  1342. // Go read the ATmega32u4 datasheet.
  1343. // And this: http://blog.saikoled.com/post/43165849837/secret-konami-cheat-code-to-high-resolution-pwm-on
  1344. # ifdef BACKLIGHT_PWM_TIMER
  1345. // TimerX setup, Fast PWM mode count to TOP set in ICRx
  1346. TCCRxA = _BV(WGM11); // = 0b00000010;
  1347. // clock select clk/1
  1348. TCCRxB = _BV(WGM13) | _BV(WGM12) | _BV(CS10); // = 0b00011001;
  1349. # else // hardware PWM
  1350. // Pin PB7 = OCR1C (Timer 1, Channel C)
  1351. // Compare Output Mode = Clear on compare match, Channel C = COM1C1=1 COM1C0=0
  1352. // (i.e. start high, go low when counter matches.)
  1353. // WGM Mode 14 (Fast PWM) = WGM13=1 WGM12=1 WGM11=1 WGM10=0
  1354. // Clock Select = clk/1 (no prescaling) = CS12=0 CS11=0 CS10=1
  1355. /*
  1356. 14.8.3:
  1357. "In fast PWM mode, the compare units allow generation of PWM waveforms on the OCnx pins. Setting the COMnx1:0 bits to two will produce a non-inverted PWM [..]."
  1358. "In fast PWM mode the counter is incremented until the counter value matches either one of the fixed values 0x00FF, 0x01FF, or 0x03FF (WGMn3:0 = 5, 6, or 7), the value in ICRn (WGMn3:0 = 14), or the value in OCRnA (WGMn3:0 = 15)."
  1359. */
  1360. TCCRxA = _BV(COMxx1) | _BV(WGM11); // = 0b00001010;
  1361. TCCRxB = _BV(WGM13) | _BV(WGM12) | _BV(CS10); // = 0b00011001;
  1362. # endif
  1363. // Use full 16-bit resolution. Counter counts to ICR1 before reset to 0.
  1364. ICRx = TIMER_TOP;
  1365. backlight_init();
  1366. # ifdef BACKLIGHT_BREATHING
  1367. if (is_backlight_breathing()) {
  1368. breathing_enable();
  1369. }
  1370. # endif
  1371. }
  1372. # endif // hardware backlight
  1373. #else // no backlight
  1374. __attribute__((weak)) void backlight_init_ports(void) {}
  1375. __attribute__((weak)) void backlight_set(uint8_t level) {}
  1376. #endif // backlight
  1377. #ifdef HD44780_ENABLED
  1378. # include "hd44780.h"
  1379. #endif
  1380. // Functions for spitting out values
  1381. //
  1382. void send_dword(uint32_t number) { // this might not actually work
  1383. uint16_t word = (number >> 16);
  1384. send_word(word);
  1385. send_word(number & 0xFFFFUL);
  1386. }
  1387. void send_word(uint16_t number) {
  1388. uint8_t byte = number >> 8;
  1389. send_byte(byte);
  1390. send_byte(number & 0xFF);
  1391. }
  1392. void send_byte(uint8_t number) {
  1393. uint8_t nibble = number >> 4;
  1394. send_nibble(nibble);
  1395. send_nibble(number & 0xF);
  1396. }
  1397. void send_nibble(uint8_t number) {
  1398. switch (number) {
  1399. case 0:
  1400. register_code(KC_0);
  1401. unregister_code(KC_0);
  1402. break;
  1403. case 1 ... 9:
  1404. register_code(KC_1 + (number - 1));
  1405. unregister_code(KC_1 + (number - 1));
  1406. break;
  1407. case 0xA ... 0xF:
  1408. register_code(KC_A + (number - 0xA));
  1409. unregister_code(KC_A + (number - 0xA));
  1410. break;
  1411. }
  1412. }
  1413. __attribute__((weak)) uint16_t hex_to_keycode(uint8_t hex) {
  1414. hex = hex & 0xF;
  1415. if (hex == 0x0) {
  1416. return KC_0;
  1417. } else if (hex < 0xA) {
  1418. return KC_1 + (hex - 0x1);
  1419. } else {
  1420. return KC_A + (hex - 0xA);
  1421. }
  1422. }
  1423. void api_send_unicode(uint32_t unicode) {
  1424. #ifdef API_ENABLE
  1425. uint8_t chunk[4];
  1426. dword_to_bytes(unicode, chunk);
  1427. MT_SEND_DATA(DT_UNICODE, chunk, 5);
  1428. #endif
  1429. }
  1430. __attribute__((weak)) void led_set_user(uint8_t usb_led) {}
  1431. __attribute__((weak)) void led_set_kb(uint8_t usb_led) { led_set_user(usb_led); }
  1432. __attribute__((weak)) void led_init_ports(void) {}
  1433. __attribute__((weak)) void led_set(uint8_t usb_led) {
  1434. #if defined(BACKLIGHT_CAPS_LOCK) && defined(BACKLIGHT_ENABLE)
  1435. // Use backlight as Caps Lock indicator
  1436. uint8_t bl_toggle_lvl = 0;
  1437. if (IS_LED_ON(usb_led, USB_LED_CAPS_LOCK) && !backlight_config.enable) {
  1438. // Turning Caps Lock ON and backlight is disabled in config
  1439. // Toggling backlight to the brightest level
  1440. bl_toggle_lvl = BACKLIGHT_LEVELS;
  1441. } else if (IS_LED_OFF(usb_led, USB_LED_CAPS_LOCK) && backlight_config.enable) {
  1442. // Turning Caps Lock OFF and backlight is enabled in config
  1443. // Toggling backlight and restoring config level
  1444. bl_toggle_lvl = backlight_config.level;
  1445. }
  1446. // Set level without modify backlight_config to keep ability to restore state
  1447. backlight_set(bl_toggle_lvl);
  1448. #endif
  1449. led_set_kb(usb_led);
  1450. }
  1451. //------------------------------------------------------------------------------
  1452. // Override these functions in your keymap file to play different tunes on
  1453. // different events such as startup and bootloader jump
  1454. __attribute__((weak)) void startup_user() {}
  1455. __attribute__((weak)) void shutdown_user() {}
  1456. //------------------------------------------------------------------------------