Skip to content

Instantly share code, notes, and snippets.

@davidonet
Last active February 15, 2018 05:22
Show Gist options
  • Save davidonet/8278459 to your computer and use it in GitHub Desktop.
Save davidonet/8278459 to your computer and use it in GitHub Desktop.
Using ElectricImp to pilot LPD8806 ledstrip from a webpage
function requestHandler(request, response) {
try {
if ("color" in request.query) {
local ledState = request.query.color.tointeger();
local sleep=10;
if ("sleep" in request.query){
sleep = request.query.sleep.tointeger();
}
params<-{"color":ledState,"sleepTime":sleep};
device.send("color",params );
}
if("white"in request.query){
device.send("white", request.query.white.tointeger() );
}
response.header("Access-Control-Allow-Origin", "*");
response.header("Access-Control-Allow-Methods", "GET");
response.send(200, "OK");
} catch (ex) {
response.send(500, "Internal Server Error: " + ex);
}
}
// register the HTTP handler
http.onrequest(requestHandler);
/*
EImp 1m LedStrip
Pin5 -> CI
Pin7 -> DI
*/
hardware.spi257.configure(SIMPLEX_TX | MSB_FIRST | CLOCK_IDLE_LOW, 4000);
local data=blob(32*3+1);
local curColor = 127;
local curWhite = 0;
imp.sleep(0.3);
server.log("init");
function clear() {
full(127,127,127);
full(0,0,0);
}
function full(r,g,b) {
for(local pixel=0;pixel<32;pixel++){
data[(pixel*3)]= 0x80 | g;
data[(pixel*3)+1]= 0x80 | r;
data[(pixel*3)+2]= 0x80 | b;
}
data[32*3]=0x00;
hardware.spi257.write(data);
}
function update() {
hardware.spi257.write(data);
}
function setPixel(pixel,r,g,b) {
data[(pixel*3)]= 0x80 | g;
data[(pixel*3)+1]= 0x80 | r;
data[(pixel*3)+2]= 0x80 | b;
}
function wheel(pixel,WheelPos){
local r, g, b;
switch(WheelPos / 128)
{
case 0:
r = 127 - WheelPos % 128; //Red down
g = WheelPos % 128; // Green up
b = 0; //blue off
break;
case 1:
g = 127 - WheelPos % 128; //green down
b = WheelPos % 128; //blue up
r = 0; //red off
break;
case 2:
b = 127 - WheelPos % 128; //blue down
r = WheelPos % 128; //red up
g = 0; //green off
break;
}
setPixel(pixel,r,g,b);
}
function fullWheel(wheelPos) {
for(local pixel=0;pixel<32;pixel++){
wheel(pixel,wheelPos);
}
update();
}
clear();
function slide(params){
local newColor = params["color"];
if(curColor!=newColor){
local sleepTime = params["sleepTime"];
server.log("slide to : "+newColor+" with "+sleepTime+" ms delay");
local incr=1;
if(newColor<curColor)
incr=-1;
for(;curColor!=newColor;curColor+=incr){
fullWheel(curColor);
imp.sleep(sleepTime/1000.0);
}
server.log("done sliding");
}
}
function white(value){
if(curWhite!=value ){
local incr=1;
if(value<curWhite)
incr=-1;
for(;curWhite!=value;curWhite+=incr){
full(curWhite,curWhite,curWhite);
imp.sleep(0.02);
}
}
}
agent.on("color", slide);
agent.on("white", white);
<!DOCTYPE html>
<html>
<head>
<title>ElectricImp Test</title>
<script src='http://code.jquery.com/jquery-2.0.3.min.js'></script>
</head>
<style>
</style>
<body>
<div>
<label>Type</label>
<input type="radio" name="usetype" value="white" checked>
Monochrome</input>
<input type="radio" name="usetype" value="color">
Color</input>
</div>
<label>Value</label>
<input type="range" id="sliderBar" min="0" max="127" step="1" value="0">
<script>
$(function() {
var currentValue = $('#sliderBar').val();
setInterval(function() {
if (currentValue != $('#sliderBar').val()) {
if ($('input:radio[name=usetype]')[0].checked) {
$.get('https://agent.electricimp.com/<YOUROWNAGENTID>?white=' + $('#sliderBar').val());
} else {
$.get('https://agent.electricimp.com/<YOUROWNAGENTID>?color=' + 2 * $('#sliderBar').val());
}
}
}, 500);
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment