summaryrefslogtreecommitdiff
path: root/keyboards/ploopyco/opt_encoder.c
blob: 8698351c26cc8610355fac8cfd3e9248bad2ad59 (plain)
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/* Copyright 2020 Christopher Courtney, aka Drashna Jael're  (@drashna) <drashna@live.com>
 * Copyright 2020 Ploopy Corporation
 *
 * 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 <http://www.gnu.org/licenses/>.
 */
#include "opt_encoder.h"

enum State state;

/* Variables used for scroll wheel functionality. */
bool lohif;
bool hilof;
int  lowA;
int  highA;
bool cLowA;
bool cHighA;
int  lowIndexA;
int  highIndexA;
bool lowOverflowA;
bool highOverflowA;
int  lowB;
int  highB;
bool cLowB;
bool cHighB;
int  lowIndexB;
int  highIndexB;
bool lowOverflowB;
bool highOverflowB;
int  scrollThresholdA;
int  scrollThresholdB;
int  arLowA[SCROLLER_AR_SIZE];
int  arHighA[SCROLLER_AR_SIZE];
int  arLowB[SCROLLER_AR_SIZE];
int  arHighB[SCROLLER_AR_SIZE];

/* Setup function for the scroll wheel. Initializes
   the relevant variables. */
void opt_encoder_init(void) {
    state            = HIHI;
    lohif            = false;
    hilof            = false;
    lowA             = 1023;
    highA            = 0;
    cLowA            = false;
    cHighA           = false;
    lowIndexA        = 0;
    highIndexA       = 0;
    lowOverflowA     = false;
    highOverflowA    = false;
    lowB             = 1023;
    highB            = 0;
    cLowB            = false;
    cHighB           = false;
    lowIndexB        = 0;
    highIndexB       = 0;
    lowOverflowB     = false;
    highOverflowB    = false;
    scrollThresholdA = 0;
    scrollThresholdB = 0;
}

int opt_encoder_handler(int curA, int curB) {
    if (lowOverflowA == false || highOverflowA == false) calculateThresholdA(curA);
    if (lowOverflowB == false || highOverflowB == false) calculateThresholdB(curB);

    bool LO = false;
    bool HI = true;
    bool sA, sB;
    int  ret = 0;

    if (curA < scrollThresholdA)
        sA = LO;
    else
        sA = HI;

    if (curB < scrollThresholdB)
        sB = LO;
    else
        sB = HI;

    if (state == HIHI) {
        if (sA == LO && sB == HI) {
            state = LOHI;
            if (hilof) {
                ret   = 1;
                hilof = false;
            }
        } else if (sA == HI && sB == LO) {
            state = HILO;
            if (lohif) {
                ret   = -1;
                lohif = false;
            }
        }
    }

    else if (state == HILO) {
        if (sA == HI && sB == HI) {
            state = HIHI;
            hilof = true;
            lohif = false;
        } else if (sA == LO && sB == LO) {
            state = LOLO;
            hilof = true;
            lohif = false;
        }
    }

    else if (state == LOLO) {
        if (sA == HI && sB == LO) {
            state = HILO;
            if (lohif) {
                ret   = 1;
                lohif = false;
            }
        } else if (sA == LO && sB == HI) {
            state = LOHI;
            if (hilof) {
                ret   = -1;
                hilof = false;
            }
        }
    }

    else {  // state must be LOHI
        if (sA == HI && sB == HI) {
            state = HIHI;
            lohif = true;
            hilof = false;
        } else if (sA == LO && sB == LO) {
            state = LOLO;
            lohif = true;
            hilof = false;
        }
    }

    return ret;
}

void calculateThresholdA(int curA) { scrollThresholdA = calculateThreshold(curA, &lowA, &highA, &cLowA, &cHighA, arLowA, arHighA, &lowIndexA, &highIndexA, &lowOverflowA, &highOverflowA); }

void calculateThresholdB(int curB) { scrollThresholdB = calculateThreshold(curB, &lowB, &highB, &cLowB, &cHighB, arLowB, arHighB, &lowIndexB, &highIndexB, &lowOverflowB, &highOverflowB); }

int calculateThreshold(int cur, int* low, int* high, bool* cLow, bool* cHigh, int arLow[], int arHigh[], int* lowIndex, int* highIndex, bool* lowOverflow, bool* highOverflow) {
    if (cur < *low) *low = cur;
    if (cur > *high) *high = cur;

    int curThresh = thresholdEquation(*low, *high);
    int range     = *high - *low;

    // The range is enforced to be over a certain limit because noise
    // can cause erroneous readings, making these calculations unstable.
    if (range >= SCROLL_THRESH_RANGE_LIM) {
        if (cur < curThresh) {
            if (*cHigh == true) {
                // We were just high, and now we crossed to low.
                // high reflects a sample of a high reading.
                arHigh[*highIndex] = *high;
                incrementIndex(highIndex, highOverflow);
                int midpoint = ((*high - *low) / 2) + *low;
                *low         = midpoint;
                *high        = midpoint;
                *cLow        = false;
                *cHigh       = false;
            } else {
                *cLow = true;
            }
        }
        if (cur > curThresh) {
            if (*cLow == true) {
                // We were just low, and now we crossed to high.
                // low reflects a sample of a low reading.
                arLow[*lowIndex] = *low;
                incrementIndex(lowIndex, lowOverflow);
                int midpoint = ((*high - *low) / 2) + *low;
                *low         = midpoint;
                *high        = midpoint;
                *cLow        = false;
                *cHigh       = false;
            } else {
                *cHigh = true;
            }
        }
    }

    int calcHigh = 0;
    if (*highOverflow == true) {
        for (int i = 0; i < SCROLLER_AR_SIZE; i++) {
            calcHigh += arHigh[i];
        }
        calcHigh = calcHigh / SCROLLER_AR_SIZE;
    } else if (*highIndex != 0) {
        for (int i = 0; i < *highIndex; i++) {
            calcHigh += arHigh[i];
        }
        calcHigh = calcHigh / *highIndex;
    } else {
        calcHigh = *high;
    }

    int calcLow = 0;
    if (*lowOverflow == true) {
        for (int i = 0; i < SCROLLER_AR_SIZE; i++) {
            calcLow += arLow[i];
        }
        calcLow = calcLow / SCROLLER_AR_SIZE;
    } else if (*lowIndex != 0) {
        for (int i = 0; i < *lowIndex; i++) {
            calcLow += arLow[i];
        }
        calcLow = calcLow / *lowIndex;
    } else {
        calcLow = *low;
    }

    return thresholdEquation(calcLow, calcHigh);
}

int thresholdEquation(int lo, int hi) { return ((hi - lo) / 3) + lo; }

void incrementIndex(int* index, bool* ovflw) {
    if (*index < SCROLLER_AR_SIZE - 1)
        (*index)++;
    else {
        *index = 0;
        *ovflw = true;
    }
}