Laminazione a LED
demo-kit/ è quello che simula il dispositivo per la demo nella pagina di inizio. Il dispositivo stesso non ne ha bisogno.
demo-kit/ è quello che simula il dispositivo per la demo nella pagina di inizio. Il dispositivo stesso non ne ha bisogno.
// The pretend LED strip for the demo of lights (../../demo-kit/README.md): the same answers the sketch gives
// (../esp32/lights/lights.ino), kept in ctx.state. While nobody touches the page (no POST for 20 s) the
// pretend device walks through a few scenes by itself, so the demo is not a still picture; the page sees
// each change in its regular GET /state, exactly as it would see a change made from a second phone.
(function () {
var EFFECTS = [
{ id: 'solid', name: 'Solid' },
{ id: 'rainbow', name: 'Rainbow' },
{ id: 'breathe', name: 'Breathe' },
{ id: 'chase', name: 'Chase' },
{ id: 'sparkle', name: 'Sparkle' },
{ id: 'fire', name: 'Fire' },
{ id: 'aurora', name: 'Aurora' },
{ id: 'sunrise', name: 'Sunrise' },
];
var SCENES = [
{ effect: 'aurora', color: '#7c5cff' },
{ effect: 'chase', color: '#26c6ff' },
{ effect: 'rainbow', color: '#26c6ff' },
{ effect: 'breathe', color: '#ff3d8b' },
{ effect: 'fire', color: '#ff7a1a' },
{ effect: 'sparkle', color: '#2dffb3' },
{ effect: 'sunrise', color: '#ffb46b' },
{ effect: 'solid', color: '#ffb46b' },
];
var IDLE_SECONDS = 20; // after a POST the visitor is in charge for this long
var SCENE_SECONDS = 9;
var ctx = czDemo.context;
ctx.state.lights = { on: true, brightness: 204, color: SCENES[0].color, effect: SCENES[0].effect, speed: 55, leds: 60 };
ctx.state.scene = 0;
ctx.state.lastPost = -IDLE_SECONDS;
ctx.state.lastScene = 0;
function isEffect(id) {
return EFFECTS.some(function (effect) {
return effect.id === id;
});
}
function clampInt(value, min, max) {
return Math.max(min, Math.min(max, Math.round(value)));
}
function current() {
var state = ctx.state.lights;
var now = ctx.seconds();
if (now - ctx.state.lastPost >= IDLE_SECONDS && now - ctx.state.lastScene >= SCENE_SECONDS) {
ctx.state.scene = (ctx.state.scene + 1) % SCENES.length;
ctx.state.lastScene = now;
state.effect = SCENES[ctx.state.scene].effect;
state.color = SCENES[ctx.state.scene].color;
state.on = true;
}
return { on: state.on, brightness: state.brightness, color: state.color, effect: state.effect, speed: state.speed, leds: state.leds };
}
czDemo.define({
device: { name: 'desk-lights', title: 'Desk lights' },
routes: {
'GET /state': function () {
return current();
},
// Any subset of the fields; what is missing or not valid stays as it was, as in the sketch.
'POST /state': function (request) {
var wanted = request.json();
if (!wanted || typeof wanted !== 'object') return ctx.respond(400, { error: 'expected a JSON object' });
var state = ctx.state.lights;
if (typeof wanted.on === 'boolean') state.on = wanted.on;
if (typeof wanted.brightness === 'number' && isFinite(wanted.brightness)) state.brightness = clampInt(wanted.brightness, 0, 255);
if (typeof wanted.color === 'string' && /^#[0-9a-f]{6}$/i.test(wanted.color)) state.color = wanted.color.toLowerCase();
if (typeof wanted.effect === 'string' && isEffect(wanted.effect)) state.effect = wanted.effect;
if (typeof wanted.speed === 'number' && isFinite(wanted.speed)) state.speed = clampInt(wanted.speed, 0, 100);
ctx.state.lastPost = ctx.seconds();
ctx.state.lastScene = ctx.seconds();
return current();
},
'GET /effects': function () {
return EFFECTS;
},
'GET /.cz/ui.json': function () {
return { name: 'Lights', entry: '/.cz/ui.html', version: '1', theme: { background: '#07060d', foreground: '#d6ccff' } };
},
},
});
})();