encoder_actions.c 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. static uint8_t encoder_state[NUM_ENCODERS] = {0};
  20. static keypos_t encoder_cw[NUM_ENCODERS] = ENCODERS_CW_KEY;
  21. static keypos_t encoder_ccw[NUM_ENCODERS] = ENCODERS_CCW_KEY;
  22. void encoder_action_unregister(void) {
  23. for (int index = 0; index < NUM_ENCODERS; ++index) {
  24. if (encoder_state[index]) {
  25. keyevent_t encoder_event = (keyevent_t) {
  26. .key = encoder_state[index] >> 1 ? encoder_cw[index] : encoder_ccw[index],
  27. .pressed = false,
  28. .time = (timer_read() | 1)
  29. };
  30. encoder_state[index] = 0;
  31. action_exec(encoder_event);
  32. }
  33. }
  34. }
  35. void encoder_action_register(uint8_t index, bool clockwise) {
  36. keyevent_t encoder_event = (keyevent_t) {
  37. .key = clockwise ? encoder_cw[index] : encoder_ccw[index],
  38. .pressed = true,
  39. .time = (timer_read() | 1)
  40. };
  41. encoder_state[index] = (clockwise ^ 1) | (clockwise << 1);
  42. action_exec(encoder_event);
  43. }
  44. void matrix_scan_kb(void) {
  45. encoder_action_unregister();
  46. matrix_scan_user();
  47. }
  48. bool encoder_update_kb(uint8_t index, bool clockwise) {
  49. encoder_action_register(index, clockwise);
  50. // don't return user actions, because they are in the keymap
  51. // encoder_update_user(index, clockwise);
  52. return true;
  53. };
  54. #endif