diff --git a/Software/NodeJS/libs/commands.js b/Software/NodeJS/libs/commands.js index df0350e9..6410defa 100644 --- a/Software/NodeJS/libs/commands.js +++ b/Software/NodeJS/libs/commands.js @@ -73,6 +73,12 @@ module.exports = { // Sets leds similar to a bar graph, reversible , chainableRgbLedSetLevel : [95] + // Grove IR sensor + // Read the button from IR sensor + , irRead : [21] + // Set pin for the IR reciever + , irRecvPin : [22] + // This allows us to be more specific about which commands contain unused bytes , unused : 0 }; diff --git a/Software/NodeJS/libs/index.js b/Software/NodeJS/libs/index.js index fc247bd5..92ea12b7 100644 --- a/Software/NodeJS/libs/index.js +++ b/Software/NodeJS/libs/index.js @@ -18,5 +18,6 @@ module.exports.GrovePi = { , RTCI2C: require('./sensors/rtcI2cSensor') , TemperatureAnalog: require('./sensors/temperatureAnalogSensor') , UltrasonicDigital: require('./sensors/ultrasonicDigitalSensor') + , IRReceiver: require('./sensors/IRReceiverSensor') } } \ No newline at end of file diff --git a/Software/NodeJS/libs/sensors/IRReceiverSensor.js b/Software/NodeJS/libs/sensors/IRReceiverSensor.js new file mode 100644 index 00000000..a5502f8a --- /dev/null +++ b/Software/NodeJS/libs/sensors/IRReceiverSensor.js @@ -0,0 +1,33 @@ +var util = require('util') +var Sensor = require('./base/sensor') +var commands = require('../commands') + +function IRReceiverSensor(pin) { + pin += 1 + Sensor.apply(this, Array.prototype.slice.call(arguments)) + this.pin = pin +} +util.inherits(IRReceiverSensor, Sensor); +IRReceiverSensor.prototype = new DigitalSensor() + +IRReceiverSensor.prototype.read = function() { + this.write(commands.unused) + var writeRet = this.board.writeBytes(commands.irRead.concat([commands.unused, commands.unused, commands.unused])) + if (writeRet) { + this.board.readByte() + var bytes = this.board.readBytes(22) + if (bytes instanceof Buffer && bytes[1] != 255) { + bytes.slice(0,1) + return bytes + } else { + return false + } + } else { + return false + } +} +IRReceiverSensor.prototype.write = function(value) { + return this.board.writeBytes(commands.irRecvPin.concat([this.pin, value, commands.unused])) +} + +module.exports = DigitalSensor \ No newline at end of file