From b3e82b565f6439f6c728d0997bc5a1c35ae9ff06 Mon Sep 17 00:00:00 2001 From: Ghosh Date: Sun, 5 Apr 2015 15:30:45 +0530 Subject: [PATCH 1/3] Modified the blink.js script to make it work with the latest Chrome Serial API. Aditionally, added logic to keep checking for serial ports if the extension is launched before the arduino is connected to the computer. --- MyChromeArduinoBlink/blink.js | 106 ++++++++++++++++++++++++---------- 1 file changed, 75 insertions(+), 31 deletions(-) diff --git a/MyChromeArduinoBlink/blink.js b/MyChromeArduinoBlink/blink.js index cc497f0..46e7d76 100755 --- a/MyChromeArduinoBlink/blink.js +++ b/MyChromeArduinoBlink/blink.js @@ -1,42 +1,69 @@ var connectionId = -1; -var readBuffer = ""; function setPosition(position) { var buffer = new ArrayBuffer(1); var uint8View = new Uint8Array(buffer); uint8View[0] = position; - chrome.serial.write(connectionId, buffer, function() {}); + chrome.serial.send(connectionId, buffer, function() {}); }; -function onRead(readInfo) { - var uint8View = new Uint8Array(readInfo.data); - var value = String.fromCharCode(uint8View[0]); - +var lock = false; +var arrayReceived=[]; //array of Uint8Arrays +function onRead(readInfo){ + // only append the newly available data to arrayReceived. + arrayReceived = arrayReceived.concat(new Uint8Array(readInfo.data)); + process();//operates on the global arrayReceived array. +} + +var readBuffer = ''; +// made global because in one invocation a portion of a command could be sent. +// we want to the buffer to include incomplete commands from previous invocations of process() too. +// e.g. in one iteration process could only get 1a2c0a1 +// and the next iteration could be a3c0a, so we want the one from the last invocation to paired up +// with a from the next invocation. +function process(){ + // synchronous function + if(lock == false){ + lock = true; + var command = ''; + for(var index=0; index < arrayReceived.length; index++){ + // iterate over all the Uint8Arrays. global variable arrayReceived. + var uint8View = arrayReceived.shift(); + for(var innerIndex=0; innerIndex < uint8View.length; innerIndex++){ + var data = String.fromCharCode(uint8View[innerIndex]); + // data is always a single character. for 11 data is first 1 and then + // in the next iteration another 1 is sent. + console.log("data : " + data); + if(data === 'a' || data === 'b' || data === 'c'){ + command = data; + performAction(command,readBuffer); + command = ''; + readBuffer = ''; + data = ''; + } + readBuffer += data; + } + } + lock = false; + } +} + +function performAction(value,readBuffer){ if (value == "a") // Light on and off { console.log("CMD[a]: " + readBuffer); var opat = isNaN(parseInt(readBuffer)) ? 0 : parseInt(readBuffer); document.getElementById('image').style.opacity = (opat* 0.7) + 0.3; - readBuffer = ""; } else if (value == "b") // Return blink length value { - readBuffer = ""; } else if (value == "c") // Blink Count { console.log("CMD[c]: " + readBuffer); document.getElementById('blinkCount').innerText = readBuffer; - readBuffer = ""; - } - else - { - - readBuffer += value; } - // Keep on reading. - chrome.serial.read(connectionId, 1, onRead); }; function onOpen(openInfo) { @@ -49,22 +76,24 @@ function onOpen(openInfo) { setStatus('Connected'); setPosition(0); - chrome.serial.read(connectionId, 1, onRead); + chrome.serial.onReceive.addListener(onRead); }; function setStatus(status) { document.getElementById('status').innerText = status; } -function buildPortPicker(ports) { - var eligiblePorts = ports.filter(function(port) { +function buildPortPicker(deviceInfoList) { + var eligiblePorts = deviceInfoList.filter(function(deviceInfo) { + var port = deviceInfo.path; return !port.match(/[Bb]luetooth/) && port.match(/\/dev\/tty/); }); var portPicker = document.getElementById('port-picker'); eligiblePorts.forEach(function(port) { var portOption = document.createElement('option'); - portOption.value = portOption.innerText = port; + console.log(port.path); + portOption.value = portOption.innerText = port.path; portPicker.appendChild(portOption); }); @@ -78,19 +107,34 @@ function buildPortPicker(ports) { } function openSelectedPort() { - var portPicker = document.getElementById('port-picker'); - var selectedPort = portPicker.options[portPicker.selectedIndex].value; - chrome.serial.open(selectedPort, onOpen); + var portPicker = document.getElementById('port-picker'); + var selectedPortObject = portPicker.options[portPicker.selectedIndex] + if(typeof selectedPortObject !== 'undefined'){ + var selectedPort = selectedPortObject.value; + console.log("selected port " + selectedPort); + chrome.serial.connect(selectedPort,{'bitrate':9600}, onOpen); + }else{ + setTimeout(checkConnection,3000); + // this is to ensure that if someone launches the app + // before pluging in the arduino. The app doesn't sit there doing nothing. + } } onload = function() { - document.getElementById('position-input').onchange = function() { - setPosition(parseInt(this.value, 10)); - }; - - chrome.serial.getPorts(function(ports) { - buildPortPicker(ports) - openSelectedPort(); - }); + document.getElementById('position-input').onchange = function() { + setPosition(parseInt(this.value, 10)); + }; + checkConnection(); }; + +function checkConnection(){ + console.log("Checking connection."); + chrome.serial.getDevices(function(deviceInfoList) { + // for (var i=0; i Date: Sun, 5 Apr 2015 15:33:39 +0530 Subject: [PATCH 2/3] Updated the README.md --- MyChromeArduinoBlink/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/MyChromeArduinoBlink/README.md b/MyChromeArduinoBlink/README.md index 288416c..5c7d507 100755 --- a/MyChromeArduinoBlink/README.md +++ b/MyChromeArduinoBlink/README.md @@ -6,7 +6,7 @@ The Arduino sketch also sends back the blink count. This way you can figure out To mimic what the Arduino is doing on pin 13 the chrome log turns on and off just like it would on the Arduino pin 13. -The port filters out Bluetooth and only looks for /dev/tty strings, this worked on Mac OS X and Chrome 27 but not sure if its the same on Windows. +The port filters out Bluetooth and only looks for /dev/tty strings, this worked on Mac OS X and Chrome 41 but not sure if its the same on Windows. ## Install Chrome App From 800e41f5123ba8a9a9482d28359308f3d89f9c2d Mon Sep 17 00:00:00 2001 From: kunalghosh Date: Sun, 5 Apr 2015 15:37:26 +0530 Subject: [PATCH 3/3] Updated the README.md with the OS version --- MyChromeArduinoBlink/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/MyChromeArduinoBlink/README.md b/MyChromeArduinoBlink/README.md index 5c7d507..0abd069 100755 --- a/MyChromeArduinoBlink/README.md +++ b/MyChromeArduinoBlink/README.md @@ -6,7 +6,7 @@ The Arduino sketch also sends back the blink count. This way you can figure out To mimic what the Arduino is doing on pin 13 the chrome log turns on and off just like it would on the Arduino pin 13. -The port filters out Bluetooth and only looks for /dev/tty strings, this worked on Mac OS X and Chrome 41 but not sure if its the same on Windows. +The port filters out Bluetooth and only looks for /dev/tty strings, this worked on Mac OS X Mavericks and Chrome 41 but not sure if its the same on Windows. ## Install Chrome App