X-Git-Url: https://git-public.kairo.at/?p=tricorder.git;a=blobdiff_plain;f=js%2Ftricorder.js;h=de33ae3a630e2c2eed70a95e946c314aea24b8fd;hp=2dc920767f39b9bb17974ff22fb99ed40ad6a91a;hb=bdfbab86ab3c17a41ab88de60a69427738661392;hpb=14a5754752cd2b02a9f87f27ae54772a3797126c diff --git a/js/tricorder.js b/js/tricorder.js index 2dc9207..de33ae3 100644 --- a/js/tricorder.js +++ b/js/tricorder.js @@ -5,6 +5,8 @@ var gStardate, gSDBase; var gSounds = {}; +navigator.getUserMedia = (navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia); + window.onload = function() { setTimeout(updateStardate, 0); gSounds.scan = new Audio("sound/scan.opus"); @@ -14,20 +16,39 @@ window.onload = function() { gSounds.keyaction = new Audio("sound/key-action.opus"); gSounds.keypress = new Audio("sound/key-press.opus"); + document.getElementById("fullScreenButton").addEventListener("click", + function(aEvent) { toggleFullscreen(); }, false); + + var navItems = document.getElementById("navlist").children; + for (var i = 0; i <= navItems.length - 1; i++) { + navItems[i].addEventListener("click", + function(aEvent) { + switchModule(aEvent.target.id.replace("nav", "")); + }, false); + } + gSounds.launch.play(); window.addEventListener("beforeunload", function( event ) { gSounds.shutdown.play(); }, false); } +window.onresize = function() { + if (document.getElementById("sectSound").classList.contains("active")) { + gModSound.resize(); + } +} + function updateStardate() { if (!gStardate) gStardate = document.getElementById("stardate"); var curDate = new Date(); + // Star Trek famously premiered on Thursday, September 8, 1966, at 8:30 p.m. + // See http://www.startrek.com/article/what-if-the-original-star-trek-had-debuted-on-friday-nights if (!gSDBase) - gSDBase = new Date("September 8, 1966 20:00:00 EST"); + gSDBase = new Date("September 8, 1966 20:30:00 EST"); var sdateval = (curDate - gSDBase) / (86400 * 365.2425); gStardate.textContent = sdateval.toFixed(1); @@ -60,7 +81,7 @@ function toggleFullscreen() { } } -function switchModule(modname) { +function switchModule(aModname) { gSounds.keyaction.play(); var sections = document.getElementsByTagName('section'); for (var i = 0; i <= sections.length - 1; i++) { @@ -74,10 +95,13 @@ function switchModule(modname) { navs[i].classList.remove("active"); } - document.getElementById("nav" + modname).classList.add("active"); - document.getElementById("sect" + modname).classList.add("active"); + var navItem = document.getElementById("nav" + aModname); + navItem.classList.add("active"); + document.getElementById("sect" + aModname).classList.add("active"); + document.getElementById("mainHeader").textContent = + (aModname == "Other") ? "Web Tricorder" : navItem.textContent; - window["gMod" + modname].activate(); + window["gMod" + aModname].activate(); } var gModPos = { @@ -88,28 +112,43 @@ var gModPos = { document.getElementById("posavail").style.display = "block"; this.watchID = navigator.geolocation.watchPosition( function(position) { - document.getElementById("posLat").textContent = position.coords.latitude; - document.getElementById("posLong").textContent = position.coords.longitude; - document.getElementById("posAlt").textContent = position.coords.altitude; - document.getElementById("posAcc").textContent = position.coords.accuracy; - document.getElementById("posAltAcc").textContent = position.coords.altitudeAccuracy; - document.getElementById("posHead").textContent = position.coords.heading || "---"; - document.getElementById("posSpd").textContent = position.coords.speed || "---"; + document.getElementById("posLat").textContent = + position.coords.latitude + "°"; + document.getElementById("posLong").textContent = + position.coords.longitude + "°"; + document.getElementById("posAlt").textContent = + position.coords.altitude.toFixed(0) + " m"; + document.getElementById("posAcc").textContent = + position.coords.accuracy.toFixed(0) + " m"; + document.getElementById("posAltAcc").textContent = + position.coords.altitudeAccuracy.toFixed(0) + " m"; + document.getElementById("posHead").textContent = + position.coords.heading ? position.coords.heading.toFixed(0) + "°" : "---"; + document.getElementById("posSpd").textContent = + position.coords.speed ? position.coords.speed.toFixed(1) + " m/s" : "---"; var locTime = new Date(position.timestamp); document.getElementById("posTime").textContent = locTime.toISOString(); }, function(error) { // See https://developer.mozilla.org/en/Using_geolocation#Handling_errors - document.getElementById("posLat").textContent = error.message; - document.getElementById("posLong").textContent = "..."; - document.getElementById("posAlt").textContent = "..."; - document.getElementById("posAcc").textContent = "..."; - document.getElementById("posAltAcc").textContent = "..."; - document.getElementById("posHead").textContent = "..."; - document.getElementById("posSpd").textContent = "..."; - document.getElementById("posTime").textContent = "..."; + if (error.message) { + document.getElementById("posLat").textContent = error.message; + document.getElementById("posLong").textContent = "..."; + document.getElementById("posAlt").textContent = "..."; + document.getElementById("posAcc").textContent = "..."; + document.getElementById("posAltAcc").textContent = "..."; + document.getElementById("posHead").textContent = "..."; + document.getElementById("posSpd").textContent = "..."; + document.getElementById("posTime").textContent = "..."; + setTimeout(function() { gModPos.deactivate(); }, 5000); + } + else { + document.getElementById("posunavail").style.display = "block"; + document.getElementById("posavail").style.display = "none"; + } + gSounds.scan.pause(); }, - {enableHighAccuracy: true} + {enableHighAccuracy: true, maximumAge: 10000, timeout: 60000} ); } else { @@ -143,6 +182,12 @@ var gModGrav = { document.getElementById("gravavail").style.display = "block"; window.addEventListener("deviceorientation", this.orientEvent, true); window.addEventListener("devicemotion", this.motionEvent, true); + setTimeout(function() { + if ((document.getElementById("gravAlpha").textContent == "...") && + (document.getElementById("gravX").textContent == "...")) { + gModGrav.deactivate(); + } + }, 3000); }, deactivate: function() { gSounds.scan.pause(); @@ -150,6 +195,15 @@ var gModGrav = { window.removeEventListener("devicemotion", this.motionEvent, true); document.getElementById("gravunavail").style.display = "block"; document.getElementById("gravavail").style.display = "none"; + //document.getElementById("gravAbs").textContent = "..."; + document.getElementById("gravAlpha").textContent = "..."; + document.getElementById("gravBeta").textContent = "..."; + document.getElementById("gravGamma").textContent = "..."; + document.getElementById("gravTotal").textContent = "..."; + document.getElementById("gravX").textContent = "..."; + document.getElementById("gravY").textContent = "..."; + document.getElementById("gravZ").textContent = "..."; + //document.getElementById("gravRot").textContent = "..."; }, orientEvent: function(orientData) { //document.getElementById("gravAbs").textContent = orientData.absolute; @@ -158,6 +212,11 @@ var gModGrav = { document.getElementById("gravGamma").textContent = orientData.gamma.toFixed(1) + "°"; }, motionEvent: function(event) { + var gravTotal = + Math.sqrt(Math.pow(event.accelerationIncludingGravity.x, 2) + + Math.pow(event.accelerationIncludingGravity.y, 2) + + Math.pow(event.accelerationIncludingGravity.z, 2)); + document.getElementById("gravTotal").textContent = gravTotal.toFixed(2) + " m/s²"; document.getElementById("gravX").textContent = event.accelerationIncludingGravity.x.toFixed(2) + " m/s²"; document.getElementById("gravY").textContent = event.accelerationIncludingGravity.y.toFixed(2) + " m/s²"; document.getElementById("gravZ").textContent = event.accelerationIncludingGravity.z.toFixed(2) + " m/s²"; @@ -165,7 +224,125 @@ var gModGrav = { }, } -var gModAcou = { +var gModSound = { + activate: function() { + //gSounds.scan.play(); + if (navigator.getUserMedia && (window.AudioContext || window.webkitAudioContext)) { + document.getElementById("soundunavail").style.display = "none"; + document.getElementById("soundavail").style.display = "block"; + navigator.getUserMedia({ audio: true }, + function(aLocalMediaStream) { + gModSound.mAudio.stream = aLocalMediaStream; + gModSound.mAudio.context = new (window.AudioContext || window.webkitAudioContext)(); + gModSound.mAudio.input = gModSound.mAudio.context.createMediaStreamSource(gModSound.mAudio.stream); + // Could put a filter in between like in http://webaudioapi.com/samples/microphone/ + gModSound.mDisplay.canvas = document.getElementById("soundcanvas"); + gModSound.mDisplay.context = gModSound.mDisplay.canvas.getContext("2d"); + gModSound.rebuildCanvas(); + }, + function(err) { + document.getElementById("soundunavail").style.display = "block"; + document.getElementById("soundavail").style.display = "none"; + console.log(err); + } + ); + } + else { + document.getElementById("soundunavail").style.display = "block"; + document.getElementById("soundavail").style.display = "none"; + } + }, + rebuildCanvas: function() { + if (gModSound.mDisplay.AFRequestID) { + window.cancelAnimationFrame(gModSound.mDisplay.AFRequestID); + gModSound.mDisplay.AFRequestID = 0; + } + gModSound.mDisplay.canvas.height = document.getElementById("soundavail").clientHeight - 4; + gModSound.mDisplay.canvas.width = document.getElementById("soundavail").clientWidth; + gModSound.mAudio.frequencySlices = (gModSound.mDisplay.canvas.width > 512) ? + 512 : + Math.pow(2, Math.floor(Math.log(gModSound.mDisplay.canvas.width) / Math.LN2)); + //console.log("slices: " + gModSound.mAudio.frequencySlices); + gModSound.mAudio.analyzer = gModSound.mAudio.context.createAnalyser(); + // Make the FFT four times as large as needed as the upper three quarters turn out to be useless. + gModSound.mAudio.analyzer.fftSize = gModSound.mAudio.frequencySlices * 4; + //console.log("FFT: " + gModSound.mAudio.analyzer.fftSize); + gModSound.mAudio.input.connect(gModSound.mAudio.analyzer); + gModSound.mDisplay.context.setTransform(1, 0, 0, -(gModSound.mDisplay.canvas.height/256), 0, gModSound.mDisplay.canvas.height); + gModSound.mDisplay.active = true; + gModSound.mDisplay.AFRequestID = window.requestAnimationFrame(gModSound.paintAnalyzerFrame); + }, + mAudio: { + frequencySlices: 32, // Must be a multiple of 2 (see AnalyserNode.fftSize) + }, + mDisplay: { + active: false, + }, + paintAnalyzerFrame: function(aTimestamp) { + var data = new Uint8Array(gModSound.mAudio.frequencySlices); + gModSound.mAudio.analyzer.getByteFrequencyData(data); + gModSound.mDisplay.context.clearRect(0, 0, gModSound.mDisplay.canvas.width, gModSound.mDisplay.canvas.height); + // Out of experience, only the first half of the slices are actually useful. + var wid = gModSound.mDisplay.canvas.width / gModSound.mAudio.frequencySlices; + var fill = "#9C9CFF"; + for (var i = 0; i < gModSound.mAudio.frequencySlices; ++i) { + var newFill = (data[i] > 200) ? "#FF0000" : ((data[i] > 100) ? "#FFCF00" : "#9C9CFF"); + if (fill != newFill) { gModSound.mDisplay.context.fillStyle = newFill; fill = newFill; } + gModSound.mDisplay.context.fillRect(i*wid, 0, wid, data[i]); + } + if (gModSound.mDisplay.active) + gModSound.mDisplay.AFRequestID = window.requestAnimationFrame(gModSound.paintAnalyzerFrame); + }, + resize: function() { + gModSound.rebuildCanvas(); + }, + deactivate: function() { + if (gModSound.mDisplay.active) { + gModSound.mAudio.stream.stop(); + gModSound.mDisplay.active = false; + } + gSounds.scan.pause(); + }, +} + +var gModDev = { + activate: function() { + gSounds.scan.play(); + this.batteryTimer = + setInterval(function () { gModDev.updateBattery(); }, 100); + }, + deactivate: function() { + clearTimeout(this.batteryTimer); + gSounds.scan.pause(); + }, + updateBattery: function() { + document.getElementById("devBattLevel").textContent = + (navigator.battery.level * 100).toFixed(1) + "%"; + if (navigator.battery.charging) { + if (navigator.battery.chargingTime == 0 || + navigator.battery.chargingTime == Infinity) { + document.getElementById("devBattStatus").textContent = "charging"; + } + else { + document.getElementById("devBattStatus").textContent = + "charging, " + navigator.battery.chargingTime + "s remaining"; + } + } + else { + if (navigator.battery.dischargingTime == 0 || + navigator.battery.dischargingTime == Infinity) { + document.getElementById("devBattStatus").textContent = "discharging"; + } + else { + document.getElementById("devBattStatus").textContent = + navigator.battery.dischargingTime + "s usage remaining"; + } + } + }, + batteryTimer: null, +} + +var gModOther = { activate: function() { //gSounds.scan.play(); },