Interrupt Imp while it's running code?

I’m relatively new to Electric Imp and Squirrel. I’m using IMP001 with an April Rev3 (20121016), in case that’s relevant.
I’m using the Imp to control one RGB LED (WS2801, from Adafruit. datasheet).


April is plugged into USB power (and jumper is set accordingly). LED has its own (USB-based) power supply. LED ground is connected to April ground. LED green (SPI clock) is connected to Pin5 LED yellow (SPI MOSI) is connected to Pin7


I'd like to be able to control the LED's pattern with HTTP requests made to the agent. I have a simple program that takes in an array of hex values and cycles the LED from color to color fluidly. The problem is when I send another request to the agent the imp does not get interrupted. I've tried a couple of solutions to this, but the ideal one would be one involving threads or child processes that I could fork and kill as necessary.

I’ve tried using global variables as interrupt/ready flags so that a new command coming down from the agent would interrupt the previous command. I’ve also tried imp.sleep(t) but that didn’t do anything apparent. I’ve also tried storing the LED program with but ran into memory/serialization problems.

Can anyone think of a way to interrupt the Imp on the fly, while it’s running code?

My code is on Github, if you want to take a deeper look.


Try using imp.wakeup instead of imp.sleep? I believe your code just needs to be able to yield long enough to run the incoming requests.

Yep, instead of an infinite loop (which does not allow the tcp thread to run) you should re-schedule your update loop with imp.wakeup, ie:

`function iteration() {
// do stuff

// call ourselves back in 10ms
imp.wakeup(0.01, iteration);

// start iteration

The imp gives priority to the user code, hence you need to yield to let messages flow.

Thanks! that appears to work for my needs. Now I just need to come up with an LED program language that won’t exceed the Imp’s memory :stuck_out_tongue: