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.

68 lines
2.2 KiB

  1. /* Copyright 2020 Neil Brian Ramirez
  2. * Copyright 2021 drashna jael're (@drashna)
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include "encoder_actions.h"
  18. #if defined(VIA_ENABLE) && defined(ENCODER_ENABLE)
  19. # ifdef ENCODERS
  20. static uint8_t encoder_state[ENCODERS] = {0};
  21. static keypos_t encoder_cw[ENCODERS] = ENCODERS_CW_KEY;
  22. static keypos_t encoder_ccw[ENCODERS] = ENCODERS_CCW_KEY;
  23. # endif
  24. void encoder_action_unregister(void) {
  25. # ifdef ENCODERS
  26. for (int index = 0; index < ENCODERS; ++index) {
  27. if (encoder_state[index]) {
  28. keyevent_t encoder_event = (keyevent_t) {
  29. .key = encoder_state[index] >> 1 ? encoder_cw[index] : encoder_ccw[index],
  30. .pressed = false,
  31. .time = (timer_read() | 1)
  32. };
  33. encoder_state[index] = 0;
  34. action_exec(encoder_event);
  35. }
  36. }
  37. # endif
  38. }
  39. void encoder_action_register(uint8_t index, bool clockwise) {
  40. # ifdef ENCODERS
  41. keyevent_t encoder_event = (keyevent_t) {
  42. .key = clockwise ? encoder_cw[index] : encoder_ccw[index],
  43. .pressed = true,
  44. .time = (timer_read() | 1)
  45. };
  46. encoder_state[index] = (clockwise ^ 1) | (clockwise << 1);
  47. action_exec(encoder_event);
  48. # endif
  49. }
  50. void matrix_scan_kb(void) {
  51. encoder_action_unregister();
  52. matrix_scan_user();
  53. }
  54. bool encoder_update_kb(uint8_t index, bool clockwise) {
  55. encoder_action_register(index, clockwise);
  56. // don't return user actions, because they are in the keymap
  57. // encoder_update_user(index, clockwise);
  58. return true;
  59. };
  60. #endif