From 54850cde26700572ef1abc6c3abb3721449ae0fa Mon Sep 17 00:00:00 2001 From: Stefan Sterz Date: Thu, 14 Jan 2016 21:19:41 +0100 Subject: [PATCH] refactor code The communication with the arduino is now handled by a designated module (called ar-com). --- nodejs/ar-com/arduino.js | 55 ++++++++++++++++++++++++++++++++++++++++ nodejs/routes/index.js | 31 +++------------------- 2 files changed, 59 insertions(+), 27 deletions(-) create mode 100644 nodejs/ar-com/arduino.js diff --git a/nodejs/ar-com/arduino.js b/nodejs/ar-com/arduino.js new file mode 100644 index 0000000..f49e9f3 --- /dev/null +++ b/nodejs/ar-com/arduino.js @@ -0,0 +1,55 @@ +var events = require('events'), + child = require('child_process'), + util = require('util'), + serial = require("serialport").SerialPort; + +var Arduino = function (baudrate) { + this.baudrate = baudrate && baudrate || 9600; + this.writeBuffer = []; + this.success; +} + +Arduino.prototype.setup = function () { + child.exec('cd ../arduino && ino build && ino upload', function(err, stdout, stderr){ + + if(err){console.log('Please install the ino utility and connect arduino!\nIf there are other open programs that communicate with the arduino, close them!'); return this.success = false;} + + var buff = stdout.slice(stdout.indexOf('Guessing serial port ... ')+25); + buff = buff.slice(0, buff.indexOf('\n')); + console.log('Guessing serial port ... ' + buff); + + this.serial = new serial(buff, { + baudrate: this.baudrate, + parser: serial.parsers.readline('\n') + }, false); + + this.serial.open(function (error) { + if ( error ) { + console.log('Please check arduino!') + return this.success = false; + } + + serialPort.write(new Buffer('00000000')); + }); + + this.success = true; + this.processWriteBuffer(); + }); +} + +Arduino.prototype.write = function (m) { + if (this.success) { + this.serial.write(m); + } else { + this.writeBuffer.push(m); + console.log('Board isn\'t available (yet?).') + } +} + +Arduino.prototype.processWriteBuffer = function () { + while (this.writeBuffer.length > 0) { + this.write(this.writeBuffer.shift()); + } +} + +module.exports = Arduino; \ No newline at end of file diff --git a/nodejs/routes/index.js b/nodejs/routes/index.js index f796f0f..66f1b18 100644 --- a/nodejs/routes/index.js +++ b/nodejs/routes/index.js @@ -1,32 +1,9 @@ var express = require('express'); var router = express.Router(); -var child = require("child_process"); +var Arduino = new require('../ar-com/arduino'); +var arduino = new Arduino(); -var SerialPort = require("serialport").SerialPort -var success; -child.exec('cd ../arduino && ino build && ino upload', function(err, stdout, stderr){ - - if(err){console.log('Please install the ino utility and connect arduino!\nIf there are other open programs that communicate with the arduino, close them!'); return success = false;} - - var buff = stdout.slice(stdout.indexOf('Guessing serial port ... ')+25); - buff = buff.slice(0, buff.indexOf('\n')); - console.log('Guessing serial port ... ' + buff); - - serialPort = new SerialPort(buff, { - baudrate: 9600 - }, false); - - serialPort.open(function (error) { - if ( error ) { - console.log('Please check arduino!') - return successfalse; - } - }); - - return success = true; -}); - -//console.log(success); +arduino.setup() /* GET home page. */ router.get('/', function(req, res, next) { @@ -34,7 +11,7 @@ router.get('/', function(req, res, next) { }); router.get('/toggleLight', function (req, res) { - if(success) { serialPort.write(new Buffer('~','ascii')); } else {console.log("arduino not connected")} + arduino.write(new Buffer('~','ascii')); res.send('switch light'); });