Permalink
Browse files

Add debouncing, reduce latency 2.7ms->0.7ms

Implements a simple no-latency per-key debounce method
that just doesn't repoll a key for transitions until
5ms after it last transitioned.
  • Loading branch information...
trishume committed Dec 29, 2017
1 parent b913e6a commit 372c2056d705211fb5554a6975eeca34b59f0bc8
Showing with 23 additions and 4 deletions.
  1. +1 −1 src/Info.h
  2. +2 −0 src/MCPMatrixSource.cpp
  3. +7 −0 src/MatrixSource.cpp
  4. +12 −0 src/MatrixSource.h
  5. +0 −3 src/PolyType.cpp
  6. +1 −0 src/TeensyMatrixSource.cpp
View
@@ -1 +1 @@
#define VERSION_STR "v0.8.0: Shiny"
#define VERSION_STR "v0.8.1: Bounce"
View
@@ -52,6 +52,8 @@ void MCPMatrixSource::start() {
void MCPMatrixSource::update() {
// TODO maybe reset once in N scans, like every second
// start();
startingScan();
for(int r=0; r<MAT_ROWS; r++) {
store8(GPIOB, 0xFF & ~(1<<r));
byte in = read8(GPIOA);
View
@@ -1,11 +1,18 @@
#include "MatrixSource.h"
#include <Arduino.h>
MatrixSource::MatrixSource(int offset) : out(0), colOffset(offset) {
for(int i=0; i<MAT_ROWS; i++){
for(int j=0; j<MAT_COLS; j++) {
matrix[i][j] = 0;
lastEvent[i][j] = 0;
}
}
scanStart = 0;
}
void MatrixSource::startingScan() {
scanStart = micros();
}
void MatrixSource::sendEvent(int r, int c, int val) {
View
@@ -3,10 +3,14 @@
#include "PacketTypes.h"
#include "Pipe.h"
#include <Arduino.h>
#define MAT_ROWS 6
#define MAT_COLS 7
// The debounce delay before re-polling a key in microseconds
#define DEBOUNCE_TIME 5000
class MatrixSource
{
public:
@@ -17,11 +21,19 @@ class MatrixSource
virtual void update() = 0;
protected:
int matrix[MAT_ROWS][MAT_COLS];
unsigned long lastEvent[MAT_ROWS][MAT_COLS];
unsigned long scanStart;
void sendEvent(int r, int c, int val);
void startingScan();
inline void setState(int r, int c, int val) {
if(matrix[r][c] != val) {
if(scanStart < lastEvent[r][c] + DEBOUNCE_TIME) {
// Serial.println("debounce");
return;
}
matrix[r][c] = val;
lastEvent[r][c] = scanStart;
sendEvent(r, c, val);
}
}
View
@@ -91,7 +91,4 @@ void loop() {
// }
sleepCounter.tick();
// TODO implement debounce so this is unnecessary
delay(2);
}
@@ -12,6 +12,7 @@ void TeensyMatrixSource::start() {
}
void TeensyMatrixSource::update() {
startingScan();
pinMode(10,INPUT_PULLUP);
for(int r=0; r<MAT_ROWS; r++) {
for(int i=0;i<MAT_ROWS;i++) {

0 comments on commit 372c205

Please sign in to comment.