Last active
August 29, 2015 13:57
-
-
Save colegleason/9755070 to your computer and use it in GitHub Desktop.
[wearscript] MYO Hue Tug of war
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 style="width:100%; height:100%; overflow:hidden"> | |
<body style="width:100%; height:100%; overflow:hidden; margin:0"> | |
<script data-require="jquery" data-semver="2.0.3" src="http://code.jquery.com/jquery-2.0.3.min.js"></script> | |
<script> | |
var HUE_ID = 1; | |
var PLAYER_A = 1; | |
var PLAYER_B = 3; | |
var TARGET = 2; | |
function getRandomInt (min, max) { | |
return Math.floor(Math.random() * (max - min + 1)) + min; | |
} | |
// See http://en.wikipedia.org/wiki/Lab_color_space#Forward_transformation | |
function xyzToLab(x, y, z) { | |
function f(t) { | |
if (t > Math.pow(6.0/29, 3)) { | |
return Math.pow(t, 1.0/3); | |
} else { | |
return (1.0/3) * Math.pow(29.0/6, 2) * t + (4.0/29); | |
} | |
} | |
var L = 116 * f(y) - 16; | |
var a = 500 * (f(x) - f(y)); | |
var b = 200 * (f(y) - f(z)); | |
return [L, a, b]; | |
} | |
function colorDist(a, b) { | |
WS.log(JSON.stringify(a.state.xy)) | |
a.state.xy.push(0) | |
b.state.xy.push(0) | |
var a_lab = xyzToLab.apply(this, a.state.xy); | |
var b_lab = xyzToLab.apply(this, b.state.xy); | |
return Math.sqrt(Math.pow(a_lab[1] - b_lab[1], 2) - Math.pow(a_lab[2] - b_lab[2], 2)); | |
} | |
function setupGame() { | |
if (HUE_ID == PLAYER_A) { | |
WS.say("You are player A!"); | |
} else if (HUE_ID == PLAYER_B) { | |
WS.say("You are player B!"); | |
} | |
var players = getRandomInt(0, 65535); | |
setLightColor(TARGET, getRandomInt(0, 65535)); | |
setLightColor(PLAYER_A, players); | |
setLightColor(PLAYER_B, players); | |
setLightOn(TARGET, true); | |
setLightOn(PLAYER_A, true); | |
setLightOn(PLAYER_B, true); | |
} | |
function setLightColor(id, color) { | |
var data = {hue:color}; | |
$.ajax({ | |
url: 'http://192.168.1.66/api/wearscript/lights/'+id+'/state', | |
type: 'PUT', | |
dataType: 'application/json', | |
data: JSON.stringify(data), | |
success: function(data) { | |
WS.log(JSON.stringify(data)) | |
console.log('success',data); | |
}, | |
error: function(data){ | |
WS.log(data) | |
} | |
}); | |
} | |
function setLightOn(id, on) { | |
var data = {on: on}; | |
$.ajax({ | |
url: 'http://192.168.1.66/api/wearscript/lights/'+id+'/state', | |
type: 'PUT', | |
dataType: 'application/json', | |
data: JSON.stringify(data), | |
success: function(data) { | |
WS.log(JSON.stringify(data)) | |
console.log('success',data); | |
}, | |
error: function(data){ | |
WS.log(data) | |
} | |
}); | |
} | |
function getLight(id, cb) { | |
$.ajax({ | |
url: 'http://192.168.1.66/api/wearscript/lights/'+id, | |
type: 'GET', | |
success: function(data) { | |
cb(data); | |
}, | |
error: function(data){ | |
WS.log(data); | |
} | |
}); | |
} | |
function getLights(cb) { | |
getLight(PLAYER_A, function(a) { | |
getLight(PLAYER_B, function(b) { | |
getLight(TARGET, function(target) { | |
return cb(a,b,target) | |
}) | |
}) | |
}) | |
} | |
function gameOver(cb) { | |
getLight(TARGET, function(light) { | |
cb(!light.state.on) | |
}) | |
} | |
function setLights(a, b, target) { | |
setLightColor(PLAYER_A, a); | |
setLightColor(PLAYER_B, b); | |
setLightColor(TARGET, target); | |
} | |
function endGame() { | |
getLights(function(a,b,target) { | |
var a_diff = colorDist(a, target); | |
var b_diff = colorDist(b, target); | |
WS.log(JSON.stringify([a.xy, b.xy, target.xy, a_diff, b_diff])); | |
if (a_diff > b_diff) { | |
setLightOn(PLAYER_A,true); | |
setLightOn(PLAYER_B, false); | |
setLightOn(TARGET, false); | |
} else { | |
setLightOn(PLAYER_A, false); | |
setLightOn(PLAYER_B,true); | |
setLightOn(TARGET, false); | |
} | |
}); | |
} | |
function server() { | |
WS.myoTrain() | |
// Currently one of {NONE, FIST, FINGERS_SPREAD, WAVE_IN, WAVE_OUT} | |
WS.gestureCallback('onMyo', function (g) { | |
WS.say(g); | |
WS.log(g); | |
gameOver(function(done) { | |
if (done) { | |
if (g == 'FIST') { | |
setupGame(); | |
} else { | |
return; | |
} | |
} | |
if (g == "FINGERS_SPREAD") { | |
endGame(); | |
} else if (g == "WAVE_IN") { | |
getLight(HUE_ID, function(light) { | |
if (light.state.hue < 0) | |
light.state.hue = 65535; | |
setLightColor(HUE_ID, light.state.hue - 5000); | |
}); | |
} else if (g == "WAVE_OUT") { | |
getLight(HUE_ID, function(light) { | |
if (light.state.hue > 65535) | |
light.state.hue = 0; | |
setLightColor(HUE_ID, light.state.hue + 5000) | |
}); | |
} | |
}) | |
}) | |
} | |
function main() { | |
if (WS.scriptVersion(1)) return; | |
WS.serverConnect('{{WSUrl}}', server); | |
setupGame(); | |
} | |
window.onload = main; | |
</script> | |
</body> | |
</html> |
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
{"name":"Color tug-of-war"} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment