Finalized on/off-animation mode
This commit is contained in:
parent
7fad229821
commit
626fe42a90
|
@ -1,4 +1,3 @@
|
||||||
let onBoardLed = ffi('int get_led_gpio_pin(void)')();
|
|
||||||
let addColor = ffi('void addColor(char *,int,int,int)');
|
let addColor = ffi('void addColor(char *,int,int,int)');
|
||||||
let LEDDefinition_addByName = ffi('void LEDDefinition_addByName(char *, char *, char *, char *)');
|
let LEDDefinition_addByName = ffi('void LEDDefinition_addByName(char *, char *, char *, char *)');
|
||||||
let addAnimationStep = ffi('void addAnimationStep(int, char *, int)');
|
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 NeoPixel_set = ffi('void NeoPixel_set(int,int,int,int)');
|
||||||
let LEDStateEngine_getMinTickTime = ffi('double LEDStateEngine_getMinTickTime(void)');
|
let LEDStateEngine_getMinTickTime = ffi('double LEDStateEngine_getMinTickTime(void)');
|
||||||
let LEDStateEngine_getMaxTickTime = ffi('double LEDStateEngine_getMaxTickTime(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)');
|
||||||
|
|
53
fs/init.js
53
fs/init.js
|
@ -9,10 +9,11 @@ load('api_file.js');
|
||||||
load('api_dlite.js');
|
load('api_dlite.js');
|
||||||
|
|
||||||
// Helper C function get_led_gpio_pin() in src/main.c returns built-in LED GPIO
|
// 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 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() {
|
let getInfo = function() {
|
||||||
return JSON.stringify({
|
return JSON.stringify({
|
||||||
|
@ -62,7 +63,6 @@ let animationFile = Cfg.get('led.animationFile');
|
||||||
let lampsFile = Cfg.get('led.lampsFile');
|
let lampsFile = Cfg.get('led.lampsFile');
|
||||||
let useDefaults = Cfg.get('led.useDefaults');
|
let useDefaults = Cfg.get('led.useDefaults');
|
||||||
let tickDuration = Cfg.get('led.tickDuration');
|
let tickDuration = Cfg.get('led.tickDuration');
|
||||||
let brightnessAdjustment = Cfg.get('led.brightness');
|
|
||||||
let numberOfLeds = configNumLeds, numberOfLedDefs = 0; // from config files, count led definition entries
|
let numberOfLeds = configNumLeds, numberOfLedDefs = 0; // from config files, count led definition entries
|
||||||
|
|
||||||
/* Create test pattern */
|
/* Create test pattern */
|
||||||
|
@ -95,10 +95,6 @@ function allLedOff() {
|
||||||
NeoPixel_show();
|
NeoPixel_show();
|
||||||
}
|
}
|
||||||
|
|
||||||
function adjustBrightness(value) {
|
|
||||||
return Math.floor(value * brightnessAdjustment/100);
|
|
||||||
}
|
|
||||||
|
|
||||||
function loadColorDefs() {
|
function loadColorDefs() {
|
||||||
// Load Color definitions
|
// Load Color definitions
|
||||||
let json = File.read(colorFile);
|
let json = File.read(colorFile);
|
||||||
|
@ -181,16 +177,23 @@ function initialize() {
|
||||||
print('NumLEDs:', numberOfLeds);
|
print('NumLEDs:', numberOfLeds);
|
||||||
print('Ticks:', getTicks());
|
print('Ticks:', getTicks());
|
||||||
print('Tick duration:', tickDuration, 'ms');
|
print('Tick duration:', tickDuration, 'ms');
|
||||||
print('Brightness:', brightnessAdjustment, '%');
|
print('Brightness:', LEDStateEngine_getBrightness(), '%');
|
||||||
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<numberOfLeds; ++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), 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();
|
initialize();
|
||||||
allLedOn();
|
allLedOn();
|
||||||
print('***** Starting timer to delay test pattern generation');
|
print('***** Starting timer to delay test pattern generation');
|
||||||
|
@ -199,18 +202,20 @@ Timer.set(1500, false, function() {
|
||||||
showLedTestPattern();
|
showLedTestPattern();
|
||||||
print('***** Start LED state engine and LED-update timer', getInfo());
|
print('***** Start LED state engine and LED-update timer', getInfo());
|
||||||
LEDStateEngine_start();
|
LEDStateEngine_start();
|
||||||
Timer.set(10000, true, function() {
|
Timer.set(30000, true, function() {
|
||||||
print("Timer: minTickTime =", LEDStateEngine_getMinTickTime(), ", maxTickTime =", LEDStateEngine_getMaxTickTime());
|
print("Timer: minTickTime =", LEDStateEngine_getMinTickTime(),
|
||||||
|
"ms, maxTickTime =", LEDStateEngine_getMaxTickTime(), "ms");
|
||||||
}, null);
|
}, null);
|
||||||
}, null);
|
}, null);
|
||||||
|
|
||||||
/* ------ RPC Handlers ------- */
|
/* ------ RPC Handlers ------- */
|
||||||
print('**** Adding RPC handlers');
|
print('**** Adding RPC handlers');
|
||||||
|
print(' led.setBrightness');
|
||||||
RPC.addHandler('led.setBrightness', function(args) {
|
RPC.addHandler('led.setBrightness', function(args) {
|
||||||
// print(args);
|
// print(args);
|
||||||
if (args !== undefined && args.level !== undefined) {
|
if (args !== undefined && args.level !== undefined) {
|
||||||
if (args.level > 0 && args.level <= 100) {
|
if (args.level > 0 && args.level <= 100) {
|
||||||
brightnessAdjustment = args.level;
|
LEDStateEngine_setBrightness(args.level);
|
||||||
return { result: 'ok' };
|
return { result: 'ok' };
|
||||||
} else {
|
} else {
|
||||||
return { error: 'Brightness level must be in the range 1..100' };
|
return { error: 'Brightness level must be in the range 1..100' };
|
||||||
|
@ -218,29 +223,33 @@ RPC.addHandler('led.setBrightness', function(args) {
|
||||||
} else {
|
} else {
|
||||||
return { error: 'level is required having a value in the range 1..100' };
|
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);
|
}, null);
|
||||||
|
print(' led.pause');
|
||||||
|
|
||||||
RPC.addHandler('led.pause', function(args) {
|
RPC.addHandler('led.pause', function(args) {
|
||||||
LEDStateEngine_pause();
|
LEDStateEngine_pause();
|
||||||
return { result: 'ok' };
|
return { result: 'ok' };
|
||||||
}, null);
|
}, null);
|
||||||
|
print(' led.run');
|
||||||
RPC.addHandler('led.run', function(args) {
|
RPC.addHandler('led.run', function(args) {
|
||||||
LEDStateEngine_start();
|
LEDStateEngine_start();
|
||||||
return { result: 'ok' };
|
return { result: 'ok' };
|
||||||
}, null);
|
}, null);
|
||||||
|
print(' led.getColors');
|
||||||
RPC.addHandler('led.getColors', function(args) {
|
RPC.addHandler('led.getColors', function(args) {
|
||||||
let colors = JSON.parse(File.read(colorFile));
|
let colors = JSON.parse(File.read(colorFile));
|
||||||
return { result: 'ok', colors: colors };
|
return { result: 'ok', colors: colors };
|
||||||
}, null);
|
}, null);
|
||||||
|
print(' led.getLamps');
|
||||||
RPC.addHandler('led.getLamps', function(args) {
|
RPC.addHandler('led.getLamps', function(args) {
|
||||||
let lamps = JSON.parse(File.read(lampsFile));
|
let lamps = JSON.parse(File.read(lampsFile));
|
||||||
return { result: 'ok', lamps: lamps };
|
return { result: 'ok', lamps: lamps };
|
||||||
}, null);
|
}, null);
|
||||||
|
print(' led.getAnimations');
|
||||||
RPC.addHandler('led.getAnimations', function(args) {
|
RPC.addHandler('led.getAnimations', function(args) {
|
||||||
let anims = JSON.parse(File.read(animationFile));
|
let anims = JSON.parse(File.read(animationFile));
|
||||||
return { result: 'ok', animations: anims };
|
return { result: 'ok', animations: anims };
|
||||||
|
@ -266,7 +275,7 @@ function recoverFile(filename) {
|
||||||
File.rename(filename, newerFilename);
|
File.rename(filename, newerFilename);
|
||||||
File.rename(backupFilename, filename);
|
File.rename(backupFilename, filename);
|
||||||
}
|
}
|
||||||
|
print(' led.putColors');
|
||||||
RPC.addHandler('led.putColors', function(args) {
|
RPC.addHandler('led.putColors', function(args) {
|
||||||
if (args !== undefined && args.colors !== undefined) {
|
if (args !== undefined && args.colors !== undefined) {
|
||||||
saveFile(colorFile, args.colors);
|
saveFile(colorFile, args.colors);
|
||||||
|
@ -275,7 +284,7 @@ RPC.addHandler('led.putColors', function(args) {
|
||||||
return { error: 'colors: {name, red, green, blue} is required' };
|
return { error: 'colors: {name, red, green, blue} is required' };
|
||||||
}
|
}
|
||||||
}, null);
|
}, null);
|
||||||
|
print(' led.putLamps');
|
||||||
RPC.addHandler('led.putLamps', function(args) {
|
RPC.addHandler('led.putLamps', function(args) {
|
||||||
if (args !== undefined && args.lamps !== undefined) {
|
if (args !== undefined && args.lamps !== undefined) {
|
||||||
saveFile(lampsFile, args.lamps);
|
saveFile(lampsFile, args.lamps);
|
||||||
|
@ -284,7 +293,7 @@ RPC.addHandler('led.putLamps', function(args) {
|
||||||
return { error: 'lamps: {level, room, id, onColor} is required' };
|
return { error: 'lamps: {level, room, id, onColor} is required' };
|
||||||
}
|
}
|
||||||
}, null);
|
}, null);
|
||||||
|
print(' led.putAnimations');
|
||||||
RPC.addHandler('led.putAnimations', function(args) {
|
RPC.addHandler('led.putAnimations', function(args) {
|
||||||
if (args !== undefined && args.animations !== undefined) {
|
if (args !== undefined && args.animations !== undefined) {
|
||||||
saveFile(animationFile, args.animations);
|
saveFile(animationFile, args.animations);
|
||||||
|
|
|
@ -4,7 +4,7 @@
|
||||||
#define AnimationConfig_loaded
|
#define AnimationConfig_loaded
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include "fw/src/mgos.h"
|
#include "mgos.h"
|
||||||
#include "common/cs_dbg.h"
|
#include "common/cs_dbg.h"
|
||||||
#include "mjs.h"
|
#include "mjs.h"
|
||||||
#include "LEDDefinition.h"
|
#include "LEDDefinition.h"
|
||||||
|
|
|
@ -4,7 +4,7 @@
|
||||||
#define LEDDefinition_included
|
#define LEDDefinition_included
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include "fw/src/mgos.h"
|
#include "mgos.h"
|
||||||
#include "mjs.h"
|
#include "mjs.h"
|
||||||
#include "mongoose/mongoose.h"
|
#include "mongoose/mongoose.h"
|
||||||
|
|
||||||
|
|
|
@ -9,6 +9,7 @@
|
||||||
|
|
||||||
static LEDStateEngine theLEDStateEngine;
|
static LEDStateEngine theLEDStateEngine;
|
||||||
static int ticks = 0;
|
static int ticks = 0;
|
||||||
|
static int brightness = 50;
|
||||||
|
|
||||||
int getTicks(void) { return ticks; }
|
int getTicks(void) { return ticks; }
|
||||||
|
|
||||||
|
@ -26,6 +27,7 @@ void LEDStateEngine_setNumberOfLeds(int numberOfLeds) {
|
||||||
void LEDStateEngine_init(int ledPin, int numberOfLeds) {
|
void LEDStateEngine_init(int ledPin, int numberOfLeds) {
|
||||||
theLEDStateEngine.comment = "";
|
theLEDStateEngine.comment = "";
|
||||||
theLEDStateEngine.pin = ledPin;
|
theLEDStateEngine.pin = ledPin;
|
||||||
|
brightness = mgos_sys_config_get_led_brightness();
|
||||||
LEDStateEngine_setNumberOfLeds(numberOfLeds);
|
LEDStateEngine_setNumberOfLeds(numberOfLeds);
|
||||||
for (int i=0; i<numberOfLeds; ++i) {
|
for (int i=0; i<numberOfLeds; ++i) {
|
||||||
LEDState_init(i, LEDDefinition_get(i));
|
LEDState_init(i, LEDDefinition_get(i));
|
||||||
|
@ -56,16 +58,27 @@ void LEDStateEngine_tick() {
|
||||||
if (usedTime > maxTickTime) maxTickTime = usedTime;
|
if (usedTime > maxTickTime) maxTickTime = usedTime;
|
||||||
if (usedTime < minTickTime) minTickTime = usedTime;
|
if (usedTime < minTickTime) minTickTime = usedTime;
|
||||||
}
|
}
|
||||||
double LEDStateEngine_getMinTickTime() { return minTickTime; }
|
double LEDStateEngine_getMinTickTime() { return 1000 * minTickTime; }
|
||||||
double LEDStateEngine_getMaxTickTime() { return maxTickTime; }
|
double LEDStateEngine_getMaxTickTime() { return 1000 * maxTickTime; }
|
||||||
|
|
||||||
static uint8_t adjustBrightness(uint8_t value) {
|
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) {
|
static void updateLedDisplay(LEDState *state) {
|
||||||
NeoPixel_set(state->index,
|
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) {
|
void LEDState_tick(int ledNum) {
|
||||||
|
|
|
@ -7,8 +7,8 @@
|
||||||
#include "mgos_sys_config.h"
|
#include "mgos_sys_config.h"
|
||||||
#include "mgos_system.h"
|
#include "mgos_system.h"
|
||||||
#include "mgos_timers.h"
|
#include "mgos_timers.h"
|
||||||
#include "fw/src/mgos_hal.h"
|
#include "mgos_hal.h"
|
||||||
#include "fw/src/mgos_dlsym.h"
|
#include "mgos_dlsym.h"
|
||||||
#include "mjs.h"
|
#include "mjs.h"
|
||||||
#include "LEDDefinition.h"
|
#include "LEDDefinition.h"
|
||||||
#include "AnimationConfig.h"
|
#include "AnimationConfig.h"
|
||||||
|
@ -35,7 +35,7 @@ int get_led_gpio_pin(void) {
|
||||||
return LED_GPIO;
|
return LED_GPIO;
|
||||||
}
|
}
|
||||||
|
|
||||||
LEDStateEngine ledStateEngine;
|
// static LEDStateEngine ledStateEngine;
|
||||||
static bool stateEngineRunning = false;
|
static bool stateEngineRunning = false;
|
||||||
static bool pausedReported = false;
|
static bool pausedReported = false;
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue