-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathwa-led.html
166 lines (148 loc) · 4.06 KB
/
wa-led.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
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
161
162
163
164
165
166
<html>
<head>
<script type="text/javascript" src="../webduino-js/src/module/Led.js"></script>
</head>
<body>
<script>
(function() {
var proto = Object.create(HTMLElement.prototype, {
pin: {
get: function() {
return this.getAttribute('pin');
},
set: function(val) {
this.setAttribute('pin', val);
}
},
intensity: {
get: function() {
return this.getAttribute('intensity');
},
set: function(val) {
this.setAttribute('intensity', val);
}
},
gnd: {
get: function() {
return this.getAttribute('gnd');
},
set: function(val) {
this.setAttribute('gnd', val);
}
},
state: {
get: function() {
return this.getAttribute('state');
},
set: function(val) {
this.setAttribute('state', val);
}
}
});
proto.init_ = function(board) {
var observer, config,
Led = webduino.module.Led;
this.led = new Led(board, board.getDigitalPin(this.pin));
this._statecb = null;
this._blinkInterval = null;
// Initial led state according to wa-led state
this.attributeChangedCallback('state', null, this.state);
};
proto.createGnd_ = function() {
var gndLed;
if (this.gnd) {
if (this.gndLed) {
this.gndLed.parentNode.removeChild(this.gndLed);
}
gndLed = document.createElement('wa-led');
gndLed.setAttribute('pin', this.gnd);
this.parentNode.appendChild(gndLed);
this.gndLed = gndLed;
}
};
/**
* Set led state to on.
* @param {Function} [callback] - Led state changed callback.
*/
proto.on = function(callback) {
this._statecb = callback;
this.state = 'on';
};
proto._on = function() {
this.led.on(this._statecb);
};
/**
* Set led state to off.
* @param {Function} [callback] - Led state changed callback.
*/
proto.off = function(callback) {
this._statecb = callback;
this.state = 'off';
};
proto._off = function() {
this.led.off(this._statecb);
};
/**
* Toggle led state between on/off according to current state.
* @param {Function} [callback] - Led state changed callback.
*/
proto.toggle = function(callback) {
this._statecb = callback;
this.state = this.state === 'blink' || this.state === 'on' ? 'off' : 'on';
};
/**
* Set led state to blink. Both msec and callback are optional
* and can be passed as the only one parameter.
* @param {number} [msec] - Led blinking interval.
* @param {Function} [callback] - Led state changed callback.
*/
proto.blink = function(msec, callback) {
if (arguments.length === 1 && typeof arguments[0] === 'function') {
callback = arguments[0];
}
this.state = null;
this._blinkInterval = msec;
this._statecb = callback;
this.state = 'blink';
};
proto._blink = function() {
this.led.blink(this._blinkInterval, this._statecb);
};
proto.attributeChangedCallback = function(attrName, oldVal, newVal) {
if (this.led) {
switch (attrName) {
case 'state':
if (newVal === 'on') {
this._on();
} else if (newVal === 'off') {
this._off();
} else if (newVal === 'blink') {
this._blink();
this._blinkInterval = null;
}
this._statecb = null;
break;
case 'gnd':
this.createGnd_();
break;
case 'intensity':
this.led.intensity = newVal;
break;
}
}
};
proto.createdCallback = function() {
this.createGnd_();
};
proto.detachedCallback = function() {
if (this.gndLed) {
this.gndLed.parentNode.removeChild(this.gndLed);
}
};
document.registerElement('wa-led', {
prototype: proto
});
})();
</script>
</body>
</html>