Reading and using config files for lamps, colors and animation

This commit is contained in:
Dirk Jahnke 2017-12-02 08:03:56 +01:00
parent 54a6ee1cd1
commit 3f369f3d6b
8 changed files with 188 additions and 65 deletions

16
fs/animations.cfg Normal file
View File

@ -0,0 +1,16 @@
[
{"led": 0, "mode": "on", "ticks": 10},
{"led": 0, "mode": "off", "ticks": 10},
{"led": 1, "mode": "on", "ticks": 20},
{"led": 1, "mode": "off", "ticks": 20},
{"led": 2, "mode": "on", "ticks": 40},
{"led": 2, "mode": "off", "ticks": 40},
{"led": 3, "mode": "on", "ticks": 80},
{"led": 3, "mode": "off", "ticks": 80},
{"led": 4, "mode": "on", "ticks": 160},
{"led": 4, "mode": "off", "ticks": 160},
{"led": 5, "mode": "on", "ticks": 320},
{"led": 5, "mode": "off", "ticks": 320},
{"led": 6, "mode": "on", "ticks": 640},
{"led": 6, "mode": "off", "ticks": 640}
]

7
fs/colors.cfg Normal file
View File

@ -0,0 +1,7 @@
[
{"name": "off", "red": 0, "green": 0, "blue": 0},
{"name": "candle", "red": 50, "green": 45, "blue": 10},
{"name": "warmWhite", "red": 100, "green": 100, "blue": 80},
{"name": "coldWhite", "red": 100, "green": 100, "blue": 100},
{"name": "brightWhite", "red": 255, "green": 255, "blue": 255}
]

View File

@ -6,6 +6,7 @@ load('api_sys.js');
load('api_rpc.js'); load('api_rpc.js');
load('api_timer.js'); load('api_timer.js');
load("api_math.js"); load("api_math.js");
load("api_file.js");
load("api_neopixel.js"); load("api_neopixel.js");
@ -58,9 +59,10 @@ Net.setStatusEventHandler(function(ev, arg) {
// Initialize LED controller // Initialize LED controller
let addColor = ffi('void addColor(char *,int,int,int)'); let addColor = ffi('void addColor(char *,int,int,int)');
let addLedDefinition = ffi('void addLedDefinition(char *, char *, char *, int, int, int)'); let addLedDefinition = ffi('void addLedDefinition(char *, char *, char *, char *)');
let addAnimationStep = ffi('void addAnimationStep(int, int, int)'); 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 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_start = ffi('void startLEDStateEngine(void)');
let LEDStateEngine_pause = ffi('void pauseLEDStateEngine(void)'); let LEDStateEngine_pause = ffi('void pauseLEDStateEngine(void)');
let LEDState_getRed = ffi('int LEDState_getLedRed(int)'); let LEDState_getRed = ffi('int LEDState_getLedRed(int)');
@ -76,9 +78,19 @@ let LEDDefinition_getOnColorRed = ffi('int LEDDefinition_getOnColorRed(int)');
let LEDDefinition_getOnColorGreen = ffi('int LEDDefinition_getOnColorGreen(int)'); let LEDDefinition_getOnColorGreen = ffi('int LEDDefinition_getOnColorGreen(int)');
let LEDDefinition_getOnColorBlue = ffi('int LEDDefinition_getOnColorBlue(int)'); let LEDDefinition_getOnColorBlue = ffi('int LEDDefinition_getOnColorBlue(int)');
let getTicks = ffi('int getTicks(void)'); let getTicks = ffi('int getTicks(void)');
let printColor = ffi('void printColor(char *)');
let pin = Cfg.get('led.pin'), numPixels = Cfg.get('led.count'), colorOrder = NeoPixel.GRB; let pin = Cfg.get('led.pin');
let numPixels = Cfg.get('led.count');
let colorOrder = NeoPixel.GRB;
let strip = NeoPixel.create(pin, numPixels, colorOrder); 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 */ /* Create test pattern */
function createLedTestPattern() { function createLedTestPattern() {
@ -93,31 +105,6 @@ function createLedTestPattern() {
if (switchMod === 4) strip.setPixel(i, 0, 100, 100); if (switchMod === 4) strip.setPixel(i, 0, 100, 100);
if (switchMod === 5) strip.setPixel(i, 0, 0, 100); if (switchMod === 5) strip.setPixel(i, 0, 0, 100);
if (switchMod < 0 || switchMod > 5) print("WRONG -- should never reach this in switch statement!"); if (switchMod < 0 || switchMod > 5) print("WRONG -- should never reach this in switch statement!");
/*
switch (switchMod) {
case 0:
strip.setPixel(i, 100, 100, 100);
break;
case 1:
strip.setPixel(i, 100, 0, 0);
break;
case 2:
strip.setPixel(i, 100, 100, 0);
break;
case 3:
strip.setPixel(i, 0, 100, 0);
break;
case 4:
strip.setPixel(i, 0, 100, 100);
break;
case 5:
strip.setPixel(i, 0, 0, 100);
break;
default:
print("WRONG -- should never reach this in switch statement!");
}
*/
// strip.setPixel(i, 100, 100, 100);
} }
strip.show(); strip.show();
} }
@ -135,34 +122,105 @@ function allLedOff() {
strip.show(); strip.show();
} }
function adjustBrightness(value) {
return Math.floor(value * brightnessAdjustment/100);
}
// initialize LEDs // initialize LEDs
let i;
allLedOff(); 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('End of initialization:');
print('LedPin:', pin); print('LedPin:', pin);
print('NumLEDs:', numPixels); print('NumLEDs:', numberOfLeds);
print('Ticks:', getTicks()); print('Ticks:', getTicks());
let i; print('Brightness:', brightnessAdjustment, '%');
print('LED Update Cycle:', updateCycle, 'ms');
print('LED', 'Color', 'R', 'G', 'B', 'Tick', 'Level', 'Room', 'Id'); print('LED', 'Color', 'R', 'G', 'B', 'Tick', 'Level', 'Room', 'Id');
print('---', '-----', '---', '---', '-----', '---', '-----', '----', '--'); print('---', '-----', '---', '---', '-----', '---', '-----', '----', '--');
for (i=0; i<numPixels; ++i) { 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_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), LEDDefinition_getOnColorRed(i), LEDDefinition_getOnColorGreen(i), LEDDefinition_getOnColorBlue(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)));
} }
Timer.set(500 /* 0,5 sec */, false /* repeat */, function() {
createLedTestPattern(); allLedOn();
print('Created LED test pattern, uptime:', Sys.uptime(), getInfo()); Timer.set(1000, false, function() {
LEDStateEngine_start(); allLedOff();
Timer.set(100, true, function () { Timer.set(1000 /* 0,5 sec */, false /* repeat */, function() {
let i; createLedTestPattern();
for (i=0; i<numPixels; ++i) { print('Created LED test pattern, uptime:', Sys.uptime(), getInfo());
strip.setPixel(i, LEDState_getRed(i), LEDState_getGreen(i), LEDState_getBlue(i)); LEDStateEngine_start();
} Timer.set(updateCycle, true, function () {
strip.show(); 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);
}, null); }, null);
/* /*
Timer.set(30000, true, function () { Timer.set(30000, true, function () {
let i; let i;

9
fs/lamps.cfg Normal file
View File

@ -0,0 +1,9 @@
[
{"level": "EG", "room": "Wohnzimmer", "id": "eg-wozi", "onColor": "candle"},
{"level": "EG", "room": "Buero", "id": "eg-office", "onColor": "coldWhite"},
{"level": "EG", "room": "Bad", "id": "eg-bad", "onColor": "warmWhite"},
{"level": "EG", "room": "Kueche", "id": "eg-kueche", "onColor": "warmWhite"},
{"level": "EG", "room": "Flur", "id": "eg-flur", "onColor": "candle"},
{"level": "OG", "room": "Wohnzimmer", "id": "og-wozi", "onColor": "candle"},
{"level": "OG", "room": "Buero", "id": "og-office", "onColor": "coldWhite"}
]

13
mos.yml
View File

@ -1,7 +1,7 @@
version: "1.0" version: "0.8"
arch: esp8266 arch: esp8266
author: mongoose-os author: Dirk Jahnke
description: NeoPixel app to control model railroad house lightnings description: dlite - NeoPixel app to control model railroad house lightnings
mongoose_os_version: ${mos_version} mongoose_os_version: ${mos_version}
sources: sources:
- src - src
@ -32,8 +32,13 @@ config_schema:
- ["i2c.enable", true] - ["i2c.enable", true]
- ["led", "o", {"title": "LED control settings"}] - ["led", "o", {"title": "LED control settings"}]
- ["led.pin", "i", 2, {title: "Pin for data to WS2812 based LED chain"}] - ["led.pin", "i", 2, {title: "Pin for data to WS2812 based LED chain"}]
- ["led.useDefaults", "b", true, {title: "Use default values for colors, led-definitions, animations"}] - ["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.count", "i", 32, {title: "Number of LEDs connected in the chain"}]
- ["led.updateCycle", "i", 500, {title: "Frequency of LED update in ms"}]
- ["led.brightness", "i", 30, {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"}]
- ["led.animationFile", "s", "animations.cfg", {title: "File name containing animation definitions"}]
build_vars: build_vars:
MGOS_ENABLE_ONEWIRE: 1 MGOS_ENABLE_ONEWIRE: 1
cflags: [] cflags: []

View File

@ -1,6 +1,7 @@
/* LEDDefinition.cpp */ /* LEDDefinition.cpp */
#include "LEDDefinition.h" #include "LEDDefinition.h"
#include "common/str_util.h"
LEDColor offColor = { name: "off", red: 0, green: 0, blue: 0}; LEDColor offColor = { name: "off", red: 0, green: 0, blue: 0};
LEDColor dimmedRedColor = { name: "dimmedRed", red: 80, green: 0, blue: 0}; LEDColor dimmedRedColor = { name: "dimmedRed", red: 80, green: 0, blue: 0};
@ -20,7 +21,7 @@ LEDColor *LEDColor_get(char *name) {
void LEDColor_add(char *name, uint8_t red, uint8_t green, uint8_t blue) { void LEDColor_add(char *name, uint8_t red, uint8_t green, uint8_t blue) {
if (numberOfColors < MAX_COLORS) { if (numberOfColors < MAX_COLORS) {
ledColor[numberOfColors].name = name; ledColor[numberOfColors].name = strdup(name);
ledColor[numberOfColors].red = red; ledColor[numberOfColors].red = red;
ledColor[numberOfColors].green = green; ledColor[numberOfColors].green = green;
ledColor[numberOfColors].blue = blue; ledColor[numberOfColors].blue = blue;
@ -61,10 +62,10 @@ LEDDefinition *LEDDefinition_get(int ledNum) {
void LEDDefinition_add(char *level, char *room, char *id, uint8_t red, uint8_t green, uint8_t blue) { void LEDDefinition_add(char *level, char *room, char *id, uint8_t red, uint8_t green, uint8_t blue) {
if (numberOfLEDDefinitions < MAX_LEDS) { if (numberOfLEDDefinitions < MAX_LEDS) {
ledDefinition[numberOfLEDDefinitions].level = level; ledDefinition[numberOfLEDDefinitions].level = strdup(level);
ledDefinition[numberOfLEDDefinitions].room = room; ledDefinition[numberOfLEDDefinitions].room = strdup(room);
ledDefinition[numberOfLEDDefinitions].id = id; ledDefinition[numberOfLEDDefinitions].id = strdup(id);
ledDefinition[numberOfLEDDefinitions].onColor.name = "autoColor"; ledDefinition[numberOfLEDDefinitions].onColor.name = strdup("autoColor");
ledDefinition[numberOfLEDDefinitions].onColor.red = red; ledDefinition[numberOfLEDDefinitions].onColor.red = red;
ledDefinition[numberOfLEDDefinitions].onColor.green = green; ledDefinition[numberOfLEDDefinitions].onColor.green = green;
ledDefinition[numberOfLEDDefinitions].onColor.blue = blue; ledDefinition[numberOfLEDDefinitions].onColor.blue = blue;

View File

@ -9,7 +9,7 @@
#include "mongoose/mongoose.h" #include "mongoose/mongoose.h"
#include "common/cs_dbg.h" #include "common/cs_dbg.h"
#define MAX_LEDS 100 #define MAX_LEDS 50
typedef enum LEDMode_e { LEDMode_on, LEDMode_off, LEDMode_blink, LEDMode_tv, LEDMode_fire } LEDMode; typedef enum LEDMode_e { LEDMode_on, LEDMode_off, LEDMode_blink, LEDMode_tv, LEDMode_fire } LEDMode;

View File

@ -55,34 +55,61 @@ enum mgos_app_init_result mgos_app_init(void) {
int numberOfLeds = mgos_sys_config_get_led_count(); int numberOfLeds = mgos_sys_config_get_led_count();
bool loadDefaultData = mgos_sys_config_get_led_useDefaults(); bool loadDefaultData = mgos_sys_config_get_led_useDefaults();
LOG(LL_DEBUG, ("LEDColor_init(loadDefaultData=%s)", loadDefaultData ? "true" : "false"));
mgos_msleep(50);
LEDColor_init(loadDefaultData); LEDColor_init(loadDefaultData);
LOG(LL_DEBUG, ("LEDDefinition_init(numberOfLeds=%d, loadDefaultData=%s)", numberOfLeds, loadDefaultData ? "true" : "false"));
mgos_msleep(50);
LEDDefinition_init(numberOfLeds, loadDefaultData); LEDDefinition_init(numberOfLeds, loadDefaultData);
LOG(LL_DEBUG, ("AnimationConfig_init(loadDefaultData=%s)", loadDefaultData ? "true" : "false"));
mgos_msleep(50);
AnimationConfig_init(loadDefaultData); AnimationConfig_init(loadDefaultData);
LEDStateEngine_init(numberOfLeds);
if (loadDefaultData) { if (loadDefaultData) {
stateEngineRunning = true; LOG(LL_DEBUG, ("LEDStateEngine_init(numberOfLeds=%d)", numberOfLeds));
mgos_msleep(50);
LEDStateEngine_init(numberOfLeds);
// stateEngineRunning = true;
} else {
LOG(LL_INFO, ("LEDStateEngine needs to be initialized, not started yet as LEDDefinition not loaded so far"));
} }
mgos_set_timer(5000, false, delayed_boot, NULL); // after 5 seconds boot the functions mgos_set_timer(5000, false, delayed_boot, NULL); // after 5 seconds boot the functions
return MGOS_APP_INIT_SUCCESS; return MGOS_APP_INIT_SUCCESS;
} }
void printColor(char *name) {
LEDColor *c = LEDColor_get(name);
LOG(LL_INFO, ("Color %s: %d/%d/%d", name, c->red, c->green, c->blue));
}
// javascript adapter functions: // javascript adapter functions:
void addColor(char *name, int red, int green, int blue) { void addColor(char *name, int red, int green, int blue) {
LEDColor_add(name, red, green, blue); LEDColor_add(name, red, green, blue);
} }
void addLedDefinition(char *level, char *room, char *id, int red, int green, int blue) { void addLedDefinition(char *level, char *room, char *id, char *onColorName) {
LEDDefinition_add(level, room, id, red, green, blue); LEDColor *c = LEDColor_get(onColorName);
LEDDefinition_add(level, room, id, c->red, c->green, c->blue);
} }
void addAnimationStep(int ledIndex, int ledMode, int duration) { void addAnimationStep(int ledIndex, char *ledMode, int duration) {
LEDMode m = LEDMode_off; LEDMode m = LEDMode_off;
switch (ledMode) { if (strcmp(ledMode, "on") == 0 || strcmp(ledMode, "LEDMode_on") == 0) {
case 1: m = LEDMode_on; break; m = LEDMode_on;
case 2: m = LEDMode_off; break; }
case 3: m = LEDMode_blink; break; else if (strcmp(ledMode, "off") == 0 || strcmp(ledMode, "LEDMode_off") == 0) {
case 4: m = LEDMode_tv; break; m = LEDMode_off;
case 5: m = LEDMode_fire; break; }
default: m = LEDMode_off; else if (strcmp(ledMode, "blink") == 0 || strcmp(ledMode, "LEDMode_blink") == 0) {
m = LEDMode_blink;
}
else if (strcmp(ledMode, "tv") == 0 || strcmp(ledMode, "LEDMode_tv") == 0) {
m = LEDMode_tv;
}
else if (strcmp(ledMode, "fire") == 0 || strcmp(ledMode, "LEDMode_fire") == 0) {
m = LEDMode_fire;
}
else {
m = LEDMode_off;
LOG(LL_ERROR, ("addAnimationStep: Invalid LEDMode %s, using LEDMode_off", ledMode));
} }
AnimationStep_add(ledIndex, m, duration); AnimationStep_add(ledIndex, m, duration);
} }