Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
[wearscript] BT Battery Discharge
<html style="width:100%; height:100%; overflow:hidden">
<body style="width:100%; height:100%; overflow:hidden; margin:0">
<script>
function btsend(data) {
var addr = '00:00:12:06:57:19';
WS.bluetoothPair(addr, function () {
WS.log('Sending: ' + data);
WS.bluetoothWrite(addr, data);
});
}
function epoch() {
return (new Date).getTime() / 1000;
}
function reset() {
WS.cameraOff();
var sensors = ['gps', 'accelerometer', 'orientation', 'magneticField', 'gyroscope',
'light', 'gravity', 'linearAcceleration', 'rotationVector'];
for (var i = 0; i < sensors.length; i++)
WS.sensorOff(sensors[i]);
WS.activityDestroy();
}
function testScreen() {
WS.wake();
WS.activityCreate();
}
function testSmallCameraWithScreen() {
WS.wake();
WS.activityCreate();
WS.cameraOn(.0001);
}
function testSensors() {
var sensors = ['gps', 'accelerometer', 'orientation', 'magneticField', 'gyroscope',
'light', 'gravity', 'linearAcceleration', 'rotationVector'];
for (var i = 0; i < sensors.length; i++)
WS.sensorOn(sensors[i]);
}
function main() {
if (WS.scriptVersion(1)) return;
charging = true;
cnt = 0;
btsend('1');
reset();
chargeTimes = [];
funcTimes = [];
funcs = [testScreen, testSmallCameraWithScreen, testSensors];
chargeStart = epoch();
dischargeStart = epoch();
WS.sensorOn('battery', .1, function (data) {
if (charging && data.values[0] >= .98) {
charging = false;
WS.say('Discharging');
dischargeStart = epoch();
chargeTimes.push(dischargeStart - chargeStart);
WS.log(JSON.stringify({chargeTimes: chargeTimes, funcTimes: funcTimes}));
} else if (!charging && data.values[0] <= .97) {
charging = true;
WS.say('Charging');
chargeStart = epoch();
funcTimes.push([cnt, chargeStart - dischargeStart])
WS.log(JSON.stringify({chargeTimes: chargeTimes, funcTimes: funcTimes}));
reset();
var curFunc = cnt % funcs.length;
funcs[curFunc]();
WS.log('Running: ' + curFunc);
cnt += 1;
}
if (charging)
btsend('1');
else
btsend('0');
WS.log(JSON.stringify(data) + ' Screen: ' + WS.screen());
});
WS.dataLog(true, false, .15);
}
window.onload = main;
</script>
</body>
</html>
{"name":"Example"}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment