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 <algernon@madhouse-project.org>
pull/389/head
Gergely Nagy 8 years ago
parent e7e4ba0690
commit f2787a5692

@ -40,15 +40,10 @@ The plugin provides two methods on the `HeatmapEffect` object:
### `.configure(updateFrequency)` ### `.configure(updateFrequency)`
> Sets up the update frequency of the heatmap. The smaller this value is, the > Sets up the update frequency of the heatmap, to update every `updateFrequency`
> more often the heatmap gets updated, but that comes with a cost: updating the > milliseconds. Updating the heatmap incurs a significant performance penalty,
> heatmap takes a fairly large amount of computation, and is not fast. Doing it > and should not be done too often. Doing it too rarely, on the other hand, make
> often will considerably slow down the keyboard, and that is rarely a desirable > it much less useful. One has to strike a reasonable balance.
> 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.
> >
> Defaults to *500*. > Defaults to *500*.

@ -22,8 +22,8 @@ namespace Akela {
uint8_t Heatmap::heatmap[ROWS][COLS]; uint8_t Heatmap::heatmap[ROWS][COLS];
uint16_t Heatmap::totalKeys; uint16_t Heatmap::totalKeys;
uint8_t Heatmap::highestCount; uint8_t Heatmap::highestCount;
uint16_t Heatmap::updateFrequency = 50; uint16_t Heatmap::updateFrequency = 500;
uint16_t Heatmap::loopCount; 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}}; 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 void
Heatmap::update (void) { Heatmap::update (void) {
loopCount++; if (startTime && (millis () - startTime) < updateFrequency)
if (loopCount < updateFrequency)
return; return;
loopCount = 0; startTime = millis ();
for (uint8_t r = 0; r < ROWS; r++) { for (uint8_t r = 0; r < ROWS; r++) {
for (uint8_t c = 0; c < COLS; c++) { for (uint8_t c = 0; c < COLS; c++) {
uint8_t cap = max(totalKeys, 1); uint8_t cap = max(totalKeys, 1);

@ -36,7 +36,7 @@ namespace Akela {
static uint16_t totalKeys; static uint16_t totalKeys;
static uint8_t highestCount; static uint8_t highestCount;
static uint16_t updateFrequency; static uint16_t updateFrequency;
static uint16_t loopCount; static uint32_t startTime;
static const float heatColors[][3]; static const float heatColors[][3];

Loading…
Cancel
Save