1 | var Board = require("./board");
|
2 | var Collection = require("./mixins/collection");
|
3 | var Pins = Board.Pins;
|
4 | var util = require("util");
|
5 | var priv = new Map();
|
6 |
|
7 | function 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 |
|
56 |
|
57 |
|
58 | Relay.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 |
|
69 | Relay.prototype.close = Relay.prototype.on;
|
70 |
|
71 |
|
72 |
|
73 |
|
74 |
|
75 | Relay.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 |
|
86 | Relay.prototype.open = Relay.prototype.off;
|
87 |
|
88 |
|
89 |
|
90 |
|
91 |
|
92 | Relay.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 |
|
106 |
|
107 |
|
108 |
|
109 |
|
110 | function 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 |
|
122 | util.inherits(Relays, Collection);
|
123 |
|
124 |
|
125 |
|
126 |
|
127 |
|
128 |
|
129 |
|
130 |
|
131 |
|
132 |
|
133 |
|
134 |
|
135 |
|
136 |
|
137 |
|
138 |
|
139 |
|
140 |
|
141 |
|
142 |
|
143 |
|
144 |
|
145 |
|
146 |
|
147 |
|
148 |
|
149 |
|
150 |
|
151 |
|
152 |
|
153 |
|
154 |
|
155 |
|
156 |
|
157 |
|
158 |
|
159 |
|
160 | Collection.installMethodForwarding(
|
161 | Relays.prototype, Relay.prototype
|
162 | );
|
163 |
|
164 |
|
165 |
|
166 | Relay.Array = Relays;
|
167 | Relay.Collection = Relays;
|
168 |
|
169 |
|
170 | if (!!process.env.IS_TEST_MODE) {
|
171 | Relay.purge = function() {
|
172 | priv.clear();
|
173 | };
|
174 | }
|
175 |
|
176 | module.exports = Relay;
|