1 | var Board = require("./board");
|
2 | var Collection = require("./mixins/collection");
|
3 | var Emitter = require("events").EventEmitter;
|
4 | var Fn = require("./fn");
|
5 | var util = require("util");
|
6 |
|
7 | var aliases = {
|
8 | close: ["close", "closed", "on"],
|
9 | open: ["open", "off"]
|
10 | };
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 | function Switch(opts) {
|
29 |
|
30 | if (!(this instanceof Switch)) {
|
31 | return new Switch(opts);
|
32 | }
|
33 |
|
34 |
|
35 |
|
36 |
|
37 | var trigger = Fn.debounce(function(key) {
|
38 | aliases[key].forEach(function(type) {
|
39 | this.emit(type, null);
|
40 | }, this);
|
41 | }, 5);
|
42 |
|
43 | Board.Component.call(
|
44 | this, opts = Board.Options(opts)
|
45 | );
|
46 |
|
47 |
|
48 | opts.type = opts.type || "NO";
|
49 |
|
50 |
|
51 | var isNormallyOpen = opts.type === "NO";
|
52 | var raw = null;
|
53 | var invert = typeof opts.invert !== "undefined" ?
|
54 | opts.invert : (isNormallyOpen || false);
|
55 |
|
56 |
|
57 | var closeValue = 1;
|
58 | var openValue = 0;
|
59 |
|
60 | if (invert) {
|
61 | closeValue ^= 1;
|
62 | openValue ^= 1;
|
63 | }
|
64 |
|
65 | this.io.pinMode(this.pin, this.io.MODES.INPUT);
|
66 |
|
67 | if (isNormallyOpen) {
|
68 | this.io.digitalWrite(this.pin, this.io.HIGH);
|
69 | }
|
70 |
|
71 | this.io.digitalRead(this.pin, function(data) {
|
72 | raw = data;
|
73 |
|
74 | trigger.call(this, this.isOpen ? "open" : "close");
|
75 | }.bind(this));
|
76 |
|
77 | Object.defineProperties(this, {
|
78 | value: {
|
79 | get: function() {
|
80 | return Number(this.isOpen);
|
81 | }
|
82 | },
|
83 | invert: {
|
84 | get: function() {
|
85 | return invert;
|
86 | },
|
87 | set: function(value) {
|
88 | invert = value;
|
89 | closeValue = invert ? 0 : 1;
|
90 | openValue = invert ? 1 : 0;
|
91 | }
|
92 | },
|
93 | closeValue: {
|
94 | get: function() {
|
95 | return closeValue;
|
96 | },
|
97 | set: function(value) {
|
98 | closeValue = value;
|
99 | openValue = value ^ 1;
|
100 | }
|
101 | },
|
102 | openValue: {
|
103 | get: function() {
|
104 | return openValue;
|
105 | },
|
106 | set: function(value) {
|
107 | openValue = value;
|
108 | closeValue = value ^ 1;
|
109 | }
|
110 | },
|
111 | isOpen: {
|
112 | get: function() {
|
113 | return raw === openValue;
|
114 | }
|
115 | },
|
116 | isClosed: {
|
117 | get: function() {
|
118 | return raw === closeValue;
|
119 | }
|
120 | },
|
121 | });
|
122 | }
|
123 |
|
124 | util.inherits(Switch, Emitter);
|
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 | function Switches(numsOrObjects) {
|
153 | if (!(this instanceof Switches)) {
|
154 | return new Switches(numsOrObjects);
|
155 | }
|
156 |
|
157 | Object.defineProperty(this, "type", {
|
158 | value: Switch
|
159 | });
|
160 |
|
161 | Collection.Emitter.call(this, numsOrObjects);
|
162 | }
|
163 |
|
164 | util.inherits(Switches, Collection.Emitter);
|
165 |
|
166 | Collection.installMethodForwarding(
|
167 | Switches.prototype, Switch.prototype
|
168 | );
|
169 |
|
170 |
|
171 | Switch.Collection = Switches;
|
172 |
|
173 |
|
174 |
|
175 | module.exports = Switch;
|