-
Notifications
You must be signed in to change notification settings - Fork 5
/
rgbled-controller.js
144 lines (132 loc) · 3.27 KB
/
rgbled-controller.js
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
/**
* Crafted by Obero on 30/09/14
* For Epitech Innovation Hub
*/
"use strict";
var galil = require('./gpio-galileo-helper');
/**
* RgbledController helps you to manipulate an RGB led.
*
* @class Rgbledcontroller
* @constructor
*/
var RgbledController = function()
{
/**
* Sets pin ids to match it with colors
* @property pin
* @type {{r: number, g: number, b: number}}
* @default {'r' : 3, 'g' : 10, 'b' : 11}
*/
this.pin =
{
'r' : 3,
'g' : 10,
'b' : 11
};
/**
* @property current
* @type {{r: number, g: number, b: number}}
* @default {'r' : 0, 'g' : 0, 'b' : 0}
*/
this.current =
{
'r' : 0,
'g' : 0,
'b' : 0
};
this.blink_interval = -1;
};
/**
* Helper : Turns the rgb led totally off
*
* @method turnOff
*/
RgbledController.prototype.turnOff = function(force)
{
console.log('[RGB] turning off');
if (typeof(force) === true && this.blink_interval != -1)
{
clearInterval(this.blink_interval);
this.blink_interval = -1;
}
for (var index in this.pin)
{
if (this.pin.hasOwnProperty(index))
{
var color = index.toString();
galil.openPin(this.pin[color]);
galil.setPinDirection(this.pin[color], "out");
galil.setPinPortDrive(this.pin[color], "strong");
galil.writePin(this.pin[color], "0");
galil.closePin(this.pin[color]);
}
}
};
/**
* Helper : Sets the r, g, and b values for the led
*
* @method setRgbValues
* @param {Object} rgb Values are either 0 or 1
*/
// TODO: use pwm to set all sorts of colors, and modulate it smoother
RgbledController.prototype.setRgbValue = function(rgb, force)
{
var that = this;
if (force === true && this.blink_interval != -1)
{
clearInterval(this.blink_interval);
this.blink_interval = -1;
}
console.log('[RGB] setting to ' + rgb['r'] + ', ' + rgb['g'] + ', ' + rgb['b']);
for (var index in this.pin)
{
if (this.pin.hasOwnProperty(index))
{
galil.openPin(this.pin[index]);
galil.setPinDirection(this.pin[index], "out");
galil.setPinPortDrive(this.pin[index], "strong");
if (rgb[index] == 1)
{
galil.writePin(this.pin[index], "1");
}
else
{
galil.writePin(this.pin[index], "0");
}
galil.closePin(that.pin[index]);
}
}
};
RgbledController.prototype.setRgbValueTimer = function(rgb, timer, force)
{
var that = this;
this.setRgbValue(rgb, force);
setTimeout(function()
{
that.turnOff();
}, timer);
};
RgbledController.prototype.blinkColor = function(rgb, interval, count)
{
var that = this;
var timer = 0;
count = typeof count !== 'undefined' ? count : -1;
this.blink_interval = setInterval(function()
{
if (timer % 2 == 0)
{
that.setRgbValue(rgb);
}
else
{
that.turnOff();
}
timer++;
if (count != -1 && timer > count && that.blink_interval != -1)
{
that.turnOff(true);
}
}, interval);
};
module.exports = new RgbledController;