forked from mstrens/grbl_controller_esp32
-
Notifications
You must be signed in to change notification settings - Fork 0
/
actions.cpp
374 lines (340 loc) · 13.7 KB
/
actions.cpp
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
#include "config.h"
#include "language.h"
#include "draw.h"
#include "TFT_eSPI_ms/TFT_eSPI.h"
#include "FS.h"
#include "actions.h"
#include "menu_file.h"
#include "telnet.h"
#include "cmd.h"
#include "com.h"
// create for touchscreeen
extern TFT_eSPI tft ;
extern uint8_t prevPage ;
extern uint8_t currentPage ;
extern boolean updateFullPage ;
extern boolean updatePartPage ;
extern boolean waitReleased ;
extern uint8_t statusPrinting ;
extern char machineStatus[9]; // Iddle, Run, Alarm, ...
//extern char lastMsg[80] ;
extern char grblLastMessage[STR_GRBL_BUF_MAX_SIZE] ;
extern boolean grblLastMessageChanged;
extern uint16_t firstFileToDisplay ;
extern uint16_t sdFileDirCnt ;
extern char cmdName[7][17] ; // store the names of the commands
extern uint8_t cmdToSend ;
extern M_Page mPages[_P_MAX_PAGES] ;
extern uint8_t currentBtn ;
extern uint8_t justPressedBtn;
extern uint8_t justReleasedBtn;
extern uint8_t longPressedBtn;
extern uint32_t beginChangeBtnMillis ;
extern volatile boolean waitOk ;
//extern uint32_t cntSameMove ;
extern uint8_t jog_status ;
extern boolean jogCancelFlag ;
extern boolean jogCmdFlag ;
extern uint32_t startMoveMillis ;
extern int8_t jogDistX ;
extern int8_t jogDistY ;
extern int8_t jogDistZ ;
extern float moveMultiplier ;
uint32_t prevAutoMoveMillis ;
#define SOFT_RESET 0x18
void fGoToPage(uint8_t param) {
// Serial.print( "go to page : " ) ; Serial.println( param) ; // just for testing // to do
prevPage = currentPage ;
currentPage = param ;
updateFullPage = true ;
waitReleased = true ; // discard "pressed" until a release
// delay(10000);
}
void fGoToPageAndClearMsg(uint8_t param) {
fillMsg(" ") ;
grblLastMessage[0] = 0 ; // clear grbl last message ([....]
grblLastMessageChanged = true ;
prevPage = currentPage ;
currentPage = param ;
updateFullPage = true ;
waitReleased = true ; // discard "pressed" until a release
}
void fGoBack(uint8_t param) {
//Serial.print( "go back : " ) ; Serial.println( prevPage) ; // just for testing // to do
if (prevPage >= _P_INFO && prevPage < _P_MAX_PAGES ) { // if there are sevral back, prevPage = 0 and we will go back to _P_INFO
currentPage = prevPage ;
} else {
currentPage = _P_INFO ;
prevPage = _P_INFO ;
}
updateFullPage = true ;
waitReleased = true ; // discard "pressed" until a release
}
void fHome(uint8_t param) {
if( machineStatus[0] == 'I' || machineStatus[0] == 'A' ) {
#define HOME_CMD "$H"
Serial2.println(HOME_CMD) ;
} else {
fillMsg(__INVALID_BTN_HOME ) ;
}
waitReleased = true ; // discard "pressed" until a release
}
void fUnlock(uint8_t param) {
if( machineStatus[0] == 'A') { // if grbl is in alarm
Serial2.println("$X") ; // send command to unlock
//Serial.println("$X has been sent");
}
// Stay on current page
waitReleased = true ; // discard "pressed" until a release
}
void fReset(uint8_t param) {
Serial2.print( (char) SOFT_RESET) ;
waitReleased = true ; // discard "pressed" until a release
fillMsg( " " );
}
void fCancel(uint8_t param) {
if( statusPrinting == PRINTING_FROM_SD || statusPrinting == PRINTING_PAUSED ) {
statusPrinting = PRINTING_STOPPED ;
closeFileToRead() ;
Serial2.print( (char) SOFT_RESET) ;
}
currentPage = _P_INFO ; // go to page Info
updateFullPage = true ; // force a redraw even if current page does not change
waitReleased = true ; // discard "pressed" until a release
}
void fPause(uint8_t param) {
if( statusPrinting == PRINTING_FROM_SD && ( machineStatus[0] == 'R' || machineStatus[0] == 'J' ) ) { // test on J added mainly for test purpose
#define PAUSE_CMD "!"
Serial2.print(PAUSE_CMD) ;
statusPrinting = PRINTING_PAUSED ;
updateFullPage = true ; //
}
waitReleased = true ; // discard "pressed" until a release
}
void fResume(uint8_t param) {
if( statusPrinting == PRINTING_PAUSED && machineStatus[0] == 'H') {
#define RESUME_CMD "~"
Serial2.print(RESUME_CMD) ;
statusPrinting = PRINTING_FROM_SD ;
updateFullPage = true ; // we have to redraw the buttons because Resume should become Pause
}
waitReleased = true ; // discard "pressed" until a release
}
void fDist( uint8_t param ) {
uint8_t newDist = mPages[_P_MOVE].boutons[3] ; // convertit la position du bouton en type de bouton
//Serial.print("newDist=") ; Serial.println(newDist) ;
if ( ++newDist > _D10 ) newDist = _D_AUTO ; // increase and reset to min value if to big
mPages[_P_MOVE].boutons[3] = newDist ; // update the button to display
mButtonDraw( 4 , newDist ) ; // draw a button at position (from 1 to 8)
//updateFullPage = true ; // force a redraw of buttons
waitReleased = true ; // discard "pressed" until a release
}
void fMove( uint8_t param ) {
float distance ;
uint32_t moveMillis = millis() ;
static uint32_t prevMoveMillis ;
if ( mPages[_P_MOVE].boutons[3] == _D_AUTO ) {
handleAutoMove(param) ; // process in a similar way as Nunchuk
} else if (justPressedBtn) { // just pressed in non auto mode
switch ( mPages[_P_MOVE].boutons[3] ) { // we suppose that the distance is defined by the 4th button on first line so idx = 3
case _D0_01 :
distance = 0.01;
break ;
case _D0_1 :
distance = 0.1;
break ;
case _D1:
distance = 1 ;
break ;
case _D10 :
distance = 10 ;
break ;
}
Serial2.println("") ; Serial2.print("$J=G91 G21 ") ;
switch ( justPressedBtn ) { // we convert the position of the button into the type of button
case 1 : Serial2.print("X") ; break ;
case 5 : Serial2.print("X-") ; break ;
case 2 : Serial2.print("Y") ; break ;
case 6 : Serial2.print("Y-") ; break ;
case 3 : Serial2.print("Z") ; break ;
case 7 : Serial2.print("Z-") ; break ;
}
Serial2.print(distance) ; Serial2.println (" F100") ;
//Serial.print("move for button") ; Serial.print(justPressedBtn) ;Serial.print(" ") ; Serial.print(distance) ; Serial.println (" F100") ;
updatePartPage = true ; // force a redraw of data
waitReleased = true ; // discard "pressed" until a release // todo change to allow repeated press on the same button
}
}
void handleAutoMove( uint8_t param) { // in Auto mode, we support long press to increase speed progressively, we cancel jog when released
// param contains the touch being pressed or the released if no touch has been pressed
#define AUTO_MOVE_REPEAT_DELAY 100
uint8_t pressedBtn = 0 ;
static uint32_t cntSameAutoMove = 0 ;
//float moveMultiplier ;
uint32_t autoMoveMillis = millis() ;
if ( justReleasedBtn ) {
jogCancelFlag = true ; // cancel any previous move when a button is released (even before asking for another jog
cntSameAutoMove = 0 ; // reset the counter
//Serial.println("cancel jog") ;
} else {
jogCancelFlag = false ;
}
if ( ( autoMoveMillis - prevAutoMoveMillis ) < AUTO_MOVE_REPEAT_DELAY ) {
return ;
}
if ( justPressedBtn) {
cntSameAutoMove = 0 ; // reset the counter when we just press the button
pressedBtn = justPressedBtn ;
prevAutoMoveMillis = autoMoveMillis ;
} else if ( longPressedBtn ) {
pressedBtn = longPressedBtn ;
prevAutoMoveMillis = autoMoveMillis ;
}
if ( ( pressedBtn ) && (jogCmdFlag == false) ) {
if (cntSameAutoMove == 0 ) {
// moveMultiplier = 0.01 ;
startMoveMillis = millis();
}
//else if (cntSameAutoMove < 5 ) { // avoid to send to fast a new move
// moveMultiplier = 0.0 ;
//} else if (cntSameAutoMove < 10 ) {
// moveMultiplier = 0.01 ;
//} else if (cntSameAutoMove < 15 ) {
// moveMultiplier = 0.1 ;
//} else if (cntSameAutoMove < 20 ) {
// moveMultiplier = 1 ;
//} else {
// moveMultiplier = 2 ;
//}
cntSameAutoMove++ ;
jogDistX = 0 ; // reset all deplacements
jogDistY = 0 ;
jogDistZ = 0 ;
switch ( pressedBtn ) { // fill one direction of move
case 1 : jogDistX = 1 ; break ;
case 5 : jogDistX = -1 ; break ;
case 2 : jogDistY = 1 ; break ;
case 6 : jogDistY = -1 ; break ;
case 3 : jogDistZ = 1 ; break ;
case 7 : jogDistZ = -1 ; break ;
}
jogCmdFlag = true ; // the flag will inform the send module that there is a command to be sent based on moveMultiplier and preMove.
}
}
void fSdFilePrint(uint8_t param ){ // lance l'impression d'un fichier; param contains l'index (0 à 3) du fichier à ouvrir
//Serial.println("enter fsFilePrint") ;
if ( ! setFileToRead( param ) ) { // try to open the file to be printed ; in case of error, go back to Info page (lastMsg is already filled)
//Serial.println("SetFileToRead is false") ;
currentPage = _P_INFO ;
updateFullPage = true ;
waitReleased = true ; // discard "pressed" until a release
return ;
}
if ( fileToReadIsDir () ) { // if user press a directory, then change the directory
//Serial.println("FileToRead is dir") ;
if ( ! changeDirectory() ) {
//Serial.println("changeDirectory is false") ;
currentPage = _P_INFO ; // in case of error, goes to info page
updateFullPage = true ;
waitReleased = true ; // discard "pressed" until a release
return ;
} // else = change dir is ok, button have been updated, screen must be reloaded
updateFullPage = true ;
waitReleased = true ;
return ;
} else if ( fileIsCmd() ) { // when a "command" file is selected, it will not be executed it but will be saved in SPIFFS
// note : this function also fill LastMsg (e.g. with the file name when it is a file to be printed
updateFullPage = true ;
waitReleased = true ;
return ;
} else { // file can be printed
waitOk = false ;
Serial2.print(PAUSE_CMD) ;
delay(10);
Serial2.print("?") ;
//waitOk = false ; // do not wait for OK before sending char.
statusPrinting = PRINTING_PAUSED ; // initially it was PRINTING_FROM_SD ; // change the status, so char will be read and sent in main loop
prevPage = currentPage ; // go to INFO page
currentPage = _P_INFO ;
updateFullPage = true ;
waitReleased = true ; // discard "pressed" until a release
}
}
void fSdMove(uint8_t param) { // param contient _LEFT ou _RIGTH
if ( param == _LEFT ) {
if ( firstFileToDisplay > 4 ) {
firstFileToDisplay -= 4 ;
} else {
firstFileToDisplay = 1 ;
}
} else if ( ( param == _RIGHT ) && ( (firstFileToDisplay + 4) <= sdFileDirCnt ) ) {
firstFileToDisplay += 4 ;
if ( ( firstFileToDisplay + 4) > sdFileDirCnt ) firstFileToDisplay = sdFileDirCnt - 3 ;
} else { // move one level up
sdMoveUp() ;
}
// look in the directory for another file and upload the list of button.
//Serial.print("firstFileToDisplay=") ; Serial.println(firstFileToDisplay);
updateFullPage = true ;
waitReleased = true ; // discard "pressed" until a release
}
void fSetXYZ(uint8_t param) { // param contient le n° de la commande
switch (param) {
case _SETX : Serial2.println("G10 L20 P1 X0") ; break ;
case _SETY : Serial2.println("G10 L20 P1 Y0") ; break ;
case _SETZ : Serial2.println("G10 L20 P1 Z0") ; break ;
case _SETXYZ : Serial2.println("G10 L20 P1 X0 Y0 Z0") ; break ;
}
waitReleased = true ; // discard "pressed" until a release
// update will be done when we receive a new GRBL status message
}
void fCmd(uint8_t param) { // param contient le n° de la commande (valeur = _CMD1, ...)
cmdToSend = param ; // fill the index of the command to send; sending will be done in communication module
// create file name and try to open SPIFFS cmd file
char spiffsCmdName[21] = "/Cmd0_" ; // begin with fix text
spiffsCmdName[4] = param - _CMD1 + '1' ; // fill the cmd number (from 1...7)
strcat( spiffsCmdName , cmdName[param - _CMD1]) ; // add the cmd name to the first part
if ( ! spiffsOpenCmdFile( spiffsCmdName ) ) {
fillMsg(__CMD_NOT_RETRIEVED ) ;
currentPage = _P_INFO ;
updateFullPage = true ;
waitReleased = true ; // discard "pressed" until a release
return ;
}
waitOk = false ; // do not wait for OK before sending char.
statusPrinting = PRINTING_CMD ;
currentPage = _P_INFO ;
updateFullPage = true ;
waitReleased = true ; // discard "pressed" until a release
}
void fStartUsb(uint8_t param){
if( statusPrinting == PRINTING_STOPPED ) {
while ( Serial.available() ) { // clear the incomming buffer
Serial.read() ;
}
statusPrinting = PRINTING_FROM_USB ;
}
currentPage = _P_INFO ; // go to page Info
updateFullPage = true ;
waitReleased = true ;
}
void fStartTelnet(uint8_t param){
if( statusPrinting == PRINTING_STOPPED ) {
checkTelnetConnection();
if ( telnetIsConnected() ) {
clearTelnetBuffer() ;
statusPrinting = PRINTING_FROM_TELNET ;
//fillMsg( "Connected to telnet" );
} else {
fillMsg( __NO_TELNET_CONNECTION );
}
}
currentPage = _P_INFO ; // go to page Info
updateFullPage = true ;
waitReleased = true ;
}
void fStopPc(uint8_t param){
statusPrinting = PRINTING_STOPPED ;
updateFullPage = true ; // force a redraw even if current page does not change
waitReleased = true ; // discard "pressed" until a release
}