encoder.c 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. /* Copyright 2020 Josef Adamcik
  2. * Modification for VIA support and RGB underglow by Jens Bonk-Wiltfang
  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 2 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. //Setting up what encoder rotation does. If your encoder can be pressed as a button, that function can be set in Via.
  18. #ifdef ENCODER_ENABLE
  19. static uint8_t encoder_state[2] = {0};
  20. //Use these keymap positions to specify the encoder functions on rotate.
  21. static keypos_t encoder_ccw[2] = {{6, 3}, {6, 8}};
  22. static keypos_t encoder_cw[2] = {{6, 1}, {6, 6}};
  23. void encoder_action_unregister(void) {
  24. for (int index = 0; index < 2; ++index) {
  25. if (encoder_state[index]) {
  26. keyevent_t encoder_event = (keyevent_t){.key = encoder_state[index] >> 1 ? encoder_cw[index] : encoder_ccw[index], .pressed = false, .time = (timer_read() | 1)};
  27. encoder_state[index] = 0;
  28. action_exec(encoder_event);
  29. }
  30. }
  31. }
  32. void encoder_action_register(uint8_t index, bool clockwise) {
  33. keyevent_t encoder_event = (keyevent_t){.key = clockwise ? encoder_cw[index] : encoder_ccw[index], .pressed = true, .time = (timer_read() | 1)};
  34. encoder_state[index] = (clockwise ^ 1) | (clockwise << 1);
  35. action_exec(encoder_event);
  36. }
  37. void matrix_scan_user(void) { encoder_action_unregister(); }
  38. bool encoder_update_user(uint8_t index, bool clockwise) {
  39. encoder_action_register(index, clockwise);
  40. return false;
  41. };
  42. #endif