From 3a64a9c55ea0e9ff904f88d590abfaf665c17102 Mon Sep 17 00:00:00 2001 From: Jack Humbert Date: Fri, 26 Oct 2018 20:21:20 -0400 Subject: [PATCH] use encoders for thk --- keyboards/planck/thk/config.h | 12 +++++++-- keyboards/planck/thk/matrix.c | 49 ----------------------------------- keyboards/planck/thk/rules.mk | 4 ++- quantum/quantum.h | 2 +- 4 files changed, 14 insertions(+), 53 deletions(-) diff --git a/keyboards/planck/thk/config.h b/keyboards/planck/thk/config.h index 4ce23fe596..96eb238a69 100644 --- a/keyboards/planck/thk/config.h +++ b/keyboards/planck/thk/config.h @@ -19,11 +19,19 @@ along with this program. If not, see . #include "config_common.h" -// #define MATRIX_ROW_PINS { A7, A6, A5, A4 } -// #define MATRIX_COL_PINS { D7, C2, C3, C4, C5, C6, C7, A3, A2, A1, A0, B0 } +#undef MATRIX_ROW_PINS +#undef MATRIX_COL_PINS + +#define MATRIX_ROW_PINS { A7, A6, A5, A4 } +#define MATRIX_COL_PINS { D7, C2, C3, C4, C5, C6, C7, A3, A2, A1, A0, B0 } // #define DIODE_DIRECTION COL2ROW + +#define NUMBER_OF_ENCODERS 2 +#define ENCODERS_PAD_A { B4, B2 } +#define ENCODERS_PAD_B { B3, B1 } + #define TAPPING_TOGGLE 3 #define NO_UART 1 diff --git a/keyboards/planck/thk/matrix.c b/keyboards/planck/thk/matrix.c index c0e27b665e..b2028bfae6 100644 --- a/keyboards/planck/thk/matrix.c +++ b/keyboards/planck/thk/matrix.c @@ -33,10 +33,6 @@ static uint8_t debouncing = DEBOUNCE; static matrix_row_t matrix[MATRIX_ROWS]; static matrix_row_t matrix_debouncing[MATRIX_ROWS]; -static uint8_t encoder_state[2] = {0}; -static int8_t encoder_value[2] = {0}; -static int8_t encoder_LUT[] = { 0, -1, 1, 0, 1, 0, 0, -1, -1, 0, 0, 1, 0, 1, -1, 0 }; - static bool dip_switch[4] = {0, 0, 0, 0}; __attribute__ ((weak)) @@ -45,29 +41,8 @@ void dip_update(uint8_t index, bool active) { } __attribute__ ((weak)) void dip_update_kb(uint8_t index, bool active) { } -__attribute__ ((weak)) -void encoder_update(uint8_t index, bool clockwise) { } - bool last_dip_switch[4] = {0}; -#ifndef ENCODER_RESOLUTION - #define ENCODER_RESOLUTION 4 -#endif - -#define NUMBER_OF_ENCODERS 2 - -uint8_t read_encoder_state(uint8_t index) { - switch (index) { - case 0: - return (((PINB & (1 << 4)) << 0) ? 1 : 0) | ((((PINB & (1 << 3)) << 0) ? 1 : 0) << 1); - break; - case 1: - return (((PINB & (1 << 2)) << 0) ? 1 : 0) | ((((PINB & (1 << 1)) << 0) ? 1 : 0) << 1); - break; - } - return 0; -} - void matrix_init(void) { // disables JTAG so we can use them as columns @@ -78,13 +53,6 @@ void matrix_init(void) { DDRD &= ~((1 << 0) | (1 << 1) | (1 << 4) | (1 << 6)); PORTD |= ((1 << 0) | (1 << 1) | (1 << 4) | (1 << 6)); - // encoder setup (input, pull-up) - DDRB &= ~((1 << 1) | (1 << 2) | (1 << 3) | (1 << 4)); - PORTB |= ((1 << 1) | (1 << 2) | (1 << 3) | (1 << 4)); - - encoder_state[0] = read_encoder_state(0); - encoder_state[1] = read_encoder_state(1); - // rows (output) DDRA |= ((1 << 7) | (1 << 6) | (1 << 5) | (1 << 4)); PORTA |= ((1 << 7) | (1 << 6) | (1 << 5) | (1 << 4)); @@ -124,23 +92,6 @@ uint8_t matrix_scan(void) { } memcpy(last_dip_switch, dip_switch, sizeof(&dip_switch)); - - // encoders - for (uint8_t i = 0; i < NUMBER_OF_ENCODERS; i++) { - encoder_state[i] <<= 2; - - encoder_state[i] |= read_encoder_state(i); - - encoder_value[i] += encoder_LUT[encoder_state[i] & 0xF]; - if (encoder_value[i] >= ENCODER_RESOLUTION) { - encoder_update(i, 0); - } - if (encoder_value[i] <= -ENCODER_RESOLUTION) { // direction is arbitrary here, but this clockwise - encoder_update(i, 1); - } - encoder_value[i] %= ENCODER_RESOLUTION; - } - // actual matrix scan for (uint8_t c = 0; c < MATRIX_ROWS; c++) { switch (c) { diff --git a/keyboards/planck/thk/rules.mk b/keyboards/planck/thk/rules.mk index c7834a476c..ad3f3859e6 100644 --- a/keyboards/planck/thk/rules.mk +++ b/keyboards/planck/thk/rules.mk @@ -36,7 +36,7 @@ MOUSEKEY_ENABLE = yes EXTRAKEY_ENABLE = yes CONSOLE_ENABLE = no COMMAND_ENABLE = yes -KEY_LOCK_ENABLE = yes +KEY_LOCK_ENABLE = no NKRO_ENABLE = no # Nkey Rollover - if this doesn't work, see here: https://github.com/tmk/tmk_keyboard/wiki/FAQ#nkro-doesnt-work AUDIO_ENABLE = yes @@ -53,3 +53,5 @@ SRC = matrix.c PROGRAM_CMD = ./util/atmega32a_program.py $(TARGET).hex LAYOUTS = ortho_4x12 planck_mit planck_grid + +ENCODER_ENABLE = yes diff --git a/quantum/quantum.h b/quantum/quantum.h index 3f1b84fda2..0e29c0fa20 100644 --- a/quantum/quantum.h +++ b/quantum/quantum.h @@ -161,7 +161,7 @@ extern uint32_t default_layer_state; } } - #define readPin(pin) (PIN_ADDRESS(pin, 0) & _BV(pin & 0xF)) + #define readPin(pin) ((PIN_ADDRESS(pin, 0) & _BV(pin & 0xF)) != 0) #elif defined(PROTOCOL_CHIBIOS) #define pin_t ioline_t #define setPinInput(pin) palSetLineMode(pin, PAL_MODE_INPUT)