1
1
// requires
2
- var sr = require ( './signal-request.js' ) . SignalRetriever ;
2
+ var sr = require ( './signal-request.js' ) . SignalRetriever ,
3
+ five = require ( 'johnny-five' ) ;
3
4
4
5
// constants
5
6
var INTERVAL = 500 ,
6
7
URL = 'http://mifi.afrihost.com/api/monitoring/status' ;
7
8
8
9
// app variables
9
- var timeoutId , previousReading ;
10
-
11
- // main application loop
12
- timeoutId = setTimeout ( function retrieve ( ) {
13
- // use our signal retriever module to get the current signal strength
14
- sr . retrieve ( URL , function ( err , signal ) {
15
- if ( err || ! signal . SignalStrength ) {
16
- clearTimeout ( timeoutId ) ;
17
- console . log ( err || 'signal strength not available' ) ;
18
- return ;
19
- }
20
-
21
- var strength = parseInt ( signal . SignalStrength [ 0 ] , 10 ) ;
22
- if ( ! previousReading || previousReading !== strength ) {
23
- previousReading = strength ;
24
-
25
- // strength changed, let other modules know
26
- console . log ( previousReading ) ;
27
- }
28
-
29
- // poll again
30
- setTimeout ( retrieve , INTERVAL ) ;
31
- } ) ;
32
- } , INTERVAL ) ;
10
+ var timeoutId , previousReading , board , leds ;
11
+
12
+ // initialize the arduino board
13
+ board = new five . Board ( {
14
+ port : "COM12"
15
+ } ) ;
16
+
17
+ // mapping function
18
+ function map ( x , minIn , maxIn , minOut , maxOut ) {
19
+ return ( x - minIn ) * ( maxOut - minOut ) / ( maxIn - minIn ) + minOut ;
20
+ }
21
+
22
+ board . on ( 'ready' , function ( ) {
23
+ // setup the led array (all pwm pins)
24
+ leds = new five . Led . Array ( [ 3 , 5 , 6 , 9 , 10 ] ) ;
25
+
26
+ // main application loop
27
+ timeoutId = setTimeout ( function retrieve ( ) {
28
+ // use our signal retriever module to get the current signal strength
29
+ sr . retrieve ( URL , function ( err , signal ) {
30
+ if ( err || ! signal . SignalStrength ) {
31
+ clearTimeout ( timeoutId ) ;
32
+ console . log ( err || 'signal strength not available' ) ;
33
+ return ;
34
+ }
35
+
36
+ var strength = parseInt ( signal . SignalStrength [ 0 ] , 10 ) , ledStrength ;
37
+
38
+ if ( ! previousReading || previousReading !== strength ) {
39
+ previousReading = strength ;
40
+
41
+ // switch correct leds on/off
42
+ ledStrength = map ( previousReading , 0 , 100 , 1 , leds . length ) ;
43
+
44
+ // log new strength
45
+ console . log ( '1. reading:' + previousReading + ', mapped:' + ledStrength ) ;
46
+
47
+ // switch on/off any leds
48
+ leds . each ( function ( led , i ) {
49
+ led [ ( i + 1 ) < ledStrength ? 'on' : 'off' ] . call ( this ) ;
50
+ } ) ;
51
+
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 ) ;
60
+ }
61
+
62
+ // poll again
63
+ setTimeout ( retrieve , INTERVAL ) ;
64
+ } ) ;
65
+ } , INTERVAL ) ;
66
+ } ) ;
0 commit comments