-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
197 lines (166 loc) · 5.16 KB
/
main.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
var mraa = require('mraa'), //require mraa
app = require('http').createServer(handler),
io = require('socket.io').listen(app),
fs = require('fs'),
_ = require('lodash'),
//PWMPins
ledPins = {
0:{ pin:3, led:null, state: 1, delay: 2000, running: true},
1:{ pin:5, led:null, state: 1, delay: 2000, running: true},
2:{ pin:6, led:null, state: 1, delay: 2000, running: true},
3:{ pin:9, led:null, state: 1, delay: 2000, running: true},
4:{ pin:10, led:null, state: 1, delay: 2000, running: true}
};
photoPins = {
0:{ pin: 0, sensor: null },
1:{ pin: 1, sensor: null },
2:{ pin: 2, sensor: null },
3:{ pin: 3, sensor: null },
4:{ pin: 4,sensor: null },
};
console.log('MRAA Version: ' + mraa.getVersion()); //write the mraa version to the Intel XDK console
_.forEach(ledPins, function(index){
index.led = new mraa.Gpio(index.pin);
index.state = 1;
index.led.dir(mraa.DIR_OUT);
index.led.write(index.state);
index.led.write(0);
});
//var analogPin0 = new mraa.Aio(0); //setup access analog input Analog pin #0 (A0)
//var analogValue = analogPin0.read(); //read the value of the analog pin
_.forEach(photoPins, function(index){
index.sensor = new mraa.Aio(index.pin);
});
led0Delay(ledPins[0]);
led1Delay(ledPins[1]);
led2Delay(ledPins[2]);
led3Delay(ledPins[3]);
led4Delay(ledPins[4]);
photo0Delay(photoPins[0].sensor);
photo1Delay(photoPins[1].sensor);
photo2Delay(photoPins[2].sensor);
photo3Delay(photoPins[3].sensor);
photo4Delay(photoPins[4].sensor);
function led0Delay(ledData){
if(ledData.running){
if(ledData.state){
setState(ledData);
setTimeout(function() {led0Delay(ledData);}, ledData.delay);
} else {
setState(ledData);
setTimeout(function() {led0Delay(ledData);}, 200);
}
}
}
function led1Delay(ledData){
// console.log("LED1: ", ledData);
if(ledData.running){
if(ledData.state){
setState(ledData);
setTimeout(function() {led1Delay(ledData);}, ledData.delay);
} else {
setState(ledData);
setTimeout(function() {led1Delay(ledData);}, 200);
}
}
}
function led2Delay(ledData){
if(ledData.running){
if(ledData.state){
setState(ledData);
setTimeout(function() {led2Delay(ledData);}, ledData.delay);
} else {
setState(ledData);
setTimeout(function() {led2Delay(ledData);}, 200);
}
}
}
function led3Delay(ledData){
if(ledData.running){
if(ledData.state){
setState(ledData);
setTimeout(function() {led3Delay(ledData);}, ledData.delay);
} else {
setState(ledData);
setTimeout(function() {led3Delay(ledData);}, 200);
}
}
}
function led4Delay(ledData){
if(ledData.running){
if(ledData.state){
setState(ledData);
setTimeout(function() {led4Delay(ledData);}, ledData.delay);
} else {
setState(ledData);
setTimeout(function() {led4Delay(ledData);}, 200);
}
}
}
function setState(ledData){
if(ledData.state == 0){
ledData.state = 1;
} else {
ledData.state = 0;
}
ledData.led.write(ledData.state);
}
function photo0Delay(sensor){
var value = sensor.read();
io.sockets.emit('photo0',{ data:value});
// console.log("Photo"+sensor.pin+" data: ", value)
setTimeout(function() {photo0Delay(sensor);}, 100)
}
function photo1Delay(sensor){
var value = sensor.read();
io.sockets.emit('photo1',{ data:value});
// console.log("Photo"+sensor.pin+" data: ", value)
setTimeout(function() {photo1Delay(sensor);}, 100)
}
function photo2Delay(sensor){
var value = sensor.read();
io.sockets.emit('photo2',{ data:value});
// console.log("Photo"+sensor.pin+" data: ", value)
setTimeout(function() {photo2Delay(sensor);}, 100)
}
function photo3Delay(sensor){
var value = sensor.read();
io.sockets.emit('photo3',{ data:value});
// console.log("Photo"+sensor.pin+" data: ", value)
setTimeout(function() {photo3Delay(sensor);}, 100)
}
function photo4Delay(sensor){
var value = sensor.read();
io.sockets.emit('photo4',{ data:value});
// console.log("Photo"+sensor.pin+" data: ", value)
setTimeout(function() {photo4Delay(sensor);}, 100)
}
//=======================================================================================================
// SOCKET.IO
// handle web server
function handler (req, res) {
fs.readFile(__dirname + '/public/views/index.html',
function (err, data) {
if (err) {
res.writeHead(500);
return res.end('Error loading index.html');
}
// console.log("Server started");
res.writeHead(200);
res.end(data);
});
}
// make web server listen on port 80
app.listen(3000);
io.sockets.on('connection', function(socket){
socket.on('onOff', function(data) {
// console.log("got the call", data);
ledPins[parseInt(data.pin)].running = data.status;
// console.log(parseInt(ledPins[data.pin]));
});
// if led delay message emitted
socket.on('led', function (data) {
// console.log("********SET*******",data);
ledPins[parseInt(data.pin)].delay = data.interval;
});
});