Imp.onidle: pin.configure(inputMode, callback)

Hi,
I am trying to understand the wakeup priorities when using imp.onidle(). I understand that an interrupt wakes up the imp when it is sleeping. What happens to the imp when it is sleeping and I have a pin configured as “pin.configure(DIGITAL_IN, callback)”. Does this pin also act as an interrupt or is it disabled since the imp is sleeping.

Can you clarify what you mean by “sleeping”?

The sleepfor() etc calls put the imp into a deep sleep; from that mode, the only wakes possible are on the RTC or on a rising edge of pin1/pinW. A wake on an edge does not call any event handlers, but will be reported as a WAKEREASON code. No pin change callbacks have any effect, as the CPU is totally powered off.

If the imp is running but idle (not running any squirrel), then any pin change callbacks that have been sent will fire.

Hi,
I might me confusing some terminologies here. I am using the Imp2 EVB with an LIS3DH accel int1 connected to pin 1 of the imp.

I have the following lines in my code

imp.onidle(function() {
server.sleepfor(900);
});

and the following function that gets called when there is an interrupt on Pin 1

if (hardware.wakereason() == WAKEREASON_PIN) {
server.log(“Int Awake”);
pulse();
int_flag++;
}

My question is that if I have pin 2 configured as pin.configure(DIGITAL_IN, foo()), then will the function foo() be called if there is an input on pin 2.

I am assuming from your post that if the imp is in deep sleep then foo() will not be called when input changes on pin 2.

Correct, the pin 2 trigger will only be called if there is a change when the imp is awake.

One small note: your callback would be pin.configure(DIGITAL_IN, foo) (note! not foo() otherwise the callback will be the result of calling foo(), which is fine if it returned a function but that’s not usually the way you’d do that)

Got it. Just a follow-up. Once the imp “wakes” up from sleep mode, does rerun all the imp code from line 1, or does it run predefined segments of the code.

In other words, for the following pseudo code, will the initialization of the constant variables, i2c, wake 2 etc be rerun when the imp wakes up from sleep or does it only run he code segments in the two if loops.

// code block with initialization

const PULSE = “\x7F”;
const ZERO_OFFSET = 70569;
const GAIN = 2079;

i2c <- hardware.i2c89;
wake <- hardware.pin1;

i2c.configure(CLOCK_SPEED_400_KHZ);
wake.configure(DIGITAL_IN_WAKEUP);

if (hardware.wakereason() == WAKEREASON_PIN) {
//code block if wakeup due to interrupt
}

if (hardware.wakereason() == WAKEREASON_TIMER) {
//code block if wakeup when timer expires
}

imp.onidle(function() {
server.sleepfor(900);
});

It’s the former

When it wakes, the imp runs its Squirrel code entirely afresh, so variables will return to their initial, code-declared values. If there’s any data you need to preserve across such warm restarts, you can use the nv table. This preserves data across warm starts, but not power cycles - for that you need to send your data to the agent and store it with server.save().

Thanks

Hi,
I have another question regarding the behavior of imp across warm restarts, or whether I can use the sleepfor() in my code at all.

At a high level, my code waits for a button to be pressed. Once it detects the button press, then it measures and uploads data to the server.

I also have a couple of LED displays that indicate the battery level and the state of the WiFi connection. This information can be polled and updated every 5-15 minutes, or when the button is pressed.

I was wondering if there is a way I can use any of the Imp power saving features in my code.

Thanks