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.

51 lines
1.7 KiB

  1. /* Copyright 2020 Neil Brian Ramirez
  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 3 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 "encoder_action.h"
  17. #ifdef ENCODERS
  18. static uint8_t encoder_state[ENCODERS] = {0};
  19. static keypos_t encoder_cw[ENCODERS] = ENCODERS_CW_KEY;
  20. static keypos_t encoder_ccw[ENCODERS] = ENCODERS_CCW_KEY;
  21. #endif
  22. void encoder_action_unregister(void) {
  23. #ifdef ENCODERS
  24. for (int index = 0; index < ENCODERS; ++index) {
  25. if (encoder_state[index]) {
  26. keyevent_t encoder_event = (keyevent_t) {
  27. .key = encoder_state[index] >> 1 ? encoder_cw[index] : encoder_ccw[index],
  28. .pressed = false,
  29. .time = (timer_read() | 1)
  30. };
  31. encoder_state[index] = 0;
  32. action_exec(encoder_event);
  33. }
  34. }
  35. #endif
  36. }
  37. void encoder_action_register(uint8_t index, bool clockwise) {
  38. #ifdef ENCODERS
  39. keyevent_t encoder_event = (keyevent_t) {
  40. .key = clockwise ? encoder_cw[index] : encoder_ccw[index],
  41. .pressed = true,
  42. .time = (timer_read() | 1)
  43. };
  44. encoder_state[index] = (clockwise ^ 1) | (clockwise << 1);
  45. action_exec(encoder_event);
  46. #endif
  47. }