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.9 KiB

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