Last active
April 25, 2021 19:37
-
-
Save pimpelsang/1e7a2b67bd0dea468b348412d4a96af0 to your computer and use it in GitHub Desktop.
This file contains hidden or 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> | |
| <head> | |
| <title>wedo 2 piezo speaker test</title> | |
| <script src="https://cdn.jsdelivr.net/npm/node-poweredup@latest/dist/browser/poweredup.js"></script> | |
| <style type="text/css"> | |
| #controls { | |
| display: none; | |
| } | |
| #controls > button { | |
| font-size: 50pt; | |
| } | |
| </style> | |
| </head> | |
| <body> | |
| <button id="connect" onclick="connect()">connect</button> | |
| <div id="controls"> | |
| <button id="c">test C tone</button> | |
| <button id="police">police</button> | |
| <button id="honk">honk</button> | |
| </div> | |
| <script> | |
| if (!PoweredUP.isWebBluetooth) { | |
| alert("Your browser does not support the Web Bluetooth."); | |
| } | |
| const poweredUP = new PoweredUP.PoweredUP(); | |
| poweredUP.on("discover", onDiscover); | |
| let hub, speaker, led; | |
| function connect() { | |
| poweredUP.scan(); | |
| document.getElementById('connect').style.display='none'; | |
| } | |
| async function onDiscover(newHub) { | |
| hub = newHub; | |
| console.log(`Discovered ${hub.name}!`); | |
| await hub.connect(); | |
| speaker = await hub.waitForDeviceByType(PoweredUP.Consts.DeviceType.PIEZO_BUZZER); | |
| console.log(`Got speaker!`); | |
| led = await hub.waitForDeviceByType(PoweredUP.Consts.DeviceType.HUB_LED); | |
| led.setColor(PoweredUP.Consts.ColorNames.NONE); | |
| document.getElementById('controls').style.display='block'; | |
| } | |
| document.getElementById("c").addEventListener('click', c); | |
| document.getElementById("police").addEventListener('click', () => { police(); }); | |
| document.getElementById("honk").addEventListener('click', () => { honk(); }); | |
| function c(e) { | |
| speaker.playTone(261, 300) | |
| } | |
| async function police() { | |
| for(i=0; i<5; i++) { | |
| led.setColor(PoweredUP.Consts.ColorNames.RED); | |
| await speaker.playTone(850, 500) | |
| led.setColor(PoweredUP.Consts.ColorNames.BLUE); | |
| await speaker.playTone(750, 500) | |
| } | |
| led.setColor(PoweredUP.Consts.ColorNames.NONE); | |
| } | |
| async function honk(e) { | |
| led.setColor(PoweredUP.Consts.ColorNames.WHITE); | |
| await speaker.playTone(1100, 1000) | |
| led.setColor(PoweredUP.Consts.ColorNames.NONE); | |
| } | |
| </script> | |
| </body> | |
| </html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment