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.

62 lines
2.1 KiB

  1. /*
  2. Copyright 2021 Stanrc85
  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. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. #include "stanrc85.h"
  15. // Backlight timeout feature
  16. // Modified from https://www.reddit.com/r/MechanicalKeyboards/comments/ix65z0/looking_for_qmk_led_idle_timeout_info/g64yov7/
  17. #define BACKLIGHT_TIMEOUT 10 // in minutes
  18. static uint16_t idle_timer = 0;
  19. static uint8_t halfmin_counter = 0;
  20. static bool led_on = true;
  21. static bool rgb_on = true;
  22. static bool rgb_was_on = false;
  23. void matrix_scan_user(void) {
  24. // idle_timer needs to be set one time
  25. if (idle_timer == 0) idle_timer = timer_read();
  26. if ( (led_on && timer_elapsed(idle_timer) > 30000) || (rgb_on && timer_elapsed(idle_timer) > 30000)) {
  27. halfmin_counter++;
  28. idle_timer = timer_read();
  29. }
  30. if ( (led_on && halfmin_counter >= BACKLIGHT_TIMEOUT * 2) || (rgb_on && halfmin_counter >= BACKLIGHT_TIMEOUT * 2)) {
  31. if(rgblight_is_enabled()) {
  32. rgb_was_on = true;
  33. rgblight_disable_noeeprom();
  34. led_on = false;
  35. rgb_on = false;
  36. } else {
  37. rgb_was_on = false;
  38. }
  39. halfmin_counter = 0;
  40. }
  41. };
  42. bool process_record_keymap(uint16_t keycode, keyrecord_t *record) {
  43. if (record->event.pressed) {
  44. if (led_on == false || rgb_on == false ) {
  45. if (rgb_was_on) {
  46. rgblight_enable_noeeprom();
  47. led_on = true;
  48. rgb_on = true;
  49. }
  50. }
  51. idle_timer = timer_read();
  52. halfmin_counter = 0;
  53. }
  54. return true;
  55. }