mirror of
https://github.com/clockworkpi/DevTerm.git
synced 2025-12-12 10:18:49 +01:00
remove unused code related to the trackball (debouncer, glider and ratemeter)
This commit is contained in:
parent
7c93eac0bb
commit
7c523a7718
@ -1,53 +0,0 @@
|
||||
#ifndef DEBOUNCER_H
|
||||
#define DEBOUNCER_H
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
typedef uint8_t millis_t;
|
||||
|
||||
const millis_t DEBOUNCE_MS = 5;
|
||||
|
||||
/**
|
||||
@brief Asymmetric debouncer
|
||||
*/
|
||||
class Debouncer {
|
||||
public:
|
||||
Debouncer();
|
||||
void updateTime(millis_t delta);
|
||||
bool sample(bool value);
|
||||
private:
|
||||
millis_t timeout;
|
||||
};
|
||||
|
||||
template<typename T, T millis>
|
||||
class Timeout {
|
||||
public:
|
||||
Timeout() {
|
||||
timeout = 0;
|
||||
}
|
||||
|
||||
void updateTime(millis_t delta) {
|
||||
if (timeout > delta) {
|
||||
timeout -= delta;
|
||||
} else {
|
||||
timeout = 0;
|
||||
}
|
||||
}
|
||||
|
||||
void expire() {
|
||||
timeout = 0;
|
||||
}
|
||||
|
||||
bool get() const {
|
||||
return timeout == 0;
|
||||
}
|
||||
|
||||
void reset() {
|
||||
timeout = millis;
|
||||
}
|
||||
private:
|
||||
uint16_t timeout;
|
||||
};
|
||||
|
||||
|
||||
#endif
|
||||
@ -1,23 +0,0 @@
|
||||
#include "debouncer.h"
|
||||
|
||||
Debouncer::Debouncer()
|
||||
: timeout(0)
|
||||
{
|
||||
}
|
||||
|
||||
void Debouncer::updateTime(millis_t delta) {
|
||||
if (timeout > delta) {
|
||||
timeout -= delta;
|
||||
} else {
|
||||
timeout = 0;
|
||||
}
|
||||
}
|
||||
|
||||
bool Debouncer::sample(bool value) {
|
||||
if (value || timeout == 0) {
|
||||
timeout = DEBOUNCE_MS;
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
@ -1,29 +0,0 @@
|
||||
#ifndef GLIDER_H
|
||||
#define GLIDER_H
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
|
||||
class Glider {
|
||||
public:
|
||||
Glider();
|
||||
void setDirection(int8_t);
|
||||
void update(float velocity, uint16_t sustain);
|
||||
void updateSpeed(float velocity);
|
||||
void stop();
|
||||
|
||||
struct GlideResult {
|
||||
int8_t value;
|
||||
bool stopped;
|
||||
};
|
||||
GlideResult glide(uint8_t delta);
|
||||
|
||||
public:
|
||||
int8_t direction;
|
||||
float speed;
|
||||
uint16_t sustain;
|
||||
uint16_t release;
|
||||
float error;
|
||||
};
|
||||
|
||||
#endif
|
||||
@ -1,60 +0,0 @@
|
||||
#include <cmath>
|
||||
|
||||
#include "glider.h"
|
||||
#include "math.h"
|
||||
|
||||
Glider::Glider()
|
||||
: speed(0),
|
||||
sustain(0),
|
||||
release(0),
|
||||
error(0)
|
||||
{}
|
||||
|
||||
void Glider::setDirection(int8_t direction) {
|
||||
if (this->direction != direction) {
|
||||
stop();
|
||||
}
|
||||
this->direction = direction;
|
||||
}
|
||||
|
||||
void Glider::update(float speed, uint16_t sustain) {
|
||||
this->speed = speed;
|
||||
this->sustain = sustain;
|
||||
this->release = sustain;
|
||||
}
|
||||
|
||||
void Glider::updateSpeed(float speed) {
|
||||
this->speed = speed;
|
||||
}
|
||||
|
||||
void Glider::stop() {
|
||||
this->speed = 0;
|
||||
this->sustain = 0;
|
||||
this->release = 0;
|
||||
this->error = 0;
|
||||
}
|
||||
|
||||
Glider::GlideResult Glider::glide(millis_t delta) {
|
||||
const auto alreadyStopped = speed == 0;
|
||||
|
||||
error += speed * delta;
|
||||
int8_t distance = 0;
|
||||
if (error > 0) {
|
||||
distance = clamp<int8_t>(std::ceil(error));
|
||||
}
|
||||
error -= distance;
|
||||
|
||||
if (sustain > 0) {
|
||||
const auto sustained = min(sustain, (uint16_t)delta);
|
||||
sustain -= sustained;
|
||||
} else if (release > 0) {
|
||||
const auto released = min(release, (uint16_t)delta);
|
||||
speed = speed * (release - released) / release;
|
||||
release -= released;
|
||||
} else {
|
||||
speed = 0;
|
||||
}
|
||||
|
||||
const int8_t result = direction * distance;
|
||||
return GlideResult { result, !alreadyStopped && speed == 0 };
|
||||
}
|
||||
@ -1,38 +0,0 @@
|
||||
#ifndef RATEMETER_H
|
||||
#define RATEMETER_H
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
#include "debouncer.h"
|
||||
|
||||
class RateMeter {
|
||||
public:
|
||||
RateMeter();
|
||||
void onInterrupt();
|
||||
void tick(millis_t delta);
|
||||
void expire();
|
||||
|
||||
uint16_t delta() const;
|
||||
// Hall sensor edges per seconds.
|
||||
// stopped: 0
|
||||
// really slow => ~3
|
||||
// medium => ~30
|
||||
// fast => < 300
|
||||
// max => 1000
|
||||
float rate() const;
|
||||
|
||||
private:
|
||||
uint32_t lastTime;
|
||||
|
||||
// really Range, emperically:
|
||||
// fast => < 5_000 us,
|
||||
// medium => 20_000 - 40_000 us
|
||||
// really slow => 250_000 us
|
||||
uint32_t averageDelta;
|
||||
|
||||
static const uint16_t CUTOFF_MS = 1000;
|
||||
// Cut off after some seconds to prevent multiple timestamp overflow (~70 mins)
|
||||
Timeout<uint16_t, CUTOFF_MS> cutoff;
|
||||
};
|
||||
|
||||
#endif
|
||||
@ -1,47 +0,0 @@
|
||||
#include <Arduino.h>
|
||||
#include <cstdint>
|
||||
|
||||
#include "ratemeter.h"
|
||||
#include "math.h"
|
||||
|
||||
RateMeter::RateMeter()
|
||||
: lastTime(0)
|
||||
{}
|
||||
|
||||
void RateMeter::onInterrupt() {
|
||||
const auto now = millis();
|
||||
if (cutoff.get()) {
|
||||
averageDelta = CUTOFF_MS;
|
||||
} else {
|
||||
const auto delta = getDelta(lastTime, now, CUTOFF_MS);
|
||||
averageDelta = (averageDelta + delta) / 2;
|
||||
}
|
||||
lastTime = now;
|
||||
cutoff.reset();
|
||||
}
|
||||
|
||||
void RateMeter::tick(millis_t delta) {
|
||||
cutoff.updateTime(delta);
|
||||
if (!cutoff.get()) {
|
||||
averageDelta += delta;
|
||||
}
|
||||
}
|
||||
|
||||
void RateMeter::expire() {
|
||||
cutoff.expire();
|
||||
}
|
||||
|
||||
uint16_t RateMeter::delta() const {
|
||||
return averageDelta;
|
||||
}
|
||||
|
||||
float RateMeter::rate() const {
|
||||
if (cutoff.get()) {
|
||||
return 0.0f;
|
||||
} else if (averageDelta == 0) {
|
||||
// to ensure range 0 ~ 1000.0
|
||||
return 1000.0f;
|
||||
} else {
|
||||
return 1000.0f / (float)averageDelta;
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user