Run continuously, but still accept http in

I have the following code running well, and it will take an array of frames submitted via a form and write them out over SPI to a 64 pixel WS2801 rgb led array. I can’t seem to figure out how to make the imp run over this output forever until it receives another update via http in. My code is below.

`function Color(out, r, g, b)
out.writen(r, ‘b’);
out.writen(g, ‘b’);
out.writen(b, ‘b’);
return out;

function hexToInteger(hex)
local result = 0;
local shift = hex.len() * 4;

// For each digit..
for(local d=0; d<hex.len(); d++)
    local digit;

    // Convert from ASCII Hex to integer
    if(hex[d] >= 0x61)
        digit = hex[d] - 0x57;
    else if(hex[d] >= 0x41)
         digit = hex[d] - 0x37;
         digit = hex[d] - 0x30;

    // Accumulate digit
    shift -= 4;
    result += digit << shift;

return result;


local cache = null;
local count = 0;

class LedInput extends InputPort
numPixels = 64;
writeWait = 0.5;

    // Call the base constructor first
    base.constructor("array", "array");
     //server.log("spi config start");
    hardware.spi257.configure(SIMPLEX_TX, 15000); // Datasheet says max 25MHz
    //server.log("spi config end");

function set(frames)
    local r = 0;
    local g = 0;
    local b = 0;
    cache = [];
    foreach(frame, value in frames)
        //size of array * 3 bytes for the colors
        local out = blob(3*numPixels);   
        foreach (key, val in value)
            //convert colors to integers
            r = hexToInteger(val.slice(1,3));
            g = hexToInteger(val.slice(3,5));
            b = hexToInteger(val.slice(5,7));
            //server.log(format("%i, %i, %i", r, g, b));
            //write the colors
            out = Color(out, b, r, g);
        //save the output to cache to play back later
        //write to the led strip
        //output the frame
        server.log(format("frame %i", count));
        //wait before writing next frame
    imp.wakeup(1, run_cache());

function run_cache()
    if(cache != null)
        //Start SPI
        //server.log("wake up : run_cache");
        //go through each cache item and write it out to spi
        foreach(frame,out in cache)
            //server.log(typeof out);
            //server.log(format("cache frame %i, %i", frame, count));
            imp.wakeup(3, run_cache());


// Register with the server
imp.configure(“Web Frames Array”, [ LedInput() ], [] );

// End of code.`

From what I’ve read in the documentation, imp.wakeup seemed to be the correct function to run. Am I wrong?

Basically, I would like the function run_cache to run forever, until a http in is made, and then go back to run_cache.

I’m also getting this strange error when I do get to function run_cache. It appears to write out the first frame(I think), but then dies with the following error:

1/11/2013 5:05:52 PM: frame 15 1/11/2013 5:05:56 PM: ERROR: the index 'hardware' does not exist 1/11/2013 5:05:56 PM: ERROR: at unknown:0 1/11/2013 5:05:56 PM: ERROR: from run_cache:105 1/11/2013 5:05:56 PM: ERROR: from run_cache:109 1/11/2013 5:05:56 PM: ERROR: from run_cache:109 1/11/2013 5:05:56 PM: ERROR: from run_cache:109 1/11/2013 5:05:56 PM: ERROR: from run_cache:109 1/11/2013 5:05:56 PM: ERROR: from run_cache:109 1/11/2013 5:05:56 PM: ERROR: from run_cache:109 1/11/2013 5:05:56 PM: ERROR: from set:92

Thank you in advance for your time :slight_smile:

You don’t actually call run_cache() to initially start the ball rolling. Have a look at the code in step 10 in the instructable Simple wireless temperature sensor updating web site with electric imp and Thermistor. You will see in it’s “main loop” (function capture()) it starts with “imp.wakeup(0.1, capture);” and then outside of any function, after the register command, it calls capture() to kick start the imp into the loop.

Hope this helps. Same thing had me confused at the start :slight_smile:

That was exactly what i needed, thank you so much!