From e1437c1859b088c4da7ffb517f8034723172cd82 Mon Sep 17 00:00:00 2001 From: Richard Titmuss Date: Sun, 20 Sep 2020 02:48:43 +0200 Subject: [Keyboard] Add Torn keyboard (#10207) * Add Torn keyboard * Apply suggestions from code review Co-authored-by: Nick Brassel * 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 * Apply suggestions from code review Co-authored-by: Ryan Co-authored-by: Richard Titmuss Co-authored-by: Nick Brassel Co-authored-by: James Young <18669334+noroadsleft@users.noreply.github.com> Co-authored-by: Drashna Jaelre Co-authored-by: Ryan --- keyboards/torn/torn_encoder.c | 86 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 keyboards/torn/torn_encoder.c (limited to 'keyboards/torn/torn_encoder.c') 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 @@ +/* + * Copyright 2020 Richard Titmuss + * Copyright 2018 Jack Humbert + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include "torn.h" +#include "mcp23018.h" + +#ifndef ENCODER_RESOLUTION +# define ENCODER_RESOLUTION 4 +#endif + +#define ENCODER_CLOCKWISE true +#define ENCODER_COUNTER_CLOCKWISE false + +static int8_t encoder_LUT[] = {0, -1, 1, 0, 1, 0, 0, -1, -1, 0, 0, 1, 0, 1, -1, 0}; + +static uint8_t encoder_state = 0; +static int8_t encoder_pulses = 0; + +extern const uint16_t PROGMEM encoder_keymaps[][2][2]; + +/** + * Tap on encoder updates using the encoder keymap + */ +void encoder_update_kb(uint8_t index, bool clockwise) { + int layer = get_highest_layer(layer_state); + + uint16_t code; + do { + code = pgm_read_word(&encoder_keymaps[layer--][index][clockwise]); + } while (code == KC_TRNS); + + tap_code16(code); +} + +static bool encoder_read_state(uint8_t *state) { + uint8_t mcp23018_pin_state; + mcp23018_status_t status = mcp23018_readReg(GPIOB, &mcp23018_pin_state, 1); + if (status == 0) { + *state = (mcp23018_pin_state & 0b110000) >> 4; + return true; + } + return false; +} + +static void encoder_update(int8_t index, uint8_t state) { + encoder_pulses += encoder_LUT[state & 0xF]; + if (encoder_pulses >= ENCODER_RESOLUTION) { + encoder_update_kb(index, ENCODER_CLOCKWISE); + } + if (encoder_pulses <= -ENCODER_RESOLUTION) { // direction is arbitrary here, but this clockwise + encoder_update_kb(index, ENCODER_COUNTER_CLOCKWISE); + } + encoder_pulses %= ENCODER_RESOLUTION; +} + +/** + * Read the secondary encoder over i2c + */ +void secondary_encoder_read(void) { + uint8_t state; + if (encoder_read_state(&state)) { + encoder_state <<= 2; + encoder_state |= state; + encoder_update(1, encoder_state); + } +} + +/** + * Initialize the secondary encoder over i2c + */ +void secondary_encoder_init(void) { encoder_read_state(&encoder_state); } -- cgit v1.2.3