~ruther/qmk_firmware

ref: cd374b1500b6d581c133f18d45a4a774d9e0f313 qmk_firmware/keyboards/ploopyco/common/opt_encoder_tiny.c -rw-r--r-- 3.9 KiB
cd374b15 — Ryan `clueboard/card`: Swap layout and alias (#24007) 9 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/* Copyright 2023 Leorize <leorize+oss@disroot.org>
 * Copyright 2011 Ben Buxton <bb@cactii.net>
 *
 * 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 3 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 <http://www.gnu.org/licenses/>.
 */
#include "opt_encoder.h"
#include <stdbool.h>
#include <stdint.h>

/* An extremely simple implementation of the encoder:
 *
 * For read out, the mechanism mimics that of a Schmitt trigger, with
 * statically defined high/low thresholds used instead of computing
 * one at runtime.
 *
 * The advantage of this approach is computing less in the decoder
 * implementation and allow the state to be measured before the wheel
 * moved.
 *
 * Compared to opt_encoder_simple.c, the use of an intermediary state
 * reduces sensitivity and de-sensitize against tiny movements caused
 * when lifting finger off the wheel.
 *
 * For turning decoded values into rotation, an algorithm inspired by
 * http://www.buxtronix.net/2011/10/rotary-encoders-done-properly.html
 * is employed.
 */

#if !defined(ENCODER_LOW_THRES_A) || !defined(ENCODER_HIGH_THRES_A)
#    error "The thresholds for phototransistors A is not defined in config.h"
#endif
#if !defined(ENCODER_LOW_THRES_B) || !defined(ENCODER_HIGH_THRES_B)
#    error "The thresholds for phototransistors B is not defined in config.h"
#endif
/*
 * Sample values, captured for a Ploopy Mini Trackball
 *
 * The following min-max values was captured by aggregating data recorded
 * when debug_encoder is enabled:
 *
 *   A: min: 0, max: 214
 *   B: min: 0, max: 204
 *
 * The threshold specified is then defined at the 1/4 and the 3/4 points.
 *
 * As these values might vary between units, you're encouraged to
 * measure your own.
 */
#if 0
#    define ENCODER_LOW_THRES_A 53
#    define ENCODER_HIGH_THRES_A 161
#    define ENCODER_LOW_THRES_B 52
#    define ENCODER_HIGH_THRES_B 153
#endif

/* Utilities for composing the encoder state */
#define MAKE_STATE(HI_A, HI_B) (((uint8_t)((HI_A) & 0x1) << 1) | ((uint8_t)((HI_B) & 0x1)))
#define STATE_A(st) ((st & 0x2) >> 1)
#define STATE_B(st) (st & 0x1)

typedef enum {
    START,
    DOWN_BEGIN,
    UP_BEGIN,
    START_MID,
    DOWN_BEGIN_MID,
    UP_BEGIN_MID,
    STATE_MASK    = 0xf, /* 0b1111 */
    EMIT_UP       = 0x10,
    EMIT_UP_MID   = EMIT_UP & START_MID,
    EMIT_DOWN     = 0x80,
    EMIT_DOWN_MID = EMIT_DOWN & START_MID,
    EMIT_MASK     = 0xf0
} encoder_state_t;

static encoder_state_t state;
static uint8_t         encState;

static const uint8_t transitions[] = {
    // clang-format off
    // START -> 00, 01, 10, 11
    START, DOWN_BEGIN, UP_BEGIN, START_MID,
    // DOWN_BEGIN -> 00, 01, 10, 11
    START, DOWN_BEGIN, START, EMIT_DOWN_MID,
    // UP_BEGIN -> 00, 01, 10, 11
    START, START, UP_BEGIN, EMIT_UP_MID,
    // START_MID -> 00, 01, 10, 11
    START, UP_BEGIN_MID, DOWN_BEGIN_MID, START_MID,
    // DOWN_BEGIN_MID -> 00, 01, 10, 11
    EMIT_DOWN, START_MID, DOWN_BEGIN_MID, START_MID,
    // UP_BEGIN_MID -> 00, 01, 10, 11
    EMIT_UP, UP_BEGIN_MID, START_MID, START_MID,
    // clang-format on
};

void opt_encoder_init(void) {
    state    = START;
    encState = 0;
}

int8_t opt_encoder_handler(uint16_t encA, uint16_t encB) {
    encState = MAKE_STATE((STATE_A(encState) & (encA > ENCODER_LOW_THRES_A)) | (encA > ENCODER_HIGH_THRES_A), (STATE_B(encState) & (encB > ENCODER_LOW_THRES_B)) | (encB > ENCODER_HIGH_THRES_B));
    state    = transitions[((state & STATE_MASK) << 2) + encState];
    return state & EMIT_MASK;
}
Do not follow this link