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/>.
24 #ifndef ENCODER_RESOLUTION
25 #define ENCODER_RESOLUTION 4
28 #ifndef NUMBER_OF_ENCODERS
29 #error "Number of encoders not defined by NUMBER_OF_ENCODERS"
32 #if !defined(ENCODERS_PAD_A) || !defined(ENCODERS_PAD_B)
33 #error "No encoder pads defined by ENCODERS_PAD_A and ENCODERS_PAD_B"
36 static pin_t encoders_pad_a
[NUMBER_OF_ENCODERS
] = ENCODERS_PAD_A
;
37 static pin_t encoders_pad_b
[NUMBER_OF_ENCODERS
] = ENCODERS_PAD_B
;
39 static int8_t encoder_LUT
[] = { 0, -1, 1, 0, 1, 0, 0, -1, -1, 0, 0, 1, 0, 1, -1, 0 };
41 static uint8_t encoder_state
[NUMBER_OF_ENCODERS
] = {0};
44 // slave half encoders come over as second set of encoders
45 static int8_t encoder_value
[NUMBER_OF_ENCODERS
* 2] = {0};
47 static int8_t encoder_value
[NUMBER_OF_ENCODERS
] = {0};
50 __attribute__ ((weak
))
51 void encoder_update_user(int8_t index
, bool clockwise
) { }
53 __attribute__ ((weak
))
54 void encoder_update_kb(int8_t index
, bool clockwise
) {
55 encoder_update_user(index
, clockwise
);
58 void encoder_init(void) {
59 for (int i
= 0; i
< NUMBER_OF_ENCODERS
; i
++) {
60 setPinInputHigh(encoders_pad_a
[i
]);
61 setPinInputHigh(encoders_pad_b
[i
]);
63 encoder_state
[i
] = (readPin(encoders_pad_a
[i
]) << 0) | (readPin(encoders_pad_b
[i
]) << 1);
67 void encoder_read(void) {
68 for (int i
= 0; i
< NUMBER_OF_ENCODERS
; i
++) {
69 encoder_state
[i
] <<= 2;
70 encoder_state
[i
] |= (readPin(encoders_pad_a
[i
]) << 0) | (readPin(encoders_pad_b
[i
]) << 1);
71 encoder_value
[i
] += encoder_LUT
[encoder_state
[i
] & 0xF];
72 if (encoder_value
[i
] >= ENCODER_RESOLUTION
) {
73 encoder_update_kb(i
, false);
75 if (encoder_value
[i
] <= -ENCODER_RESOLUTION
) { // direction is arbitrary here, but this clockwise
76 encoder_update_kb(i
, true);
78 encoder_value
[i
] %= ENCODER_RESOLUTION
;
83 void encoder_state_raw(uint8_t* slave_state
) {
84 memcpy(slave_state
, encoder_state
, sizeof(encoder_state
));
87 void encoder_update_raw(uint8_t* slave_state
) {
88 for (int i
= 0; i
< NUMBER_OF_ENCODERS
; i
++) {
89 encoder_value
[NUMBER_OF_ENCODERS
+ i
] += encoder_LUT
[slave_state
[i
] & 0xF];
90 if (encoder_value
[NUMBER_OF_ENCODERS
+ i
] >= ENCODER_RESOLUTION
) {
91 encoder_update_kb(NUMBER_OF_ENCODERS
+ i
, false);
93 if (encoder_value
[NUMBER_OF_ENCODERS
+ i
] <= -ENCODER_RESOLUTION
) { // direction is arbitrary here, but this clockwise
94 encoder_update_kb(NUMBER_OF_ENCODERS
+ i
, true);
96 encoder_value
[NUMBER_OF_ENCODERS
+ i
] %= ENCODER_RESOLUTION
;