Parent directory

torn_encoder.c

2891 bytes
 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
19#include "torn.h"
20#include "mcp23018.h"
21
22#ifndef ENCODER_RESOLUTION
23#    define ENCODER_RESOLUTION 4
24#endif
25
26#define ENCODER_CLOCKWISE true
27#define ENCODER_COUNTER_CLOCKWISE false
28
29static int8_t encoder_LUT[] = {0, -1, 1, 0, 1, 0, 0, -1, -1, 0, 0, 1, 0, 1, -1, 0};
30
31static uint8_t encoder_state  = 0;
32static int8_t  encoder_pulses = 0;
33
34__attribute__((weak)) extern const uint16_t PROGMEM encoder_keymaps[][2][2];
35
36const uint16_t encoder_default[2][2] =  { { KC_PGDN, KC_PGUP }, { KC_VOLD, KC_VOLU } };
37
38/**
39 * Tap on encoder updates using the encoder keymap
40 */
41bool encoder_update_kb(uint8_t index, bool clockwise) {
42    // if (!encoder_update_user(index, clockwise)) return false;
43
44    uint16_t code;
45
46    if (encoder_keymaps) {
47      uint8_t layer = get_highest_layer(layer_state);
48      do {
49          code = pgm_read_word(&encoder_keymaps[layer--][index][clockwise]);
50      } while (code == KC_TRNS);
51    } else {
52      code = encoder_default[index][clockwise];
53    }
54
55    tap_code16(code);
56    return true;
57}
58
59static bool encoder_read_state(uint8_t *state) {
60    uint8_t           mcp23018_pin_state;
61    mcp23018_status_t status = mcp23018_readReg(GPIOB, &mcp23018_pin_state, 1);
62    if (status == 0) {
63        *state = (mcp23018_pin_state & 0b110000) >> 4;
64        return true;
65    }
66    return false;
67}
68
69static void encoder_update(int8_t index, uint8_t state) {
70    encoder_pulses += encoder_LUT[state & 0xF];
71    if (encoder_pulses >= ENCODER_RESOLUTION) {
72        encoder_update_kb(index, ENCODER_CLOCKWISE);
73    }
74    if (encoder_pulses <= -ENCODER_RESOLUTION) {  // direction is arbitrary here, but this clockwise
75        encoder_update_kb(index, ENCODER_COUNTER_CLOCKWISE);
76    }
77    encoder_pulses %= ENCODER_RESOLUTION;
78}
79
80/**
81 * Read the secondary encoder over i2c
82 */
83void secondary_encoder_read(void) {
84    uint8_t state;
85    if (encoder_read_state(&state)) {
86        encoder_state <<= 2;
87        encoder_state |= state;
88        encoder_update(1, encoder_state);
89    }
90}
91
92/**
93 * Initialize the secondary encoder over i2c
94 */
95void secondary_encoder_init(void) { encoder_read_state(&encoder_state); }