Skip to content

Commit 499eae3

Browse files
committed
Split the device controls into separate files
1 parent 5fc6f06 commit 499eae3

File tree

6 files changed

+83
-59
lines changed

6 files changed

+83
-59
lines changed

app.js

Lines changed: 14 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ var app = require('http').createServer(handler)
22
, io = require('socket.io').listen(app)
33
, fs = require('fs')
44
, jf = require('jsonfile')
5-
, arduino = require('duino')
65
, util = require('util')
76

87
if (process.argv.length < 3) {
@@ -11,6 +10,7 @@ if (process.argv.length < 3) {
1110
}
1211

1312
var config = jf.readFileSync(process.argv[2]);
13+
console.log('Using config: ' + util.inspect(config));
1414

1515
app.listen(8080);
1616

@@ -27,65 +27,31 @@ function handler(req, res) {
2727
});
2828
}
2929

30-
var boards = {};
31-
30+
var devices = {};
3231
for (var deviceName in config.devices) {
33-
var descriptor = config.devices[deviceName]
32+
var descriptor = config.devices[deviceName];
3433
if (fs.existsSync(descriptor.device)) {
34+
var deviceModule = require(descriptor.module);
35+
devices[deviceName] = deviceModule.create(descriptor);
3536
console.log('Connected device: ' + deviceName);
36-
boards[deviceName] = {
37-
board: new arduino.Board({
38-
debug: true,
39-
device: descriptor.device
40-
})
41-
};
37+
} else {
38+
console.log('Device "' + deviceName + '" not found');
4239
}
4340
}
4441

45-
var PWM_R = 5;
46-
var PWM_L = 10;
47-
var DIR_R = 4;
48-
var DIR_L = 12;
49-
var ROBOT_LED = 13;
50-
5142
io.sockets.on('connection', function (socket) {
5243
socket.on('led toggle', function (data) {
53-
var boardInfo = boards[data.device];
54-
if (boardInfo) {
55-
var board = boardInfo.board;
56-
var isLedOn = boardInfo.isLedOn = !boardInfo.isLedOn;
44+
var device = devices[data.device];
45+
if (device && device.board) {
46+
var board = device.board;
47+
var isLedOn = device.isLedOn = !device.isLedOn;
5748
board.digitalWrite(13, isLedOn ? board.HIGH : board.LOW);
5849
}
5950
});
6051

61-
socket.on('analogWrite', function (data) {
62-
console.log('analogWrite(11,' + data.value + ')');
63-
board.analogWrite(11, data.value);
64-
});
65-
66-
socket.on('robot', function (data) {
67-
console.log(data)
68-
if (boards['robot']) {
69-
var boardInfo = boards['robot'];
70-
var board = boardInfo.board;
71-
if ('left' in data) {
72-
if (data.left == 0) {
73-
board.analogWrite(PWM_L, 0);
74-
} else {
75-
board.analogWrite(PWM_L, Math.abs(data.left));
76-
board.digitalWrite(DIR_L, data.left < 0 ? board.HIGH : board.LOW);
77-
}
78-
}
79-
if ('right' in data) {
80-
if (data.right == 0) {
81-
board.analogWrite(PWM_R, 0);
82-
} else {
83-
board.analogWrite(PWM_R, Math.abs(data.right));
84-
board.digitalWrite(DIR_R, data.right < 0 ? board.HIGH : board.LOW);
85-
}
86-
}
87-
}
88-
});
52+
if (devices['robot']) {
53+
devices['robot'].bindSocket(socket);
54+
}
8955

9056
socket.on('disconnect', function () {
9157
});

config.macosx.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
{
22
"devices": {
33
"light": {
4-
"device": "/dev/tty.usbmodem1d1141"
4+
"device": "/dev/tty.usbmodem1d1141",
5+
"module": "./light.js"
56
},
67
"robot": {
7-
"device": "/dev/tty.usbmodem1d1131"
8+
"device": "/dev/tty.usbmodem1d1131",
9+
"module": "./robot.js"
810
}
911
}
1012
}

config.raspi.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
{
22
"devices": {
33
"light": {
4-
"device": "/dev/serial/by-id/usb-Arduino__www.arduino.cc__0043_753303039343513031E1-if00"
4+
"device": "/dev/serial/by-id/usb-Arduino__www.arduino.cc__0043_753303039343513031E1-if00",
5+
"module": "./light.js"
56
},
67
"robot": {
7-
"device": "/dev/serial/by-id/usb-Arduino__www.arduino.cc__0043_75330303934351202011-if00"
8+
"device": "/dev/serial/by-id/usb-Arduino__www.arduino.cc__0043_75330303934351202011-if00",
9+
"module": "./robot.js"
810
}
911
}
1012
}

index.html

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,6 @@
1212
});
1313
}
1414

15-
function setAnalog(value) {
16-
socket.emit('analogWrite', { value: value });
17-
}
18-
1915
function robot(data) {
2016
socket.emit('robot', data);
2117
}
@@ -25,9 +21,6 @@
2521
<button onclick="ledToggle('light')">Toggle LED - light</button>
2622
<button onclick="ledToggle('robot')">Toggle LED - robot</button>
2723
<hr>
28-
<input id="analogVal" value="0">
29-
<button onclick="setAnalog(parseInt(document.getElementById('analogVal').value))">Set analog</button>
30-
<hr>
3124
<button style="width: 200px;" onclick="robot({left: 255})">Left forward</button>
3225
<button style="width: 200px;" onclick="robot({right: 255})">Right forward</button>
3326
<br>

light.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
var arduino = require('duino')
2+
3+
var Light = function (descriptor) {
4+
this.board = new arduino.Board({
5+
debug: true,
6+
device: descriptor.device
7+
});
8+
};
9+
10+
exports.create = function (descriptor) {
11+
return new Light(descriptor);
12+
};
13+

robot.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
var arduino = require('duino')
2+
3+
var PWM_R = 5;
4+
var PWM_L = 10;
5+
var DIR_R = 4;
6+
var DIR_L = 12;
7+
var ROBOT_LED = 13;
8+
9+
var Robot = function (descriptor) {
10+
this.board = new arduino.Board({
11+
debug: true,
12+
device: descriptor.device
13+
});
14+
};
15+
16+
Robot.prototype.setLeft = function (value) {
17+
if (value == 0) {
18+
this.board.analogWrite(PWM_L, 0);
19+
} else {
20+
this.board.analogWrite(PWM_L, Math.abs(value));
21+
this.board.digitalWrite(DIR_L, value < 0 ? this.board.HIGH : this.board.LOW);
22+
}
23+
};
24+
25+
Robot.prototype.setRight - function (value) {
26+
if (value == 0) {
27+
this.board.analogWrite(PWM_R, 0);
28+
} else {
29+
this.board.analogWrite(PWM_R, Math.abs(value));
30+
this.board.digitalWrite(DIR_R, value < 0 ? this.board.HIGH : this.board.LOW);
31+
}
32+
};
33+
34+
Robot.prototype.bindSocket = function (socket) {
35+
socket.on('robot', function (data) {
36+
if ('left' in data) {
37+
this.setLeft(data.left);
38+
}
39+
if ('right' in data) {
40+
this.setRight(data.right);
41+
}
42+
});
43+
};
44+
45+
exports.create = function (descriptor) {
46+
return new Robot(descriptor);
47+
};
48+

0 commit comments

Comments
 (0)