Finalized on/off-animation mode

This commit is contained in:
Dirk Jahnke 2017-12-09 18:35:57 +01:00
parent 7fad229821
commit 626fe42a90
6 changed files with 56 additions and 33 deletions

View File

@ -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)');

View File

@ -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<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)));
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));
}
}
Timer.set(1500, false, function() {
Timer.set(300, false, function() {
initialize();
allLedOn();
print('***** Starting timer to delay test pattern generation');
@ -199,18 +202,20 @@ Timer.set(1500, false, function() {
showLedTestPattern();
print('***** Start LED state engine and LED-update timer', getInfo());
LEDStateEngine_start();
Timer.set(10000, true, function() {
print("Timer: minTickTime =", LEDStateEngine_getMinTickTime(), ", maxTickTime =", LEDStateEngine_getMaxTickTime());
Timer.set(30000, true, function() {
print("Timer: minTickTime =", LEDStateEngine_getMinTickTime(),
"ms, maxTickTime =", LEDStateEngine_getMaxTickTime(), "ms");
}, null);
}, null);
/* ------ RPC Handlers ------- */
print('**** Adding RPC handlers');
print(' led.setBrightness');
RPC.addHandler('led.setBrightness', function(args) {
// print(args);
if (args !== undefined && args.level !== undefined) {
if (args.level > 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);

View File

@ -4,7 +4,7 @@
#define AnimationConfig_loaded
#include <stdio.h>
#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
#endif

View File

@ -4,7 +4,7 @@
#define LEDDefinition_included
#include <stdio.h>
#include "fw/src/mgos.h"
#include "mgos.h"
#include "mjs.h"
#include "mongoose/mongoose.h"

View File

@ -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<numberOfLeds; ++i) {
LEDState_init(i, LEDDefinition_get(i));
@ -56,16 +58,27 @@ void LEDStateEngine_tick() {
if (usedTime > 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) {

View File

@ -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;