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)`
> 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*.

@ -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);

@ -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];

Loading…
Cancel
Save