Skip to content

Instantly share code, notes, and snippets.

@kurtisnelson
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kurtisnelson/9479719 to your computer and use it in GitHub Desktop.
Save kurtisnelson/9479719 to your computer and use it in GitHub Desktop.
[wearscript] Shows a live graph of your battery, updating up to every second.
<html style="width:100%; height:100%; overflow:hidden">
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bacon.js/0.7.2/bacon.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/0.2.0/Chart.min.js"></script>
</head>
<body style="width:100%; height:100%; overflow:hidden; margin:0; background-color: black; color: white">
<canvas id="canvas" width="640" height="360" style="display:block"></canvas>
<script>
function BaconWS() {
var sensorStream;
this.sensorStream = function(type) {
if (type != null){
thisStream = Bacon.fromBinder(function(sink) {
WS.sensorOn(type, 1, function(data) {
sink(data);
});
});
if(sensorStream != null){
sensorStream = sensorStream.merge(thisStream);
}else{
sensorStream = thisStream;
}
}
return sensorStream;
}
}
BaconWS = new BaconWS();
</script>
<script>
var myChart;
var chartData = {
labels : ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19"],
datasets : [
{
fillColor : "rgba(220,220,220,0.5)",
strokeColor : "rgba(220,220,220,1)",
pointColor : "rgba(220,220,220,1)",
pointStrokeColor : "#fff",
data : []
}
]
};
var optionsNoAnimation = {
animation : false
}
var ctx = document.getElementById("canvas").getContext("2d");
function server() {
}
function main() {
myChart = new Chart(ctx);
myChart.Line(chartData);
if (WS.scriptVersion(1)) return;
WS.serverConnect('{{WSUrl}}', 'server');
WS.say('Prepare for battery death');
//Put battery events in the stream
BaconWS.sensorStream(WS.sensor('battery'));
BaconWS.sensorStream()
.map(function(value){
return value['values'][0];
})
.throttle(1000)
.slidingWindow(20, 1)
.onValue(function(value){
WS.log("Battery: " + value[value.length - 1]);
chartData['datasets'][0]['data'] = value;
myChart.Line(chartData, optionsNoAnimation);
});
WS.dataLog(true, false, 1);
WS.liveCardCreate(false, 5);
//kill battery here
}
window.onload = main;
</script>
</body>
</html>
{
"name": "Battery Graph"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment