1
0
Fork 0

[Keyboard] Overhaul ploopyco devices (#22967)

This commit is contained in:
Drashna Jael're 2024-03-14 22:15:44 -07:00 committed by GitHub
parent 359cec14fa
commit 68e8d74188
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
51 changed files with 355 additions and 1909 deletions

View file

@ -0,0 +1,32 @@
/* 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/>.
*/
#pragma once
#include <stdint.h>
#ifndef SCROLLER_AR_SIZE
# define SCROLLER_AR_SIZE 31
#endif
#ifndef SCROLL_THRESH_RANGE_LIM
# define SCROLL_THRESH_RANGE_LIM 10
#endif
void opt_encoder_init(void);
/* Return the rotation direction, positive value means clockwise, negative value
* means counter-clockwise */
int8_t opt_encoder_handler(uint16_t curA, uint16_t curB);

View file

@ -0,0 +1,254 @@
/* 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"
#include <stdbool.h>
enum State { HIHI, HILO, LOLO, LOHI };
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];
void calculateThresholdA(int curA);
void calculateThresholdB(int curB);
int calculateThreshold(int cur, int* low, int* high, bool* cLow, bool* cHigh, int arLow[], int arHigh[], int* lowIndex, int* highIndex, bool* lowOverflow, bool* highOverflow);
int thresholdEquation(int lo, int hi);
void incrementIndex(int* index, bool* ovflw);
/* 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;
}
int8_t opt_encoder_handler(uint16_t curA, uint16_t 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;
}
}

View file

@ -0,0 +1,142 @@
/* Copyright 2020 Christopher Courtney, aka Drashna Jael're (@drashna) <drashna@live.com>
* Copyright 2020 Ploopy Corporation
* Copyright 2022 Leorize <leorize+oss@disroot.org>
*
* 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"
#include "util.h"
#include <stdbool.h>
#include <stdint.h>
/* An alternative implementation for interpreting the encoder status:
*
* From graphing the phototransistor voltages, the peak and baseline appears to
* be rather stable. Therefore there is no need to average them out, and instead
* just simply store the min and max voltages of each phototransistor.
*
* This algorithm then distinguish between high and low states by employing an
* approach similar to a Schmitt trigger: a low and high threshold is defined
* for each phototransistor based on their min and max voltages.
*
* Currently, the thresholds are:
*
* * High threshold: The upper quarter of the voltage range.
* * Low threshold: The lower quarter of the voltage range.
*
* these thresholds are defined for each phototransistor.
*
* For a state to cross from high -> low, it must fall below the low threshold.
* Similarly, to cross from low -> high, the voltage must be higher than the
* high threshold.
*
* Having two distinct thresholds filters out the bulk of noise from the
* phototransistors.
*
* For converting the resulting high and low signals into rotation, a simple
* quadrature decoder is used.
*/
/* The minimum value returned by the ADC */
#define ENCODER_MIN 0
/* The maximum value returned by the ADC */
#define ENCODER_MAX 1023
/* 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)
#define LOLO MAKE_STATE(0, 0)
#define HILO MAKE_STATE(1, 0)
#define LOHI MAKE_STATE(0, 1)
typedef enum {
CALIBRATION, /* Recalibrate encoder state by waiting for a 01 -> 00 or 10 -> 00 transistion */
DECODE /* Translate changes in the encoder state into movement */
} encoder_state_t;
static encoder_state_t mode;
static uint8_t lastState;
static uint16_t lowA;
static uint16_t highA;
static uint16_t lowB;
static uint16_t highB;
#define MOVE_UP 1
#define MOVE_DOWN -1
#define MOVE_NONE 0
#define MOVE_ERR 0x7F
static const uint8_t movement[] = {
// 00 -> 00, 01, 10, 11
MOVE_NONE, MOVE_DOWN, MOVE_UP, MOVE_ERR,
// 01 -> 00, 01, 10, 11
MOVE_UP, MOVE_NONE, MOVE_ERR, MOVE_DOWN,
// 10 -> 00, 01, 10, 11
MOVE_DOWN, MOVE_ERR, MOVE_NONE, MOVE_UP,
// 11 -> 00, 01, 10, 11
MOVE_ERR, MOVE_UP, MOVE_DOWN, MOVE_NONE};
void opt_encoder_init(void) {
mode = CALIBRATION;
lastState = 0;
lowA = ENCODER_MAX;
lowB = ENCODER_MAX;
highA = ENCODER_MIN;
highB = ENCODER_MIN;
}
int8_t opt_encoder_handler(uint16_t encA, uint16_t encB) {
int8_t result = 0;
highA = MAX(encA, highA);
lowA = MIN(encA, lowA);
highB = MAX(encB, highB);
lowB = MIN(encB, lowB);
/* Only compute the thresholds after a large enough range is established */
if (highA - lowA > SCROLL_THRESH_RANGE_LIM && highB - lowB > SCROLL_THRESH_RANGE_LIM) {
const int16_t lowThresholdA = (highA + lowA) / 4;
const int16_t highThresholdA = (highA + lowA) - lowThresholdA;
const int16_t lowThresholdB = (highB + lowB) / 4;
const int16_t highThresholdB = (highB + lowB) - lowThresholdB;
uint8_t state = MAKE_STATE(STATE_A(lastState) ? encA > lowThresholdA : encA > highThresholdA, STATE_B(lastState) ? encB > lowThresholdB : encB > highThresholdB);
switch (mode) {
case CALIBRATION:
if ((lastState == HILO && state == LOLO) || (lastState == LOHI && state == LOLO))
mode = DECODE;
else
mode = CALIBRATION;
break;
case DECODE:
result = movement[lastState * 4 + state];
/* If we detect a state change that should not be possible,
* then the wheel might have moved too fast and we need to
* recalibrate the encoder position. */
mode = result == MOVE_ERR ? CALIBRATION : mode;
result = result == MOVE_ERR ? MOVE_NONE : result;
break;
}
lastState = state;
}
return result;
}

View file

@ -0,0 +1,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;
}