Skip to content

Commit 89ac410

Browse files
committed
small bug fixes
1 parent 3cb67cf commit 89ac410

File tree

1 file changed

+27
-18
lines changed

1 file changed

+27
-18
lines changed

signal-monitor.js

+27-18
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ var sr = require('./signal-request.js').SignalRetriever,
44

55
// constants
66
var INTERVAL = 500,
7+
DEBUG = true,
78
URL = 'http://mifi.afrihost.com/api/monitoring/status';
89

910
// app variables
@@ -14,7 +15,7 @@ board = new five.Board({
1415
port: "COM12"
1516
});
1617

17-
// mapping function
18+
// mapping function (taken from arduino)
1819
function map(x, minIn, maxIn, minOut, maxOut) {
1920
return (x - minIn) * (maxOut - minOut) / (maxIn - minIn) + minOut;
2021
}
@@ -33,30 +34,38 @@ board.on('ready', function() {
3334
return;
3435
}
3536

36-
var strength = parseInt(signal.SignalStrength[0], 10), ledStrength;
37+
var strength = parseInt(signal.SignalStrength[0], 10),
38+
ledStrength, rounded, fraction, mapped;
3739

3840
if (!previousReading || previousReading !== strength) {
39-
previousReading = strength;
40-
4141
// switch correct leds on/off
42-
ledStrength = map(previousReading, 0, 100, 1, leds.length);
42+
ledStrength = map(strength, 0, 100, 1, leds.length);
4343

4444
// log new strength
45-
console.log('1. reading:' + previousReading + ', mapped:' + ledStrength);
45+
DEBUG && console.log('1. reading:' + strength + ', mapped:' + ledStrength);
46+
47+
if (strength === 0) {
48+
//do something interesting, put everything off for now
49+
leds.off();
50+
} else {
51+
// switch on/off any leds
52+
leds.each(function(led, i) {
53+
led[(i + 1) < ledStrength ? 'on' : 'off'].call(this);
54+
});
4655

47-
// switch on/off any leds
48-
leds.each(function(led, i) {
49-
led[(i + 1) < ledStrength ? 'on' : 'off'].call(this);
50-
});
56+
// now show fractional strength
57+
rounded = Math.floor(ledStrength),
58+
fraction = ledStrength - rounded,
59+
mapped = Math.floor(map(fraction, 0, 1, 1, 255));
5160

52-
// now show fractional strength
53-
var rounded = Math.floor(ledStrength),
54-
fraction = ledStrength - rounded,
55-
mapped = Math.floor(map(fraction, 0, 1, 1, 255));
56-
57-
// log and set
58-
console.log('2. rounded: ' + rounded + ', fraction: ' + fraction + ', mapped:' + mapped);
59-
leds[rounded].brightness(mapped);
61+
if (fraction > 0) {
62+
// log and set
63+
DEBUG && console.log('2. rounded: ' + rounded + ', fraction: ' + fraction + ', mapped:' + mapped);
64+
leds[rounded].brightness(mapped);
65+
}
66+
}
67+
68+
previousReading = strength;
6069
}
6170

6271
// poll again

0 commit comments

Comments
 (0)