From f2787a569235e22e4b23580bf9bf40dd821e2bf2 Mon Sep 17 00:00:00 2001 From: Gergely Nagy Date: Fri, 10 Feb 2017 09:34:55 +0100 Subject: [PATCH] Use a timer instead of a loop counter Loop counters are an unreliable way to track time, use a timer instead. Fixes #2. Signed-off-by: Gergely Nagy --- README.md | 13 ++++--------- src/Akela/Heatmap.cpp | 11 +++++------ src/Akela/Heatmap.h | 2 +- 3 files changed, 10 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index e6d67183..4ef3066c 100644 --- a/README.md +++ b/README.md @@ -40,15 +40,10 @@ The plugin provides two methods on the `HeatmapEffect` object: ### `.configure(updateFrequency)` -> Sets up the update frequency of the heatmap. The smaller this value is, the -> more often the heatmap gets updated, but that comes with a cost: updating the -> heatmap takes a fairly large amount of computation, and is not fast. Doing it -> often will considerably slow down the keyboard, and that is rarely a desirable -> thing. -> -> Nevertheless, the frequency preferred is a very subjective thing, which is why -> this function exists. Give it a number, and it will wait that many scan cycles -> between updates. +> Sets up the update frequency of the heatmap, to update every `updateFrequency` +> milliseconds. Updating the heatmap incurs a significant performance penalty, +> and should not be done too often. Doing it too rarely, on the other hand, make +> it much less useful. One has to strike a reasonable balance. > > Defaults to *500*. diff --git a/src/Akela/Heatmap.cpp b/src/Akela/Heatmap.cpp index e067af27..92df231b 100644 --- a/src/Akela/Heatmap.cpp +++ b/src/Akela/Heatmap.cpp @@ -22,8 +22,8 @@ namespace Akela { uint8_t Heatmap::heatmap[ROWS][COLS]; uint16_t Heatmap::totalKeys; uint8_t Heatmap::highestCount; - uint16_t Heatmap::updateFrequency = 50; - uint16_t Heatmap::loopCount; + uint16_t Heatmap::updateFrequency = 500; + uint32_t Heatmap::startTime; const float Heatmap::heatColors[][3] = {{0.0, 0.0, 0.0}, {0.1, 1, 0.1}, {1, 1, 0.1}, {1, 0.1, 0.1}}; @@ -104,12 +104,11 @@ namespace Akela { void Heatmap::update (void) { - loopCount++; - - if (loopCount < updateFrequency) + if (startTime && (millis () - startTime) < updateFrequency) return; - loopCount = 0; + startTime = millis (); + for (uint8_t r = 0; r < ROWS; r++) { for (uint8_t c = 0; c < COLS; c++) { uint8_t cap = max(totalKeys, 1); diff --git a/src/Akela/Heatmap.h b/src/Akela/Heatmap.h index 789e1e19..e01294c6 100644 --- a/src/Akela/Heatmap.h +++ b/src/Akela/Heatmap.h @@ -36,7 +36,7 @@ namespace Akela { static uint16_t totalKeys; static uint8_t highestCount; static uint16_t updateFrequency; - static uint16_t loopCount; + static uint32_t startTime; static const float heatColors[][3];