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;
else
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;
constructor()
{
// Call the base constructor first
base.constructor("array", "array");
//server.log("spi config start");
hardware.spi257.configure(SIMPLEX_TX, 15000); // Datasheet says max 25MHz
hardware.pin5.configure(DIGITAL_OUT);
hardware.pin5.write(0);
imp.sleep(0.01);
hardware.pin5.write(1);
hardware.configure(SPI_257);
//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
cache.append(out);
//write to the led strip
hardware.spi257.write(out);
//output the frame
server.log(format("frame %i", count));
count++;
//wait before writing next frame
imp.sleep(writeWait);
}
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);
hardware.spi257.write(out);
//server.log(format("cache frame %i, %i", frame, count));
count++;
imp.sleep(writeWait);
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