From b1fe7621ebb086a519778030381eb5729e949523 Mon Sep 17 00:00:00 2001 From: alaviss Date: Sat, 13 Aug 2022 18:01:44 +0000 Subject: RFC: add a simple implementation of the ploopy optical encoder (#17912) --- keyboards/ploopyco/mouse/mouse.c | 2 +- keyboards/ploopyco/opt_encoder.c | 11 +- keyboards/ploopyco/opt_encoder.h | 42 +----- keyboards/ploopyco/opt_encoder_simple.c | 147 +++++++++++++++++++++ keyboards/ploopyco/trackball/trackball.c | 2 +- keyboards/ploopyco/trackball_mini/trackball_mini.c | 4 +- 6 files changed, 165 insertions(+), 43 deletions(-) create mode 100644 keyboards/ploopyco/opt_encoder_simple.c (limited to 'keyboards') diff --git a/keyboards/ploopyco/mouse/mouse.c b/keyboards/ploopyco/mouse/mouse.c index 25ebd1ee2d..cde71205d0 100644 --- a/keyboards/ploopyco/mouse/mouse.c +++ b/keyboards/ploopyco/mouse/mouse.c @@ -111,7 +111,7 @@ void process_wheel(void) { int dir = opt_encoder_handler(p1, p2); if (dir == 0) return; - encoder_update_kb(0, dir == 1); + encoder_update_kb(0, dir > 0); } report_mouse_t pointing_device_task_kb(report_mouse_t mouse_report) { diff --git a/keyboards/ploopyco/opt_encoder.c b/keyboards/ploopyco/opt_encoder.c index 8698351c26..226db0a809 100644 --- a/keyboards/ploopyco/opt_encoder.c +++ b/keyboards/ploopyco/opt_encoder.c @@ -15,6 +15,9 @@ * along with this program. If not, see . */ #include "opt_encoder.h" +#include + +enum State { HIHI, HILO, LOLO, LOHI }; enum State state; @@ -44,6 +47,12 @@ 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) { @@ -70,7 +79,7 @@ void opt_encoder_init(void) { scrollThresholdB = 0; } -int opt_encoder_handler(int curA, int curB) { +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); diff --git a/keyboards/ploopyco/opt_encoder.h b/keyboards/ploopyco/opt_encoder.h index 17c25bc86f..8e5159f91c 100644 --- a/keyboards/ploopyco/opt_encoder.h +++ b/keyboards/ploopyco/opt_encoder.h @@ -16,7 +16,7 @@ */ #pragma once -#include +#include #ifndef SCROLLER_AR_SIZE # define SCROLLER_AR_SIZE 31 @@ -26,41 +26,7 @@ # define SCROLL_THRESH_RANGE_LIM 10 #endif -enum State { HIHI, HILO, LOLO, LOHI }; - -extern enum State state; - -/* Variables used for scroll wheel functionality. */ -extern bool lohif; -extern bool hilof; -extern int lowA; -extern int highA; -extern bool cLowA; -extern bool cHighA; -extern int lowIndexA; -extern int highIndexA; -extern bool lowOverflowA; -extern bool highOverflowA; -extern int lowB; -extern int highB; -extern bool cLowB; -extern bool cHighB; -extern int lowIndexB; -extern int highIndexB; -extern bool lowOverflowB; -extern bool highOverflowB; -extern int scrollThresholdA; -extern int scrollThresholdB; -extern int arLowA[SCROLLER_AR_SIZE]; -extern int arHighA[SCROLLER_AR_SIZE]; -extern int arLowB[SCROLLER_AR_SIZE]; -extern 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); - void opt_encoder_init(void); -int opt_encoder_handler(int curA, int curB); +/* Return the rotation direction, positive value means clockwise, negative value + * means counter-clockwise */ +int8_t opt_encoder_handler(uint16_t curA, uint16_t curB); diff --git a/keyboards/ploopyco/opt_encoder_simple.c b/keyboards/ploopyco/opt_encoder_simple.c new file mode 100644 index 0000000000..c30deb9ca4 --- /dev/null +++ b/keyboards/ploopyco/opt_encoder_simple.c @@ -0,0 +1,147 @@ +/* Copyright 2020 Christopher Courtney, aka Drashna Jael're (@drashna) + * Copyright 2020 Ploopy Corporation + * Copyright 2022 Leorize + * + * 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 . + */ +#include "opt_encoder.h" +#include "util.h" +#include +#include + +/* 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; +} diff --git a/keyboards/ploopyco/trackball/trackball.c b/keyboards/ploopyco/trackball/trackball.c index bda21b5c88..0b5f313ba6 100644 --- a/keyboards/ploopyco/trackball/trackball.c +++ b/keyboards/ploopyco/trackball/trackball.c @@ -112,7 +112,7 @@ void process_wheel(void) { int dir = opt_encoder_handler(p1, p2); if (dir == 0) return; - encoder_update_kb(0, dir == 1); + encoder_update_kb(0, dir > 0); } report_mouse_t pointing_device_task_kb(report_mouse_t mouse_report) { diff --git a/keyboards/ploopyco/trackball_mini/trackball_mini.c b/keyboards/ploopyco/trackball_mini/trackball_mini.c index e4b4a47c2b..b6349ea459 100644 --- a/keyboards/ploopyco/trackball_mini/trackball_mini.c +++ b/keyboards/ploopyco/trackball_mini/trackball_mini.c @@ -111,10 +111,10 @@ void process_wheel(void) { if (debug_encoder) dprintf("OPT1: %d, OPT2: %d\n", p1, p2); - uint8_t dir = opt_encoder_handler(p1, p2); + int8_t dir = opt_encoder_handler(p1, p2); if (dir == 0) return; - encoder_update_kb(0, dir == 1); + encoder_update_kb(0, dir > 0); } void pointing_device_init_kb(void) { -- cgit v1.2.3