Created
July 20, 2022 21:59
-
-
Save misterhay/db77ecbe0eac35b5a3995d59c640ddf2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%%html | |
<script> | |
requirejs.config({ | |
paths: { | |
'phidget22': ['https://unpkg.com/phidget22@^3.10/browser/phidget22'], | |
}, // strip .js ^, require adds it back | |
}); | |
var usbconn; | |
require(['phidget22'], function(phidget22) { | |
console.log(phidget22); // or whatever | |
if(usbconn == null){ | |
usbconn = new phidget22.USBConnection({ | |
name: "USB Devices", | |
onError: (code, msg) => { console.error("Connection Error:", msg); } | |
}); | |
} | |
console.log(usbconn) | |
usbconn.connect().catch(err => { | |
usbconn.delete(); | |
console.error("Error connecting to USB", err); | |
alert('Failed to connect to USB: ' + err); | |
}); | |
openHumidityPhidget(); | |
return {}; | |
}); | |
function openHumidityPhidget(){ | |
require(['phidget22'], function(phidget22) { | |
let humiditySensor = new phidget22.HumiditySensor(); | |
humiditySensor.onHumidityChange = function (humidity) { | |
document.getElementById("humidity").innerHTML = humidity.toFixed(2).toString() + "%"; | |
}; | |
humiditySensor.open(); | |
return {}; | |
}); | |
} | |
function connectPhidgets(){ | |
require(['phidget22'], function(phidget22) { | |
console.log(usbconn); // or whatever | |
try { | |
usbconn.requestWebUSBDeviceAccess(); | |
} catch (err) { | |
console.error('Request device error', err); | |
}; | |
return {}; | |
}); | |
} | |
</script> | |
<div> | |
<button onclick="connectPhidgets()">Connect Phidgets</button> | |
<p id="humidity"</p> | |
</div> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment