diff --git a/fs/api_dlite.js b/fs/api_dlite.js index 1c3eda1..8efd6e1 100644 --- a/fs/api_dlite.js +++ b/fs/api_dlite.js @@ -1,4 +1,3 @@ -let onBoardLed = ffi('int get_led_gpio_pin(void)')(); let addColor = ffi('void addColor(char *,int,int,int)'); let LEDDefinition_addByName = ffi('void LEDDefinition_addByName(char *, char *, char *, char *)'); let addAnimationStep = ffi('void addAnimationStep(int, char *, int)'); @@ -25,3 +24,5 @@ let NeoPixel_clear = ffi('void NeoPixel_clear(void)'); let NeoPixel_set = ffi('void NeoPixel_set(int,int,int,int)'); let LEDStateEngine_getMinTickTime = ffi('double LEDStateEngine_getMinTickTime(void)'); let LEDStateEngine_getMaxTickTime = ffi('double LEDStateEngine_getMaxTickTime(void)'); +let LEDStateEngine_getBrightness = ffi('int LEDStateEngine_getBrightness(void)'); +let LEDStateEngine_setBrightness = ffi('void LEDStateEngine_setBrightness(int)'); diff --git a/fs/init.js b/fs/init.js index 661a90e..928f2c3 100644 --- a/fs/init.js +++ b/fs/init.js @@ -9,10 +9,11 @@ load('api_file.js'); load('api_dlite.js'); // Helper C function get_led_gpio_pin() in src/main.c returns built-in LED GPIO -let button = Cfg.get('pins.button'); +// let button = Cfg.get('pins.button'); let topic = '/devices/' + Cfg.get('device.id') + '/events'; +// let onBoardLed = ffi('int get_led_gpio_pin(void)')(); -print('LED GPIO:', onBoardLed, 'button GPIO:', button); +// print('LED GPIO:', onBoardLed, 'button GPIO:', button); let getInfo = function() { return JSON.stringify({ @@ -62,7 +63,6 @@ let animationFile = Cfg.get('led.animationFile'); let lampsFile = Cfg.get('led.lampsFile'); let useDefaults = Cfg.get('led.useDefaults'); let tickDuration = Cfg.get('led.tickDuration'); -let brightnessAdjustment = Cfg.get('led.brightness'); let numberOfLeds = configNumLeds, numberOfLedDefs = 0; // from config files, count led definition entries /* Create test pattern */ @@ -95,10 +95,6 @@ function allLedOff() { NeoPixel_show(); } -function adjustBrightness(value) { - return Math.floor(value * brightnessAdjustment/100); -} - function loadColorDefs() { // Load Color definitions let json = File.read(colorFile); @@ -181,16 +177,23 @@ function initialize() { print('NumLEDs:', numberOfLeds); print('Ticks:', getTicks()); print('Tick duration:', tickDuration, 'ms'); - print('Brightness:', brightnessAdjustment, '%'); + print('Brightness:', LEDStateEngine_getBrightness(), '%'); print('LED', 'Color', 'R', 'G', 'B', 'Tick', 'Level', 'Room', 'Id'); print('---', '-----', '---', '---', '-----', '---', '-----', '----', '--'); for (i=0; i 0 && args.level <= 100) { - brightnessAdjustment = args.level; + LEDStateEngine_setBrightness(args.level); return { result: 'ok' }; } else { return { error: 'Brightness level must be in the range 1..100' }; @@ -218,29 +223,33 @@ RPC.addHandler('led.setBrightness', function(args) { } else { return { error: 'level is required having a value in the range 1..100' }; } +}, "{level: %d}"); +print(' led.getBrightness'); +RPC.addHandler('led.getBrightness', function(args) { + let brightness = LEDStateEngine_getBrightness(); + return { result: 'ok', brightness: brightness }; }, null); - - +print(' led.pause'); RPC.addHandler('led.pause', function(args) { LEDStateEngine_pause(); return { result: 'ok' }; }, null); - +print(' led.run'); RPC.addHandler('led.run', function(args) { LEDStateEngine_start(); return { result: 'ok' }; }, null); - +print(' led.getColors'); RPC.addHandler('led.getColors', function(args) { let colors = JSON.parse(File.read(colorFile)); return { result: 'ok', colors: colors }; }, null); - +print(' led.getLamps'); RPC.addHandler('led.getLamps', function(args) { let lamps = JSON.parse(File.read(lampsFile)); return { result: 'ok', lamps: lamps }; }, null); - +print(' led.getAnimations'); RPC.addHandler('led.getAnimations', function(args) { let anims = JSON.parse(File.read(animationFile)); return { result: 'ok', animations: anims }; @@ -266,7 +275,7 @@ function recoverFile(filename) { File.rename(filename, newerFilename); File.rename(backupFilename, filename); } - +print(' led.putColors'); RPC.addHandler('led.putColors', function(args) { if (args !== undefined && args.colors !== undefined) { saveFile(colorFile, args.colors); @@ -275,7 +284,7 @@ RPC.addHandler('led.putColors', function(args) { return { error: 'colors: {name, red, green, blue} is required' }; } }, null); - +print(' led.putLamps'); RPC.addHandler('led.putLamps', function(args) { if (args !== undefined && args.lamps !== undefined) { saveFile(lampsFile, args.lamps); @@ -284,7 +293,7 @@ RPC.addHandler('led.putLamps', function(args) { return { error: 'lamps: {level, room, id, onColor} is required' }; } }, null); - +print(' led.putAnimations'); RPC.addHandler('led.putAnimations', function(args) { if (args !== undefined && args.animations !== undefined) { saveFile(animationFile, args.animations); diff --git a/src/AnimationConfig.h b/src/AnimationConfig.h index ed71659..2eb25ce 100644 --- a/src/AnimationConfig.h +++ b/src/AnimationConfig.h @@ -4,7 +4,7 @@ #define AnimationConfig_loaded #include -#include "fw/src/mgos.h" +#include "mgos.h" #include "common/cs_dbg.h" #include "mjs.h" #include "LEDDefinition.h" @@ -30,4 +30,4 @@ extern uint16_t getNumberOfAnimationSteps(); extern void AnimationStep_add(uint8_t ledIndex, LEDMode m, uint32_t duration); extern uint16_t AnimationConfig_getNextAnimationStepForLED(uint8_t led, uint16_t currentAnimationStep); -#endif \ No newline at end of file +#endif diff --git a/src/LEDDefinition.h b/src/LEDDefinition.h index 3589f0c..85a6b95 100644 --- a/src/LEDDefinition.h +++ b/src/LEDDefinition.h @@ -4,7 +4,7 @@ #define LEDDefinition_included #include -#include "fw/src/mgos.h" +#include "mgos.h" #include "mjs.h" #include "mongoose/mongoose.h" diff --git a/src/LEDState.c b/src/LEDState.c index 40135c8..19b873b 100644 --- a/src/LEDState.c +++ b/src/LEDState.c @@ -9,6 +9,7 @@ static LEDStateEngine theLEDStateEngine; static int ticks = 0; +static int brightness = 50; int getTicks(void) { return ticks; } @@ -26,6 +27,7 @@ void LEDStateEngine_setNumberOfLeds(int numberOfLeds) { void LEDStateEngine_init(int ledPin, int numberOfLeds) { theLEDStateEngine.comment = ""; theLEDStateEngine.pin = ledPin; + brightness = mgos_sys_config_get_led_brightness(); LEDStateEngine_setNumberOfLeds(numberOfLeds); for (int i=0; i maxTickTime) maxTickTime = usedTime; if (usedTime < minTickTime) minTickTime = usedTime; } -double LEDStateEngine_getMinTickTime() { return minTickTime; } -double LEDStateEngine_getMaxTickTime() { return maxTickTime; } +double LEDStateEngine_getMinTickTime() { return 1000 * minTickTime; } +double LEDStateEngine_getMaxTickTime() { return 1000 * maxTickTime; } static uint8_t adjustBrightness(uint8_t value) { - return (value * mgos_sys_config_get_led_brightness()/100); + return (value * brightness)/100; +} + +int LEDStateEngine_getBrightness() { return brightness; } +void LEDStateEngine_setBrightness(int newBrightness) { + if (newBrightness > 0 && newBrightness <= 100) { + brightness = newBrightness; + } else { + LOG(LL_ERROR, ("invalid brightness value %d", newBrightness)); + } } static void updateLedDisplay(LEDState *state) { NeoPixel_set(state->index, - adjustBrightness(state->currentColor.red), adjustBrightness(state->currentColor.green), adjustBrightness(state->currentColor.blue)); + adjustBrightness(state->currentColor.red), + adjustBrightness(state->currentColor.green), + adjustBrightness(state->currentColor.blue)); } void LEDState_tick(int ledNum) { diff --git a/src/main.c b/src/main.c index 2094084..2db8dd1 100644 --- a/src/main.c +++ b/src/main.c @@ -7,8 +7,8 @@ #include "mgos_sys_config.h" #include "mgos_system.h" #include "mgos_timers.h" -#include "fw/src/mgos_hal.h" -#include "fw/src/mgos_dlsym.h" +#include "mgos_hal.h" +#include "mgos_dlsym.h" #include "mjs.h" #include "LEDDefinition.h" #include "AnimationConfig.h" @@ -35,7 +35,7 @@ int get_led_gpio_pin(void) { return LED_GPIO; } -LEDStateEngine ledStateEngine; +// static LEDStateEngine ledStateEngine; static bool stateEngineRunning = false; static bool pausedReported = false;