logo

qmk_firmware

custom branch of QMK firmware git clone https://anongit.hacktivis.me/git/qmk_firmware.git

torn_encoder.c (2891B)


  1. /*
  2. * Copyright 2020 Richard Titmuss <richard.titmuss@gmail.com>
  3. * Copyright 2018 Jack Humbert <jack.humb@gmail.com>
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include "torn.h"
  19. #include "mcp23018.h"
  20. #ifndef ENCODER_RESOLUTION
  21. # define ENCODER_RESOLUTION 4
  22. #endif
  23. #define ENCODER_CLOCKWISE true
  24. #define ENCODER_COUNTER_CLOCKWISE false
  25. static int8_t encoder_LUT[] = {0, -1, 1, 0, 1, 0, 0, -1, -1, 0, 0, 1, 0, 1, -1, 0};
  26. static uint8_t encoder_state = 0;
  27. static int8_t encoder_pulses = 0;
  28. __attribute__((weak)) extern const uint16_t PROGMEM encoder_keymaps[][2][2];
  29. const uint16_t encoder_default[2][2] = { { KC_PGDN, KC_PGUP }, { KC_VOLD, KC_VOLU } };
  30. /**
  31. * Tap on encoder updates using the encoder keymap
  32. */
  33. bool encoder_update_kb(uint8_t index, bool clockwise) {
  34. // if (!encoder_update_user(index, clockwise)) return false;
  35. uint16_t code;
  36. if (encoder_keymaps) {
  37. uint8_t layer = get_highest_layer(layer_state);
  38. do {
  39. code = pgm_read_word(&encoder_keymaps[layer--][index][clockwise]);
  40. } while (code == KC_TRNS);
  41. } else {
  42. code = encoder_default[index][clockwise];
  43. }
  44. tap_code16(code);
  45. return true;
  46. }
  47. static bool encoder_read_state(uint8_t *state) {
  48. uint8_t mcp23018_pin_state;
  49. mcp23018_status_t status = mcp23018_readReg(GPIOB, &mcp23018_pin_state, 1);
  50. if (status == 0) {
  51. *state = (mcp23018_pin_state & 0b110000) >> 4;
  52. return true;
  53. }
  54. return false;
  55. }
  56. static void encoder_update(int8_t index, uint8_t state) {
  57. encoder_pulses += encoder_LUT[state & 0xF];
  58. if (encoder_pulses >= ENCODER_RESOLUTION) {
  59. encoder_update_kb(index, ENCODER_CLOCKWISE);
  60. }
  61. if (encoder_pulses <= -ENCODER_RESOLUTION) { // direction is arbitrary here, but this clockwise
  62. encoder_update_kb(index, ENCODER_COUNTER_CLOCKWISE);
  63. }
  64. encoder_pulses %= ENCODER_RESOLUTION;
  65. }
  66. /**
  67. * Read the secondary encoder over i2c
  68. */
  69. void secondary_encoder_read(void) {
  70. uint8_t state;
  71. if (encoder_read_state(&state)) {
  72. encoder_state <<= 2;
  73. encoder_state |= state;
  74. encoder_update(1, encoder_state);
  75. }
  76. }
  77. /**
  78. * Initialize the secondary encoder over i2c
  79. */
  80. void secondary_encoder_init(void) { encoder_read_state(&encoder_state); }