1
1
var cncserver = require ( "cncserver" ) ;
2
2
var serialport = require ( "./node_modules/cncserver/node_modules/serialport/serialport.js" ) ;
3
3
var SerialPort = serialport . SerialPort ;
4
+ var serialPort = { } ;
4
5
5
6
var port = process . argv [ 2 ] ;
6
7
8
+ var x = 0 ;
9
+ var y = 0 ;
10
+ var b = 0 ;
11
+
12
+ var lastB = 0 ;
13
+ var lastX = 0 ;
14
+ var lastY = 0 ;
15
+
16
+ var gettingWater = false ;
17
+ var movingPen = false ;
18
+
19
+ var doubleClickWait = false ;
20
+ var reWaterInterval = 0 ;
21
+
22
+ var drawPt = { x : 0 , y : 0 } ;
23
+
24
+
7
25
// Actually try to init the connection and handle the various callbacks
8
26
cncserver . start ( {
9
27
success : function ( ) {
@@ -25,92 +43,83 @@ cncserver.start({
25
43
} ) ;
26
44
27
45
function startReading ( ) {
28
- var x = 0 ;
29
- var y = 0 ;
30
- var b = 0 ;
31
-
32
- var lastB = 0 ;
33
- var lastX = 0 ;
34
- var lastY = 0 ;
35
-
36
- var gettingWater = false ;
37
- var movingPen = false ;
38
-
39
- var doubleClickWait = false ;
40
- var reWaterInterval = 0 ;
41
-
42
- var drawPt = { x : 0 , y : 0 } ;
43
-
44
- var serialPort = new SerialPort ( port , {
46
+ serialPort = new SerialPort ( port , {
45
47
baudrate : 9600 ,
46
- parser : serialport . parsers . readline ( "\n" )
48
+ parser : serialport . parsers . readline ( "\n" ) ,
49
+ disconnectedCallback : tryReconnect
47
50
} ) ;
48
51
49
- serialPort . on ( "data" , function ( data ) {
50
- var d = data . split ( '|' ) ;
51
- x = parseInt ( d [ 0 ] ) ;
52
- y = parseInt ( d [ 1 ] ) ;
53
- b = parseInt ( d [ 2 ] ) ;
54
-
55
- // Button get water
56
- if ( b !== lastB ) {
57
- lastB = b ;
58
- if ( b === 0 ) {
59
- if ( ! gettingWater ) {
60
- if ( ! doubleClickWait ) {
61
- doubleClickWait = true ;
62
- console . log ( 'Single button...' ) ;
63
- reWaterInterval = setTimeout ( function ( ) {
64
- doubleClickWait = false ;
65
- console . log ( 'Button Pressed/Released! getting Water' ) ;
66
- getWater ( drawPt ) ;
67
- } , 250 ) ;
68
- } else {
69
- console . log ( 'Double button! Parking.' ) ;
70
- clearTimeout ( reWaterInterval ) ;
71
- movingPen = true ;
72
- doubleClickWait = false ;
73
- cncserver . setHeight ( 'up' ) ;
74
- cncserver . setPen ( { x : 0 , y : 0 , park : true } , function ( ) {
75
- movingPen = false ;
76
- } ) ;
77
- }
52
+ serialPort . on ( "open" , function ( ) {
53
+ console . log ( 'Open!' ) ;
54
+ serialPort . on ( "data" , controlData ) ;
55
+ } ) ;
56
+ }
78
57
58
+
59
+ // Event data callback from littleBits Leonardo
60
+ function controlData ( data ) {
61
+ var d = data . split ( '|' ) ;
62
+ x = parseInt ( d [ 0 ] ) ;
63
+ y = parseInt ( d [ 1 ] ) ;
64
+ b = parseInt ( d [ 2 ] ) ;
65
+
66
+ // Button get water
67
+ if ( b !== lastB ) {
68
+ lastB = b ;
69
+ if ( b === 0 ) {
70
+ if ( ! gettingWater ) {
71
+ if ( ! doubleClickWait ) {
72
+ doubleClickWait = true ;
73
+ console . log ( 'Single button...' ) ;
74
+ reWaterInterval = setTimeout ( function ( ) {
75
+ doubleClickWait = false ;
76
+ console . log ( 'Button Pressed/Released! getting Water' ) ;
77
+ getWater ( drawPt ) ;
78
+ } , 250 ) ;
79
79
} else {
80
- console . log ( 'Already Getting Water' ) ;
80
+ console . log ( 'Double button! Parking.' ) ;
81
+ clearTimeout ( reWaterInterval ) ;
82
+ movingPen = true ;
83
+ doubleClickWait = false ;
84
+ cncserver . setHeight ( 'up' ) ;
85
+ cncserver . setPen ( { x : 0 , y : 0 , park : true } , function ( ) {
86
+ movingPen = false ;
87
+ } ) ;
81
88
}
82
- }
83
- }
84
89
85
- // X/Y Movement
86
- if ( ! gettingWater && ! movingPen ) {
87
- drawPt = {
88
- x : ( lastX + 1 ) / 10.24 ,
89
- y : ( lastY + 1 ) / 10.24
90
- } ;
91
-
92
- if ( x !== lastX && x !== lastX - 1 && x !== lastX + 1 ) {
93
- lastX = x ;
94
- drawPt . x = ( x + 1 ) / 10.24 ;
95
- movingPen = true ;
90
+ } else {
91
+ console . log ( 'Already Getting Water' ) ;
96
92
}
93
+ }
94
+ }
97
95
98
- if ( y !== lastY && y !== lastY - 1 && y !== lastY + 1 ) {
99
- lastY = y ;
100
- drawPt . y = ( y + 1 ) / 10.24 ;
101
- movingPen = true ;
102
- }
96
+ // X/Y Movement
97
+ if ( ! gettingWater && ! movingPen ) {
98
+ drawPt = {
99
+ x : ( lastX + 1 ) / 10.24 ,
100
+ y : ( lastY + 1 ) / 10.24
101
+ } ;
102
+
103
+ if ( x !== lastX && x !== lastX - 1 && x !== lastX + 1 ) {
104
+ lastX = x ;
105
+ drawPt . x = ( x + 1 ) / 10.24 ;
106
+ movingPen = true ;
107
+ }
103
108
104
- // If this is set, then we had a change enough to set this
105
- if ( movingPen ) {
106
- //console.log(drawPt);
107
- cncserver . setPen ( drawPt , function ( ) {
108
- movingPen = false ;
109
- } ) ;
110
- }
109
+ if ( y !== lastY && y !== lastY - 1 && y !== lastY + 1 ) {
110
+ lastY = y ;
111
+ drawPt . y = ( y + 1 ) / 10.24 ;
112
+ movingPen = true ;
111
113
}
112
114
113
- } ) ;
115
+ // If this is set, then we had a change enough to set this
116
+ if ( movingPen ) {
117
+ //console.log(drawPt);
118
+ cncserver . setPen ( drawPt , function ( ) {
119
+ movingPen = false ;
120
+ } ) ;
121
+ }
122
+ }
114
123
}
115
124
116
125
// Wrapper for getting water
@@ -145,3 +154,26 @@ function connectLeonardo(callback) {
145
154
}
146
155
} ) ;
147
156
}
157
+
158
+ // Trys again and again to reconnect, every 2 seconds
159
+ function tryReconnect ( ) {
160
+ console . log ( 'Arduino Disconnected! Trying to reconnect...' ) ;
161
+ var tryReconnect = setInterval ( function ( ) {
162
+ try {
163
+ serialPort . open ( function ( ) {
164
+ if ( serialPort . isOpen ( ) ) {
165
+ clearInterval ( tryReconnect ) ;
166
+ console . log ( 'Reconnect succeeded!' ) ;
167
+
168
+ // Data callback needs to be rebound... for some reason :P
169
+ serialPort . on ( "data" , controlData ) ;
170
+ } else {
171
+ console . log ( 'Connect fail...' ) ;
172
+ }
173
+ } ) ;
174
+ } catch ( e ) {
175
+ // Didn't reconnect
176
+ console . log ( 'Reconnect failed, trying again...' , e ) ;
177
+ }
178
+ } , 2000 ) ;
179
+ }
0 commit comments