mirror of
https://github.com/GoogleChrome/chrome-extensions-samples.git
synced 2026-04-03 14:39:37 +07:00
91 lines
2.4 KiB
JavaScript
91 lines
2.4 KiB
JavaScript
var connectionId = -1;
|
|
|
|
function setPosition(position) {
|
|
var buffer = new ArrayBuffer(1);
|
|
var uint8View = new Uint8Array(buffer);
|
|
uint8View[0] = '0'.charCodeAt(0) + position;
|
|
chrome.serial.send(connectionId, buffer, function() {});
|
|
};
|
|
|
|
function onReceive(receiveInfo) {
|
|
if (receiveInfo.connectionId !== connectionId)
|
|
return;
|
|
|
|
var uint8View = new Uint8Array(receiveInfo.data);
|
|
var value = uint8View[uint8View.length - 1] - '0'.charCodeAt(0);
|
|
var rotation = value * 18.0;
|
|
document.getElementById('image').style.webkitTransform =
|
|
'rotateZ(' + rotation + 'deg)';
|
|
};
|
|
|
|
function onError(errorInfo) {
|
|
console.warn("Receive error on serial connection: " + errorInfo.error);
|
|
};
|
|
|
|
chrome.serial.onReceive.addListener(onReceive);
|
|
chrome.serial.onReceiveError.addListener(onError);
|
|
|
|
function onOpen(connectionInfo) {
|
|
if (!connectionInfo) {
|
|
setStatus('Could not open');
|
|
return;
|
|
}
|
|
connectionId = connectionInfo.connectionId;
|
|
setStatus('Connected');
|
|
setPosition(0);
|
|
};
|
|
|
|
function setStatus(status) {
|
|
document.getElementById('status').innerText = status;
|
|
}
|
|
|
|
function buildPortPicker(ports) {
|
|
var eligiblePorts = ports.filter(function(port) {
|
|
return !port.path.match(/[Bb]luetooth/);
|
|
});
|
|
|
|
var portPicker = document.getElementById('port-picker');
|
|
eligiblePorts.forEach(function(port) {
|
|
var portOption = document.createElement('option');
|
|
portOption.value = portOption.innerText = port.path;
|
|
portPicker.appendChild(portOption);
|
|
});
|
|
|
|
portPicker.onchange = function() {
|
|
if (connectionId != -1) {
|
|
chrome.serial.disconnect(connectionId, openSelectedPort);
|
|
return;
|
|
}
|
|
openSelectedPort();
|
|
};
|
|
}
|
|
|
|
function openSelectedPort() {
|
|
var portPicker = document.getElementById('port-picker');
|
|
var selectedPort = portPicker.options[portPicker.selectedIndex].value;
|
|
chrome.serial.connect(selectedPort, onOpen);
|
|
}
|
|
|
|
onload = function() {
|
|
var tv = document.getElementById('tv');
|
|
navigator.webkitGetUserMedia(
|
|
{video: true},
|
|
function(stream) {
|
|
tv.classList.add('working');
|
|
document.getElementById('camera-output').src =
|
|
webkitURL.createObjectURL(stream);
|
|
},
|
|
function() {
|
|
tv.classList.add('broken');
|
|
});
|
|
|
|
document.getElementById('position-input').onchange = function() {
|
|
setPosition(parseInt(this.value, 10));
|
|
};
|
|
|
|
chrome.serial.getDevices(function(ports) {
|
|
buildPortPicker(ports)
|
|
openSelectedPort();
|
|
});
|
|
};
|