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.

55 lines
1.9 KiB

  1. /* Copyright 2021 KDon<370490639@qq.com>
  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 2 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 "kabedon98e.h"
  17. static uint8_t encoder_state[ENCODERS] = {0};
  18. static keypos_t encoder_cw[ENCODERS] = ENCODERS_CW_KEY;
  19. static keypos_t encoder_ccw[ENCODERS] = ENCODERS_CCW_KEY;
  20. void encoder_action_unregister(void) {
  21. for (int index = 0; index < ENCODERS; ++index) {
  22. if (encoder_state[index]) {
  23. keyevent_t encoder_event = (keyevent_t) {
  24. .key = encoder_state[index] >> 1 ? encoder_cw[index] : encoder_ccw[index],
  25. .pressed = false,
  26. .time = (timer_read() | 1)
  27. };
  28. encoder_state[index] = 0;
  29. action_exec(encoder_event);
  30. }
  31. }
  32. }
  33. void encoder_action_register(uint8_t index, bool clockwise) {
  34. keyevent_t encoder_event = (keyevent_t) {
  35. .key = clockwise ? encoder_cw[index] : encoder_ccw[index],
  36. .pressed = true,
  37. .time = (timer_read() | 1)
  38. };
  39. encoder_state[index] = (clockwise ^ 1) | (clockwise << 1);
  40. action_exec(encoder_event);
  41. }
  42. void matrix_scan_kb(void) {
  43. encoder_action_unregister();
  44. matrix_scan_user();
  45. }
  46. bool encoder_update_kb(uint8_t index, bool clockwise) {
  47. if (!encoder_update_user(index, clockwise)) { return false; }
  48. encoder_action_register(index, clockwise);
  49. return true;
  50. };