UNPKG

node-red-contrib-reterminal-dm

Version:

The node accessing for Seeed Studio reTerminal DM various data such as buzzer, LED, light, touch positions.

116 lines (88 loc) 3.26 kB
const InputEvent = require('npm-reterminal-dm'); const dev = require('npm-reterminal-dm/lib/deviceid'); const led = require('npm-reterminal-dm/lib/led'); const buzzer = require('npm-reterminal-dm/lib/buzzer'); const light = require('npm-reterminal-dm/lib/light'); const path = require('path'); module.exports = function (RED) { function NodeRedReTerminalLEDUsrRed(config) { RED.nodes.createNode(this, config); var node = this; node.on('input', async function (msg) { if( msg.payload == true ){ led.usrRedOn(); this.status({fill:"green",shape:"dot",text:"Usr Red LED : on"}); } else if( msg.payload == false ){ led.usrRedOff(); this.status({fill:"green",shape:"dot",text:"Usr Red LED : off"}); } }); } RED.nodes.registerType("led_usr_red", NodeRedReTerminalLEDUsrRed, { }); function NodeRedReTerminalBuzzer(config) { RED.nodes.createNode(this, config); var node = this; node.on('input', async function (msg) { if( msg.payload == true ){ buzzer.buzzerOn(); this.status({fill:"green",shape:"dot",text:"Buzzer : on"}); } else if( msg.payload == false ){ buzzer.buzzerOff(); this.status({fill:"gray",shape:"dot",text:"Buzzer : off"}); } }); } RED.nodes.registerType("buzzer", NodeRedReTerminalBuzzer, { }); function NodeRedReTerminalLightSensor(config) { RED.nodes.createNode(this, config); var node = this; /* const spawn = require('child_process').spawn; const childprocess_buttons = spawn('node', [ path.join( __dirname , 'childprocess-light.js' )]); this.status({fill:"green",shape:"dot",text:"light sensor listened"}); childprocess_buttons.stdout.on('data', (data) => { let str_data = String(data); const json_data = JSON.parse(str_data); msg = {}; msg.payload = json_data; node.send(msg); }); */ const loopInterval = config.pollInterval; let timerID = setInterval( function () { const sensor_value = Number(light.lightSense()) const json_data = { light:sensor_value } msg = {}; msg.payload = json_data; node.send(msg); }, loopInterval ); // この処理がないとタイマーが残る模様 this.on('close', function() { clearInterval(timerID); }); } RED.nodes.registerType("light_sensor", NodeRedReTerminalLightSensor, { }); function NodeRedReTerminalTouch(config) { RED.nodes.createNode(this, config); var node = this; const spawn = require('child_process').spawn; const childprocess_buttons = spawn('node', [ path.join( __dirname , 'childprocess-touch.js' )]); this.status({fill:"green",shape:"dot",text:"touch panel listened"}); childprocess_buttons.stdout.on('data', (data) => { let str_data = String(data); const json_data = JSON.parse(str_data); msg = {}; msg.payload = json_data; node.send(msg); }); } RED.nodes.registerType("touch_panel", NodeRedReTerminalTouch, { }); }