Created
February 24, 2014 23:51
-
-
Save bwhite/9199776 to your computer and use it in GitHub Desktop.
[wearscriptold] Working copy of cards, currently integrated
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 style="width:100%; height:100%; overflow:hidden"> | |
<head> | |
<!--<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> | |
function Cards(cards) { | |
this.cards = cards || []; | |
this.add = function (text, info) { | |
var isFunc = function (x) {return typeof x === 'function'}; | |
var isObj = function (x) {return typeof x === 'object'}; | |
var isUndef = function (x) {return typeof x === 'undefined'}; | |
var isStr = function (x) {return typeof x === 'string'}; | |
var click, select, children, menu; | |
var card = {card: {type: "card", text: text, info: info}}; | |
var extras = Array.prototype.slice.call(arguments).slice(2); | |
if (extras.length > 0 && isFunc(extras[0]) || isUndef(extras[0])) { // Select | |
if (isFunc(extras[0])) | |
card.select = extras[0]; | |
extras = extras.slice(1); | |
} | |
if (extras.length > 0 && isFunc(extras[0]) || isUndef(extras[0])) { // Click | |
if (isFunc(extras[0])) | |
card.click = extras[0]; | |
extras = extras.slice(1); | |
} | |
if (extras.length > 0 && isObj(extras[0])) { // Children | |
card.children = extras[0]; | |
extras = extras.slice(1); | |
} else if (extras.length > 0 && extras.length % 2 == 0) { // Menu | |
card.menu = []; | |
for (var i = 0; i < extras.length; i += 2) { | |
if (!isStr(extras[i]) || (!isFunc(extras[i + 1]) && !isUndef(extras[i + 1]))) | |
break; | |
card.menu.push({'label': extras[i], 'callback': extras[i + 1]}); | |
} | |
} | |
this.cards.push(card); | |
return this; | |
} | |
} | |
function WearScript() { | |
this.callbacks = {}; | |
this.cbCount = 0; | |
this.scriptVersion = function (num) { | |
WSRAW.scriptVersion(num); | |
} | |
this.shutdown = function () { | |
WSRAW.shutdown(); | |
} | |
this.sensor = function (x) { | |
return WSRAW.sensor(x); | |
} | |
this.sensors = function () { | |
return JSON.parse(WSRAW.sensors()); | |
} | |
this._funcwrap = function (func) { | |
var getType = {}; | |
var isFunction = func && getType.toString.call(func) === '[object Function]'; | |
if (isFunction) { | |
var funcName = 'WSCB' + this.cbCount; | |
this.cbCount += 1; | |
window[funcName] = func; | |
return funcName; | |
} | |
return func; | |
} | |
this.sensorOn = function(type, period, callback) { | |
WSRAW.sensorOn(type, period, this._funcwrap(callback)); | |
} | |
this.sensorOff = function(type) { | |
WSRAW.sensorOff(type); | |
} | |
this.dataLog = function (remote, local, period) { | |
WSRAW.dataLog(remote, local, period); | |
} | |
this.say = function (msg) { | |
WSRAW.say(msg); | |
} | |
this.qr = function (callback) { | |
WSRAW.qr(this._funcwrap(callback)); | |
} | |
this.log = function (msg) { | |
WSRAW.log(msg); | |
} | |
this.displayWebView = function () { | |
WSRAW.displayWebView(); | |
} | |
this.displayCardTree = function () { | |
WSRAW.displayCardTree(); | |
} | |
this.cardTree = function (tree) { | |
WSRAW.cardTree(JSON.stringify(tree.cards)) | |
} | |
this.cameraOn = function (period) { | |
WSRAW.cameraOn(period); | |
} | |
this.cameraPhoto = function () { | |
WSRAW.cameraPhoto(); | |
} | |
this.cameraVideo = function () { | |
WSRAW.cameraVideo(); | |
} | |
this.cameraOff = function () { | |
WSRAW.cameraOff(); | |
} | |
this.activityCreate = function () { | |
WSRAW.activityCreate(); | |
} | |
this.activityDestroy = function () { | |
WSRAW.activityDestroy(); | |
} | |
this.wifiOn = function (callback) { | |
// TODO: Handle no callback case | |
WSRAW.wifiOn(this._funcwrap(callback)); | |
} | |
this.wifiOff = function () { | |
WSRAW.wifiOff(); | |
} | |
this.wifiScan = function () { | |
WSRAW.wifiScan(); | |
} | |
this.serverConnect = function (server, callback) { | |
WSRAW.serverConnect(server, this._funcwrap(callback)); | |
} | |
this.wake = function () { | |
WSRAW.wake(); | |
} | |
this.sound = function (sound) { | |
WSRAW.sound(sound); | |
} | |
this.gestureCallback = function (event, callback) { | |
WSRAW.gestureCallback(event, this._funcwrap(callback)); | |
} | |
this.speechRecognize = function (prompt, callback) { | |
WSRAW.speechRecognize(prompt, this._funcwrap(callback)); | |
} | |
this.liveCardCreate = function (nonSilent, period) { | |
WSRAW.liveCardCreate(nonSilent, period); | |
} | |
this.liveCardDestroy = function () { | |
WSRAW.liveCardDestroy(); | |
} | |
} | |
function onGesture(name) { | |
WS.log('onGesture: ' + name); | |
} | |
function onFingerCountChanged(i, i2) { | |
WS.log('onFingerCountChanged: ' + i + ', ' + i2); | |
} | |
function onScroll(v, v2, v3) { | |
WS.log('onScroll: ' + v + ', ' + v2 + ', ' + v3); | |
} | |
function onTwoFingerScroll(v, v2, v3) { | |
WS.log('onTwoFingerScroll: ' + v + ', ' + v2 + ', ' + v3); | |
} | |
function speech(data) { | |
WS.log('speech: ' + data); | |
WS.say('you said ' + data); | |
} | |
function server() { | |
WS.displayCardTree(); | |
var tree = new Cards(); | |
tree.add('Summary', 'Auto', function () {WS.say('selected')}, function () {WS.say('tap')}); // Select + Tap | |
tree.add('Summary', 'Auto', 'Say', function () {WS.say('Say clicked')}, 'Log', function () {WS.log('log clicked')}); // Menu | |
tree.add('Summary', 'Auto', function () {WS.say('selected')}, 'Say', function () {WS.say('Say clicked')}); // Menu + Select | |
tree.show(); | |
return; | |
tree.add('Summary', 'Auto').add('A', '', (new Cards()).add('A0').add('A1').add('A2')) | |
//tree.add() | |
//tree.add('Summary', 'Auto').add('A', '', (new Cards()).add('A0').add('A1').add('A2')).add('B', '', (new Cards()).add('B0').add('B1').add('B2').add('B3')).add('C', '').add('D', '').add('E', ''); | |
WS.cardTree(tree) | |
return; | |
WS.log('Welcome to WearScript'); | |
WS.say('Welcome to WearScript'); | |
var sensors = ['gps', 'accelerometer', 'magneticField', 'orientation', 'gyroscope', | |
'light', 'gravity', 'linearAcceleration', 'rotationVector']; | |
for (var i = 0; i < sensors.length; i++) | |
WS.sensorOn(WS.sensor(sensors[i]), .15, function (data) { | |
// Changes canvas color with head rotation | |
if (data['type'] == WS.sensor('orientation')) { | |
ctx.fillStyle = 'hsl(' + data['values'][0] + ', 90%, 50%)' | |
ctx.fillRect(0, 0, 640, 360); | |
} | |
}); | |
WS.cameraOn(2); | |
WS.dataLog(false, true, .15); | |
var gestures = ['onGesture', 'onFingerCountChanged', 'onScroll', 'onTwoFingerScroll']; | |
for (var i = 0; i < gestures.length; i++) | |
WS.gestureCallback(gestures[i], gestures[i]); | |
WS.liveCardCreate(false, .2, 'Open', function () {WS.wake();WS.activityCreate();}, 'Close', function () {WS.shutdown();}); | |
WSRAW.sound('SUCCESS') | |
//WS.speechRecognize('Say Something', 'speech'); | |
//WS.cameraPhoto(); | |
//WS.cameraVideo(); | |
//WS.cameraOff(); | |
//WS.shutdown(); | |
} | |
function main() { | |
WS = new WearScript(); | |
if (WS.scriptVersion(1)) return; | |
ctx = 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