@@ -4,6 +4,7 @@ var sr = require('./signal-request.js').SignalRetriever,
4
4
5
5
// constants
6
6
var INTERVAL = 500 ,
7
+ DEBUG = true ,
7
8
URL = 'http://mifi.afrihost.com/api/monitoring/status' ;
8
9
9
10
// app variables
@@ -14,7 +15,7 @@ board = new five.Board({
14
15
port : "COM12"
15
16
} ) ;
16
17
17
- // mapping function
18
+ // mapping function (taken from arduino)
18
19
function map ( x , minIn , maxIn , minOut , maxOut ) {
19
20
return ( x - minIn ) * ( maxOut - minOut ) / ( maxIn - minIn ) + minOut ;
20
21
}
@@ -33,30 +34,38 @@ board.on('ready', function() {
33
34
return ;
34
35
}
35
36
36
- var strength = parseInt ( signal . SignalStrength [ 0 ] , 10 ) , ledStrength ;
37
+ var strength = parseInt ( signal . SignalStrength [ 0 ] , 10 ) ,
38
+ ledStrength , rounded , fraction , mapped ;
37
39
38
40
if ( ! previousReading || previousReading !== strength ) {
39
- previousReading = strength ;
40
-
41
41
// switch correct leds on/off
42
- ledStrength = map ( previousReading , 0 , 100 , 1 , leds . length ) ;
42
+ ledStrength = map ( strength , 0 , 100 , 1 , leds . length ) ;
43
43
44
44
// 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
+ } ) ;
46
55
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 ) ) ;
51
60
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 ;
60
69
}
61
70
62
71
// poll again
0 commit comments