Skip to content

Commit

Permalink
Web Examples and Node.js Examples
Browse files Browse the repository at this point in the history
  • Loading branch information
sbstjn committed Mar 3, 2012
1 parent 37ad181 commit 49b41cd
Show file tree
Hide file tree
Showing 24 changed files with 12,515 additions and 1 deletion.
96 changes: 96 additions & 0 deletions app-old.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
var kickstart = require('kickstart').withConfig({'name': 'localhost', 'port': 8080, 'path': __dirname});
var Noduino = new require('./libs/Noduino')({'debug': true});
var Socket = function(options) { this.io = null; this.sockets = {}; this.arduinos = []; this.checkSocket(); };
Socket.prototype.checkSocket = function() { if (this.io == null) { this.io = require('socket.io'); } };
Socket.prototype.bootstrap = function() {
var that = this;
this.io.sockets.on('connection', function(socket) {
that.sockets[socket.id] = socket;

/**
* Handle incoming requests for connect to an arduino board
*/
that.sockets[socket.id].on('board.connect', function(data) {
if (!data) {
var data = {}; }
if (!data.id) {
data.id = 0; }

if (that.arduinos[data.id]) {
return socket.emit('response', {'msg': 'board.connect', 'response': 'ready' }); }
Noduino.connect({'type': 'serial'}, function(err, board) {
that.arduinos[data.id] = board;
socket.emit('response', {'msg': 'board.connect', 'response': (!err ? 'ready' : 'failed') });
});
});
});
};
Socket.prototype.listen = function(port) { this.io = this.io.listen(port); this.bootstrap(); };

var NoduinoServer = new Socket();
NoduinoServer.listen(8090);

var srv = kickstart.srv();

srv.all('/', function(req, res) {
var examples = {
'basics': [ '> ls /dev | grep usb',
'crw-rw-rw- 1 root wheel 18, 17 24 Feb 22:54 cu.usbmodem1d11',
'crw-rw-rw- 1 root wheel 18, 16 24 Feb 22:00 tty.usbmodem1d11'].join('\n'),
'node': [ '> npm install',
'> node app.js',
'Listening on http://localhost:8080'].join('\n'),
'toggleLED': ["var Noduino = new require('./libs/Noduino')({'debug': true});",
"Noduino.connect({'type': 'socket'}, function(err, board) {",
" if (err) { return console.log(err); }",
" board.withLED({pin: 13}, function(err, LED) {",
" if (err) { return console.log(err); }",
"",
" LED.blink(250);",
" });",
"});"].join('\n'),
'connect': [ "var Noduino = new require('./libs/Noduino')({'debug': true});",
"Noduino.connect({'type': 'socket'}, function(err, board) {",
" if (err) { return console.log(err); }",
" ",
" console.log('Connected to board');",
"});"].join('\n'),
'listenButton': [ "var Noduino = new require('./libs/Noduino')({'debug': true});",
"Noduino.connect({'type': 'socket'}, function(err, board) {",
" if (err) { return console.log(err); }",
" ",
" board.withButton({pin: 9}, function(err, Button)) {",
" Button.on(cnst.HIGH, function(mode) {",
" console.log('pushed!');",
" });",
" Button.on(cnst.LOW, function(mode) {",
" console.log('released!');",
" });",
" })",
"});"].join('\n'),
'analogRead': [ 'noduino.connect(function(err, board) {',
' if (err) { return console.log(\'failed to connect!\'); }',
' console.log(\'connected\');',
'});'].join('\n'),
'digitalRead': [ 'noduino.connect(function(err, board) {',
' board.registerPin({pin: 9}, function(err, pin) {',
' pin.digitalRead(function(err, value) {',
' console.log(\'received value: \' + value);',
' });',
' })',
'});'].join('\n'),
'digitalWrite': [ 'noduino.connect(function(err, board) {',
' board.registerPin({pin: 9}, function(err, pin) {',
' pin.digitalWrite(const.HIGH, function(err, value) {',
' console.log(\'set pin to value: \' + value);',
' });',
' })',
'});'].join('\n')


}
res.render('home', {title: 'noduino', 'examples': examples});
});

var router = kickstart.listen();
console.log("Listening on http://%s:%d", kickstart.conf().name, router.address().port);
2 changes: 1 addition & 1 deletion duino
Submodule duino updated from 2cdb2b to 715095
75 changes: 75 additions & 0 deletions examples.snippet
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
###basics
> ls /dev | grep usb
crw-rw-rw- 1 root wheel 18, 17 24 Feb 22:54 cu.usbmodem1d11
crw-rw-rw- 1 root wheel 18, 16 24 Feb 22:00 tty.usbmodem1d11

###node
> npm install
> node app.js
Listening on http://localhost:8080

###toggleLED
require(['libs/Noduino', 'libs/Noduino.Socket'], function(NoduinoObj, Connector) {
var Noduino = new NoduinoObj({debug: true, host: 'http://localhost:8090'}, Connector);
Noduino.connect(function(err, board) {
if (err) { return console.log(err); }
board.withLED({pin: 13}, function(err, LED) {
if (err) { return console.log(err); }

LED.blink(250);
LED.on('change', function() }
console.log('LED changed to ' + (data.mode == Noduino.HIGH ? '[on]' : '[off]'));
});
});
})
});

###connect
require(['libs/Noduino', 'libs/Noduino.Socket'], function(NoduinoObj, Connector) {
var Noduino = new NoduinoObj({debug: true, host: 'http://localhost:8090'}, Connector);
Noduino.connect(function(err, board) {
if (err) { return console.log(err); }

console.log('Connected to board');
});
});

###listenButton
var Noduino = new require('./libs/Noduino')({'debug': true});
Noduino.connect({'type': 'socket'}, function(err, board) {
if (err) { return console.log(err); }
board.withButton({pin: 9}, function(err, Button)) {
Button.on(cnst.HIGH, function(mode) {
console.log('pushed!');
});

Button.on(cnst.LOW, function(mode) {
console.log('released!');
});
})
});

###analogRead
noduino.connect(function(err, board) {
if (err) { return console.log('failed to connect!'); }

console.log('connected');
});

###digitalRead
noduino.connect(function(err, board) {
board.registerPin({pin: 9}, function(err, pin) {
pin.digitalRead(function(err, value) {
console.log('received value: ' + value);
});
});
});

###digitalWrite
noduino.connect(function(err, board) {
board.registerPin({pin: 9}, function(err, pin) {
pin.digitalWrite(const.HIGH, function(err, value) {
console.log('set pin to value: ' + value);
});
})
});
7 changes: 7 additions & 0 deletions public/scripts/domready.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

67 changes: 67 additions & 0 deletions public/scripts/events.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
define(function() {

var e1 = null;
var e2 = null;
var e3 = null;
function Events() {

}

Events.bind = function() {
/**
* Exampel #1
*/
$('#e1-buttonConnect').click(function(e) {
e.preventDefault();

$('#e1-exampleConnection .alert').addClass('hide');
$('#e1-exampleConnection .alert-info').removeClass('hide');
$('#e1-exampleConnection .alert-info').html('Trying to connect to your Arduino…');
require(['example-1'], function(example) {
example.handle();
});
});

$('#e2-buttonStart').click(function(e) {
e.preventDefault();
$('#e2-secondStep .alert').addClass('hide');

if ($('#e2-interval').val()*1 < 25) {
$('#e2-secondStep .alert-error').removeClass('hide');
$('#e2-secondStep .alert-error').html('Interval less than 25ms is not allowed!');
} else if (e2 == null) {
$('#e2-secondStep .alert-error').removeClass('hide');
$('#e2-secondStep .alert-error').html('Connect to your Arduino first!');
} else {
e2.start($('#e2-pinValue').val(), $('#e2-interval').val());
}
});

$('#e2-buttonConnect').click(function(e) {
e.preventDefault();

$('#e2-exampleConnection .alert').addClass('hide');
$('#e2-exampleConnection .alert-info').removeClass('hide');
$('#e2-exampleConnection .alert-info').html('Trying to connect to your Arduino…');
require(['example-2'], function(example) {
e2 = example;
example.handle();
});
});

$('#e3-buttonConnect').click(function(e) {
e.preventDefault();

$('#e3-exampleConnection .alert').addClass('hide');
$('#e3-exampleConnection .alert-info').removeClass('hide');
$('#e3-exampleConnection .alert-info').html('Trying to connect to your Arduino…');
require(['example-3'], function(example) {
example.handle();
});
});


};

return Events;
});
22 changes: 22 additions & 0 deletions public/scripts/example-1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
define(function() {
function Example() {

}

Example.handle = function() {

require(['libs/Noduino', 'libs/Noduino.Socket'], function(NoduinoObj, Connector) {
var Noduino = new NoduinoObj({debug: true, host: 'http://localhost:8090'}, Connector);
Noduino.connect(function(err, board) {
$('#e1-exampleConnection .alert').addClass('hide');
if (err) {
$('#e1-exampleConnection .alert-error').removeClass('hide'); }
else {
$('#e1-exampleConnection .alert-success').removeClass('hide'); }
});
});

};

return Example;
});
48 changes: 48 additions & 0 deletions public/scripts/example-2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
define(function() {
function Example2() {
this.board = null;
this.led = null;
}

Example2.handle = function() {
var that = this;
require(['libs/Noduino', 'libs/Noduino.Socket'], function(NoduinoObj, Connector) {
var Noduino = new NoduinoObj({debug: true, host: 'http://localhost:8090'}, Connector);
Noduino.connect(function(err, board) {
$('#e2-exampleConnection .alert').addClass('hide');
if (err) {
$('#e2-exampleConnection .alert-error').removeClass('hide'); }
else {
$('#e2-exampleConnection .alert-success').removeClass('hide');
that.board = board;
}
});
});

};

Example2.start = function(pin, interval) {
var that = this;
if (!that.led) {
this.board.withLED({pin: pin}, function(err, LED) {
if (err) { return console.log(err); }

that.led = LED;
that.led.blink(interval);
that.led.on('change', function(data) {
if (data.mode == '000') {
$('#e2-status').removeClass('label-success');
$('#e2-status').html('OFF');
} else {
$('#e2-status').addClass('label-success');
$('#e2-status').html('ON');
}
});
});
} else {
that.led.blink(interval);
}
};

return Example2;
});
34 changes: 34 additions & 0 deletions public/scripts/example-3.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
define(function() {
function Example3() {

}

Example3.handle = function() {

require(['libs/Noduino', 'libs/Noduino.Socket'], function(NoduinoObj, Connector) {
var Noduino = new NoduinoObj({debug: true, host: 'http://localhost:8090'}, Connector);
Noduino.connect(function(err, board) {
$('#e3-exampleConnection .alert').addClass('hide');
if (err) {
$('#e3-exampleConnection .alert-error').removeClass('hide'); }
else {
$('#e3-exampleConnection .alert-success').removeClass('hide'); }

board.withButton({pin: 6}, function(err, Button) {
Button.on('change', function(B) {
if (B.pushed) {
$('#e3-exampleConnection #buttonStatus').html('pushed');
$('#e3-exampleConnection #buttonStatus').addClass('label-success');
} else {
$('#e3-exampleConnection #buttonStatus').html('not pushed');
$('#e3-exampleConnection #buttonStatus').removeClass('label-success');
}
});
});
});
});

};

return Example3;
});
Loading

0 comments on commit 49b41cd

Please sign in to comment.