Skip to content

Instantly share code, notes, and snippets.

@bwhite
Created May 2, 2014 00:03
Show Gist options
  • Save bwhite/11464794 to your computer and use it in GitHub Desktop.
Save bwhite/11464794 to your computer and use it in GitHub Desktop.
[wearscript] Magnet ring phone to glass
<html style="width:100%; height:100%; overflow:hidden">
<body style="width:100%; height:100%; overflow:hidden; margin:0">
<script>
function main() {
if (WS.scriptVersion(1)) return;
}
window.onload = main;
</script></body></html>
{"name":"Control",
"scripts": {"android:glass": "glass.html", "android:phone": "phone.html"}
}
<html style="width:100%; height:100%; overflow:hidden">
<body style="width:100%; height:100%; overflow:hidden; margin:0">
<script>
function magnetRing(callback) {
state = undefined;
medianBuffer = [];
var medianSize = 15;
median = undefined;
minDist = 10;
minEventTime = .25;
lastEvent = 0;
WS.sensorOn('magneticField', .2, function (data) {
var v = data['values'];
var mag = Math.sqrt(v[0] * v[0] + v[1] * v[1] + v[2] * v[2]);
medianBuffer.push(mag);
if (medianBuffer.length >= medianSize) {
medianBuffer.sort();
median = medianBuffer[Math.round(medianSize / 2)];
medianBuffer = [];
WS.log('Median: ' + median);
}
if (!median)
return;
var d = mag - median;
if (d > minDist || d < -minDist) {
var curTime = (new Date).getTime() / 1000;
if (curTime - lastEvent < minEventTime)
return;
if (state !== 1) {
lastEvent = curTime;
state = 1;
//WS.say('1');
//WS.log('1: ' + mag);
callback();
}
} else {
if (state !== 0) {
state = 0;
//WS.say('0');
//WS.log('0: ' + mag);
}
}
});
}
function wake() {
WS.publish('android:glass', 'lambda', 'WS.wake()');
}
function main() {
if (WS.scriptVersion(1)) return;
WS.say('running');
myoOn = false;
magnetOn = false;
// Pebble
WS.subscribe("gesture:pebble:singleClick:UP", function () {
wake();
WS.control('SWIPE_LEFT');
});
WS.subscribe("gesture:pebble:singleClick:DOWN", function () {
wake();
WS.control('SWIPE_RIGHT');
});
WS.subscribe("gesture:pebble:singleClick:SELECT", function () {
WS.control('TAP');
});
WS.subscribe("gesture:pebble:longClick:SELECT", function () {
WS.control('SWIPE_DOWN');
});
WS.subscribe("gesture:pebble:longClick:UP", function () {
myoOn = !myoOn;
if (myoOn)
magnetOn = false;
WS.say('Myo ' + (myoOn ? 'on' : 'off'));
});
WS.subscribe("gesture:pebble:longClick:DOWN", function () {
magnetOn = !magnetOn;
if (magnetOn)
myoOn = false;
WS.say('Magnet ' + (magnetOn ? 'on' : 'off'));
});
// Magnet ring
magnetRing(function () {
if (!magnetOn)
return;
wake();
WS.control('SWIPE_RIGHT');
});
// Myo
WS.subscribe("gesture:myo:WAVE_IN", function () {
if (!myoOn)
return;
WS.control('SWIPE_LEFT');
});
WS.subscribe("gesture:myo:WAVE_OUT", function () {
if (!myoOn)
return;
WS.control('SWIPE_RIGHT');
});
WS.subscribe("gesture:myo:FIST", function () {
if (!myoOn)
return;
WS.control('TAP');
});
WS.subscribe("gesture:myo:FINGERS_SPREAD", function () {
if (!myoOn)
return;
WS.control('SWIPE_DOWN');
});
WS.myoPair(WS.myoTrain);
}
window.onload = main;
</script></body></html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment