UNPKG

3.19 kBJavaScriptView Raw
1var Board = require("./board");
2var Collection = require("./mixins/collection");
3var Emitter = require("events").EventEmitter;
4var Fn = require("./fn");
5var util = require("util");
6
7var aliases = {
8 close: ["close", "closed", "on"],
9 open: ["open", "off"]
10};
11
12
13/**
14 * Switch
15 * @constructor
16 *
17 * five.Switch();
18 *
19 * five.Switch({
20 * pin: 10
21 * });
22 *
23 *
24 * @param {Object} opts [description]
25 *
26 */
27
28function Switch(opts) {
29
30 if (!(this instanceof Switch)) {
31 return new Switch(opts);
32 }
33
34 // Create a 5 ms debounce boundary on event triggers
35 // this avoids button events firing on
36 // press noise and false positives
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 // Resolve the default type to Normally Open
48 opts.type = opts.type || "NO";
49
50 // Is this instance Normally Open?
51 var isNormallyOpen = opts.type === "NO";
52 var raw = null;
53 var invert = typeof opts.invert !== "undefined" ?
54 opts.invert : (isNormallyOpen || false);
55
56 // Logical Defaults
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
124util.inherits(Switch, Emitter);
125
126
127/**
128 * Fired when the Switch is close
129 *
130 * @event
131 * @name close
132 * @memberOf Switch
133 */
134
135
136/**
137 * Fired when the Switch is opened
138 *
139 * @event
140 * @name open
141 * @memberOf Switch
142 */
143
144
145/**
146 * Switches()
147 * new Switches()
148 *
149 * Constructs an Array-like instance of all servos
150 */
151
152function 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
164util.inherits(Switches, Collection.Emitter);
165
166Collection.installMethodForwarding(
167 Switches.prototype, Switch.prototype
168);
169
170// Assign Switches Collection class as static "method" of Switch.
171Switch.Collection = Switches;
172
173
174
175module.exports = Switch;