Improved timer handling

Instead of calculating the elapsed time every time we check the timer,
calculate the projected end-time when we start the timer, and just
compare millis() against that.

Also removes the `.configure()` method, in favour of making the
`timeOut` property public.

Signed-off-by: Gergely Nagy <algernon@madhouse-project.org>
pull/389/head
Gergely Nagy 8 years ago
parent ce45b212d7
commit 7a5a1b2b33

@ -39,21 +39,23 @@ is highly recommended.
The plugin provides two methods on the `HeatmapEffect` object:
### `.configure(updateFrequency)`
> 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*.
### `.activate()`
> When called, immediately activates the Heatmap effect. Mostly useful in the
> `setup()` method of the Sketch, or in macros that are meant to switch to the
> heatmap effect, no matter where we are in the list.
### `.timeOut`
> The number of milliseconds to wait between updating the heatmap. 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.
>
> Not strictly a method, it is a variable one can assign a new value to.
>
> Defaults to *500*.
## Further reading
Starting from the [example][plugin:example] is the recommended way of getting

@ -23,8 +23,8 @@ namespace KaleidoscopePlugins {
uint8_t Heatmap::heatmap[ROWS][COLS];
uint16_t Heatmap::totalKeys;
uint8_t Heatmap::highestCount;
uint16_t Heatmap::updateFrequency = 500;
uint32_t Heatmap::startTime;
uint16_t Heatmap::updateDelay = 500;
uint32_t Heatmap::endTime;
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}};
@ -74,11 +74,6 @@ namespace KaleidoscopePlugins {
loop_hook_use (this->loopHook);
}
void
Heatmap::configure (uint16_t updateFreq) {
updateFrequency = updateFreq;
}
Key
Heatmap::eventHook (Key mappedKey, byte row, byte col, uint8_t keyState) {
// if it is a synthetic key, skip it.
@ -105,10 +100,10 @@ namespace KaleidoscopePlugins {
void
Heatmap::update (void) {
if (startTime && (millis () - startTime) < updateFrequency)
if (endTime && (millis () > endTime))
return;
startTime = millis ();
endTime = millis () + updateDelay;
for (uint8_t r = 0; r < ROWS; r++) {
for (uint8_t c = 0; c < COLS; c++) {

@ -28,15 +28,14 @@ namespace KaleidoscopePlugins {
virtual void begin (void) final;
static void configure (uint16_t updateFrequency);
static uint16_t updateDelay;
virtual void update (void) final;
private:
static uint8_t heatmap[ROWS][COLS];
static uint16_t totalKeys;
static uint8_t highestCount;
static uint16_t updateFrequency;
static uint32_t startTime;
static uint32_t endTime;
static const float heatColors[][3];

Loading…
Cancel
Save