Skip to content
Browse files

Demo of trivial IOIO interaction.

Please see README for more information.
  • Loading branch information...
1 parent aedc469 commit 05eec5a15997ae85400e899529afe0f24495e92e Bryan Yeung committed Jul 4, 2012
Showing with 125 additions and 0 deletions.
  1. +13 −0 ioio/README
  2. +6 −0 ioio/background.js
  3. +6 −0 ioio/index.html
  4. +86 −0 ioio/main.js
  5. +14 −0 ioio/manifest.json
View
13 ioio/README
@@ -0,0 +1,13 @@
+- Very simple IOIO client
+
+- Tested with this hardware:
+http://www.adafruit.com/blog/2012/06/15/new-product-ioio-mint-portable-android-development-kit/
+
+- Constants/protocol taken from:
+https://github.com/ytai/ioio/wiki/
+
+Caveats:
+- The bluetooth API is only available on Chrom(e|ium)OS
+- Resource clean-up isn't happening properly yet: you will likely have to disable/enable bluetooth between runs of the program or the connection will fail
+
+Questions/comments: bryeung@chromium.org
View
6 ioio/background.js
@@ -0,0 +1,6 @@
+chrome.experimental.app.onLaunched.addListener(function() {
+ chrome.appWindow.create('index.html', {
+ width: 640,
+ height: 480
+ });
+});
View
6 ioio/index.html
@@ -0,0 +1,6 @@
+<html>
+ <body>
+ <pre id="log"></pre>
+ </body>
+ <script src="main.js"></script>
+</html>
View
86 ioio/main.js
@@ -0,0 +1,86 @@
+function log(msg) {
+ var msg_str = (typeof(msg) == 'object') ? JSON.stringify(msg) : msg;
+ console.log(msg_str);
+
+ var l = document.getElementById('log');
+ if (l) {
+ l.innerText += msg_str + '\n';
+ }
+}
+
+var kUUID = '00001101-0000-1000-8000-00805f9b34fb';
+
+var level = 1;
+var pin = 0;
+function runAtInterval(socket) {
+ return function() {
+ var buffer = new ArrayBuffer(4);
+ var view = new Uint8Array(buffer);
+
+ // Set the level of pin0 to level
+ // constants taken from here:
+ // https://github.com/ytai/ioio/wiki/
+ view[2] = 4;
+ view[3] = pin << 2 | level;
+ level = (level == 0) ? 1 : 0;
+
+ chrome.experimental.bluetooth.write({socketId:socket.id, data:buffer},
+ function(bytes) {
+ if (chrome.runtime.lastError) {
+ log('Write error: ' + chrome.runtime.lastError.message);
+ } else {
+ log('wrote ' + bytes + ' bytes');
+ }
+ });
+ };
+}
+
+var socketId_;
+var intervalId_;
+var connectCallback = function(socket) {
+ if (socket) {
+ log('Connected! Socket ID is: ' + socket.id + ' on service ' +
+ socket.serviceUuid);
+ socketId_ = socket.id;
+
+ // Set pin0 as output.
+ var buffer = new ArrayBuffer(2);
+ var view = new Uint8Array(buffer);
+ // constants taken from here:
+ // https://github.com/ytai/ioio/wiki/
+ view[0] = 3;
+ view[1] = pin << 2 | 2;
+ chrome.experimental.bluetooth.write({socketId:socket.id, data:buffer},
+ function(bytes) {
+ if (chrome.runtime.lastError) {
+ log('Write error: ' + chrome.runtime.lastError.message);
+ } else {
+ log('wrote ' + bytes + ' bytes');
+ }
+ });
+
+ intervalId_ = window.setInterval(runAtInterval(socket), 1000);
+ } else {
+ log('Failed to connect.');
+ }
+};
+
+var connectToDevice = function(result) {
+ if (chrome.runtime.lastError) {
+ log('Error searching for a device to connect to.');
+ return;
+ }
+ if (result.length == 0) {
+ log('No devices found to connect to.');
+ return;
+ }
+ for (var i in result) {
+ var device = result[i];
+ log('Connecting to device: ' + device.name + ' @ ' + device.address);
+ chrome.experimental.bluetooth.connect(
+ {deviceAddress: device.address, serviceUuid: kUUID}, connectCallback);
+ }
+};
+
+log('Starting IOIO demo...');
+chrome.experimental.bluetooth.getDevices({uuid: kUUID}, connectToDevice);
View
14 ioio/manifest.json
@@ -0,0 +1,14 @@
+{
+ "manifest_version": 2,
+ "name": "IOIO Demo",
+ "description": "Blinks the on-board LED",
+ "version": "1.0",
+ "app": {
+ "background": {
+ "scripts": ["background.js"]
+ }
+ },
+ "permissions": [
+ "experimental"
+ ]
+}

0 comments on commit 05eec5a

Please sign in to comment.
Something went wrong with that request. Please try again.