Jumperless/JumperlessNano/Adafruit_TinyUSB_Arduino/docs/examples/webusb-rgb/application.js
2024-03-14 11:36:30 -07:00

55 lines
1.4 KiB
JavaScript
Executable File

(function() {
'use strict';
document.addEventListener('DOMContentLoaded', event => {
let connectButton = document.querySelector("#connect");
let statusDisplay = document.querySelector('#status');
let port;
function connect() {
port.connect().then(() => {
statusDisplay.textContent = '';
connectButton.textContent = 'Disconnect';
port.onReceiveError = error => {
console.error(error);
};
}, error => {
statusDisplay.textContent = error;
});
}
connectButton.addEventListener('click', function() {
if (port) {
port.disconnect();
connectButton.textContent = 'Connect';
statusDisplay.textContent = '';
port = null;
} else {
serial.requestPort().then(selectedPort => {
port = selectedPort;
connect();
}).catch(error => {
statusDisplay.textContent = error;
});
}
});
serial.getPorts().then(ports => {
if (ports.length === 0) {
statusDisplay.textContent = 'No device found.';
} else {
statusDisplay.textContent = 'Connecting...';
port = ports[0];
connect();
}
});
let colorPicker = document.getElementById("color_picker");
colorPicker.addEventListener("change", function(event) {
port.send(new TextEncoder("utf-8").encode(colorPicker.value));
});
});
})();