UNPKG

2.96 kBJavaScriptView Raw
1var Board = require("./board");
2var Collection = require("./mixins/collection");
3var Pins = Board.Pins;
4var util = require("util");
5var priv = new Map();
6
7function Relay(opts) {
8
9 if (!(this instanceof Relay)) {
10 return new Relay(opts);
11 }
12
13 var pinValue = typeof opts === "object" ? opts.pin : opts;
14
15 Board.Component.call(
16 this, opts = Board.Options(opts)
17 );
18
19 opts.type = opts.type || "NO";
20
21 var state = {
22 isInverted: opts.type === "NC",
23 isOn: false,
24 value: null,
25 };
26
27 priv.set(this, state);
28
29 Object.defineProperties(this, {
30 value: {
31 get: function() {
32 return Number(this.isOn);
33 }
34 },
35 type: {
36 get: function() {
37 return state.isInverted ? "NC" : "NO";
38 }
39 },
40 isOn: {
41 get: function() {
42 return state.isOn;
43 }
44 }
45 });
46
47 if (Pins.isFirmata(this) &&
48 (typeof pinValue === "string" && pinValue[0] === "A")) {
49 this.pin = this.io.analogPins[+pinValue.slice(1)];
50 this.io.pinMode(this.pin, this.io.MODES.OUTPUT);
51 }
52}
53
54/**
55 * on Turn the relay on
56 * @return {Relay}
57 */
58Relay.prototype.on = function() {
59 var state = priv.get(this);
60
61 this.io.digitalWrite(
62 this.pin, state.isInverted ? this.io.LOW : this.io.HIGH
63 );
64 state.isOn = true;
65
66 return this;
67};
68
69Relay.prototype.close = Relay.prototype.on;
70
71/**
72 * off Turn the relay off
73 * @return {Relay}
74 */
75Relay.prototype.off = function() {
76 var state = priv.get(this);
77
78 this.io.digitalWrite(
79 this.pin, state.isInverted ? this.io.HIGH : this.io.LOW
80 );
81 state.isOn = false;
82
83 return this;
84};
85
86Relay.prototype.open = Relay.prototype.off;
87
88/**
89 * toggle Toggle the on/off state of the relay
90 * @return {Relay}
91 */
92Relay.prototype.toggle = function() {
93 var state = priv.get(this);
94
95 if (state.isOn) {
96 this.off();
97 } else {
98 this.on();
99 }
100
101 return this;
102};
103
104/**
105 * Relays()
106 * new Relays()
107 *
108 * Constructs an Array-like instance of all relays
109 */
110function Relays(numsOrObjects) {
111 if (!(this instanceof Relays)) {
112 return new Relays(numsOrObjects);
113 }
114
115 Object.defineProperty(this, "type", {
116 value: Relay
117 });
118
119 Collection.call(this, numsOrObjects);
120}
121
122util.inherits(Relays, Collection);
123
124/*
125 * Relays, on()
126 *
127 * Turn all relays on
128 *
129 * eg. collection.on();
130 *
131 *
132 * Relays, off()
133 *
134 * Turn all relays off
135 *
136 * eg. collection.off();
137 *
138 *
139 * Relays, open()
140 *
141 * Open all relays
142 *
143 * eg. collection.open();
144 *
145 *
146 * Relays, close()
147 *
148 * Close all relays
149 *
150 * eg. collection.close();
151 *
152 *
153 * Relays, toggle()
154 *
155 * Toggle the state of all relays
156 *
157 * eg. collection.toggle();
158 */
159
160Collection.installMethodForwarding(
161 Relays.prototype, Relay.prototype
162);
163
164// Assign Relays Collection class as static "method" of Relay.
165// TODO: Eliminate .Array for 1.0.0
166Relay.Array = Relays;
167Relay.Collection = Relays;
168
169/* istanbul ignore else */
170if (!!process.env.IS_TEST_MODE) {
171 Relay.purge = function() {
172 priv.clear();
173 };
174}
175
176module.exports = Relay;