encoders.c 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /*
  2. Copyright 2022 Eric Gebhart <e.a.gebhart@gmail.com>
  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. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. #ifdef ENCODER_ENABLE
  15. #include "encoders.h"
  16. #include USERSPACE_H
  17. encoder_action_t encoder_actions[] = {
  18. #include "encoders.def"
  19. };
  20. uint8_t NUM_ENCODER_ACTIONS = sizeof(encoder_actions) / sizeof(encoder_action_t);
  21. bool encoder_update_user(uint8_t index, bool clockwise) {
  22. // do it twice, once for layer actions, once for non layer specific actions.
  23. if (!do_encoder_action(index, clockwise, true)){
  24. do_encoder_action(index, clockwise, false);
  25. }
  26. return false;
  27. }
  28. bool do_encoder_action(uint8_t index, bool clockwise, bool layer_actions) {
  29. uint8_t mods = get_mods();
  30. encoder_action_t *action;
  31. // look for a match.
  32. // on the layer, not on any layer.
  33. // with the mods, or no mods.
  34. for (int i = 0; i < NUM_ENCODER_ACTIONS; ++i) {
  35. action = &encoder_actions[i];
  36. // this encoder, or another.
  37. if (action->index != index)
  38. continue;
  39. // skip non layer specific actions and visa versa
  40. // two pass system, once for layers, again for
  41. // actions without layers.
  42. if (layer_actions){
  43. if (action->layer == LAYER_NONE ||
  44. action->layer != biton32(layer_state)){
  45. continue;
  46. }
  47. }else if (action->layer != LAYER_NONE)
  48. continue;
  49. // no mods, or these mods.
  50. if ((mods && (action->mods == MOD_NONE)) ||
  51. (mods && (mods != action->mods)))
  52. continue;
  53. // found one.
  54. if (clockwise) {
  55. if (action->clockwise != 0) {
  56. tap_code16(action->clockwise);
  57. } else if (action->cw_func != NULL) {
  58. action->cw_func();
  59. }
  60. } else {
  61. if (action->counter_clockwise != 0) {
  62. tap_code16(action->counter_clockwise);
  63. } else if (action->ccw_func != NULL) {
  64. action->ccw_func();
  65. }
  66. }
  67. }
  68. return false;
  69. }
  70. #endif