Skip to content

Commit 47f502c

Browse files
committed
working on gpio and web sockets
1 parent 302482c commit 47f502c

File tree

7 files changed

+286
-0
lines changed

7 files changed

+286
-0
lines changed

coder-apps/tests/gpio_test/app/app.js

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
var gpio = require("gpio");
2+
gpio.logging = true;
3+
4+
var gpioID = 7; //pin 26, bottom right header
5+
var gpioDevice;
6+
var connected = false; //ensure only one process talks to us at a time.
7+
8+
exports.settings={};
9+
//These are dynamically updated by the runtime
10+
//settings.appname - the app id (folder) where your app is installed
11+
//settings.viewpath - prefix to where your view html files are located
12+
//settings.staticurl - base url path to static assets /static/apps/appname
13+
//settings.appurl - base url path to this app /app/appname
14+
//settings.device_name - name given to this coder by the user, Ie."Billy's Coder"
15+
//settings.coder_owner - name of the user, Ie. "Suzie Q."
16+
//settings.coder_color - hex css color given to this coder.
17+
18+
exports.get_routes = [
19+
{ path:'/', handler:'index_handler' },
20+
];
21+
22+
exports.post_routes = [
23+
];
24+
25+
exports.socketio_routes = [
26+
{ key:'connect', handler:'on_socket_connect' },
27+
{ key:'gpio', handler:'on_socket_gpio' },
28+
];
29+
30+
31+
var connections = {};
32+
33+
exports.index_handler = function( req, res ) {
34+
var tmplvars = {};
35+
tmplvars['static_url'] = exports.settings.staticurl;
36+
tmplvars['app_name'] = exports.settings.appname;
37+
tmplvars['app_url'] = exports.settings.appurl;
38+
tmplvars['device_name'] = exports.settings.device_name;
39+
40+
res.render( exports.settings.viewpath + '/index', tmplvars );
41+
};
42+
43+
44+
var enableGPIO = function() {
45+
console.log("Enabling GPIO " + gpioID );
46+
gpioDevice = gpio.export( gpioID, {
47+
ready: function() {
48+
//Pause briefly after pin is exported.
49+
//There seems to be an error if you try to immediately access it.
50+
setTimeout( function() {
51+
console.log("GPIO value: on");
52+
gpioDevice.setDirection("out");
53+
gpioDevice.set(1, function() {
54+
console.log("GPIO should be on");
55+
});
56+
//blinkLED();
57+
}, 100 );
58+
}
59+
});
60+
};
61+
var disableGPIO = function() {
62+
console.log("Disabling GPIO" + gpioID );
63+
gpioDevice.removeAllListeners();
64+
gpioDevice.reset();
65+
gpioDevice.unexport();
66+
};
67+
68+
var ledval = 0;
69+
var blinkLED = function() {
70+
if ( !connected ) {
71+
return;
72+
}
73+
74+
gpioDevice.set( ledval );
75+
if ( ledval == 0 ) {
76+
ledval = 1;
77+
} else {
78+
ledval = 0;
79+
}
80+
81+
//run this method again after half a second
82+
setTimeout( blinkLED, 500 );
83+
};
84+
85+
86+
87+
exports.on_socket_connect = function( socket, data ) {
88+
console.log( 'socket connect from ID: ' + socket.socketID );
89+
console.log( data );
90+
91+
if ( !connected ) {
92+
enableGPIO();
93+
connected = true;
94+
}
95+
96+
connections[socket.socketID] = {
97+
socket: socket,
98+
name: data.name,
99+
id: socket.socketID
100+
};
101+
socket.on('disconnect', function() {
102+
console.log( 'socket disconnect from ID: ' + socket.socketID );
103+
delete connections[socket.socketID];
104+
105+
//Free up the GPIO when the last socket disconnects
106+
if ( Object.keys( connections ).length <= 0 ) {
107+
disableGPIO();
108+
connected = false;
109+
}
110+
});
111+
112+
};
113+
114+
115+
exports.on_socket_gpio = function( socket, data ) {
116+
switch (data.command) {
117+
case "set":
118+
setGPIO( data.value );
119+
break;
120+
case "direction":
121+
setDirection( data.direction );
122+
break;
123+
case "value":
124+
sendValue( socket );
125+
break;
126+
}
127+
};
128+
129+
var setGPIO = function( val ) {
130+
val = parseInt( val );
131+
if ( val != 0 ) {
132+
val = 1;
133+
}
134+
gpioDevice.set( val );
135+
};
136+
var setDirection = function( dir ) {
137+
if ( dir !== "in") {
138+
dir = "out";
139+
}
140+
gpioDevice.setDirection( dir );
141+
};
142+
var sendValue = function( socket ) {
143+
socket.emit( "apdata", {
144+
key: "gpiovalue",
145+
data: gpioDevice.value
146+
});
147+
};
148+
149+
exports.on_destroy = function() {
150+
};
151+
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"created": "2013-11-30",
3+
"modified": "2013-12-03",
4+
"color": "#2ecc71",
5+
"author": "",
6+
"name": "GPIO Test",
7+
"hidden": false
8+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
.pagecontent {
3+
padding: 24px;
4+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
2+
$(document).ready( function() {
3+
4+
//This code will run after your page loads
5+
Coder.socketConnection.init(function(){
6+
7+
addOutputMessage( "Connected with ID: " + Coder.socketConnection.socketID );
8+
addOutputMessage( "Click ON or OFF to enable blinking" );
9+
10+
Coder.socketConnection.sendData( 'connect', {'name':'testing'} );
11+
12+
Coder.socketConnection.addListener( 'Received gpio value', function( d ){
13+
console.log("gpio value: " + d);
14+
});
15+
16+
17+
// Blink every 100ms if enabled is on
18+
// enabled is set below by a button click.
19+
var blinkval = 0;
20+
setInterval( function() {
21+
22+
if ( blinkval ) {
23+
blinkval = 0;
24+
} else {
25+
blinkval = 1;
26+
}
27+
28+
if ( enabled ) {
29+
30+
Coder.socketConnection.sendData( 'gpio', {
31+
command: "set",
32+
value: blinkval
33+
});
34+
}
35+
}, 100 );
36+
37+
38+
39+
//Enable or disable the blinker
40+
var enabled = false;
41+
$("#on").click( function() {
42+
Coder.socketConnection.sendData( 'gpio', {
43+
command: "set",
44+
value: 1
45+
});
46+
enabled = true;
47+
});
48+
$("#off").click( function() {
49+
Coder.socketConnection.sendData( 'gpio', {
50+
command: "set",
51+
value: 0
52+
});
53+
enabled = false;
54+
});
55+
56+
});
57+
58+
addOutputMessage( "Connecting... see the debug console for log messages." );
59+
60+
});
61+
62+
63+
64+
var addOutputMessage = function( text ) {
65+
var $output = $("#output");
66+
$output.prepend( $("<p/>").text( text ) );
67+
console.log( text );
68+
}
69+

coder-apps/tests/gpio_test/static/media/.gitignore

Whitespace-only changes.
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>Coder</title>
5+
<meta charset="utf-8">
6+
<!-- Standard Coder Includes -->
7+
<script>
8+
var appname = "{{app_name}}"; //app name (id) of this app
9+
var appurl = "{{&app_url}}";
10+
var staticurl = "{{&static_url}}"; //base path to your static files /static/apps/yourapp
11+
</script>
12+
<link href="/static/apps/coderlib/css/index.css" media="screen" rel="stylesheet" type="text/css"/>
13+
<script src="/static/common/js/jquery.min.js"></script>
14+
<script src="/static/common/ace-min/ace.js" type="text/javascript" charset="utf-8"></script>
15+
<script src="/static/apps/coderlib/js/index.js"></script>
16+
<script>
17+
Coder.addBasicNav();
18+
</script>
19+
<!-- End Coder Includes -->
20+
21+
<!-- This app's includes -->
22+
<link href="{{&static_url}}/css/index.css" media="screen" rel="stylesheet" type="text/css"/>
23+
<script src="{{&static_url}}/js/index.js"></script>
24+
<!-- End apps includes -->
25+
</head>
26+
<body class="">
27+
<div class="pagecontent">
28+
<h1>GPIO Test</h1>
29+
<div id="on" class="button">ON</div>
30+
<div id="off" class="button">OFF</div>
31+
<div id="output">
32+
33+
</div>
34+
</div>
35+
</body>
36+
</html>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"name": "coder-base",
3+
"description": "kid-friendly web programming environment for pi",
4+
"version": "0.0.1",
5+
"private": true,
6+
"dependencies": {
7+
"express": "3.1.0",
8+
"redis": "0.8.2",
9+
"mustache": "0.7.2",
10+
"consolidate": "0.8.0",
11+
"socket.io": "0.9.13",
12+
"express-params": "0.0.3",
13+
"bcrypt": "0.7.4",
14+
"connect": "*",
15+
"cookie": "*",
16+
"gpio": "*",
17+
}
18+
}

0 commit comments

Comments
 (0)