Skip to content

Commit

Permalink
Correct and simplify channel validation. Closes tesselgh-10
Browse files Browse the repository at this point in the history
  • Loading branch information
rwaldron committed Nov 15, 2015
1 parent a2a954f commit 3b8ee94
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 25 deletions.
2 changes: 1 addition & 1 deletion examples/relay.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ values to the console upon latching.
var tessel = require('tessel');
var relaylib = require('../'); // Replace '../' with 'relay-mono' in your own code

var relay = relaylib.use(tessel.port['A']);
var relay = relaylib.use(tessel.port['A']);

// Wait for the module to connect
relay.on('ready', function relayReady () {
Expand Down
29 changes: 12 additions & 17 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,16 @@
var util = require('util');
var events = require('events');

var invalidChannel = 'Invalid relay channel. Expected 1 or 2.';

function isInvalidChannel(channel) {
return channel !== 1 && channel !== 2;
}

function Relay(hardware, callback) {
// Save the port
this.hardware = hardware;

// Set the gpios as output
this.hardware.digital[0].output(false);
this.hardware.digital[1].output(false);
Expand All @@ -32,9 +38,9 @@ function Relay(hardware, callback) {
util.inherits(Relay, events.EventEmitter);

Relay.prototype._setValue = function(channel, value, callback) {
var err;
if ((err = this._validChannel(channel))) {
return callback && callback(err);

if (isInvalidChannel(channel)) {
return callback && callback(new Error(invalidChannel));
}
else {
// Get the relay
Expand All @@ -56,21 +62,10 @@ Relay.prototype._setValue = function(channel, value, callback) {
}
};

Relay.prototype._validChannel = function(channel) {
var self = this;
if (!(channel > 0 || channel <= this.currentStates.length)) {
var err = new Error("Invalid relay channel. Must be 1 or 2.");
self.emit('error', err);
return err;
}
return null;
};

// Gets the state of the specified relay channel
Relay.prototype.getState = function(channel, callback) {
var err;
if ((err = this._validChannel(channel))) {
return callback && callback(err);
if (isInvalidChannel(channel)) {
return callback && callback(new Error(invalidChannel));
}
else {
callback && callback(null, this.currentStates[channel - 1]);
Expand Down
40 changes: 33 additions & 7 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,58 @@
var tessel = require('tessel');
var relayDriver = require('../');
var relayPort = tessel.port.A;
var testPort = tessel.port['B'];
var testPort = tessel.port.B;

var pinout = testPort.digital[0];
var pinin = testPort.digital[1];

var relay = relayDriver.use(relayPort);

console.log('1..5');
console.log('1..11');

relay.on('ready', function () {
console.log('# ready');
console.log('ok');

var channel = 1;
var boundOff = relay.turnOff.bind(relay);
var boundOn = relay.turnOn.bind(relay);

setup(function() {
testHelper(relay.turnOff.bind(relay), channel, 1, function() {
testHelper(relay.turnOn.bind(relay), channel, 0, function() {
testHelper(relay.turnOff.bind(relay), channel, 1, function() {
testHelper(boundOff, channel, 1, function() {
testHelper(boundOn, channel, 0, function() {
testHelper(boundOff, channel, 1, function() {
pinout.write(1, function() {
testHelper(relay.turnOn.bind(relay), channel, 1);
testHelper(boundOn, channel, 1);
});
});
});
});
});

relay._setValue(0, 1, function(error) {
console.log(error !== null ? 'ok' : 'not ok');
});

relay._setValue(3, 1, function(error) {
console.log(error !== null ? 'ok' : 'not ok');
});

relay.getState(0, function(error) {
console.log(error !== null ? 'ok' : 'not ok');
});

relay.getState(3, function(error) {
console.log(error !== null ? 'ok' : 'not ok');
});

relay.getState(1, function(error) {
console.log(error === null ? 'ok' : 'not ok');
});

relay.getState(2, function(error) {
console.log(error === null ? 'ok' : 'not ok');
});
});

function setup(cb) {
Expand Down Expand Up @@ -61,4 +87,4 @@ function testHelper(relayFunc, channel, expectedValue, cb) {

relay.on('latch', function(channel, value) {
console.log('# latch on channel ' + channel + ' switched to', value);
});
});

0 comments on commit 3b8ee94

Please sign in to comment.