2017-07-13 04:18:04 +00:00
|
|
|
load('api_config.js');
|
|
|
|
load('api_gpio.js');
|
|
|
|
load('api_mqtt.js');
|
2017-11-25 22:58:24 +00:00
|
|
|
load('api_net.js');
|
2017-07-13 04:18:04 +00:00
|
|
|
load('api_sys.js');
|
2017-07-13 09:31:50 +00:00
|
|
|
load('api_rpc.js');
|
2017-07-13 04:18:04 +00:00
|
|
|
load('api_timer.js');
|
2017-11-27 05:47:27 +00:00
|
|
|
load("api_math.js");
|
|
|
|
load("api_neopixel.js");
|
2017-11-25 22:58:24 +00:00
|
|
|
|
2017-07-13 04:18:04 +00:00
|
|
|
|
|
|
|
// Helper C function get_led_gpio_pin() in src/main.c returns built-in LED GPIO
|
2017-11-27 05:47:27 +00:00
|
|
|
let onBoardLed = ffi('int get_led_gpio_pin(void)')();
|
2017-11-25 22:58:24 +00:00
|
|
|
let button = Cfg.get('pins.button');
|
|
|
|
let topic = '/devices/' + Cfg.get('device.id') + '/events';
|
|
|
|
|
2017-11-27 05:47:27 +00:00
|
|
|
print('LED GPIO:', onBoardLed, 'button GPIO:', button);
|
2017-07-13 04:18:04 +00:00
|
|
|
|
|
|
|
let getInfo = function() {
|
2017-11-25 22:58:24 +00:00
|
|
|
return JSON.stringify({
|
|
|
|
total_ram: Sys.total_ram(),
|
|
|
|
free_ram: Sys.free_ram()
|
|
|
|
});
|
2017-07-13 04:18:04 +00:00
|
|
|
};
|
|
|
|
|
2017-11-25 22:58:24 +00:00
|
|
|
// Blink built-in LED every second
|
2017-11-27 05:47:27 +00:00
|
|
|
/*
|
|
|
|
GPIO.set_mode(onBoardLed, GPIO.MODE_OUTPUT);
|
|
|
|
Timer.set(5000, true, function() {
|
|
|
|
let value = GPIO.toggle(onBoardLed);
|
2017-11-25 22:58:24 +00:00
|
|
|
print(value ? 'Tick' : 'Tock', 'uptime:', Sys.uptime(), getInfo());
|
|
|
|
}, null);
|
2017-11-27 05:47:27 +00:00
|
|
|
*/
|
2017-11-25 22:58:24 +00:00
|
|
|
|
2017-07-13 04:18:04 +00:00
|
|
|
// Publish to MQTT topic on a button press. Button is wired to GPIO pin 0
|
2017-11-25 22:58:24 +00:00
|
|
|
/*
|
|
|
|
GPIO.set_button_handler(button, GPIO.PULL_UP, GPIO.INT_EDGE_NEG, 200, function() {
|
2017-07-13 04:18:04 +00:00
|
|
|
let message = getInfo();
|
|
|
|
let ok = MQTT.pub(topic, message, 1);
|
2017-11-25 22:58:24 +00:00
|
|
|
print('Published:', ok, topic, '->', message);
|
2017-07-13 04:18:04 +00:00
|
|
|
}, null);
|
2017-11-25 22:58:24 +00:00
|
|
|
*/
|
2017-07-13 04:18:04 +00:00
|
|
|
|
2017-11-25 22:58:24 +00:00
|
|
|
// Monitor network connectivity.
|
|
|
|
Net.setStatusEventHandler(function(ev, arg) {
|
|
|
|
let evs = '???';
|
|
|
|
if (ev === Net.STATUS_DISCONNECTED) {
|
|
|
|
evs = 'DISCONNECTED';
|
|
|
|
} else if (ev === Net.STATUS_CONNECTING) {
|
|
|
|
evs = 'CONNECTING';
|
|
|
|
} else if (ev === Net.STATUS_CONNECTED) {
|
|
|
|
evs = 'CONNECTED';
|
|
|
|
} else if (ev === Net.STATUS_GOT_IP) {
|
|
|
|
evs = 'GOT_IP';
|
|
|
|
}
|
|
|
|
print('== Net event:', ev, evs);
|
|
|
|
}, null);
|
|
|
|
|
|
|
|
// Initialize LED controller
|
|
|
|
let addColor = ffi('void addColor(char *,int,int,int)');
|
|
|
|
let addLedDefinition = ffi('void addLedDefinition(char *, char *, char *, int, int, int)');
|
|
|
|
let addAnimationStep = ffi('void addAnimationStep(int, int, int)');
|
|
|
|
let LEDMode_on=1, LEDMode_off=2, LEDMode_blink=3, LEDMode_tv=4, LEDMode_fire=5;
|
|
|
|
let LEDStateEngine_start = ffi('void startLEDStateEngine(void)');
|
|
|
|
let LEDStateEngine_pause = ffi('void pauseLEDStateEngine(void)');
|
2017-11-27 05:47:27 +00:00
|
|
|
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 pin = Cfg.get('led.pin'), numPixels = Cfg.get('led.count'), colorOrder = NeoPixel.GRB;
|
|
|
|
let strip = NeoPixel.create(pin, numPixels, colorOrder);
|
|
|
|
|
|
|
|
/* Create test pattern */
|
|
|
|
function createLedTestPattern() {
|
|
|
|
let i;
|
|
|
|
let switchMod;
|
|
|
|
for (i=0; i<numPixels; ++i) {
|
|
|
|
switchMod = i - 6 * Math.floor(i/6);
|
|
|
|
if (switchMod === 0) strip.setPixel(i, 100, 100, 100);
|
|
|
|
if (switchMod === 1) strip.setPixel(i, 100, 100, 100);
|
|
|
|
if (switchMod === 2) strip.setPixel(i, 100, 100, 0);
|
|
|
|
if (switchMod === 3) strip.setPixel(i, 0, 100, 0);
|
|
|
|
if (switchMod === 4) strip.setPixel(i, 0, 100, 100);
|
|
|
|
if (switchMod === 5) strip.setPixel(i, 0, 0, 100);
|
|
|
|
if (switchMod < 0 || switchMod > 5) print("WRONG -- should never reach this in switch statement!");
|
|
|
|
/*
|
|
|
|
switch (switchMod) {
|
|
|
|
case 0:
|
|
|
|
strip.setPixel(i, 100, 100, 100);
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
strip.setPixel(i, 100, 0, 0);
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
strip.setPixel(i, 100, 100, 0);
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
strip.setPixel(i, 0, 100, 0);
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
strip.setPixel(i, 0, 100, 100);
|
|
|
|
break;
|
|
|
|
case 5:
|
|
|
|
strip.setPixel(i, 0, 0, 100);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
print("WRONG -- should never reach this in switch statement!");
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
// strip.setPixel(i, 100, 100, 100);
|
|
|
|
}
|
|
|
|
strip.show();
|
|
|
|
}
|
|
|
|
|
|
|
|
function allLedOn() {
|
|
|
|
let i;
|
|
|
|
for (i=0; i<numPixels; ++i) {
|
|
|
|
strip.setPixel(i, 100, 100, 100);
|
|
|
|
}
|
|
|
|
strip.show();
|
|
|
|
}
|
2017-07-13 04:18:04 +00:00
|
|
|
|
2017-11-27 05:47:27 +00:00
|
|
|
function allLedOff() {
|
|
|
|
strip.clear();
|
|
|
|
strip.show();
|
|
|
|
}
|
2017-07-13 04:18:04 +00:00
|
|
|
|
2017-11-27 05:47:27 +00:00
|
|
|
|
|
|
|
// initialize LEDs
|
|
|
|
allLedOff();
|
|
|
|
|
|
|
|
print('End of initialization:');
|
|
|
|
print('LedPin:', pin);
|
|
|
|
print('NumLEDs:', numPixels);
|
|
|
|
print('Ticks:', getTicks());
|
|
|
|
let i;
|
|
|
|
print('LED', 'Color', 'R', 'G', 'B', 'Tick', 'Level', 'Room', 'Id');
|
|
|
|
print('---', '-----', '---', '---', '-----', '---', '-----', '----', '--');
|
|
|
|
for (i=0; i<numPixels; ++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), LEDDefinition_getOnColorRed(i), LEDDefinition_getOnColorGreen(i), LEDDefinition_getOnColorBlue(i), LEDState_getCurrentTick(i), LEDDefinition_getLevel(i), LEDDefinition_getRoom(i), LEDDefinition_getId(i))
|
|
|
|
}
|
|
|
|
Timer.set(500 /* 0,5 sec */, false /* repeat */, function() {
|
|
|
|
createLedTestPattern();
|
|
|
|
print('Created LED test pattern, uptime:', Sys.uptime(), getInfo());
|
|
|
|
LEDStateEngine_start();
|
|
|
|
Timer.set(100, true, function () {
|
|
|
|
let i;
|
|
|
|
for (i=0; i<numPixels; ++i) {
|
|
|
|
strip.setPixel(i, LEDState_getRed(i), LEDState_getGreen(i), LEDState_getBlue(i));
|
|
|
|
}
|
|
|
|
strip.show();
|
|
|
|
}, null);
|
|
|
|
}, null);
|
|
|
|
|
|
|
|
/*
|
|
|
|
Timer.set(30000, true, function () {
|
|
|
|
let i;
|
|
|
|
print('Ticks:', getTicks());
|
|
|
|
print('LED', 'Tick', 'Next');
|
|
|
|
print('---', '-----', '----');
|
|
|
|
for (i=0; i<numPixels; ++i) {
|
|
|
|
print(i, LEDState_getCurrentTick(i), LEDState_getNextTick(i));
|
|
|
|
}
|
|
|
|
}, null);
|
|
|
|
*/
|