Stabilized new implementation and trimmed to maximum useful speed.

This commit is contained in:
Dirk Jahnke 2017-12-07 15:45:35 +01:00
parent 94e5dce045
commit 7fad229821
6 changed files with 70 additions and 91 deletions

View File

@ -1,16 +1,16 @@
[ [
{"led": 0, "mode": "on", "ticks": 2}, {"led": 0, "mode": "on", "ticks": 4},
{"led": 0, "mode": "off", "ticks": 2}, {"led": 0, "mode": "off", "ticks": 4},
{"led": 1, "mode": "on", "ticks": 4}, {"led": 1, "mode": "on", "ticks": 8},
{"led": 1, "mode": "off", "ticks": 4}, {"led": 1, "mode": "off", "ticks": 8},
{"led": 2, "mode": "on", "ticks": 8}, {"led": 2, "mode": "on", "ticks": 16},
{"led": 2, "mode": "off", "ticks": 8}, {"led": 2, "mode": "off", "ticks": 16},
{"led": 3, "mode": "on", "ticks": 16}, {"led": 3, "mode": "on", "ticks": 32},
{"led": 3, "mode": "off", "ticks": 16}, {"led": 3, "mode": "off", "ticks": 32},
{"led": 4, "mode": "on", "ticks": 32}, {"led": 4, "mode": "on", "ticks": 64},
{"led": 4, "mode": "off", "ticks": 32}, {"led": 4, "mode": "off", "ticks": 64},
{"led": 5, "mode": "on", "ticks": 64}, {"led": 5, "mode": "on", "ticks": 128},
{"led": 5, "mode": "off", "ticks": 64}, {"led": 5, "mode": "off", "ticks": 128},
{"led": 6, "mode": "on", "ticks": 128}, {"led": 6, "mode": "on", "ticks": 256},
{"led": 6, "mode": "off", "ticks": 128} {"led": 6, "mode": "off", "ticks": 256}
] ]

27
fs/api_dlite.js Normal file
View File

@ -0,0 +1,27 @@
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)');
let LEDMode_on=1, LEDMode_off=2, LEDMode_blink=3, LEDMode_tv=4, LEDMode_fire=5;
let LEDStateEngine_init = ffi('void LEDStateEngine_init(int, int)');
let LEDStateEngine_start = ffi('void startLEDStateEngine(void)');
let LEDStateEngine_pause = ffi('void pauseLEDStateEngine(void)');
let LEDState_getRed = ffi('int LEDState_getLedRed(int)');
let LEDState_getGreen = ffi('int LEDState_getLedGreen(int)');
let LEDState_getBlue = ffi('int LEDState_getLedBlue(int)');
let LEDState_getColorName = ffi('char * LEDState_getLedColorName(int)');
let LEDState_getNextTick = ffi('int LEDState_getNextTick(int)');
let LEDState_getCurrentTick = ffi('int LEDState_getCurrentTick(int)');
let LEDDefinition_getLevel = ffi('char *LEDDefinition_getLevel(int)');
let LEDDefinition_getRoom = ffi('char *LEDDefinition_getRoom(int)');
let LEDDefinition_getId = ffi('char *LEDDefinition_getId(int)');
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 NeoPixel_show = ffi('void NeoPixel_show(void)');
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)');

View File

@ -5,13 +5,10 @@ load('api_net.js');
load('api_sys.js'); 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_file.js');
load("api_file.js"); load('api_dlite.js');
load("api_neopixel.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 onBoardLed = ffi('int get_led_gpio_pin(void)')();
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';
@ -58,41 +55,12 @@ Net.setStatusEventHandler(function(ev, arg) {
print('==> NET:', ev, evs); print('==> NET:', ev, evs);
}, null); }, null);
// Initialize LED controller
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)');
let LEDMode_on=1, LEDMode_off=2, LEDMode_blink=3, LEDMode_tv=4, LEDMode_fire=5;
let LEDStateEngine_init = ffi('void LEDStateEngine_init(int, int)');
let LEDStateEngine_start = ffi('void startLEDStateEngine(void)');
let LEDStateEngine_pause = ffi('void pauseLEDStateEngine(void)');
let LEDState_getRed = ffi('int LEDState_getLedRed(int)');
let LEDState_getGreen = ffi('int LEDState_getLedGreen(int)');
let LEDState_getBlue = ffi('int LEDState_getLedBlue(int)');
let LEDState_getColorName = ffi('char * LEDState_getLedColorName(int)');
let LEDState_getNextTick = ffi('int LEDState_getNextTick(int)');
let LEDState_getCurrentTick = ffi('int LEDState_getCurrentTick(int)');
let LEDDefinition_getLevel = ffi('char *LEDDefinition_getLevel(int)');
let LEDDefinition_getRoom = ffi('char *LEDDefinition_getRoom(int)');
let LEDDefinition_getId = ffi('char *LEDDefinition_getId(int)');
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 NeoPixel_show = ffi('void NeoPixel_show(void)');
let NeoPixel_clear = ffi('void NeoPixel_clear(void)');
let NeoPixel_set = ffi('void NeoPixel_set(int,int,int,int)');
let pin = Cfg.get('led.pin'); let pin = Cfg.get('led.pin');
let configNumLeds = Cfg.get('led.count'); let configNumLeds = Cfg.get('led.count');
let colorOrder = NeoPixel.GRB;
//let strip = NeoPixel.create(pin, configNumLeds, colorOrder);
let colorFile = Cfg.get('led.colorFile'); let colorFile = Cfg.get('led.colorFile');
let animationFile = Cfg.get('led.animationFile'); 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 updateCycle = Cfg.get('led.updateCycle');
let tickDuration = Cfg.get('led.tickDuration'); let tickDuration = Cfg.get('led.tickDuration');
let brightnessAdjustment = Cfg.get('led.brightness'); 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
@ -103,12 +71,6 @@ function showLedTestPattern() {
let switchMod; let switchMod;
for (i=0; i<configNumLeds; ++i) { for (i=0; i<configNumLeds; ++i) {
switchMod = i - 6 * Math.floor(i/6); switchMod = i - 6 * Math.floor(i/6);
/*if (switchMod === 0) strip.setPixel(i, 10, 10, 10);
if (switchMod === 1) strip.setPixel(i, 10, 10, 10);
if (switchMod === 2) strip.setPixel(i, 10, 100, 0);
if (switchMod === 3) strip.setPixel(i, 0, 10, 0);
if (switchMod === 4) strip.setPixel(i, 0, 10, 10);
if (switchMod === 5) strip.setPixel(i, 0, 0, 10);*/
if (switchMod === 0) NeoPixel_set(i, 10, 10, 10); if (switchMod === 0) NeoPixel_set(i, 10, 10, 10);
if (switchMod === 1) NeoPixel_set(i, 10, 10, 10); if (switchMod === 1) NeoPixel_set(i, 10, 10, 10);
if (switchMod === 2) NeoPixel_set(i, 10, 100, 0); if (switchMod === 2) NeoPixel_set(i, 10, 100, 0);
@ -117,24 +79,19 @@ function showLedTestPattern() {
if (switchMod === 5) NeoPixel_set(i, 0, 0, 10); if (switchMod === 5) NeoPixel_set(i, 0, 0, 10);
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!");
} }
//strip.show();
NeoPixel_show(); NeoPixel_show();
} }
function allLedOn() { function allLedOn() {
let i; let i;
for (i=0; i<configNumLeds; ++i) { for (i=0; i<configNumLeds; ++i) {
//strip.setPixel(i, 10, 10, 10);
NeoPixel_set(i, 10, 10, 10); NeoPixel_set(i, 10, 10, 10);
} }
//strip.show();
NeoPixel_show(); NeoPixel_show();
} }
function allLedOff() { function allLedOff() {
//strip.clear();
NeoPixel_clear(); NeoPixel_clear();
//strip.show();
NeoPixel_show(); NeoPixel_show();
} }
@ -203,8 +160,6 @@ function loadAnimDefs() {
} }
function initialize() { function initialize() {
// initialize LEDs
// ===============
print('***** Start initialization', getInfo()); print('***** Start initialization', getInfo());
let i; let i;
@ -216,7 +171,6 @@ function initialize() {
loadColorDefs(); loadColorDefs();
loadLedDefs(); loadLedDefs();
loadAnimDefs(); loadAnimDefs();
// Initialize LED State Engine // Initialize LED State Engine
LEDStateEngine_init(pin, numberOfLeds); LEDStateEngine_init(pin, numberOfLeds);
} }
@ -227,7 +181,6 @@ 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('LED Update Cycle:', updateCycle, 'ms');
print('Brightness:', brightnessAdjustment, '%'); print('Brightness:', brightnessAdjustment, '%');
print('LED', 'Color', 'R', 'G', 'B', 'Tick', 'Level', 'Room', 'Id'); print('LED', 'Color', 'R', 'G', 'B', 'Tick', 'Level', 'Room', 'Id');
print('---', '-----', '---', '---', '-----', '---', '-----', '----', '--'); print('---', '-----', '---', '---', '-----', '---', '-----', '----', '--');
@ -237,34 +190,22 @@ function initialize() {
} }
} }
Timer.set(3000, false, function() { Timer.set(1500, false, function() {
initialize(); initialize();
allLedOn(); allLedOn();
print('***** Starting timer to delay test pattern generation'); print('***** Starting timer to delay test pattern generation');
/*print('***** Wait 1 second for other tasks to startup', getInfo());
Timer.set(1000, false, function() {
Timer.set(1000, false, function() {*/
allLedOff(); allLedOff();
print('***** LED test pattern', getInfo()); print('***** LED test pattern', getInfo());
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(updateCycle, true, function () { print("Timer: minTickTime =", LEDStateEngine_getMinTickTime(), ", maxTickTime =", LEDStateEngine_getMaxTickTime());
let i; }, null);
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);
print('***** End of init.js, sub tasks still running', getInfo());
/* ------ RPC Handlers ------- */ /* ------ RPC Handlers ------- */
print('**** Adding RPC handlers');
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) {
@ -352,3 +293,5 @@ RPC.addHandler('led.putAnimations', function(args) {
return { error: 'animations: {led, mode, ticks} is required' }; return { error: 'animations: {led, mode, ticks} is required' };
} }
}, null); }, null);
print('***** End of init.js, sub tasks still running', getInfo());

View File

@ -33,9 +33,8 @@ config_schema:
- ["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", false, {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", 7, {title: "Number of LEDs connected in the chain"}]
- ["led.updateCycle", "i", 250, {title: "Frequency of LED update in ms"}] - ["led.tickDuration", "i", 25, {title: "Timebase: Duration of a tick in ms"}]
- ["led.tickDuration", "i", 250, {title: "Timebase: Duration of a tick in ms"}]
- ["led.brightness", "i", 20, {title: "Default brightness in % (1-100)"}] - ["led.brightness", "i", 20, {title: "Default brightness in % (1-100)"}]
- ["led.colorFile", "s", "colors.cfg", {title: "File name containing color definitions"}] - ["led.colorFile", "s", "colors.cfg", {title: "File name containing color definitions"}]
- ["led.lampsFile", "s", "lamps.cfg", {title: "File name containing lamp definitions"}] - ["led.lampsFile", "s", "lamps.cfg", {title: "File name containing lamp definitions"}]

View File

@ -1,7 +1,7 @@
/* LEDState.c */ /* LEDState.c */
#include "mgos_sys_config.h" #include "mgos_sys_config.h"
#include "mgos_timers.h"
#include "LEDDefinition.h" #include "LEDDefinition.h"
#include "LEDState.h" #include "LEDState.h"
#include "AnimationConfig.h" #include "AnimationConfig.h"
@ -40,23 +40,30 @@ uint16_t LEDStateEngine_getNumberOfLeds() {
return theLEDStateEngine.numberOfLeds; return theLEDStateEngine.numberOfLeds;
} }
static double minTickTime = 99999.99;
static double maxTickTime = 0;
void LEDStateEngine_tick() { void LEDStateEngine_tick() {
double startTime = mgos_uptime();
++ticks; ++ticks;
// LOG(LL_INFO, ("# %d/%d", theLEDStateEngine.numberOfLeds, ticks)); // LOG(LL_INFO, ("# %d/%d", theLEDStateEngine.numberOfLeds, ticks));
for (int i=0; i<theLEDStateEngine.numberOfLeds; ++i) { for (int i=0; i<theLEDStateEngine.numberOfLeds; ++i) {
LEDState_tick(i); LEDState_tick(i);
} }
NeoPixel_show(); NeoPixel_show();
double endTime = mgos_uptime();
double usedTime = endTime - startTime;
if (usedTime > maxTickTime) maxTickTime = usedTime;
if (usedTime < minTickTime) minTickTime = usedTime;
} }
double LEDStateEngine_getMinTickTime() { return minTickTime; }
double LEDStateEngine_getMaxTickTime() { return 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 * mgos_sys_config_get_led_brightness()/100);
} }
static void updateLedDisplay(LEDState *state) { static void updateLedDisplay(LEDState *state) {
// strip.setPixel(i, adjustBrightness(LEDState_getRed(i)), adjustBrightness(LEDState_getGreen(i)), adjustBrightness(LEDState_getBlue(i)));
// strip.setPixel(i, adjustBrightness(LEDState_getRed(i)), adjustBrightness(LEDState_getGreen(i)), adjustBrightness(LEDState_getBlue(i)));
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));
} }

View File

@ -38,6 +38,9 @@ extern void LEDStateEngine_addComment(char *comment);
extern LEDState *LEDStateEngine_getLedState(int n); extern LEDState *LEDStateEngine_getLedState(int n);
extern void LEDStateEngine_tick(); extern void LEDStateEngine_tick();
extern uint16_t LEDStateEngine_getNumberOfLeds(); extern uint16_t LEDStateEngine_getNumberOfLeds();
extern double LEDStateEngine_getMinTickTime();
extern double LEDStateEngine_getMaxTickTime();
/* LEDState */ /* LEDState */
extern void LEDState_init(int n, LEDDefinition *ledDefinition); extern void LEDState_init(int n, LEDDefinition *ledDefinition);