Skip to content

Instantly share code, notes, and snippets.

@kevint2u
Created April 12, 2014 18:37
Show Gist options
  • Save kevint2u/10550096 to your computer and use it in GitHub Desktop.
Save kevint2u/10550096 to your computer and use it in GitHub Desktop.
[wearscript] Scenery App
<html style="width:100%; height:100%; overflow:hidden">
<head>
<!-- You can include external scripts here like so... -->
<!--<script src="https://cdnjs.cloudflare.com/ajax/libs/zepto/1.0/zepto.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.5.2/underscore-min.js"></script>-->
</head>
<body style="width:100%; height:100%; overflow:hidden; margin:0">
<canvas id="canvas" width="640" height="360" style="display:block"></canvas>
<script>
// Constants
var DBG = true;
var i = 0;
var secondLineText = "Second line";
var wakeTime;
var timeDeltaText = "";
var wakeInterval = 300000;
var timeNow = new Date().getTime();
var tillNextWake = Math.floor(wakeInterval / 1000);
var deltasMaxLength = 10;
var timeDeltas = [];
var woken;
var timeDeltaTitleText = "Between Last Wakes:";
var batteryLevelText = "Battery level: ";
var showDashboard = true;
var cameraCB;
var showImageInterval = 10000;
var pauseFudgeFactor = 500;
var videoLength = 10;
// AUXILIARY FUNCTIONS
function drawModeName(name) {
upperBase = 40;
context.fillStyle = "rgb(200,200,200)";
context.font = '28pt Calibri';
context.textAlign = 'left';
context.fillText(name, 10, upperBase);
}
function drawSecondLine(name) {
upperBase = 90;
context.fillStyle = "rgb(200,200,200)";
context.font = '28pt Calibri';
context.textAlign = 'left';
context.fillText(name, 10, upperBase);
}
function drawTextLine(text, _number) {
var number = _number;
if ( number === undefined ) {
console.log("No number given, setting to 0");
number = 0;
}
baseline = 40 + 50 * number;
context.fillStyle = "rgb(200,200,200)";
context.font = '28pt Calibri';
context.textAlign = 'left';
context.fillText(text, 10, baseline);
}
function clearCanvas() {
context.fillStyle = "rgb(0, 0, 0)";
context.fillRect(0, 0, 640, 360);
}
Date.prototype.today = function () {
return ((this.getDate() < 10)?"0":"") + this.getDate() +"/"+(((this.getMonth()+1) < 10)?"0":"") + (this.getMonth()+1) +"/"+ this.getFullYear();
}
Date.prototype.timeNow = function () {
return ((this.getHours() < 10)?"0":"") + this.getHours() +":"+ ((this.getMinutes() < 10)?"0":"") + this.getMinutes() +":"+ ((this.getSeconds() < 10)?"0":"") + this.getSeconds();
}
// MAIN
function draw() {
if (showDashboard) {
clearCanvas();
drawTextLine(firstLineText, 0 );
drawTextLine(secondLineText, 1 );
drawTextLine(tillNextWake, 2 );
drawTextLine(timeDeltaTitleText, 3 );
drawTextLine(timeDeltaText, 4 );
drawTextLine(batteryLevelText, 5 );
}
// list of last times
// drawModeName("" + i);
// drawSecondLine(secondLineText);
}
currentTimeString = function () {
return new Date().today() + " @ " + new Date().timeNow();
}
timeString = function(date) {
return date.today() + " @ " + date.timeNow();
}
function server() {
if (!DBG) WS.serverConnect('0.0.0.0', null);
WS.log('Welcome to WearScript');
WS.say('Welcome to WearScript');
WS.sound('SUCCESS');
WS.dataLog(true, false, 1);
WS.sensorOn("battery", 1, function (data) {
console.log("Got battery callback");
console.log(JSON.stringify(data));
batteryLevelText = "Battery Level: " + Math.round(data.values[0] * 100) + "%";
if (Math.round(data.values[0] * 100) <= 20) WS.say("Low battery.");
batteryLevelText += " @ " + new Date(parseInt(data.timestamp * 1000)).timeNow();
});
clearCanvas();
drawModeName("Rock and roll");
var updateCurrentTime = function() {
var date = new Date();
timeNow = date.getTime();
tillNextWake = "Till Next Wake: " + Math.ceil((wakeInterval - (timeNow - wakeTime)) / 1000);
//firstLineText = currentTimeString();
firstLineText = timeString(date);
draw();
}
var updateWakeTime = function () {
if (wakeTime != undefined) {
woken = true;
}
var date = new Date();
lastWakeTime = wakeTime;
wakeTime = date.getTime();
wakeTimeString = timeString(date);
wakeTimeDelta = wakeTime - lastWakeTime;
if (woken) {
timeDeltas.unshift(Math.floor(wakeTimeDelta / 1000));
if (timeDeltas.length > deltasMaxLength) timeDeltas.pop();
}
timeDeltaText = JSON.stringify(timeDeltas);
secondLineText = "Last Wake: " + currentTimeString();
//thirdLineText = "Time since last wake: ";
//secondLineText = "current time";
draw();
}
updateCurrentTime();
updateWakeTime();
// WS.cameraOn(0.5, 480, 640, cameraCB);
setInterval(updateCurrentTime, 1000);
var img = new Image();
img.onload = function() {
context.drawImage(img, 0, 0);
}
cameraCB = function(data) {
WS.log("Got camera callback.");
showDashboard = false;
WS.cameraOff();
//context.drawImage(img, 0, 0);
img.src = 'data:image/jpg;base64,' + data;
setTimeout(function() {
showDashboard = true;
//WS.liveCardCreate(true, .45);
WS.activityDestroy();
}, showImageInterval);
//WS.say("Creating live card");
//WS.liveCardDestroy();
}
var periodic = function() {
WS.say("Cheese balls");
WS.wake();
WS.activityCreate();
WS.displayWebView();
// secondLineText = "LastWake: " + new Date().today() + " @ " + new Date().timeNow();
updateWakeTime();
WSRAW.recordWSVideo(videoLength);
WS.activityDestroy();
//setTimeout(function() {WS.cameraOn(0.5, 480, 640, cameraCB);}, pauseFudgeFactor);
}
setInterval( periodic, wakeInterval );
// Below this are more examples, uncomment to use them
WS.liveCardCreate(true, .2);
WSRAW.recordWSVideo(videoLength);
/*
var tree = new WS.Cards();
tree.add('Body text', 'Footer text', function () {WS.say('selected')}, function () {WS.say('tapped')}, 'Menu0', function () {WS.say('menu0')}, 'Menu1', function () {WS.say('menu1')});
tree.add('Body text', 'Footer text', (new WS.Cards()).add('Child0', '0').add('Child1', '1'));
WS.cardTree(tree);
WS.displayCardTree();
*/
/*
WS.speechRecognize('Say Something', function (data) {
WS.log('speech: ' + data);
WS.say('you said ' + data);
});
*/
//WS.cameraPhoto();
//WS.cameraVideo();
//WS.cameraOff();
//WS.shutdown();
}
function main() {
if (WS.scriptVersion(1)) return;
context = document.getElementById('canvas').getContext("2d");
WS.serverConnect('{{WSUrl}}', server);
}
window.onload = main;
</script>
</body>
</html>
<html style="width:100%; height:100%; overflow:hidden">
<head>
<meta charset="UTF-8">
<!-- You can include external scripts here like so... -->
<!--<script src="https://cdnjs.cloudflare.com/ajax/libs/zepto/1.0/zepto.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.5.2/underscore-min.js"></script>-->
</head>
<body style="width:100%; height:100%; overflow:hidden; margin:0">
<canvas id="canvas" width="640" height="360" style="display:block"></canvas>
<script>
// Constants
var DBG = true;
var i = 0;
var secondLineText = "Second line";
var wakeTime;
var timeDeltaText = "";
var wakeInterval = 10*60*1000;
// var wakeInterval = 10*60*1000; <-- REAL ONE
var wordInterval = 10*1000;
var timeNow = new Date().getTime();
var tillNextSet = Math.floor(wakeInterval / 1000);
var tillNextWord = Math.floor(wordInterval / 1000);
var deltasMaxLength = 10;
var timeDeltas = [];
var woken;
var timeDeltaTitleText = "Between Last Wakes:";
var batteryLevelText = "Battery level: ";
var showDashboard = true;
var cameraCB;
var showImageInterval = 10000;
var pauseFudgeFactor = 500;
var videoLength = 10;
var words= [// house and furniture
['el apartamento','la casa','el cuarto','la sala','la cocina','cuarto de cenar','vestíbulo','escaleras','el baño','bañera','ducha','la pila','azotea','amario','la mesa','la silla','sofá','escritorio','la ventana','el puerto','la pared','cama','manta','televisión','el rádio','la refrijedora','estufa','el horno','lavaplatos','microonda','la luz','la basura'],
// school
['estudiar','la escuela','el libro','el colegio','la computadora','el diccionário','el papel','el lapiz','el lapicero','profesor','estudiante','maestro','el examen','la universidad','aprender','enseñar','escribir','dibujar','leer']];
var defs = [// house and furniture
['apartment','house','room','living room','kitchen','dining room','hallway','stairs','bathroom','bathtub','shower','sink','roof','closet','table','chair','couch','desk','window','door','wall','bed','blanket','television','radio','refrigerator','stove','oven','dishwasher','microwave','light','garbage'],
// school
['study','school','book','college','computer','dictionary','paper','pencil','pen','professor','student','teacher','test','university','to learn','to teach','to write','to draw','to read']];
var contextIndex = 0;
var wordsLength = words[contextIndex].length;
var wordIndex = 1;
var wordNumber = 1;
var tempTime = 0;
var newWord = true;
var finished = false;
// AUXILIARY FUNCTIONS
function drawModeName(name) {
upperBase = 40;
context.fillStyle = "rgb(200,200,200)";
context.font = '28pt Calibri';
context.textAlign = 'left';
context.fillText(name, 10, upperBase);
}
function drawSecondLine(name) {
upperBase = 90;
context.fillStyle = "rgb(200,200,200)";
context.font = '28pt Calibri';
context.textAlign = 'left';
context.fillText(name, 10, upperBase);
}
function drawTextLine(text, _number) {
var number = _number;
if ( number === undefined ) {
console.log("No number given, setting to 0");
number = 0;
}
baseline = 88 + 100 * number;
context.fillStyle = "rgb(200,200,200)";
context.font = '60pt Calibri';
context.textAlign = 'left';
context.fillText(text, 10, baseline);
}
function drawSubTextLine(text, _number) {
var number = _number;
if ( number === undefined ) {
console.log("No number given, setting to 0");
number = 0;
}
baseline = 250 + 50 * number;
context.fillStyle = "rgb(200,200,200)";
context.font = '28pt Calibri';
context.textAlign = 'left';
context.fillText(text, 10, baseline);
}
function clearCanvas() {
context.fillStyle = "rgb(0, 0, 0)";
context.fillRect(0, 0, 640, 360);
}
Date.prototype.today = function () {
return ((this.getDate() < 10)?"0":"") + this.getDate() +"/"+(((this.getMonth()+1) < 10)?"0":"") + (this.getMonth()+1) +"/"+ this.getFullYear();
}
Date.prototype.timeNow = function () {
return ((this.getHours() < 10)?"0":"") + this.getHours() +":"+ ((this.getMinutes() < 10)?"0":"") + this.getMinutes() +":"+ ((this.getSeconds() < 10)?"0":"") + this.getSeconds();
}
// MAIN
function draw() {
if (showDashboard) {
clearCanvas();
drawTextLine(decodeURIComponent(escape(words[contextIndex][wordIndex])), 0 );
drawTextLine(decodeURIComponent(escape(defs[contextIndex][wordIndex])), 1 );
drawSubTextLine(tillNextWord, 1 );
drawSubTextLine(tillNextSet, 2 );
if(newWord == true){
WS.say(words[contextIndex][wordIndex]);
newWord = false;
}
}
// list of last times
// drawModeName("" + i);
// drawSecondLine(secondLineText);
}
currentTimeString = function () {
return new Date().today() + " @ " + new Date().timeNow();
}
timeString = function(date) {
return date.today() + " @ " + date.timeNow();
}
function server() {
if (!DBG) WS.serverConnect('0.0.0.0', null);
WS.log('Welcome to WearScript');
// WS.say('Welcome to WearScript');
WS.sound('SUCCESS');
WS.dataLog(true, false, 1);
WS.sensorOn("battery", 1, function (data) {
console.log("Got battery callback");
console.log(JSON.stringify(data));
batteryLevelText = "Battery Level: " + Math.round(data.values[0] * 100) + "%";
if (Math.round(data.values[0] * 100) <= 20) WS.say("Low battery.");
batteryLevelText += " @ " + new Date(parseInt(data.timestamp * 1000)).timeNow();
});
clearCanvas();
drawModeName("Rock and roll");
var updateCurrentWordTime = function() {
var date = new Date();
timeNow = date.getTime();
tempTime = Math.ceil((wordInterval - (timeNow - wakeTime)) / 1000);
if(!finished){
while(tempTime<0){
tempTime+=11;
}
} else {
tempTime = 0;
}
if(tempTime>=10){
if(wordNumber<6){
wordNumber+=1;
wordsLength = words[contextIndex].length;
wordIndex = getRandomInt(0,wordsLength-1);
WS.wake();
newWord = true;
} else {
tempTime = 0;
finished = true;
}
}
tillNextWord = "Next Word: " + tempTime;
draw();
}
var updateCurrentSetTime = function() {
var date = new Date();
timeNow = date.getTime();
tillNextSet = "Next Set: " + Math.ceil((wakeInterval - (timeNow - wakeTime)) / 1000);
//firstLineText = currentTimeString();
firstLineText = timeString(date);
draw();
}
var updateWakeTime = function () {
if (wakeTime != undefined) {
woken = true;
}
var date = new Date();
lastWakeTime = wakeTime;
wakeTime = date.getTime();
wakeTimeString = timeString(date);
wakeTimeDelta = wakeTime - lastWakeTime;
if (woken) {
timeDeltas.unshift(Math.floor(wakeTimeDelta / 1000));
if (timeDeltas.length > deltasMaxLength) timeDeltas.pop();
}
timeDeltaText = JSON.stringify(timeDeltas);
secondLineText = "Last Wake: " + currentTimeString();
//thirdLineText = "Time since last wake: ";
//secondLineText = "current time";
draw();
}
updateCurrentSetTime();
updateCurrentWordTime();
updateWakeTime();
function getRandomInt (min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
// WS.cameraOn(0.5, 480, 640, cameraCB);
setInterval(updateCurrentSetTime, 1000);
setInterval(updateCurrentWordTime, 1000);
var img = new Image();
img.onload = function() {
context.drawImage(img, 0, 0);
}
cameraCB = function(data) {
WS.log("Got camera callback.");
showDashboard = false;
WS.cameraOff();
//context.drawImage(img, 0, 0);
img.src = 'data:image/jpg;base64,' + data;
setTimeout(function() {
showDashboard = true;
//WS.liveCardCreate(true, .45);
WS.activityDestroy();
}, showImageInterval);
//WS.say("Creating live card");
//WS.liveCardDestroy();
}
var periodic = function() {
finished = false;
if(wordNumber!=0){
wordNumber=0;
}
newWord = true;
WS.say("Display Words");
WS.wake();
WS.activityCreate();
WS.displayWebView();
// secondLineText = "LastWake: " + new Date().today() + " @ " + new Date().timeNow();
updateWakeTime();
WSRAW.recordWSVideo(videoLength);
WS.activityDestroy();
//setTimeout(function() {WS.cameraOn(0.5, 480, 640, cameraCB);}, pauseFudgeFactor);
}
setInterval( periodic, wakeInterval );
// Below this are more examples, uncomment to use them
WS.liveCardCreate(true, .2);
WSRAW.recordWSVideo(videoLength);
/*
var tree = new WS.Cards();
tree.add('Body text', 'Footer text', function () {WS.say('selected')}, function () {WS.say('tapped')}, 'Menu0', function () {WS.say('menu0')}, 'Menu1', function () {WS.say('menu1')});
tree.add('Body text', 'Footer text', (new WS.Cards()).add('Child0', '0').add('Child1', '1'));
WS.cardTree(tree);
WS.displayCardTree();
*/
/*
WS.speechRecognize('Say Something', function (data) {
WS.log('speech: ' + data);
WS.say('you said ' + data);
});
*/
//WS.cameraPhoto();
//WS.cameraVideo();
//WS.cameraOff();
//WS.shutdown();
}
function main() {
if (WS.scriptVersion(1)) return;
context = document.getElementById('canvas').getContext("2d");
WS.serverConnect('{{WSUrl}}', server);
}
window.onload = main;
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment