From 3f369f3d6b3ed66542c860b3a02efc37f5e22556 Mon Sep 17 00:00:00 2001 From: Dirk Jahnke Date: Sat, 2 Dec 2017 08:03:56 +0100 Subject: [PATCH] Reading and using config files for lamps, colors and animation --- fs/animations.cfg | 16 +++++ fs/colors.cfg | 7 +++ fs/init.js | 142 +++++++++++++++++++++++++++++++------------- fs/lamps.cfg | 9 +++ mos.yml | 13 ++-- src/LEDDefinition.c | 11 ++-- src/LEDDefinition.h | 2 +- src/main.c | 53 +++++++++++++---- 8 files changed, 188 insertions(+), 65 deletions(-) create mode 100644 fs/animations.cfg create mode 100644 fs/colors.cfg create mode 100644 fs/lamps.cfg diff --git a/fs/animations.cfg b/fs/animations.cfg new file mode 100644 index 0000000..025d3fa --- /dev/null +++ b/fs/animations.cfg @@ -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} +] diff --git a/fs/colors.cfg b/fs/colors.cfg new file mode 100644 index 0000000..86d17aa --- /dev/null +++ b/fs/colors.cfg @@ -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} +] \ No newline at end of file diff --git a/fs/init.js b/fs/init.js index dc135b2..916e962 100644 --- a/fs/init.js +++ b/fs/init.js @@ -6,6 +6,7 @@ load('api_sys.js'); load('api_rpc.js'); load('api_timer.js'); load("api_math.js"); +load("api_file.js"); load("api_neopixel.js"); @@ -58,9 +59,10 @@ Net.setStatusEventHandler(function(ev, arg) { // Initialize LED controller let addColor = ffi('void addColor(char *,int,int,int)'); -let addLedDefinition = ffi('void addLedDefinition(char *, char *, char *, int, int, int)'); -let addAnimationStep = ffi('void addAnimationStep(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)'); @@ -76,9 +78,19 @@ 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'), 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 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() { @@ -93,31 +105,6 @@ function createLedTestPattern() { 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!"); - /* - 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(); } @@ -135,34 +122,105 @@ function allLedOff() { 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; ired, c->green, c->blue)); +} + // javascript adapter functions: void addColor(char *name, int red, int green, int blue) { LEDColor_add(name, red, green, blue); } -void addLedDefinition(char *level, char *room, char *id, int red, int green, int blue) { - LEDDefinition_add(level, room, id, red, green, blue); +void addLedDefinition(char *level, char *room, char *id, char *onColorName) { + 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; - switch (ledMode) { - case 1: m = LEDMode_on; break; - case 2: m = LEDMode_off; break; - case 3: m = LEDMode_blink; break; - case 4: m = LEDMode_tv; break; - case 5: m = LEDMode_fire; break; - default: m = LEDMode_off; + if (strcmp(ledMode, "on") == 0 || strcmp(ledMode, "LEDMode_on") == 0) { + m = LEDMode_on; + } + else if (strcmp(ledMode, "off") == 0 || strcmp(ledMode, "LEDMode_off") == 0) { + 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); }