Refactoring completed: NeoPixel implementation in C instead of js
This commit is contained in:
157
fs/init.js
157
fs/init.js
@@ -60,10 +60,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 *, char *)');
|
||||
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)');
|
||||
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)');
|
||||
@@ -80,11 +80,14 @@ 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 numPixels = Cfg.get('led.count');
|
||||
let configNumLeds = Cfg.get('led.count');
|
||||
let colorOrder = NeoPixel.GRB;
|
||||
let strip = NeoPixel.create(pin, numPixels, colorOrder);
|
||||
//let strip = NeoPixel.create(pin, configNumLeds, colorOrder);
|
||||
let colorFile = Cfg.get('led.colorFile');
|
||||
let animationFile = Cfg.get('led.animationFile');
|
||||
let lampsFile = Cfg.get('led.lampsFile');
|
||||
@@ -92,53 +95,54 @@ let useDefaults = Cfg.get('led.useDefaults');
|
||||
let updateCycle = Cfg.get('led.updateCycle');
|
||||
let tickDuration = Cfg.get('led.tickDuration');
|
||||
let brightnessAdjustment = Cfg.get('led.brightness');
|
||||
let numberOfLeds = 0; // from config files, count led definition entries
|
||||
let numberOfLeds = configNumLeds, numberOfLedDefs = 0; // from config files, count led definition entries
|
||||
|
||||
/* Create test pattern */
|
||||
function createLedTestPattern() {
|
||||
function showLedTestPattern() {
|
||||
let i;
|
||||
let switchMod;
|
||||
for (i=0; i<numPixels; ++i) {
|
||||
for (i=0; i<configNumLeds; ++i) {
|
||||
switchMod = i - 6 * Math.floor(i/6);
|
||||
if (switchMod === 0) strip.setPixel(i, 10, 10, 10);
|
||||
/*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 === 5) strip.setPixel(i, 0, 0, 10);*/
|
||||
if (switchMod === 0) 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 === 3) NeoPixel_set(i, 0, 10, 0);
|
||||
if (switchMod === 4) NeoPixel_set(i, 0, 10, 10);
|
||||
if (switchMod === 5) NeoPixel_set(i, 0, 0, 10);
|
||||
if (switchMod < 0 || switchMod > 5) print("WRONG -- should never reach this in switch statement!");
|
||||
}
|
||||
strip.show();
|
||||
//strip.show();
|
||||
NeoPixel_show();
|
||||
}
|
||||
|
||||
function allLedOn() {
|
||||
let i;
|
||||
for (i=0; i<numPixels; ++i) {
|
||||
strip.setPixel(i, 10, 10, 10);
|
||||
for (i=0; i<configNumLeds; ++i) {
|
||||
//strip.setPixel(i, 10, 10, 10);
|
||||
NeoPixel_set(i, 10, 10, 10);
|
||||
}
|
||||
strip.show();
|
||||
//strip.show();
|
||||
NeoPixel_show();
|
||||
}
|
||||
|
||||
function allLedOff() {
|
||||
strip.clear();
|
||||
strip.show();
|
||||
//strip.clear();
|
||||
NeoPixel_clear();
|
||||
//strip.show();
|
||||
NeoPixel_show();
|
||||
}
|
||||
|
||||
function adjustBrightness(value) {
|
||||
return Math.floor(value * brightnessAdjustment/100);
|
||||
}
|
||||
|
||||
// initialize LEDs
|
||||
// ===============
|
||||
print('***** Start initialization', getInfo());
|
||||
let i;
|
||||
allLedOff();
|
||||
|
||||
if (useDefaults) {
|
||||
print('Using default color definition');
|
||||
numberOfLeds = numPixels;
|
||||
startLEDStateEngine();
|
||||
} else {
|
||||
function loadColorDefs() {
|
||||
// Load Color definitions
|
||||
let json = File.read(colorFile);
|
||||
let colors = [];
|
||||
@@ -155,7 +159,9 @@ if (useDefaults) {
|
||||
}
|
||||
json = null;
|
||||
// colors = null; // NO! Do not do this, as the strings are still referenced!
|
||||
}
|
||||
|
||||
function loadLedDefs() {
|
||||
// Load LED Definitions
|
||||
let json = File.read(lampsFile);
|
||||
let ledDef = [];
|
||||
@@ -165,15 +171,21 @@ if (useDefaults) {
|
||||
} else {
|
||||
ledDef = JSON.parse(json);
|
||||
}
|
||||
numberOfLedDefs = 0;
|
||||
for (i=0; i<ledDef.length; ++i) {
|
||||
++numberOfLeds;
|
||||
++numberOfLedDefs;
|
||||
print('- addLedDefinition', ledDef[i].level, ledDef[i].room, ledDef[i].id, ledDef[i].onColor);
|
||||
addLedDefinition(ledDef[i].level, ledDef[i].room, ledDef[i].id, ledDef[i].onColor);
|
||||
LEDDefinition_addByName(ledDef[i].level, ledDef[i].room, ledDef[i].id, ledDef[i].onColor);
|
||||
// printColor(ledDef[i].onColor);
|
||||
}
|
||||
if (numberOfLedDefs > configNumLeds) {
|
||||
print('WARNING: LED definition file contains more LEDs (', numberOfLedDefs, ') than configured in system-config (', configNumLeds, ')');
|
||||
}
|
||||
json = null;
|
||||
// ledDef = null; // NO! Do not do this, as the strings are still referenced!
|
||||
|
||||
}
|
||||
|
||||
function loadAnimDefs() {
|
||||
// Load Animation Definitions
|
||||
let json = File.read(animationFile);
|
||||
let animation = [];
|
||||
@@ -188,42 +200,65 @@ if (useDefaults) {
|
||||
addAnimationStep(animation[i].led, animation[i].mode, animation[i].ticks);
|
||||
}
|
||||
json = null;
|
||||
|
||||
// Initialize LED State Engine
|
||||
LEDStateEngine_init(numberOfLeds);
|
||||
}
|
||||
print('***** End of initialization', getInfo());
|
||||
print('_______________________________________________________');
|
||||
print('LedPin:', pin);
|
||||
print('NumLEDs:', numberOfLeds);
|
||||
print('Ticks:', getTicks());
|
||||
print('Tick duration:', tickDuration, 'ms');
|
||||
print('LED Update Cycle:', updateCycle, 'ms');
|
||||
print('Brightness:', brightnessAdjustment, '%');
|
||||
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)));
|
||||
}
|
||||
|
||||
allLedOn();
|
||||
print('***** Wait 1 second for other tasks to startup', getInfo());
|
||||
Timer.set(1000, false, function() {
|
||||
function initialize() {
|
||||
// initialize LEDs
|
||||
// ===============
|
||||
print('***** Start initialization', getInfo());
|
||||
let i;
|
||||
|
||||
if (useDefaults) {
|
||||
print('Using default color definition');
|
||||
numberOfLeds = configNumLeds;
|
||||
startLEDStateEngine();
|
||||
} else {
|
||||
loadColorDefs();
|
||||
loadLedDefs();
|
||||
loadAnimDefs();
|
||||
|
||||
// Initialize LED State Engine
|
||||
LEDStateEngine_init(pin, numberOfLeds);
|
||||
}
|
||||
allLedOff();
|
||||
print('***** LED test pattern', getInfo());
|
||||
print('***** End of initialization', getInfo());
|
||||
print('_______________________________________________________');
|
||||
print('LedPin:', pin);
|
||||
print('NumLEDs:', numberOfLeds);
|
||||
print('Ticks:', getTicks());
|
||||
print('Tick duration:', tickDuration, 'ms');
|
||||
print('LED Update Cycle:', updateCycle, 'ms');
|
||||
print('Brightness:', brightnessAdjustment, '%');
|
||||
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)));
|
||||
}
|
||||
}
|
||||
|
||||
Timer.set(3000, false, function() {
|
||||
initialize();
|
||||
allLedOn();
|
||||
print('***** Starting timer to delay test pattern generation');
|
||||
/*print('***** Wait 1 second for other tasks to startup', getInfo());
|
||||
Timer.set(1000, false, function() {
|
||||
createLedTestPattern();
|
||||
print('***** Start LED state engine and LED-update timer', getInfo());
|
||||
LEDStateEngine_start();
|
||||
Timer.set(updateCycle, true, function () {
|
||||
let i;
|
||||
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);
|
||||
Timer.set(1000, false, function() {*/
|
||||
allLedOff();
|
||||
print('***** LED test pattern', getInfo());
|
||||
showLedTestPattern();
|
||||
print('***** Start LED state engine and LED-update timer', getInfo());
|
||||
LEDStateEngine_start();
|
||||
/*
|
||||
Timer.set(updateCycle, true, function () {
|
||||
let i;
|
||||
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);
|
||||
print('***** End of init.js, sub tasks still running', getInfo());
|
||||
|
||||
|
Reference in New Issue
Block a user