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-25 22:58:24 +00:00
|
|
|
|
|
|
|
let led = Cfg.get('pins.led');
|
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-25 22:58:24 +00:00
|
|
|
// let onBoardLed = ffi('int get_led_gpio_pin()')();
|
|
|
|
|
|
|
|
let button = Cfg.get('pins.button');
|
|
|
|
let topic = '/devices/' + Cfg.get('device.id') + '/events';
|
|
|
|
|
|
|
|
print('LED GPIO:', led, '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
|
|
|
|
GPIO.set_mode(led, GPIO.MODE_OUTPUT);
|
|
|
|
Timer.set(5000 /* 1 sec */, true /* repeat */, function() {
|
|
|
|
let value = GPIO.toggle(led);
|
|
|
|
print(value ? 'Tick' : 'Tock', 'uptime:', Sys.uptime(), getInfo());
|
|
|
|
}, null);
|
|
|
|
|
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-07-13 04:18:04 +00:00
|
|
|
|
|
|
|
|