2017-07-13 09:31:50 +00:00
|
|
|
load('api_rpc.js');
|
|
|
|
load("api_neopixel.js");
|
|
|
|
load("api_file.js");
|
|
|
|
|
|
|
|
|
|
|
|
let colors = {
|
|
|
|
black: {r:0, g:0, b:0},
|
|
|
|
red: {r:255, g:0, b:0},
|
|
|
|
green: {r:0, g:255, b:0},
|
|
|
|
blue: {r:0, g:0, b:255},
|
|
|
|
yellow: {r:255, g:255, b:0},
|
|
|
|
orange: {r:255, g:127, b:0},
|
|
|
|
purple: {r:255, g:0, b:255},
|
|
|
|
white: {r:255, g:255, b:255},
|
|
|
|
dimmedWhite: {r:50, g:50, b:50}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
let LightAnimation = {
|
2017-11-15 19:54:45 +00:00
|
|
|
numberOfBulbs: 11,
|
|
|
|
ledPin: 2,
|
2017-07-13 09:31:50 +00:00
|
|
|
running: false,
|
|
|
|
mode: "demo",
|
2017-11-15 19:54:45 +00:00
|
|
|
generalBrightnessDivider: 3,
|
2017-07-13 09:31:50 +00:00
|
|
|
colorSequence: [
|
|
|
|
colors.dimmedWhite,
|
|
|
|
colors.red,
|
|
|
|
colors.green,
|
|
|
|
colors.blue,
|
|
|
|
colors.yellow,
|
|
|
|
colors.orange,
|
|
|
|
colors.purple,
|
|
|
|
colors.red,
|
|
|
|
colors.red,
|
|
|
|
colors.green,
|
|
|
|
colors.green,
|
|
|
|
colors.white,
|
|
|
|
colors.white
|
|
|
|
]
|
|
|
|
};
|
|
|
|
|
|
|
|
// Add RPC handlers
|
|
|
|
|
|
|
|
RPC.addHandler('HouseLightning.Start', function(args) {
|
|
|
|
LightAnimation.running = true;
|
2017-11-15 19:54:45 +00:00
|
|
|
LightAnimation.mode = "run";
|
2017-07-13 09:31:50 +00:00
|
|
|
return {result: 'ok'};
|
|
|
|
}, null);
|
|
|
|
|
|
|
|
RPC.addHandler('HouseLightning.Stop', function(args) {
|
2017-11-15 19:54:45 +00:00
|
|
|
LightAnimation.running = true; // first we want to turn off LEDs, thus needs stop-mode
|
|
|
|
LightAnimation.mode = "stop";
|
2017-07-13 09:31:50 +00:00
|
|
|
return {result: 'ok'};
|
|
|
|
}, null);
|
|
|
|
|
|
|
|
RPC.addHandler('HouseLightning.DemoMode', function(args) {
|
|
|
|
LightAnimation.mode = "demo";
|
|
|
|
LightAnimation.running = true;
|
|
|
|
return {result: 'ok'};
|
|
|
|
}, null);
|
|
|
|
|
|
|
|
RPC.addHandler('HouseLightning.CycleMode', function(args) {
|
|
|
|
LightAnimation.mode = "cycle";
|
|
|
|
LightAnimation.running = true;
|
|
|
|
return {result: 'ok'};
|
|
|
|
}, null);
|
|
|
|
|
|
|
|
RPC.addHandler('HouseLightning.GetAnimationConfig', function(args) {
|
|
|
|
return LightAnimation.animationConfig;
|
|
|
|
}, null);
|
|
|
|
|
|
|
|
RPC.addHandler('HouseLightning.SetAnimationConfig', function(args) {
|
|
|
|
if (args !== undefined && args.config !== undefined) {
|
|
|
|
LightAnimation.animationConfig = JSON.parse(args.config);
|
|
|
|
if (LightAnimation.animationConfig.comment === undefined) {
|
|
|
|
return {error: 'config is incomplete or invalid / comment not found'};
|
|
|
|
}
|
|
|
|
if (LightAnimation.animationConfig.lights === undefined) {
|
|
|
|
return {error: 'config is incomplete or invalid / lights definition not found'};
|
|
|
|
}
|
|
|
|
if (LightAnimation.animationConfig.lights[0] === undefined) {
|
|
|
|
return {error: 'config is incomplete or invalid / lights not an array?'};
|
|
|
|
}
|
|
|
|
let i;
|
|
|
|
for (i=0; i<LightAnimation.animationConfig.lights.length; ++i) {
|
|
|
|
if (LightAnimation.animationConfig.lights[i].level === undefined) {
|
|
|
|
return {error: 'config is incomplete or invalid / level missing in lights definition'};
|
|
|
|
}
|
|
|
|
if (LightAnimation.animationConfig.lights[i].room === undefined) {
|
|
|
|
return {error: 'config is incomplete or invalid / room missing in lights definition'};
|
|
|
|
}
|
|
|
|
if (LightAnimation.animationConfig.lights[i].on === undefined) {
|
|
|
|
return {error: 'config is incomplete or invalid / on missing in lights definition'};
|
|
|
|
}
|
|
|
|
if (LightAnimation.animationConfig.lights[i].on[0] === undefined) {
|
|
|
|
return {error: 'config is incomplete or invalid / on not an array in lights definition?'};
|
|
|
|
}
|
|
|
|
if (LightAnimation.animationConfig.lights[i].on.length !== 3) {
|
|
|
|
return {error: 'config is incomplete or invalid / on must have 3 elements for R/G/B in lights definition?'};
|
|
|
|
}
|
|
|
|
if (LightAnimation.animationConfig.lights[i].id === undefined) {
|
|
|
|
return {error: 'config is incomplete or invalid / id missing in lights definition'};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return {result: 'ok'};
|
|
|
|
} else {
|
|
|
|
return {error: 'parameter config is required'};
|
|
|
|
}
|
|
|
|
}, null);
|
|
|
|
|
|
|
|
function runNeopixel(strip, shiftBy) {
|
|
|
|
let i=0, colorIndex=0, color;
|
|
|
|
|
|
|
|
for (i=0; i<LightAnimation.numberOfBulbs; ++i) {
|
|
|
|
colorIndex = shiftBy % LightAnimation.colorSequence.length;
|
|
|
|
color = LightAnimation.colorSequence[colorIndex];
|
2017-11-15 19:54:45 +00:00
|
|
|
strip.setPixel(i, color.r/LightAnimation.generalBrightnessDivider, color.g/LightAnimation.generalBrightnessDivider, color.b/LightAnimation.generalBrightnessDivider);
|
2017-07-13 09:31:50 +00:00
|
|
|
// print("runNeo shift =", shiftBy, ", colorIndex =", colorIndex, ", r =", color.r, ", g =", color.g, ", b =", color.b);
|
|
|
|
shiftBy++;
|
|
|
|
}
|
|
|
|
strip.show();
|
|
|
|
}
|
|
|
|
|
|
|
|
// initialize module
|
|
|
|
let configString = "";
|
|
|
|
configString = File.read("lights.cfg");
|
|
|
|
if (configString === "" || configString === null) configString = "***default***";
|
|
|
|
// print("configString = ", configString);
|
|
|
|
LightAnimation.animationConfig = JSON.parse(configString);
|
|
|
|
|
|
|
|
|
|
|
|
// initialize neoPixel
|
|
|
|
let colorOrder = NeoPixel.GRB;
|
|
|
|
let strip = NeoPixel.create(LightAnimation.ledPin, LightAnimation.numberOfBulbs, colorOrder);
|
|
|
|
strip.clear();
|
|
|
|
strip.show();
|
|
|
|
// initialize test pattern
|
|
|
|
let i = 0;
|
|
|
|
for (i=0; i<LightAnimation.numberOfBulbs; ++i) {
|
|
|
|
if (i % 2) {
|
|
|
|
strip.setPixel(i, 80, 20, 20);
|
|
|
|
} else {
|
|
|
|
strip.setPixel(i, 20, 80, 20);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
strip.show();
|
|
|
|
|
|
|
|
let shiftAnimation = 1;
|
|
|
|
|
|
|
|
Timer.set(1990 /* msec */, true /* repeat */, function() {
|
|
|
|
if (LightAnimation.running) {
|
|
|
|
if (LightAnimation.mode === "demo") {
|
|
|
|
runNeopixel(strip, shiftAnimation);
|
|
|
|
shiftAnimation++;
|
|
|
|
print("*** demo-mode, next shift=", shiftAnimation);
|
2017-11-15 19:54:45 +00:00
|
|
|
} else if (LightAnimation.mode === "stop") {
|
|
|
|
strip.clear();
|
|
|
|
strip.show();
|
|
|
|
LightAnimation.running = false;
|
2017-07-13 09:31:50 +00:00
|
|
|
} else print("*** no-ack, mode =", LightAnimation.mode);
|
|
|
|
} else print("*** stopped");
|
|
|
|
}, null);
|