first commit
This commit is contained in:
commit
a5a0434432
1126 changed files with 439481 additions and 0 deletions
33
Software/NodeJS/libs/sensors/base/analogSensor.js
Normal file
33
Software/NodeJS/libs/sensors/base/analogSensor.js
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
var util = require('util')
|
||||
var Sensor = require('./sensor')
|
||||
var commands = require('../../commands')
|
||||
|
||||
function AnalogSensor(pin) {
|
||||
Sensor.apply(this, Array.prototype.slice.call(arguments))
|
||||
this.pin = pin
|
||||
}
|
||||
util.inherits(AnalogSensor, Sensor)
|
||||
AnalogSensor.prototype = new AnalogSensor()
|
||||
|
||||
AnalogSensor.prototype.read = function(length) {
|
||||
if (typeof length == 'undefined')
|
||||
length = this.board.BYTESLEN
|
||||
|
||||
var writeRet = this.board.writeBytes(commands.aRead.concat([this.pin, commands.unused, commands.unused]))
|
||||
if (writeRet) {
|
||||
this.board.readByte()
|
||||
var bytes = this.board.readBytes(length)
|
||||
if (bytes instanceof Buffer) {
|
||||
return bytes[1] * 256 + bytes[2]
|
||||
} else {
|
||||
return false
|
||||
}
|
||||
} else {
|
||||
return false
|
||||
}
|
||||
}
|
||||
AnalogSensor.prototype.write = function(value) {
|
||||
return this.board.writeBytes(commands.aWrite.concat([this.pin, value, commands.unused]))
|
||||
}
|
||||
|
||||
module.exports = AnalogSensor
|
||||
25
Software/NodeJS/libs/sensors/base/digitalSensor.js
Normal file
25
Software/NodeJS/libs/sensors/base/digitalSensor.js
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
var util = require('util')
|
||||
var Sensor = require('./sensor')
|
||||
var commands = require('../../commands')
|
||||
|
||||
function DigitalSensor(pin) {
|
||||
Sensor.apply(this, Array.prototype.slice.call(arguments))
|
||||
this.pin = pin
|
||||
}
|
||||
util.inherits(DigitalSensor, Sensor)
|
||||
DigitalSensor.prototype = new DigitalSensor()
|
||||
|
||||
DigitalSensor.prototype.read = function() {
|
||||
var writeRet = this.board.writeBytes(commands.dRead.concat([this.pin, commands.unused, commands.unused]))
|
||||
if (writeRet) {
|
||||
this.board.wait(100)
|
||||
return this.board.readBytes(2)[1]
|
||||
} else {
|
||||
return false
|
||||
}
|
||||
}
|
||||
DigitalSensor.prototype.write = function(value) {
|
||||
return this.board.writeBytes(commands.dWrite.concat([this.pin, value, commands.unused]))
|
||||
}
|
||||
|
||||
module.exports = DigitalSensor
|
||||
11
Software/NodeJS/libs/sensors/base/i2cSensor.js
Normal file
11
Software/NodeJS/libs/sensors/base/i2cSensor.js
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
var util = require('util')
|
||||
var Sensor = require('./sensor')
|
||||
var commands = require('../../commands')
|
||||
|
||||
function I2cSensor() {
|
||||
Sensor.apply(this, Array.prototype.slice.call(arguments))
|
||||
}
|
||||
util.inherits(I2cSensor, Sensor)
|
||||
I2cSensor.prototype = new I2cSensor()
|
||||
|
||||
module.exports = I2cSensor
|
||||
67
Software/NodeJS/libs/sensors/base/sensor.js
Normal file
67
Software/NodeJS/libs/sensors/base/sensor.js
Normal file
|
|
@ -0,0 +1,67 @@
|
|||
var util = require('util')
|
||||
var EventEmitter = require('events').EventEmitter
|
||||
var Board = require('../../grovepi')
|
||||
|
||||
var isEqual = function(a, b) {
|
||||
if (typeof a == 'object') {
|
||||
for (var i in a) {
|
||||
if (a[i] !== b[i]) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
} else {
|
||||
return a === b
|
||||
}
|
||||
}
|
||||
|
||||
function Sensor() {
|
||||
this.board = new Board()
|
||||
this.lastValue = 0
|
||||
this.currentValue = 0
|
||||
this.streamInterval = this.watchInterval = undefined
|
||||
this.watchDelay = 100
|
||||
}
|
||||
|
||||
util.inherits(Sensor, EventEmitter)
|
||||
|
||||
Sensor.prototype.read = function() {}
|
||||
Sensor.prototype.write = function() {}
|
||||
Sensor.prototype.stream = function(delay, cb) {
|
||||
var self = this
|
||||
delay = typeof delay == 'undefined' ? self.watchDelay : delay
|
||||
|
||||
self.stopStream()
|
||||
self.streamInterval = setInterval(function onStreamInterval() {
|
||||
var res = self.read()
|
||||
cb(res)
|
||||
}, delay)
|
||||
}
|
||||
Sensor.prototype.stopStream = function() {
|
||||
var self = this
|
||||
if (typeof self.streamInterval != 'undefined')
|
||||
clearInterval(self.streamInterval)
|
||||
}
|
||||
|
||||
Sensor.prototype.watch = function(delay) {
|
||||
var self = this
|
||||
delay = typeof delay == 'undefined' ? self.watchDelay : delay
|
||||
|
||||
self.stopWatch()
|
||||
self.watchInterval = setInterval(function onInterval() {
|
||||
var res = self.read()
|
||||
|
||||
self.lastValue = self.currentValue
|
||||
self.currentValue = res
|
||||
|
||||
if (!isEqual(self.currentValue, self.lastValue))
|
||||
self.emit('change', self.currentValue)
|
||||
}, delay)
|
||||
}
|
||||
Sensor.prototype.stopWatch = function() {
|
||||
var self = this
|
||||
if (typeof self.watchInterval != 'undefined')
|
||||
clearInterval(self.watchInterval)
|
||||
}
|
||||
|
||||
module.exports = Sensor
|
||||
Loading…
Add table
Add a link
Reference in a new issue