aboutsummaryrefslogtreecommitdiff
path: root/keyboards/torn/torn_encoder.c
diff options
context:
space:
mode:
authorRichard Titmuss <richard.titmuss@gmail.com>2020-09-20 02:48:43 +0200
committerGitHub <noreply@github.com>2020-09-19 17:48:43 -0700
commite1437c1859b088c4da7ffb517f8034723172cd82 (patch)
tree9150a41bc34fea8937b2684138e826ead3f04fd2 /keyboards/torn/torn_encoder.c
parent741856dd57735dcd143987eb954ecc4a5ca2fc96 (diff)
downloadqmk_firmware-e1437c1859b088c4da7ffb517f8034723172cd82.tar.gz
qmk_firmware-e1437c1859b088c4da7ffb517f8034723172cd82.zip
[Keyboard] Add Torn keyboard (#10207)
* Add Torn keyboard * Apply suggestions from code review Co-authored-by: Nick Brassel <nick@tzarc.org> * Remove via json file * Add mcp23081_pin_t * Apply suggestions from code review Co-authored-by: James Young <18669334+noroadsleft@users.noreply.github.com> * Apply suggestions from code review Co-authored-by: James Young <18669334+noroadsleft@users.noreply.github.com> * Apply suggestions from code review Co-authored-by: Drashna Jaelre <drashna@live.com> * Apply suggestions from code review Co-authored-by: Ryan <fauxpark@gmail.com> Co-authored-by: Richard Titmuss <richardt@spotify.com> Co-authored-by: Nick Brassel <nick@tzarc.org> Co-authored-by: James Young <18669334+noroadsleft@users.noreply.github.com> Co-authored-by: Drashna Jaelre <drashna@live.com> Co-authored-by: Ryan <fauxpark@gmail.com>
Diffstat (limited to 'keyboards/torn/torn_encoder.c')
-rw-r--r--keyboards/torn/torn_encoder.c86
1 files changed, 86 insertions, 0 deletions
diff --git a/keyboards/torn/torn_encoder.c b/keyboards/torn/torn_encoder.c
new file mode 100644
index 000000000..ef9b0873f
--- /dev/null
+++ b/keyboards/torn/torn_encoder.c
@@ -0,0 +1,86 @@
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
34extern const uint16_t PROGMEM encoder_keymaps[][2][2];
35
36/**
37 * Tap on encoder updates using the encoder keymap
38 */
39void encoder_update_kb(uint8_t index, bool clockwise) {
40 int layer = get_highest_layer(layer_state);
41
42 uint16_t code;
43 do {
44 code = pgm_read_word(&encoder_keymaps[layer--][index][clockwise]);
45 } while (code == KC_TRNS);
46
47 tap_code16(code);
48}
49
50static bool encoder_read_state(uint8_t *state) {
51 uint8_t mcp23018_pin_state;
52 mcp23018_status_t status = mcp23018_readReg(GPIOB, &mcp23018_pin_state, 1);
53 if (status == 0) {
54 *state = (mcp23018_pin_state & 0b110000) >> 4;
55 return true;
56 }
57 return false;
58}
59
60static void encoder_update(int8_t index, uint8_t state) {
61 encoder_pulses += encoder_LUT[state & 0xF];
62 if (encoder_pulses >= ENCODER_RESOLUTION) {
63 encoder_update_kb(index, ENCODER_CLOCKWISE);
64 }
65 if (encoder_pulses <= -ENCODER_RESOLUTION) { // direction is arbitrary here, but this clockwise
66 encoder_update_kb(index, ENCODER_COUNTER_CLOCKWISE);
67 }
68 encoder_pulses %= ENCODER_RESOLUTION;
69}
70
71/**
72 * Read the secondary encoder over i2c
73 */
74void secondary_encoder_read(void) {
75 uint8_t state;
76 if (encoder_read_state(&state)) {
77 encoder_state <<= 2;
78 encoder_state |= state;
79 encoder_update(1, encoder_state);
80 }
81}
82
83/**
84 * Initialize the secondary encoder over i2c
85 */
86void secondary_encoder_init(void) { encoder_read_state(&encoder_state); }