-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathwa-mq2.html
74 lines (63 loc) · 1.57 KB
/
wa-mq2.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
<html>
<head>
<script type="text/javascript" src="../webduino-js/src/module/MQ2.js"></script>
</head>
<body>
<script>
(function() {
var proto = Object.create(HTMLElement.prototype, {
analogpin: {
get: function() {
return this.getAttribute('analogpin');
},
set: function(val) {
this.setAttribute('analogpin', val);
}
},
pin: {
get: function() {
return this.getAttribute('pin');
},
set: function(val) {
this.setAttribute('pin', val);
}
}
});
proto.init_ = function(board) {
var MQ2 = webduino.module.MQ2;
var pin = this.pin ? board.getDigitalPin(this.pin) : null;
var analogPin = this.analogpin || null;
this.mq2 = new MQ2(board, analogPin, pin);
};
proto.on = function(type, handler) {
var callback;
if (typeof type === 'function') {
callback = type;
if (typeof callback === 'function') {
this.mq2.on(function(val) {
val = val.toFixed(5);
callback(val);
});
}
}
if (typeof type === 'string' && typeof handler === 'function') {
this.mq2.onEvent(type, handler);
}
};
proto.off = function(type, handler) {
if (arguments.length) {
this.mq2.offEvent(type, handler);
} else {
this.mq2.off();
}
};
proto.detachedCallback = function() {
this.off();
};
document.registerElement('wa-mq2', {
prototype: proto
});
})();
</script>
</body>
</html>