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.

57 lines
1.8 KiB

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