forked from sbstjn/duino
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathled.js
More file actions
74 lines (67 loc) · 1.51 KB
/
Copy pathled.js
File metadata and controls
74 lines (67 loc) · 1.51 KB
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
/**
* Main LED constructor
* Process options
*/
/**
* Tell the board to set it up
* @param object options
*/
var Led = function (options) {
if (!options || !options.board) throw new Error('Must supply required options to LED');
this.board = options.board;
this.pin = options.pin || 13;
this.bright = 0;
this.board.pinMode(this.pin, 'out');
this.direction = -1;
}
/**
* Turn the LED on
*/
Led.prototype.on = function () {
this.board.digitalWrite(this.pin, this.board.HIGH);
this.bright = 255;
}
/* *
* Turn the LED off
*/
Led.prototype.off = function () {
this.board.digitalWrite(this.pin, this.board.LOW);
this.bright = 0;
}
/**
* Set LED analog value
* @param integer val
*/
Led.prototype.brightLevel = function(val) {
this.board.analogWrite(this.pin, this.bright = val);
}
/**
* Fade LED to opposite state
* @param integer interval time to fade
*/
Led.prototype.fade = function(interval) {
to = (interval||5000)/(255*2);
var self = this;
setInterval(function() {
if(!self.board.serial) return; //Interval too fast for debug messages on ^c
if(self.bright==0) direction = 1;
if(self.bright==255) direction = -1;
self.brightLevel(self.bright+direction);
},to);
}
/**
* Start a variable blinking pattern
* @param integer interval time between states
*/
Led.prototype.blink = function (interval) {
interval = interval || 1000;
var self = this;
setInterval(function(){
if (self.bright) {
self.off()
} else {
self.on();
}
}, interval);
}
module.exports = Led;