Skip to content

Commit

Permalink
Relay: add test and example program for use with analog pin.
Browse files Browse the repository at this point in the history
  • Loading branch information
rwaldron committed Feb 20, 2018
1 parent 83e919d commit 955665c
Show file tree
Hide file tree
Showing 8 changed files with 215 additions and 9 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ To get you up and running quickly, we provide a variety of examples for using ea

To interactively navigate the examples, visit the [Johnny-Five examples](http://johnny-five.io/examples/) page on the official website. If you want to link directly to the examples in this repo, you can use one of the following links.

**There are presently 357 example programs with code and diagrams!**
**There are presently 358 example programs with code and diagrams!**

<!--extract-start:examples-->

Expand Down Expand Up @@ -302,6 +302,7 @@ To interactively navigate the examples, visit the [Johnny-Five examples](http://
### Relay
- [Relay](https://github.com/rwaldron/johnny-five/blob/master/docs/relay.md)
- [Relay - Collection](https://github.com/rwaldron/johnny-five/blob/master/docs/relay-collection.md)
- [Relay On Analog Pin](https://github.com/rwaldron/johnny-five/blob/master/docs/relay-on-analog-pin.md)

### Shift Register
- [Shift Register](https://github.com/rwaldron/johnny-five/blob/master/docs/shift-register.md)
Expand Down
Binary file added docs/breadboard/relay-on-analog-pin.fzz
Binary file not shown.
Binary file added docs/breadboard/relay-on-analog-pin.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
65 changes: 65 additions & 0 deletions docs/relay-on-analog-pin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<!--remove-start-->

# Relay On Analog Pin

<!--remove-end-->






##### Breadboard for "Relay On Analog Pin"



![docs/breadboard/relay-on-analog-pin.png](breadboard/relay-on-analog-pin.png)<br>

Fritzing diagram: [docs/breadboard/relay-on-analog-pin.fzz](breadboard/relay-on-analog-pin.fzz)

&nbsp;




Run this example from the command line with:
```bash
node eg/relay-on-analog-pin.js
```


```javascript
var five = require('../');
var board = new five.Board();

board.on('ready', function() {

var relay = new five.Relay({
pin: "A2"
});

setInterval(function() {
relay.toggle();
}, 1000);
});

```








&nbsp;

<!--remove-start-->

## License
Copyright (c) 2012, 2013, 2014 Rick Waldron <waldron.rick@gmail.com>
Licensed under the MIT license.
Copyright (c) 2018 The Johnny-Five Contributors
Licensed under the MIT license.

<!--remove-end-->
13 changes: 13 additions & 0 deletions eg/relay-on-analog-pin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
var five = require('../');
var board = new five.Board();

board.on('ready', function() {

var relay = new five.Relay({
pin: "A2"
});

setInterval(function() {
relay.toggle();
}, 1000);
});
14 changes: 6 additions & 8 deletions lib/relay.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
var Board = require("./board");
var Collection = require("./mixins/collection");
var Pins = Board.Pins;
var util = require("util");
var priv = new Map();

Expand All @@ -9,11 +10,6 @@ function Relay(opts) {
return new Relay(opts);
}

if (opts === undefined ||
(typeof opts === "object" && opts.pin === undefined)) {
throw new Error("Relay must have a pin number");
}

var pinValue = typeof opts === "object" ? opts.pin : opts;

Board.Component.call(
Expand Down Expand Up @@ -48,9 +44,11 @@ function Relay(opts) {
}
});

this.pin = pinValue;
this.io.pinMode(this.pin, this.io.MODES.OUTPUT);

if (Pins.isFirmata(this) &&
(typeof pinValue === "string" && pinValue[0] === "A")) {
this.pin = this.io.analogPins[+pinValue.slice(1)];
this.io.pinMode(this.pin, this.io.MODES.OUTPUT);
}
}

/**
Expand Down
125 changes: 125 additions & 0 deletions test/relay.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,131 @@ exports["Relay"] = {
},
};

exports["Relay: Digital Writing to Analog Pin"] = {
setUp: function(done) {
this.sandbox = sinon.sandbox.create();
this.board = newBoard();
this.digitalWrite = this.sandbox.spy(MockFirmata.prototype, "digitalWrite");
this.pinMode = this.sandbox.spy(MockFirmata.prototype, "pinMode");

this.relay = new Relay({
pin: "A2",
board: this.board
});

this.proto = [{
name: "on"
}, {
name: "off"
}, {
name: "close"
}, {
name: "open"
}, {
name: "toggle"
}];

this.instance = [{
name: "isOn"
}, {
name: "type"
}, {
name: "value"
}];

done();
},

tearDown: function(done) {
Board.purge();
this.sandbox.restore();
done();
},

shape: function(test) {
test.expect(this.proto.length + this.instance.length);

this.proto.forEach(function(method) {
test.equal(typeof this.relay[method.name], "function");
}, this);

this.instance.forEach(function(property) {
test.notEqual(typeof this.relay[property.name], "undefined");
}, this);

test.done();
},

callsPinMode: function(test) {
test.expect(1);
test.equal(this.pinMode.callCount, 1);

test.done();
},

NC: function(test) {
test.expect(2);

// NC should send inverted values
this.relay = new Relay({
pin: "A2",
type: "NC",
board: this.board
});

this.relay.on();
test.ok(this.digitalWrite.calledWith(16, 0));

this.relay.off();
test.ok(this.digitalWrite.calledWith(16, 1));

test.done();
},

on: function(test) {
test.expect(1);

this.relay.on();
test.ok(this.digitalWrite.calledWith(16, 1));

test.done();
},

off: function(test) {
test.expect(1);

this.relay.off();
test.ok(this.digitalWrite.calledWith(16, 0));

test.done();
},

close: function(test) {
test.expect(1);
test.equal(Relay.prototype.close, Relay.prototype.on);
test.done();
},

open: function(test) {
test.expect(1);
test.equal(Relay.prototype.open, Relay.prototype.off);
test.done();
},

toggle: function(test) {
test.expect(2);

this.relay.off();
this.relay.toggle();

test.ok(this.digitalWrite.calledWith(16, 1));

this.relay.toggle();
test.ok(this.digitalWrite.calledWith(16, 0));

test.done();
},
};

exports["Relay.Collection"] = {
setUp: function(done) {
Expand Down
4 changes: 4 additions & 0 deletions tpl/programs.json
Original file line number Diff line number Diff line change
Expand Up @@ -626,6 +626,10 @@
{"name": "relay-closed", "title": "Relay - normally closed", "description": "The breadboard diagram shows a Keyes Relay, however any Relay will work."}
]
},
{
"file": "relay-on-analog-pin.js",
"title": "Relay On Analog Pin"
},
{
"file": "relay-collection.js",
"title": "Relay - Collection"
Expand Down

0 comments on commit 955665c

Please sign in to comment.