keymap.c 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /* Copyright 2022 Simon Fell
  2. *
  3. * This program is free software: you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation, either version 2 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include QMK_KEYBOARD_H
  17. enum custom_layers {
  18. _NUMPAD,
  19. _CONTROL,
  20. _ADJUST,
  21. };
  22. #define RGBLIGHT_TIMEOUT 30000 // 30 seconds
  23. // clang-format off
  24. const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
  25. [_NUMPAD] = LAYOUT(
  26. TO(_CONTROL), KC_DOT,
  27. KC_7 , KC_8, KC_9,
  28. KC_4, KC_5, KC_6, KC_0,
  29. KC_1 , KC_2 , KC_3
  30. ),
  31. [_CONTROL] = LAYOUT(
  32. TO(_ADJUST), _______,
  33. LGUI(KC_X), LGUI(KC_C), LGUI(KC_V),
  34. LGUI(KC_Q), LGUI(KC_W), LGUI(KC_N), LGUI(KC_S),
  35. KC_VOLD , KC_MUTE , KC_VOLU
  36. ),
  37. [_ADJUST] = LAYOUT(
  38. TO(_NUMPAD), QK_BOOT,
  39. XXXXXXX, XXXXXXX, XXXXXXX,
  40. RGB_MOD, XXXXXXX, XXXXXXX, XXXXXXX,
  41. RGB_TOG, RGB_VAD, RGB_VAI
  42. ),
  43. };
  44. // clang-format on
  45. layer_state_t layer_state_set_user(layer_state_t state) {
  46. switch (get_highest_layer(state)) {
  47. case _NUMPAD:
  48. rgblight_sethsv_noeeprom(170, 255, 128);
  49. rgblight_mode_noeeprom(2);
  50. break;
  51. case _CONTROL:
  52. rgblight_mode_noeeprom(3);
  53. break;
  54. case _ADJUST:
  55. rgblight_mode_noeeprom(4);
  56. break;
  57. }
  58. return state;
  59. }
  60. // turn rgb off after some amount of inactivity
  61. static uint16_t key_timer; // timer to track the last keyboard activity
  62. static bool is_rgb_timeout = false; // store if RGB has timed out or not in a boolean
  63. /* Runs at the end of each scan loop, check if RGB timeout has occurred */
  64. void housekeeping_task_user(void) {
  65. if (!is_rgb_timeout && timer_elapsed(key_timer) > RGBLIGHT_TIMEOUT) {
  66. rgblight_disable_noeeprom();
  67. is_rgb_timeout = true;
  68. }
  69. }
  70. /* Runs after each key press, check if activity occurred */
  71. void post_process_record_user(uint16_t keycode, keyrecord_t *record) {
  72. if (record->event.pressed) {
  73. key_timer = timer_read(); // store time of last refresh
  74. if (is_rgb_timeout) { // only do something if rgb has timed out
  75. is_rgb_timeout = false;
  76. rgblight_enable_noeeprom();
  77. }
  78. }
  79. }