#include "opt_encoder.h"
enum State state;
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];
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 { 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;
if (range >= SCROLL_THRESH_RANGE_LIM) {
if (cur < curThresh) {
if (*cHigh == true) {
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) {
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;
}
}