283 lines
9.2 KiB
JavaScript
283 lines
9.2 KiB
JavaScript
load('api_config.js');
|
|
load('api_gpio.js');
|
|
load('api_mqtt.js');
|
|
load('api_net.js');
|
|
load('api_sys.js');
|
|
load('api_rpc.js');
|
|
load('api_timer.js');
|
|
load("api_math.js");
|
|
load("api_file.js");
|
|
load("api_neopixel.js");
|
|
|
|
|
|
// Helper C function get_led_gpio_pin() in src/main.c returns built-in LED GPIO
|
|
let onBoardLed = ffi('int get_led_gpio_pin(void)')();
|
|
let button = Cfg.get('pins.button');
|
|
let topic = '/devices/' + Cfg.get('device.id') + '/events';
|
|
|
|
print('LED GPIO:', onBoardLed, 'button GPIO:', button);
|
|
|
|
let getInfo = function() {
|
|
return JSON.stringify({
|
|
total_ram: Sys.total_ram(),
|
|
free_ram: Sys.free_ram()
|
|
});
|
|
};
|
|
|
|
// Blink built-in LED every second
|
|
/*
|
|
GPIO.set_mode(onBoardLed, GPIO.MODE_OUTPUT);
|
|
Timer.set(5000, true, function() {
|
|
let value = GPIO.toggle(onBoardLed);
|
|
print(value ? 'Tick' : 'Tock', 'uptime:', Sys.uptime(), getInfo());
|
|
}, null);
|
|
*/
|
|
|
|
// Publish to MQTT topic on a button press. Button is wired to GPIO pin 0
|
|
/*
|
|
GPIO.set_button_handler(button, GPIO.PULL_UP, GPIO.INT_EDGE_NEG, 200, function() {
|
|
let message = getInfo();
|
|
let ok = MQTT.pub(topic, message, 1);
|
|
print('Published:', ok, topic, '->', message);
|
|
}, null);
|
|
*/
|
|
|
|
// Monitor network connectivity.
|
|
Net.setStatusEventHandler(function(ev, arg) {
|
|
let evs = '???';
|
|
if (ev === Net.STATUS_DISCONNECTED) {
|
|
evs = 'DISCONNECTED';
|
|
} else if (ev === Net.STATUS_CONNECTING) {
|
|
evs = 'CONNECTING';
|
|
} else if (ev === Net.STATUS_CONNECTED) {
|
|
evs = 'CONNECTED';
|
|
} else if (ev === Net.STATUS_GOT_IP) {
|
|
evs = 'GOT_IP';
|
|
}
|
|
print('== Net event:', ev, evs);
|
|
}, null);
|
|
|
|
// Initialize LED controller
|
|
let addColor = ffi('void addColor(char *,int,int,int)');
|
|
let addLedDefinition = ffi('void addLedDefinition(char *, char *, char *, char *)');
|
|
let addAnimationStep = ffi('void addAnimationStep(int, char *, int)');
|
|
let LEDMode_on=1, LEDMode_off=2, LEDMode_blink=3, LEDMode_tv=4, LEDMode_fire=5;
|
|
let LEDStateEngine_init = ffi('void LEDStateEngine_init(int)');
|
|
let LEDStateEngine_start = ffi('void startLEDStateEngine(void)');
|
|
let LEDStateEngine_pause = ffi('void pauseLEDStateEngine(void)');
|
|
let LEDState_getRed = ffi('int LEDState_getLedRed(int)');
|
|
let LEDState_getGreen = ffi('int LEDState_getLedGreen(int)');
|
|
let LEDState_getBlue = ffi('int LEDState_getLedBlue(int)');
|
|
let LEDState_getColorName = ffi('char * LEDState_getLedColorName(int)');
|
|
let LEDState_getNextTick = ffi('int LEDState_getNextTick(int)');
|
|
let LEDState_getCurrentTick = ffi('int LEDState_getCurrentTick(int)');
|
|
let LEDDefinition_getLevel = ffi('char *LEDDefinition_getLevel(int)');
|
|
let LEDDefinition_getRoom = ffi('char *LEDDefinition_getRoom(int)');
|
|
let LEDDefinition_getId = ffi('char *LEDDefinition_getId(int)');
|
|
let LEDDefinition_getOnColorRed = ffi('int LEDDefinition_getOnColorRed(int)');
|
|
let LEDDefinition_getOnColorGreen = ffi('int LEDDefinition_getOnColorGreen(int)');
|
|
let LEDDefinition_getOnColorBlue = ffi('int LEDDefinition_getOnColorBlue(int)');
|
|
let getTicks = ffi('int getTicks(void)');
|
|
let printColor = ffi('void printColor(char *)');
|
|
|
|
let pin = Cfg.get('led.pin');
|
|
let numPixels = Cfg.get('led.count');
|
|
let colorOrder = NeoPixel.GRB;
|
|
let strip = NeoPixel.create(pin, numPixels, colorOrder);
|
|
let colorFile = Cfg.get('led.colorFile');
|
|
let animationFile = Cfg.get('led.animationFile');
|
|
let lampsFile = Cfg.get('led.lampsFile');
|
|
let useDefaults = Cfg.get('led.useDefaults');
|
|
let updateCycle = Cfg.get('led.updateCycle');
|
|
let brightnessAdjustment = Cfg.get('led.brightness');
|
|
let numberOfLeds = 0; // from config files, count led definition entries
|
|
|
|
/* Create test pattern */
|
|
function createLedTestPattern() {
|
|
let i;
|
|
let switchMod;
|
|
for (i=0; i<numPixels; ++i) {
|
|
switchMod = i - 6 * Math.floor(i/6);
|
|
if (switchMod === 0) strip.setPixel(i, 100, 100, 100);
|
|
if (switchMod === 1) strip.setPixel(i, 100, 100, 100);
|
|
if (switchMod === 2) strip.setPixel(i, 100, 100, 0);
|
|
if (switchMod === 3) strip.setPixel(i, 0, 100, 0);
|
|
if (switchMod === 4) strip.setPixel(i, 0, 100, 100);
|
|
if (switchMod === 5) strip.setPixel(i, 0, 0, 100);
|
|
if (switchMod < 0 || switchMod > 5) print("WRONG -- should never reach this in switch statement!");
|
|
}
|
|
strip.show();
|
|
}
|
|
|
|
function allLedOn() {
|
|
let i;
|
|
for (i=0; i<numPixels; ++i) {
|
|
strip.setPixel(i, 100, 100, 100);
|
|
}
|
|
strip.show();
|
|
}
|
|
|
|
function allLedOff() {
|
|
strip.clear();
|
|
strip.show();
|
|
}
|
|
|
|
function adjustBrightness(value) {
|
|
return Math.floor(value * brightnessAdjustment/100);
|
|
}
|
|
|
|
// initialize LEDs
|
|
let i;
|
|
allLedOff();
|
|
|
|
if (useDefaults) {
|
|
print('Using default color definition');
|
|
numberOfLeds = numPixels;
|
|
startLEDStateEngine();
|
|
} else {
|
|
// Load Color definitions
|
|
let json = File.read(colorFile);
|
|
let colors = [];
|
|
|
|
print('colorJson =', json);
|
|
if (json === '') {
|
|
print('ERROR: Color definition file does not exist!');
|
|
} else {
|
|
colors = JSON.parse(json);
|
|
}
|
|
for (i=0; i<colors.length; ++i) {
|
|
print('- addColor', colors[i].name, colors[i].red, colors[i].green, colors[i].blue);
|
|
addColor(colors[i].name, colors[i].red, colors[i].green, colors[i].blue);
|
|
}
|
|
json = null;
|
|
// colors = null; // NO! Do not do this, as the strings are still referenced!
|
|
|
|
// Load LED Definitions
|
|
let json = File.read(lampsFile);
|
|
let ledDef = [];
|
|
print('ledDefFile =', json);
|
|
if (json === '') {
|
|
print('ERROR: LED definition file does not exist!');
|
|
} else {
|
|
ledDef = JSON.parse(json);
|
|
}
|
|
for (i=0; i<ledDef.length; ++i) {
|
|
++numberOfLeds;
|
|
print('- addLedDefinition', ledDef[i].level, ledDef[i].room, ledDef[i].id, ledDef[i].onColor);
|
|
addLedDefinition(ledDef[i].level, ledDef[i].room, ledDef[i].id, ledDef[i].onColor);
|
|
// printColor(ledDef[i].onColor);
|
|
}
|
|
json = null;
|
|
// ledDef = null; // NO! Do not do this, as the strings are still referenced!
|
|
|
|
// Load Animation Definitions
|
|
let json = File.read(animationFile);
|
|
let animation = [];
|
|
print('ledDefFile =', json);
|
|
if (json === '') {
|
|
print('ERROR: Animation definition file does not exist!');
|
|
} else {
|
|
animation = JSON.parse(json);
|
|
}
|
|
for (i=0; i<animation.length; ++i) {
|
|
print('- addAnimationStep', animation[i].led, animation[i].mode, animation[i].ticks);
|
|
addAnimationStep(animation[i].led, animation[i].mode, animation[i].ticks);
|
|
}
|
|
json = null;
|
|
|
|
// Initialize LED State Engine
|
|
LEDStateEngine_init(numberOfLeds);
|
|
}
|
|
|
|
print('_______________________________________________________');
|
|
print('End of initialization:');
|
|
print('LedPin:', pin);
|
|
print('NumLEDs:', numberOfLeds);
|
|
print('Ticks:', getTicks());
|
|
print('Brightness:', brightnessAdjustment, '%');
|
|
print('LED Update Cycle:', updateCycle, 'ms');
|
|
print('LED', 'Color', 'R', 'G', 'B', 'Tick', 'Level', 'Room', 'Id');
|
|
print('---', '-----', '---', '---', '-----', '---', '-----', '----', '--');
|
|
for (i=0; i<numberOfLeds; ++i) {
|
|
// print(i, LEDState_getRed(i), LEDState_getGreen(i), LEDState_getBlue(i), LEDState_getCurrentTick(i), LEDDefinition_getLevel(i), LEDDefinition_getRoom(i), LEDDefinition_getId(i))
|
|
print(i, LEDState_getColorName(i), adjustBrightness(LEDDefinition_getOnColorRed(i)), adjustBrightness(LEDDefinition_getOnColorGreen(i)), adjustBrightness(LEDDefinition_getOnColorBlue(i), LEDState_getCurrentTick(i), LEDDefinition_getLevel(i), LEDDefinition_getRoom(i), LEDDefinition_getId(i)));
|
|
}
|
|
|
|
allLedOn();
|
|
Timer.set(1000, false, function() {
|
|
allLedOff();
|
|
Timer.set(1000 /* 0,5 sec */, false /* repeat */, function() {
|
|
createLedTestPattern();
|
|
print('Created LED test pattern, uptime:', Sys.uptime(), getInfo());
|
|
LEDStateEngine_start();
|
|
Timer.set(updateCycle, true, function () {
|
|
let i;
|
|
for (i=0; i<numberOfLeds; ++i) {
|
|
strip.setPixel(i, adjustBrightness(LEDState_getRed(i)), adjustBrightness(LEDState_getGreen(i)), adjustBrightness(LEDState_getBlue(i)));
|
|
}
|
|
strip.show();
|
|
}, null);
|
|
}, null);
|
|
}, null);
|
|
|
|
|
|
/*
|
|
Timer.set(30000, true, function () {
|
|
let i;
|
|
print('Ticks:', getTicks());
|
|
print('LED', 'Tick', 'Next');
|
|
print('---', '-----', '----');
|
|
for (i=0; i<numPixels; ++i) {
|
|
print(i, LEDState_getCurrentTick(i), LEDState_getNextTick(i));
|
|
}
|
|
}, null);
|
|
*/
|
|
|
|
RPC.addHandler('led.setBrightness', function(args) {
|
|
// print(args);
|
|
if (args !== undefined && args.level !== undefined) {
|
|
if (args.level > 0 && args.level <= 100) {
|
|
brightnessAdjustment = args.level;
|
|
return { result: 'ok' };
|
|
} else {
|
|
return { error: 'Brightness level must be in the range 1..100' };
|
|
}
|
|
} else {
|
|
return { error: 'level is required having a value in the range 1..100' };
|
|
}
|
|
}, null);
|
|
|
|
|
|
RPC.addHandler('led.pause', function(args) {
|
|
LEDStateEngine_pause();
|
|
return { result: 'ok' };
|
|
}, null);
|
|
|
|
RPC.addHandler('led.run', function(args) {
|
|
LEDStateEngine_start();
|
|
return { result: 'ok' };
|
|
}, null);
|
|
|
|
RPC.addHandler('led.getColors', function(args) {
|
|
let colors = JSON.parse(File.read(colorFile));
|
|
return { result: 'ok', colors: colors };
|
|
}, null);
|
|
|
|
RPC.addHandler('led.getLamps', function(args) {
|
|
let lamps = JSON.parse(File.read(lampsFile));
|
|
return { result: 'ok', lamps: lamps };
|
|
}, null);
|
|
|
|
RPC.addHandler('led.getAnimations', function(args) {
|
|
let anims = JSON.parse(File.read(animationFile));
|
|
return { result: 'ok', animations: anims };
|
|
}, null);
|
|
|
|
function saveFile(filename, content) {
|
|
let backupFilename = 'bak-' + filename;
|
|
File.remove(backupFilename);
|
|
File.rename(filename, backupFilename);
|
|
File.write(JSON.stringify(content), filename);
|
|
let backupFilename = null;
|
|
}
|