From 27930edf87308b1e5ad81a59a0cae4207ed0f96c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Adam=20Garnier?= Date: Fri, 25 May 2018 07:19:54 +0200 Subject: [PATCH] Create backAndForth.js Hi there ! Thanks a lot for your tool: it has been quite helpful ! I was about to write myself a quick canvas helper - actually started it, then stumbled upon your work: needless to say, I was glad ;p The provided example has a helper fcn which "overwrites only were it already draw" from the beginning of the clamped array. The goal is to be able to skip turning all the leds off and then some of them on, instead directly turning off only those that were and that are no longer on. Don't know yet if this is faster than resetting all the clamped array values to 0 & then only the ones needed to red, should be IF the current algo is not that bad since we'll have to loop over less stuff & hence spend less time on looping ? -> you tell me ;p So, here come my own 2 cents on the subject ;) Can't wait to test those on real hardware ;) Keep up the good work ++ --- examples/backAndForth.js | 60 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 examples/backAndForth.js diff --git a/examples/backAndForth.js b/examples/backAndForth.js new file mode 100644 index 0000000..bc0b33c --- /dev/null +++ b/examples/backAndForth.js @@ -0,0 +1,60 @@ +// ==== rgb goTo ==== +var arr = new Uint8ClampedArray(100*3); // ledsNum * RGB +var currIdx2 = 0; +var actualIdx = 0; // not currently used + +var goToRgb = function(idx){ + console.log('currIdx before move: ' + currIdx2); + var diff = idx - currIdx2; + if(diff > 0){ + console.log('increase by ' + diff); + for(var i=0; i < idx*3; i+=3){ arr[i] = 255; } + currIdx2 += diff; + actualIdx = currIdx2*3; + } + else if (diff < 0){ + console.log('decrease by ' + diff); + var lastCurIdx = currIdx2; + currIdx2 += diff; + ////for(var i=currIdx2; i < lastCurIdx*3; i+=3){ arr[i] = 0; } + //for(var i=lastCurIdx*3; i >currIdx2; i-=3){ arr[i] = 0; } // doesn't work 4 all + //for(var i=currIdx2-1; i < idx*3; i+=3){ arr[i] = 3; } + for(var i=currIdx2*3; i < lastCurIdx*3; i++){ + console.log('decreasing ..'); + arr[i] = 0; + } + } + else if(diff == 0) { console.log('no move'); } + console.log('currIdx is now ' + currIdx2); + console.log('arr is now ' + arr.toString() ); +} + +// test out +var max = 100; +var curr = 0; +/* increasing to full only +setInterval(function(){ + if( curr < max) curr++; + else curr = 0; + goToRgb(curr); +require("neopixel").write('F1', arr); +}, 50); +*/ +/* decreasing to empty only +setInterval(function(){ + if( curr > 0) curr--; + else curr = max; + goToRgb(curr); +require("neopixel").write('F1', arr); +}, 50); +*/ +// back & forth, starting with forth +var dir = 0; // 0 == decreasing, 1 == increasing +setInterval(function(){ + if( dir == 1 && curr < max) curr++; + else if( dir == 0 && curr > 0) curr--; + else dir = !dir; + + goToRgb(curr); + require("neopixel").write('F1', arr); +}, 50);