Skip to main content

imp.cancelwakeup(timerIdentifier)

Cancels a timer previously established by imp.wakeup()

Availability

Device + Agent

Parameters

Name Type Description
timerIdentifier A timer object reference (device/agent) or name (agent only) An identifier for a timer established by imp.wakeup()

Returns

Nothing

Description

To use this method, you must already have a reference to the timer you wish to cancel. Creating a new timer with imp.wakeup() returns a reference to the new timer. Store this reference in a variable and you can later pass the reference to imp.cancelwakeup() if you need to.

If the timer has not yet fired, once cancelled it never will. Cancelling a timer that has already fired has no effect. Cancelling a timer, whether it has fired or not, does not null the reference.

The method can be applied to timers established by either a device or an agent, but the method doesn’t allow an agent to cancel a timer established by a device, or vice versa.

Named Agent Timers

When establishing timers within agent code (but not device code), you may specify an identifier for the timer created. You may use this name as an alternative identifier for the timer you wish to cancel when calling imp.cancelwakeup(). An exception will be thrown if the identifier you use identifies an inactive (cancelled or not yet established) timer.

Example Code

The following example demonstrates the specific usage of imp.cancelwakeup() in agent code. First we establish a timer using a string identifier ("wakeup_timer"), which we subsequently pass into imp.cancelwakeup() to cancel it. The second imp.wakeup() call uses the original (and still available) way of identifying a timer: by storing the reference returned by imp.wakeup() — which we then use to cancel that timer!

// Set up a timer...
imp.wakeup(60,
function() { server.log("Timer \"wakeup_timer\" woke up"); },
"wakeup_timer");
// ...but cancel it before it fires...
local timer = imp.wakeup(40, function() {
server.log("Timer \"wakeup_timer\" cancelled");
imp.cancelwakeup("wakeup_timer");
});
// ...or maybe not!
imp.cancelwakeup(timer);

The following example uses imp.cancelwakeup() to suspend a regular timer-based loop in response to the arrival of new data, in this case in index value indicating a specific color in an array of RGB colors.

// VARIABLES
local numberOfColors = 7;
// FUNCTIONS
function rnd(max) {
// Return a pseudorandom integer between 0 and max, inclusive
local f = (1.0 * math.rand() / RAND_MAX) * (max + 1);
return f.tointeger();
}
function loop() {
// Wake in 1 second and re-run loop()
imp.wakeup(1.0, loop);
// Send a color-change message 20% of the time
if (rnd(100) < 20) device.send("update.light", rnd(numberOfColors - 1));
}
// RUNTIME START
loop();
// IMPORTS
#require "WS2812.class.nut:3.0.0"
// VARIABLES
local pixel = 0;
local pixelDelta = 1;
local colors = [[0,0,64], [64,0,0], [0,64,0], [32,32,0], [0,32,32], [32,0,32], [22,22,22]];
local colorIndex = 0;
local currentColor = colors[colorIndex];
local numberOfColors = colors.len();
local numberOfPixels = 8;
local loopTimer = null;
local pixels = null;
// FUNCTIONS
// Loop function to change the LEDs’ colors at each pass
function loop() {
// Wake in 1 second and re-run loop()
loopTimer = imp.wakeup(1.0, loop);
// Clear the pixels, set the current color, and write it out
pixels.fill([0,0,0])
.set(pixel, currentColor)
.draw();
// Move to the next pixel in the sequence
pixel += pixelDelta;
if (pixel == numberOfPixels || pixel < 0) {
// If the current pixel is beyond either end of the strip, so
// 'bounce' back in the opposite direction and change the color
pixelDelta *= -1;
pixel += (2 * pixelDelta);
}
}
function updateColor(dataFromAgent) {
// If we have a color-change message from the agent,
// stop the current loop, set the color and restart the loop
if (loopTimer != null) {
imp.cancelwakeup(loopTimer);
loopTimer = null;
}
currentColor = colors[dataFromAgent];
// Restart the loop afresh
loop();
}
// RUNTIME START
// Alias an SPI bus. The following line assumes you're working with an imp004m,
// so please change the line for other imps
local spi = hardware.spiGJKL;
// Set up the WS2182s. Adjust the value of 'numberOfPixels'
// and the SPI bus you're using according to your imp and setup
pixels = WS2812(spi, numberOfPixels);
// Register incoming message handler
agent.on("update.light", updateColor);
// Begin the loop
loop();