Added update RPC for colors, lamps and animations

This commit is contained in:
Dirk Jahnke 2017-12-03 09:22:54 +01:00
parent 94910cc5e8
commit 2479cc62fd
3 changed files with 67 additions and 29 deletions

View File

@ -20,7 +20,8 @@ print('LED GPIO:', onBoardLed, 'button GPIO:', button);
let getInfo = function() {
return JSON.stringify({
total_ram: Sys.total_ram(),
free_ram: Sys.free_ram()
free_ram: Sys.free_ram(),
uptime: Sys.uptime()
});
};
@ -54,7 +55,7 @@ Net.setStatusEventHandler(function(ev, arg) {
} else if (ev === Net.STATUS_GOT_IP) {
evs = 'GOT_IP';
}
print('== Net event:', ev, evs);
print('==> NET:', ev, evs);
}, null);
// Initialize LED controller
@ -89,6 +90,7 @@ 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 tickDuration = Cfg.get('led.tickDuration');
let brightnessAdjustment = Cfg.get('led.brightness');
let numberOfLeds = 0; // from config files, count led definition entries
@ -98,12 +100,12 @@ function createLedTestPattern() {
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) strip.setPixel(i, 10, 10, 10);
if (switchMod === 1) strip.setPixel(i, 10, 10, 10);
if (switchMod === 2) strip.setPixel(i, 10, 100, 0);
if (switchMod === 3) strip.setPixel(i, 0, 10, 0);
if (switchMod === 4) strip.setPixel(i, 0, 10, 10);
if (switchMod === 5) strip.setPixel(i, 0, 0, 10);
if (switchMod < 0 || switchMod > 5) print("WRONG -- should never reach this in switch statement!");
}
strip.show();
@ -112,7 +114,7 @@ function createLedTestPattern() {
function allLedOn() {
let i;
for (i=0; i<numPixels; ++i) {
strip.setPixel(i, 100, 100, 100);
strip.setPixel(i, 10, 10, 10);
}
strip.show();
}
@ -127,6 +129,8 @@ function adjustBrightness(value) {
}
// initialize LEDs
// ===============
print('***** Start initialization', getInfo());
let i;
allLedOff();
@ -155,7 +159,7 @@ if (useDefaults) {
// Load LED Definitions
let json = File.read(lampsFile);
let ledDef = [];
print('ledDefFile =', json);
print('lampsFile =', json);
if (json === '') {
print('ERROR: LED definition file does not exist!');
} else {
@ -173,7 +177,7 @@ if (useDefaults) {
// Load Animation Definitions
let json = File.read(animationFile);
let animation = [];
print('ledDefFile =', json);
print('animationFile =', json);
if (json === '') {
print('ERROR: Animation definition file does not exist!');
} else {
@ -188,14 +192,14 @@ if (useDefaults) {
// Initialize LED State Engine
LEDStateEngine_init(numberOfLeds);
}
print('***** End of initialization', getInfo());
print('_______________________________________________________');
print('End of initialization:');
print('LedPin:', pin);
print('NumLEDs:', numberOfLeds);
print('Ticks:', getTicks());
print('Brightness:', brightnessAdjustment, '%');
print('Tick duration:', tickDuration, 'ms');
print('LED Update Cycle:', updateCycle, 'ms');
print('Brightness:', brightnessAdjustment, '%');
print('LED', 'Color', 'R', 'G', 'B', 'Tick', 'Level', 'Room', 'Id');
print('---', '-----', '---', '---', '-----', '---', '-----', '----', '--');
for (i=0; i<numberOfLeds; ++i) {
@ -204,11 +208,13 @@ for (i=0; i<numberOfLeds; ++i) {
}
allLedOn();
print('***** Wait 1 second for other tasks to startup', getInfo());
Timer.set(1000, false, function() {
allLedOff();
Timer.set(1000 /* 0,5 sec */, false /* repeat */, function() {
print('***** LED test pattern', getInfo());
Timer.set(1000, false, function() {
createLedTestPattern();
print('Created LED test pattern, uptime:', Sys.uptime(), getInfo());
print('***** Start LED state engine and LED-update timer', getInfo());
LEDStateEngine_start();
Timer.set(updateCycle, true, function () {
let i;
@ -219,19 +225,10 @@ Timer.set(1000, false, function() {
}, null);
}, null);
}, null);
print('***** End of init.js, sub tasks still running', getInfo());
/*
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 Handlers ------- */
RPC.addHandler('led.setBrightness', function(args) {
// print(args);
@ -280,3 +277,43 @@ function saveFile(filename, content) {
File.write(JSON.stringify(content), filename);
let backupFilename = null;
}
function recoverFile(filename) {
let newerFilename = 'new-' + filename;
let backupFilename = 'bak-' + filename;
let bakFileContent = File.read(backupFilename);
if (bakFileContent === '') {
Log.error('recoverFile(' + filename + '): Backup file ' + backupFilename + ' does not exist or is empty');
return;
}
File.remove(newerFilename);
File.rename(filename, newerFilename);
File.rename(backupFilename, filename);
}
RPC.addHandler('led.putColors', function(args) {
if (args !== undefined && args.colors !== undefined) {
saveFile(colorFile, args.colors);
return { result: 'ok' };
} else {
return { error: 'colors: {name, red, green, blue} is required' };
}
}, null);
RPC.addHandler('led.putLamps', function(args) {
if (args !== undefined && args.lamps !== undefined) {
saveFile(lampsFile, args.lamps);
return { result: 'ok' };
} else {
return { error: 'lamps: {level, room, id, onColor} is required' };
}
}, null);
RPC.addHandler('led.putAnimations', function(args) {
if (args !== undefined && args.animations !== undefined) {
saveFile(animationFile, args.animations);
return { result: 'ok' };
} else {
return { error: 'animations: {led, mode, ticks} is required' };
}
}, null);

View File

@ -34,7 +34,8 @@ config_schema:
- ["led.pin", "i", 2, {title: "Pin for data to WS2812 based LED chain"}]
- ["led.useDefaults", "b", false, {title: "Use default values for colors, led-definitions, animations"}]
- ["led.count", "i", 32, {title: "Number of LEDs connected in the chain"}]
- ["led.updateCycle", "i", 500, {title: "Frequency of LED update in ms"}]
- ["led.updateCycle", "i", 100, {title: "Frequency of LED update in ms"}]
- ["led.tickDuration", "i", 100, {title: "Timebase: Duration of a tick in ms"}]
- ["led.brightness", "i", 20, {title: "Default brightness in % (1-100)"}]
- ["led.colorFile", "s", "colors.cfg", {title: "File name containing color definitions"}]
- ["led.lampsFile", "s", "lamps.cfg", {title: "File name containing lamp definitions"}]

View File

@ -48,7 +48,7 @@ static void stateEngineTickTimer() {
static void delayed_boot() {
LOG(LL_INFO, ("*** Start timer for LED state engine ticks"));
mgos_set_timer(100, true, stateEngineTickTimer, NULL); // every 0.1 second call timer_cb
mgos_set_timer(mgos_sys_config_get_led_tickDuration(), true, stateEngineTickTimer, NULL); // every 0.1 second call timer_cb
}
enum mgos_app_init_result mgos_app_init(void) {