torn_encoder.c 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. extern const uint16_t PROGMEM encoder_keymaps[][2][2];
  29. /**
  30. * Tap on encoder updates using the encoder keymap
  31. */
  32. void encoder_update_kb(uint8_t index, bool clockwise) {
  33. int layer = get_highest_layer(layer_state);
  34. uint16_t code;
  35. do {
  36. code = pgm_read_word(&encoder_keymaps[layer--][index][clockwise]);
  37. } while (code == KC_TRNS);
  38. tap_code16(code);
  39. }
  40. static bool encoder_read_state(uint8_t *state) {
  41. uint8_t mcp23018_pin_state;
  42. mcp23018_status_t status = mcp23018_readReg(GPIOB, &mcp23018_pin_state, 1);
  43. if (status == 0) {
  44. *state = (mcp23018_pin_state & 0b110000) >> 4;
  45. return true;
  46. }
  47. return false;
  48. }
  49. static void encoder_update(int8_t index, uint8_t state) {
  50. encoder_pulses += encoder_LUT[state & 0xF];
  51. if (encoder_pulses >= ENCODER_RESOLUTION) {
  52. encoder_update_kb(index, ENCODER_CLOCKWISE);
  53. }
  54. if (encoder_pulses <= -ENCODER_RESOLUTION) { // direction is arbitrary here, but this clockwise
  55. encoder_update_kb(index, ENCODER_COUNTER_CLOCKWISE);
  56. }
  57. encoder_pulses %= ENCODER_RESOLUTION;
  58. }
  59. /**
  60. * Read the secondary encoder over i2c
  61. */
  62. void secondary_encoder_read(void) {
  63. uint8_t state;
  64. if (encoder_read_state(&state)) {
  65. encoder_state <<= 2;
  66. encoder_state |= state;
  67. encoder_update(1, encoder_state);
  68. }
  69. }
  70. /**
  71. * Initialize the secondary encoder over i2c
  72. */
  73. void secondary_encoder_init(void) { encoder_read_state(&encoder_state); }