├── MyChromeArduinoBlink ├── MyChromeBlink │ └── MyChromeBlink.ino ├── README.md ├── assets │ ├── icon-128x128.jpeg │ ├── icon-16x16.jpeg │ ├── screenshot-640x400.jpeg │ └── tile-440x280.jpeg ├── background.js ├── blink.js ├── chrome-logo.svg ├── main.html ├── manifest.json └── styles.css └── README.md /MyChromeArduinoBlink/MyChromeBlink/MyChromeBlink.ino: -------------------------------------------------------------------------------- 1 | /* 2 | MyChromeBlink 3 | Turns on an LED on for length you pass in seconds, 4 | then off for one second, repeatedly. 5 | 6 | Copyright 2013 Renaun Erickson @renaun http://renaun.com 7 | Use under a MIT license 8 | */ 9 | 10 | // Pin 13 has an LED connected on most Arduino boards. 11 | // give it a name: 12 | int led = 13; 13 | int lightOnLength = 1; 14 | int blinkCount = 0; 15 | 16 | // the setup routine runs once when you press reset: 17 | void setup() { 18 | Serial.begin(9600); 19 | // initialize the digital pin as an output. 20 | pinMode(led, OUTPUT); 21 | } 22 | 23 | // the loop routine runs over and over again forever: 24 | void loop() { 25 | Serial.print(1); 26 | Serial.print("a"); // My Command Values 27 | digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level) 28 | delay(lightOnLength * 1000); // wait for a second 29 | Serial.print(0); 30 | Serial.print("a"); // My Command Values 31 | digitalWrite(led, LOW); // turn the LED off by making the voltage LOW 32 | delay(1000); // wait for a second 33 | 34 | while (Serial.available() > 0) { 35 | lightOnLength = Serial.read(); 36 | Serial.print(lightOnLength); 37 | Serial.print("b"); // My Command Values 38 | if (lightOnLength <= 0) 39 | lightOnLength = 1; 40 | } 41 | 42 | Serial.print(blinkCount++); 43 | Serial.print("c"); // My Command Values 44 | } 45 | -------------------------------------------------------------------------------- /MyChromeArduinoBlink/README.md: -------------------------------------------------------------------------------- 1 | # Arduino Blink Chrome App 2 | 3 | This app displays a slider that, when dragged, causes the length of the light to be on (pin 13 on Arduino board). The Arduino sketch is included and should be uploaded to the Arduino. 4 | 5 | The Arduino sketch also sends back the blink count. This way you can figure out how to send commands and receive data from a Chrome App to the Arduino using the serial ports. 6 | 7 | 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. 8 | 9 | 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. 10 | 11 | ## Install Chrome App 12 | 13 | Go to Chrome's Tools -> Extensions then click "Load unpacked extension...". Then click on Launch to get it working. 14 | 15 | ## APIs 16 | 17 | * [Serial API](http://developer.chrome.com/trunk/apps/app.hardware.html#serial) 18 | -------------------------------------------------------------------------------- /MyChromeArduinoBlink/assets/icon-128x128.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/renaun/ArduinoExamples/a6bd54b8bb62486bc374305b370273b3200daa74/MyChromeArduinoBlink/assets/icon-128x128.jpeg -------------------------------------------------------------------------------- /MyChromeArduinoBlink/assets/icon-16x16.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/renaun/ArduinoExamples/a6bd54b8bb62486bc374305b370273b3200daa74/MyChromeArduinoBlink/assets/icon-16x16.jpeg -------------------------------------------------------------------------------- /MyChromeArduinoBlink/assets/screenshot-640x400.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/renaun/ArduinoExamples/a6bd54b8bb62486bc374305b370273b3200daa74/MyChromeArduinoBlink/assets/screenshot-640x400.jpeg -------------------------------------------------------------------------------- /MyChromeArduinoBlink/assets/tile-440x280.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/renaun/ArduinoExamples/a6bd54b8bb62486bc374305b370273b3200daa74/MyChromeArduinoBlink/assets/tile-440x280.jpeg -------------------------------------------------------------------------------- /MyChromeArduinoBlink/background.js: -------------------------------------------------------------------------------- 1 | chrome.app.runtime.onLaunched.addListener(function() { 2 | chrome.app.window.create('main.html', { 3 | bounds: { 4 | top: 0, 5 | left: 0, 6 | width: 640, 7 | height: 720 8 | } 9 | }); 10 | }) 11 | -------------------------------------------------------------------------------- /MyChromeArduinoBlink/blink.js: -------------------------------------------------------------------------------- 1 | var connectionId = -1; 2 | var readBuffer = ""; 3 | 4 | function setPosition(position) { 5 | var buffer = new ArrayBuffer(1); 6 | var uint8View = new Uint8Array(buffer); 7 | uint8View[0] = position; 8 | chrome.serial.write(connectionId, buffer, function() {}); 9 | }; 10 | 11 | function onRead(readInfo) { 12 | var uint8View = new Uint8Array(readInfo.data); 13 | var value = String.fromCharCode(uint8View[0]); 14 | 15 | if (value == "a") // Light on and off 16 | { 17 | console.log("CMD[a]: " + readBuffer); 18 | var opat = isNaN(parseInt(readBuffer)) ? 0 : parseInt(readBuffer); 19 | 20 | document.getElementById('image').style.opacity = (opat* 0.7) + 0.3; 21 | readBuffer = ""; 22 | } 23 | else if (value == "b") // Return blink length value 24 | { 25 | readBuffer = ""; 26 | } 27 | else if (value == "c") // Blink Count 28 | { 29 | console.log("CMD[c]: " + readBuffer); 30 | document.getElementById('blinkCount').innerText = readBuffer; 31 | readBuffer = ""; 32 | } 33 | else 34 | { 35 | 36 | readBuffer += value; 37 | } 38 | // Keep on reading. 39 | chrome.serial.read(connectionId, 1, onRead); 40 | }; 41 | 42 | function onOpen(openInfo) { 43 | connectionId = openInfo.connectionId; 44 | console.log("connectionId: " + connectionId); 45 | if (connectionId == -1) { 46 | setStatus('Could not open'); 47 | return; 48 | } 49 | setStatus('Connected'); 50 | 51 | setPosition(0); 52 | chrome.serial.read(connectionId, 1, onRead); 53 | }; 54 | 55 | function setStatus(status) { 56 | document.getElementById('status').innerText = status; 57 | } 58 | 59 | function buildPortPicker(ports) { 60 | var eligiblePorts = ports.filter(function(port) { 61 | return !port.match(/[Bb]luetooth/) && port.match(/\/dev\/tty/); 62 | }); 63 | 64 | var portPicker = document.getElementById('port-picker'); 65 | eligiblePorts.forEach(function(port) { 66 | var portOption = document.createElement('option'); 67 | portOption.value = portOption.innerText = port; 68 | portPicker.appendChild(portOption); 69 | }); 70 | 71 | portPicker.onchange = function() { 72 | if (connectionId != -1) { 73 | chrome.serial.close(connectionId, openSelectedPort); 74 | return; 75 | } 76 | openSelectedPort(); 77 | }; 78 | } 79 | 80 | function openSelectedPort() { 81 | var portPicker = document.getElementById('port-picker'); 82 | var selectedPort = portPicker.options[portPicker.selectedIndex].value; 83 | chrome.serial.open(selectedPort, onOpen); 84 | } 85 | 86 | onload = function() { 87 | 88 | document.getElementById('position-input').onchange = function() { 89 | setPosition(parseInt(this.value, 10)); 90 | }; 91 | 92 | chrome.serial.getPorts(function(ports) { 93 | buildPortPicker(ports) 94 | openSelectedPort(); 95 | }); 96 | }; 97 | -------------------------------------------------------------------------------- /MyChromeArduinoBlink/chrome-logo.svg: -------------------------------------------------------------------------------- 1 | 2 | 233 | -------------------------------------------------------------------------------- /MyChromeArduinoBlink/main.html: -------------------------------------------------------------------------------- 1 | 2 | 3 |
4 | 5 | 6 | 7 | 8 |