-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
test-client.js
35 lines (28 loc) · 860 Bytes
/
test-client.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
var WebScale = require('./index');
var webScale = new WebScale();
webScale.on('change:weight', function(ounces){
var pounds = roundTowardsZero(ounces/16);
var remainderOunces = (Math.round(ounces % 16 * 10)/10).toFixed(1);
console.log(pounds + " lbs. " + remainderOunces + " oz.");
});
webScale.on('error', function(err){
console.error("Oh noes.", err);
});
webScale.on('connected', function(){
console.log("Scale online.");
webScale.tare();
console.log("Tared scale.");
});
webScale.once('disconnected', function(){
console.log("Scale disconnected. Try running as root.");
webScale.on('disconnected', function(){
console.log("Scale disconnected. Reconnecting...");
});
});
function roundTowardsZero(num){
if(num >= 0){
return Math.floor(num);
} else {
return Math.ceil(num);
}
}