2 * Copyright 2018 Jack Humbert <jack.humb@gmail.com>
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 # include "split_util.h"
26 #ifndef ENCODER_RESOLUTION
27 # define ENCODER_RESOLUTION 4
30 #if !defined(ENCODERS_PAD_A) || !defined(ENCODERS_PAD_B)
31 # error "No encoder pads defined by ENCODERS_PAD_A and ENCODERS_PAD_B"
34 #define NUMBER_OF_ENCODERS (sizeof(encoders_pad_a) / sizeof(pin_t))
35 static pin_t encoders_pad_a[] = ENCODERS_PAD_A;
36 static pin_t encoders_pad_b[] = ENCODERS_PAD_B;
38 static int8_t encoder_LUT[] = {0, -1, 1, 0, 1, 0, 0, -1, -1, 0, 0, 1, 0, 1, -1, 0};
40 static uint8_t encoder_state[NUMBER_OF_ENCODERS] = {0};
43 // slave half encoders come over as second set of encoders
44 static int8_t encoder_value[NUMBER_OF_ENCODERS * 2] = {0};
46 static int8_t encoder_value[NUMBER_OF_ENCODERS] = {0};
49 __attribute__((weak)) void encoder_update_user(int8_t index, bool clockwise) {}
51 __attribute__((weak)) void encoder_update_kb(int8_t index, bool clockwise) { encoder_update_user(index, clockwise); }
53 void encoder_init(void) {
54 #if defined(SPLIT_KEYBOARD) && defined(ENCODERS_PAD_A_RIGHT) && defined(ENCODERS_PAD_B_RIGHT)
56 const pin_t encoders_pad_a_right[] = ENCODERS_PAD_A_RIGHT;
57 const pin_t encoders_pad_b_right[] = ENCODERS_PAD_B_RIGHT;
58 for (uint8_t i = 0; i < NUMBER_OF_ENCODERS; i++) {
59 encoders_pad_a[i] = encoders_pad_a_right[i];
60 encoders_pad_b[i] = encoders_pad_b_right[i];
65 for (int i = 0; i < NUMBER_OF_ENCODERS; i++) {
66 setPinInputHigh(encoders_pad_a[i]);
67 setPinInputHigh(encoders_pad_b[i]);
69 encoder_state[i] = (readPin(encoders_pad_a[i]) << 0) | (readPin(encoders_pad_b[i]) << 1);
73 void encoder_read(void) {
74 for (int i = 0; i < NUMBER_OF_ENCODERS; i++) {
75 encoder_state[i] <<= 2;
76 encoder_state[i] |= (readPin(encoders_pad_a[i]) << 0) | (readPin(encoders_pad_b[i]) << 1);
77 encoder_value[i] += encoder_LUT[encoder_state[i] & 0xF];
78 if (encoder_value[i] >= ENCODER_RESOLUTION) {
79 encoder_update_kb(i, false);
81 if (encoder_value[i] <= -ENCODER_RESOLUTION) { // direction is arbitrary here, but this clockwise
82 encoder_update_kb(i, true);
84 encoder_value[i] %= ENCODER_RESOLUTION;
89 void encoder_state_raw(uint8_t* slave_state) { memcpy(slave_state, encoder_state, sizeof(encoder_state)); }
91 void encoder_update_raw(uint8_t* slave_state) {
92 for (int i = 0; i < NUMBER_OF_ENCODERS; i++) {
93 encoder_value[NUMBER_OF_ENCODERS + i] += encoder_LUT[slave_state[i] & 0xF];
94 if (encoder_value[NUMBER_OF_ENCODERS + i] >= ENCODER_RESOLUTION) {
95 encoder_update_kb(NUMBER_OF_ENCODERS + i, false);
97 if (encoder_value[NUMBER_OF_ENCODERS + i] <= -ENCODER_RESOLUTION) { // direction is arbitrary here, but this clockwise
98 encoder_update_kb(NUMBER_OF_ENCODERS + i, true);
100 encoder_value[NUMBER_OF_ENCODERS + i] %= ENCODER_RESOLUTION;