diff --git a/GrblStream.h b/GrblStream.h new file mode 100644 index 0000000..21f9a57 --- /dev/null +++ b/GrblStream.h @@ -0,0 +1,56 @@ +#pragma once +#include +#include + +class GrblStream +{ +public: + virtual int available() = 0; + virtual char read() = 0; + virtual bool canSend(size_t n) = 0; + virtual void waitSent() = 0; + virtual size_t print(char c) = 0; + virtual size_t print(const char* str) = 0; + size_t print(float n) { return print(String(n).c_str()); } + size_t print(uint32_t n) { return print(String(n).c_str()); } + size_t println(const char* str) { size_t n = print(str); return n + print('\n'); } ; + + // You need a virtual destructor for 'delete' to work correctly: + virtual ~GrblStream() {} +}; + +class SerialGrblStream : public GrblStream +{ + HardwareSerial _serial; +public: +SerialGrblStream(HardwareSerial serial) : _serial(serial) {} + int available() override { return _serial.available(); } + bool canSend(size_t n) override { return _serial.availableForWrite() > n; } + void waitSent() override { while (_serial.availableForWrite() != 0x7F) ; } + char read() override { return _serial.read(); } + size_t print(char c) override { return _serial.print(c); } + size_t print(const char* str) override { return _serial.print(str); } +}; + +class TCPGrblStream : public GrblStream +{ + WiFiClient _netconn; +public: + TCPGrblStream(const char *host, uint16_t port, int timeout) { + if (_netconn.connect(host, port, timeout) == 0) { + throw -2; + } + _netconn.setNoDelay(true); + } + ~TCPGrblStream() { + _netconn.stop(); + }; + int available() override { return _netconn.available(); } + char read() override { return _netconn.read(); } + bool canSend(size_t n) override { return true; } + void waitSent() override { } + size_t print(char c) override { return _netconn.write((uint8_t)c); } + size_t print(const char* str) override { return _netconn.write((uint8_t *)str, strlen(str)); } +}; + +extern GrblStream* grblStream; diff --git a/README.md b/README.md index 48f352e..7b649a3 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,16 @@ # grbl_controller_esp32 +*This fork of mstrens/grbl_controller_esp32.git has been modified so +grbl_controller_esp32 can connect to a GRBL GCode controller using a +TCP connection instead of a hardwired serial port. It can thus be +used as a handheld wireless pendant for Grbl_Esp32. To configure for +TCP operation, define GRBL_IP and GRBL_PORT in config.h so they refer +to the Grbl_Esp32 system. You will also need to configure the WiFi +settings as described below.* + +*In addition, if THICK_OUTLINE is defined, the highlight for pressed +buttons is thicker and easier to see.* + This Grbl controller runs on a ESP32 This project allows to control a CNC running GRBL without having to use a pc.
@@ -228,4 +239,4 @@ Notes: - A button being added will be displayed only after the next reset of ESP32. - "Printing" a file having the same Cmd digit as an existing Cmd button will replace as well the name as the Gcode of the button - to delete a Cmd button, "Print" a file having a name equal to "delete" So, e.g. "Cmd2_delete.xxx" will delete the second button. - \ No newline at end of file + diff --git a/TFT_eSPI_ms/TFT_Drivers/ILI9341_Init.h b/TFT_eSPI_ms/TFT_Drivers/ILI9341_Init.h index 124c849..1705b68 100644 --- a/TFT_eSPI_ms/TFT_Drivers/ILI9341_Init.h +++ b/TFT_eSPI_ms/TFT_Drivers/ILI9341_Init.h @@ -127,4 +127,4 @@ pinMode(TFT_BL, OUTPUT); #endif -} \ No newline at end of file +} diff --git a/actions.cpp b/actions.cpp index eaa17a4..c3075b0 100644 --- a/actions.cpp +++ b/actions.cpp @@ -9,6 +9,8 @@ #include "cmd.h" #include "com.h" #include "log.h" +#include "GrblStream.h" + // create for touchscreeen extern TFT_eSPI tft ; @@ -98,7 +100,7 @@ void fGoBack(uint8_t param) { void fHome(uint8_t param) { if( machineStatus[0] == 'I' || machineStatus[0] == 'A' ) { #define HOME_CMD "$H" - Serial2.println(HOME_CMD) ; + grblStream->println(HOME_CMD) ; } else { fillMsg(__INVALID_BTN_HOME ) ; } @@ -107,7 +109,7 @@ void fHome(uint8_t param) { void fUnlock(uint8_t param) { if( machineStatus[0] == 'A') { // if grbl is in alarm - Serial2.println("$X") ; // send command to unlock + grblStream->println("$X") ; // send command to unlock //Serial.println("$X has been sent"); } // Stay on current page @@ -115,7 +117,7 @@ void fUnlock(uint8_t param) { } void fReset(uint8_t param) { - Serial2.print( (char) SOFT_RESET) ; + grblStream->print( (char) SOFT_RESET) ; waitReleased = true ; // discard "pressed" until a release fillMsg( " " ); @@ -125,10 +127,10 @@ void fCancel(uint8_t param) { if( statusPrinting == PRINTING_FROM_SD || statusPrinting == PRINTING_PAUSED ) { statusPrinting = PRINTING_STOPPED ; closeFileToRead() ; - Serial2.print( (char) SOFT_RESET) ; + grblStream->print( (char) SOFT_RESET) ; } else if ( statusPrinting == PRINTING_STRING ) { statusPrinting = PRINTING_STOPPED ; - Serial2.print( (char) SOFT_RESET) ; + grblStream->print( (char) SOFT_RESET) ; } currentPage = _P_INFO ; // go to page Info updateFullPage = true ; // force a redraw even if current page does not change @@ -138,7 +140,7 @@ void fCancel(uint8_t param) { 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) ; + grblStream->print(PAUSE_CMD) ; statusPrinting = PRINTING_PAUSED ; updateFullPage = true ; // } @@ -148,7 +150,7 @@ void fPause(uint8_t param) { void fResume(uint8_t param) { if( statusPrinting == PRINTING_PAUSED && machineStatus[0] == 'H') { #define RESUME_CMD "~" - Serial2.print(RESUME_CMD) ; + grblStream->print(RESUME_CMD) ; resetWaitOkWhenSdMillis() ; // we reset the time we sent the last cmd otherwise, we can get a wrong warning saying that we are missing an OK (because it seems that GRBL suspends OK while in pause) statusPrinting = PRINTING_FROM_SD ; updateFullPage = true ; // we have to redraw the buttons because Resume should become Pause @@ -187,29 +189,29 @@ void fMove( uint8_t param ) { distance = 10 ; break ; } - Serial2.println("") ; Serial2.print("$J=G91 G21 ") ; + grblStream->println("") ; grblStream->print("$J=G91 G21 ") ; //switch ( justPressedBtn ) { // we convert the position of the button into the type of button - // case 7 : Serial2.print("X") ; break ; - // case 5 : Serial2.print("X-") ; break ; - // case 2 : Serial2.print("Y") ; break ; - // case 10 : Serial2.print("Y-") ; break ; - // case 4 : Serial2.print("Z") ; break ; - // case 12 : Serial2.print("Z-") ; break ; + // case 7 : grblStream->print("X") ; break ; + // case 5 : grblStream->print("X-") ; break ; + // case 2 : grblStream->print("Y") ; break ; + // case 10 : grblStream->print("Y-") ; break ; + // case 4 : grblStream->print("Z") ; break ; + // case 12 : grblStream->print("Z-") ; break ; // } uint8_t typeOfMove ; typeOfMove = convertBtnPosToBtnIdx( currentPage , justPressedBtn ) ; switch ( typeOfMove ) { // we convert the position of the button into the type of button - case _XP : Serial2.print("X") ; break ; - case _XM : Serial2.print("X-") ; break ; - case _YP : Serial2.print("Y") ; break ; - case _YM : Serial2.print("Y-") ; break ; - case _ZP : Serial2.print("Z") ; break ; - case _ZM : Serial2.print("Z-") ; break ; - case _AP : Serial2.print("A") ; break ; - case _AM : Serial2.print("A-") ; break ; + case _XP : grblStream->print("X") ; break ; + case _XM : grblStream->print("X-") ; break ; + case _YP : grblStream->print("Y") ; break ; + case _YM : grblStream->print("Y-") ; break ; + case _ZP : grblStream->print("Z") ; break ; + case _ZM : grblStream->print("Z-") ; break ; + case _AP : grblStream->print("A") ; break ; + case _AM : grblStream->print("A-") ; break ; } - Serial2.print(distance) ; Serial2.println (" F100") ; + grblStream->print(distance) ; grblStream->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 @@ -305,9 +307,9 @@ void fSdFilePrint(uint8_t param ){ // lance l'impression d'un fichier; param c return ; } else { // file can be printed waitOk = false ; - Serial2.print(PAUSE_CMD) ; + grblStream->print(PAUSE_CMD) ; delay(10); - Serial2.print("?") ; + grblStream->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 @@ -506,7 +508,7 @@ void fOverModify (uint8_t BtnParam) { grblOverwriteCode += 0x99 ; // 0x99 is the GRBL code for 100% RPM // Serial.println("We change RPM"); Serial.println( (uint8_t) grblOverwriteCode, HEX); // to debug } - Serial2.print( (char) grblOverwriteCode ) ; + grblStream->print( (char) grblOverwriteCode ) ; updatePartPage = true ; // force a redraw of data waitReleased = true ; // discard "pressed" until a release } diff --git a/browser.cpp b/browser.cpp index 8047b08..1ff0ddc 100644 --- a/browser.cpp +++ b/browser.cpp @@ -156,9 +156,9 @@ boolean checkWifiOnSD(void){ bool wifiTypeOk = false; bool wifiPasswordOk = false; bool wifiSsidOk = false; - bool local_IPOk = false ; - bool gatewayOk = false ; - bool subnetOk = false ; + // bool local_IPOk = false ; + // bool gatewayOk = false ; + // bool subnetOk = false ; uint8_t n; // number of bytes in a line char * pBeginValue ; char * pEndValue ; @@ -217,15 +217,15 @@ boolean checkWifiOnSD(void){ } else if ( memcmp ( "LOCAL_IP=", line, sizeof("LOCAL_IP=")-1) == 0){ memcpy(local_IPChar , pBeginValue+1 , sizeValue) ; local_IPStr = local_IPChar ; - local_IPOk = true ; + // local_IPOk = true ; } else if ( memcmp ( "GATEWAY=", line, sizeof("GATEWAY=")-1) == 0){ memcpy(gatewayChar , pBeginValue+1 , sizeValue) ; gatewayStr = gatewayChar ; - gatewayOk = true ; + // gatewayOk = true ; } else if ( memcmp ( "SUBNET=", line, sizeof("SUBNET=")-1) == 0){ memcpy(subnetChar , pBeginValue+1 , sizeValue) ; subnetStr = subnetChar ; - subnetOk = true ; + // subnetOk = true ; } } } diff --git a/com.cpp b/com.cpp index 3c17a26..2c2bcca 100644 --- a/com.cpp +++ b/com.cpp @@ -10,6 +10,7 @@ #include "cmd.h" #include "log.h" #include +#include "GrblStream.h" // GRBL status are : Idle, Run, Hold, Jog, Alarm, Door, Check, Home, Sleep // a message should look like (note : GRBL sent or WPOS or MPos depending on grbl parameter : to get WPos, we have to set "$10=0" @@ -111,11 +112,11 @@ void getFromGrblAndForward( void ) { //get char from GRBL, forward them if sta static uint32_t millisLastGetGBL = 0 ; //uint8_t i = 0 ; static int cntOk = 0 ; - while (Serial2.available() ) { + while (grblStream->available() ) { #ifdef DEBUG_TO_PC //Serial.print(F("s=")); Serial.print( getGrblPosState ); Serial.println() ; #endif - c=Serial2.read() ; + c=grblStream->read() ; //#define DEBUG_RECEIVED_CHAR #ifdef DEBUG_RECEIVED_CHAR Serial.print( (char) c) ; @@ -285,7 +286,7 @@ void getFromGrblAndForward( void ) { //get char from GRBL, forward them if sta millisLastGetGBL = millis() ; newGrblStatusReceived = true ; // force a redraw if on info screen - //Serial2.println( (char) 0x18) ; // force a soft reset of grbl + //grblStream->print( (char) 0x18) ; // force a soft reset of grbl } } @@ -409,13 +410,13 @@ void sendToGrbl( void ) { case PRINTING_FROM_USB : while ( Serial.available() && statusPrinting == PRINTING_FROM_USB ) { sdChar = Serial.read() ; - Serial2.print( (char) sdChar ) ; + grblStream->print( (char) sdChar ) ; } // end while break ; case PRINTING_FROM_TELNET : while ( telnetClient.available() && statusPrinting == PRINTING_FROM_TELNET ) { sdChar = telnetClient.read() ; - Serial2.print( (char) sdChar ) ; + grblStream->print( (char) sdChar ) ; } // end while break ; case PRINTING_CMD : @@ -433,7 +434,7 @@ void sendToGrbl( void ) { currSendMillis = millis() ; // ask GRBL current status every X millis sec. GRBL replies with a message with status and position if ( currSendMillis > nextSendMillis) { nextSendMillis = currSendMillis + 300 ; - Serial2.print("?") ; + grblStream->print("?") ; } } if( statusPrinting != PRINTING_FROM_TELNET ) { // clear the telnet buffer when not in use @@ -446,7 +447,7 @@ void sendToGrbl( void ) { void sendFromSd() { // send next char from SD; close file at the end int sdChar ; waitOkWhenSdMillis = millis() + WAIT_OK_SD_TIMEOUT ; // set time out on - while ( aDir[dirLevel+1].available() > 0 && (! waitOk) && statusPrinting == PRINTING_FROM_SD && Serial2.availableForWrite() > 2 ) { + while ( aDir[dirLevel+1].available() > 0 && (! waitOk) && statusPrinting == PRINTING_FROM_SD && grblStream->canSend(2) ) { sdChar = aDir[dirLevel+1].read() ; if ( sdChar < 0 ) { statusPrinting = PRINTING_ERROR ; @@ -455,7 +456,7 @@ void sendFromSd() { // send next char from SD; close file at the end sdNumberOfCharSent++ ; if( sdChar != 13 && sdChar != ' ' ){ // 13 = carriage return; do not send the space. // to do : skip the comments - Serial2.print( (char) sdChar ) ; + grblStream->print( (char) sdChar ) ; } if ( sdChar == '\n' ) { // n= new line = line feed = 10 decimal waitOk = true ; @@ -466,18 +467,18 @@ void sendFromSd() { // send next char from SD; close file at the end aDir[dirLevel+1].close() ; // close the file when all bytes have been sent. statusPrinting = PRINTING_STOPPED ; updateFullPage = true ; // force to redraw the whole page because the buttons haved changed - //Serial2.print( (char) 0x18 ) ; //0x85) ; // cancel jog (just for testing); must be removed - Serial2.print( (char) 10 ) ; // sent a new line to be sure that Grbl handle last line. + //grblStream->print( (char) 0x18 ) ; //0x85) ; // cancel jog (just for testing); must be removed + grblStream->print( (char) 10 ) ; // sent a new line to be sure that Grbl handle last line. } } void sendFromCmd() { int sdChar ; waitOkWhenSdMillis = millis() + WAIT_OK_SD_TIMEOUT ; // set time out on - while ( spiffsAvailableCmdFile() > 0 && (! waitOk) && statusPrinting == PRINTING_CMD && Serial2.availableForWrite() > 2 ) { + while ( spiffsAvailableCmdFile() > 0 && (! waitOk) && statusPrinting == PRINTING_CMD && grblStream->canSend(2) ) { sdChar = (int) spiffsReadCmdFile() ; if( sdChar != 13){ - Serial2.print( (char) sdChar ) ; + grblStream->print( (char) sdChar ) ; } if ( sdChar == '\n' ) { waitOk = true ; @@ -486,7 +487,7 @@ void sendFromCmd() { if ( spiffsAvailableCmdFile() == 0 ) { statusPrinting = PRINTING_STOPPED ; updateFullPage = true ; // force to redraw the whole page because the buttons haved changed - Serial2.print( (char) 0x0A ) ; // sent a new line to be sure that Grbl handle last line. + grblStream->print( (char) 0x0A ) ; // sent a new line to be sure that Grbl handle last line. } } @@ -506,28 +507,28 @@ void sendFromString(){ break; case 'X' : // Put the G30 X offset //Serial.print("G30SavedX"); Serial.println(G30SavedX); - Serial2.print(G30SavedX) ; + grblStream->print(G30SavedX) ; break; case 'Y' : // Put the G30 Y offset - Serial2.print(G30SavedY) ; + grblStream->print(G30SavedY) ; break; case 'Z' : // Put some char in the flow savedWposXYZA[2] = preferences.getFloat("wposZ" , 0 ) ; // if wposZ does not exist in preferences, the function returns 0 char floatToString[20] ; gcvt(savedWposXYZA[2], 3, floatToString); // convert float to string - Serial2.print(floatToString) ; + grblStream->print(floatToString) ; ///Serial.print( "wpos Z is retrieved with value = ") ; Serial.println( floatToString ) ; // to debug break; case 'M' : // Restore modal G20/G21/G90/G91 - Serial2.print( modalAbsRel) ; + grblStream->print( modalAbsRel) ; //Serial.print( modalAbsRel) ; // to debug - Serial2.print( modalMmInch) ; + grblStream->print( modalMmInch) ; //Serial.print( modalMmInch) ; // to debug break; } } else { if( strChar != 13){ // add here handling of special character for real time process; we skip \r char - Serial2.print( strChar ) ; + grblStream->print( strChar ) ; //Serial.print (strChar) ; // to debug } if ( strChar == '\n' ) { @@ -541,7 +542,7 @@ void sendFromString(){ statusPrinting = PRINTING_STOPPED ; fillStringExecuteMsg( lastStringCmd ); // fill with a message saying the command has been executed updateFullPage = true ; // force to redraw the whole page because the buttons haved changed - Serial2.print( (char) 0x0A ) ; // sent a new line to be sure that Grbl handle last line. + grblStream->print( (char) 0x0A ) ; // sent a new line to be sure that Grbl handle last line. //Serial.println("last char has been sent") ; } } @@ -551,9 +552,8 @@ void sendJogCancelAndJog(void) { if ( jogCancelFlag ) { if ( jog_status == JOG_NO ) { //Serial.println("send a jog cancel"); - Serial2.print( (char) 0x85) ; Serial2.print("G4P0") ; Serial2.print( (char) 0x0A) ; // to be execute after a cancel jog in order to get an OK that says that grbl is Idle. - while (Serial2.availableForWrite() != 0x7F ) ; // wait that all char are sent - //Serial2.flush() ; // wait that all outgoing char are really sent.!!! in ESP32 it also clear the RX buffer what is not expected in arduino logic + grblStream->print( (char) 0x85) ; grblStream->print("G4P0") ; grblStream->print( (char) 0x0A) ; // to be execute after a cancel jog in order to get an OK that says that grbl is Idle. + grblStream->waitSent(); // wait until all chars sent waitOk = true ; jog_status = JOG_WAIT_END_CANCEL ; @@ -578,7 +578,7 @@ void sendJogCancelAndJog(void) { if ( jogCmdFlag ) { if ( jog_status == JOG_NO ) { //Serial.println( bufferAvailable[0] ) ; - if (bufferAvailable[0] > 15) { // tests shows that GRBL gives errors when we fill to much the block buffer + if (bufferAvailable[0] > 14) { // tests shows that GRBL gives errors when we fill to much the block buffer if ( sendJogCmd(startMoveMillis) ) { // if command has been sent waitOk = true ; jog_status = JOG_WAIT_END_CMD ; @@ -642,44 +642,43 @@ boolean sendJogCmd(uint32_t startTime) { } // //Serial.println("send a jog") ; - Serial2.print("$J=G91 G21") ; + grblStream->print("$J=G91 G21") ; if (jogDistX > 0) { - Serial2.print(" X") ; + grblStream->print(" X") ; } else if (jogDistX ) { - Serial2.print(" X-") ; + grblStream->print(" X-") ; } if (jogDistX ) { - Serial2.print(distanceMove) ; + grblStream->print(distanceMove) ; } if (jogDistY > 0) { - Serial2.print(" Y") ; + grblStream->print(" Y") ; } else if (jogDistY ) { - Serial2.print(" Y-") ; + grblStream->print(" Y-") ; } if (jogDistY ) { - //Serial2.print(moveMultiplier) ; - Serial2.print(distanceMove) ; + //grblStream->print(moveMultiplier) ; + grblStream->print(distanceMove) ; } if (jogDistZ > 0) { - Serial2.print(" Z") ; + grblStream->print(" Z") ; } else if (jogDistZ ) { - Serial2.print(" Z-") ; + grblStream->print(" Z-") ; } if (jogDistZ ) { - Serial2.print(distanceMove) ; + grblStream->print(distanceMove) ; } if (jogDistA > 0) { - Serial2.print(" A") ; + grblStream->print(" A") ; } else if (jogDistA ) { - Serial2.print(" A-") ; + grblStream->print(" A-") ; } if (jogDistA ) { - Serial2.print(distanceMove) ; + grblStream->print(distanceMove) ; } - //Serial2.print(" F2000"); Serial2.print( (char) 0x0A) ; - Serial2.print(" F"); Serial2.print(speedMove); Serial2.print( (char) 0x0A) ; - while (Serial2.availableForWrite() != 0x7F ) ; // wait that all char are sent - //Serial2.flush() ; // wait that all char are really sent + //grblStream->print(" F2000"); grblStream->print( (char) 0x0A) ; + grblStream->print(" F"); grblStream->print(speedMove); grblStream->print( (char) 0x0A) ; + grblStream->waitSent(); // wait until all chars are sent //Serial.print("Send cmd jog " ); Serial.print(distanceMove) ; Serial.print(" " ); Serial.print(speedMove) ;Serial.print(" " ); Serial.println(millis() - startTime ); //Serial.print(prevMoveX) ; Serial.print(" " ); Serial.print(prevMoveY) ; Serial.print(" " ); Serial.print(prevMoveZ) ;Serial.print(" ") ; Serial.println(millis()) ; diff --git a/config.h b/config.h index 7445de4..ab5d6f0 100644 --- a/config.h +++ b/config.h @@ -13,14 +13,26 @@ // for ESP32_ACT_AS_STATION , set the password to get access to your access point (router) // for ESP_ACT_AS_AP, set the password you want to use to protect your ESP32 ( can be empty) -#define MY_PASSWORD "your password" // replace by your password +#define MY_PASSWORD "your password" // replace by your password // if you use Wifi, you can (optional) define a fix IP address. Then you have to define 3 parameters // If one of next 3 parameters is "", it means that you do not want to use a fix local IP address. -#define LOCAL_IP "192.168.1.10" // fix IP address +#define LOCAL_IP "" // fix IP address #define SUBNET "255.255.255.0" // subnet mask of your local network #define GATEWAY "192.168.1.1" // gateway that have to check the IP address +// If GRBL_IP is defined, connect to the Grbl controller via a TCP +// connection. That is useful for wireless connection to Grbl_ESP32. +// Otherwise connect via Serial2. +#define GRBL_IP "192.168.2.120" + +// The default port for TCP connection to a GRBL controller is 23, +// the standard Telnet port. It is better to use a different port +// to prevent interference from other telnet clients; the other port +// should be greater than 1023 because port 0..1023 are reserved for +// standard services. The port should be changed both here and in +// the Grbl_ESP32 configuration. +#define GRBL_PORT 23 // select your language between EN, FR, DE #define LANGUAGE EN @@ -76,6 +88,8 @@ // If you upload a file having the same button position(digit 1...9, A or B) as an existing button, the new file will replace the button name and content of the previous button. // to delete a button create and execute a file having a name like Cmd3_delete where 3 is the button position (digit 1...9, A or B) to delete. +#define THICK_OUTLINE // Makes the pressed-button highlight easier to see + // select color between (or define your own) // TFT_BLACK 0x0000 /* 0, 0, 0 */ // TFT_NAVY 0x000F /* 0, 0, 128 */ @@ -162,7 +176,7 @@ #define _SETXYZA_STRING "G10 L20 P1 X0 Y0 Z0 A0\n" // ************************************* normally do not change here below **************************** // debugging -//#define DEBUG_TO_PC +#define DEBUG_TO_PC #ifdef DEBUG_TO_PC #define COPY_GRBL_TO_PC #define DEBUG_TO_PC_MENU diff --git a/draw.cpp b/draw.cpp index 4b6ac2b..027a7fa 100644 --- a/draw.cpp +++ b/draw.cpp @@ -103,6 +103,14 @@ extern char grblLastMessage[STR_GRBL_BUF_MAX_SIZE] ; extern boolean grblLastMessageChanged; //**************** normal screen definition. +// B0 and B1 can be defined via Arduino.h +#ifdef B0 + #undef B0 +#endif +#ifdef B1 + #undef B1 +#endif + #define B0 3 #define E0 3+74 #define B1 3+80 @@ -548,6 +556,19 @@ void mButtonBorder(uint8_t pos , uint16_t outline) { // draw the border of a bu } uint8_t r = min(_w, _h) / 4; // Corner radius tft.drawRoundRect( _xl, _yl , _w, _h, r, outline); +#ifdef THICK_OUTLINE + // Draw an extra-thick border so the pressed state can be seen + // more easily for people with fat fingers. In pressed state, + // we draw the extra border in the special color, reverting to + // the background color when not pressed. + if (outline == BUTTON_BORDER_NOT_PRESSED) { + outline = SCREEN_BACKGROUND; + } + tft.drawRoundRect(_xl - 1, _yl - 1, _w + 2, _h + 2, r + 1, outline); + tft.drawRoundRect(_xl - 2, _yl - 2, _w + 4, _h + 4, r + 2, outline); + tft.drawRoundRect(_xl - 3, _yl - 3, _w + 6, _h + 6, r + 3, outline); + tft.drawRoundRect(_xl - 4, _yl - 4, _w + 8, _h + 8, r + 4, outline); +#endif } uint8_t getButton( int16_t x, int16_t y , uint16_t btnDef[12][4]) { // convert x y into a button if possible @@ -650,7 +671,7 @@ void executeMainActionBtn( ) { // find and execute main action for ONE button // Basis functions -void blankTft(char * titel, uint16_t x , uint16_t y) { // blank screen and display one titel +void blankTft(const char * titel, uint16_t x , uint16_t y) { // blank screen and display one titel clearScreen() ; //tft.fillScreen( SCREEN_BACKGROUND ) ; // currently titel is not used so folowwing lines can be skipped; uncomment if titel would be used @@ -666,7 +687,7 @@ void clearScreen() { tft.fillScreen( SCREEN_BACKGROUND ) ; } -void printTft(char * text ) { // print a text on screen +void printTft(const char * text ) { // print a text on screen tft.print( text ) ; } @@ -756,6 +777,19 @@ void updateButtonsInfoPage (void) { // met à jour le set up de la page en fonct } +void drawConnectPage() { + if ( statusPrintingPrev != statusPrinting ) { + tft.setFreeFont (LABELS12_FONT) ; + tft.setTextSize(1) ; // char is 2 X magnified => + tft.setTextColor(SCREEN_NORMAL_TEXT , SCREEN_BACKGROUND ) ; // when only 1 parameter, background = fond); + tft.setTextDatum( TL_DATUM ) ; // align Left + tft.setTextPadding (200) ; + tft.drawString ( &printingStatusText[statusPrinting][0] , 5 , 0 ) ; + statusPrintingPrev = statusPrinting ; + } + drawLastMsg() ; +} + void drawDataOnInfoPage() { // to do : affiche les données sur la page d'info // USB<-->Grbl Idle (or Run, Alarm, ... grbl status) // So, printing status (blanco, ,SD-->Grbl xxx%, USB<-->Grbl , Pause, Cmd) = printing status diff --git a/draw.h b/draw.h index 9731d3d..a281ee2 100644 --- a/draw.h +++ b/draw.h @@ -70,8 +70,8 @@ void clearScreen() ; // clear tft screen void fillMPage (uint8_t _page , uint8_t _btnPos , uint8_t _boutons, uint8_t _actions , void (*_pfNext)(uint8_t) , uint8_t _parameters ) ; void initButtons() ; // initialise les noms des boutons, les boutons pour chaque page. -void blankTft(char * titel , uint16_t x , uint16_t y ) ; // clear tft screen -void printTft(char * text) ; +void blankTft(const char * titel , uint16_t x , uint16_t y ) ; // clear tft screen +void printTft(const char * text) ; boolean convertPosToXY( uint8_t pos , int32_t *_x, int32_t *_y , uint16_t btnDef[12][4]) ; uint8_t convertBtnPosToBtnIdx( uint8_t page , uint8_t btn ) ; @@ -81,6 +81,8 @@ void updateBtnState( ) ; // tester le touchscreen et mettre à jour les va void drawUpdatedBtn( ) ; // update the color of the buttons on a page (based on currentPage, justPressedBtn , justReleasedBtn, longPressedBtn) void executeMainActionBtn () ; // execute the action forseen for ONE button (if any) (change currentPage, printingStatus,... but do not change the display) +void drawConnectPage(); // Redraw the initial connection page + void drawFullPage() ; // redraw totally the page void drawPartPage() ; // update only the data on creen (not the button) diff --git a/grbl_controller_esp32.ino b/grbl_controller_esp32.ino index 3438ae1..2f7e8f8 100644 --- a/grbl_controller_esp32.ino +++ b/grbl_controller_esp32.ino @@ -68,6 +68,7 @@ Sur l'écran de base, prévoir l'affichage des infos #include #include "soc/uart_reg.h" #include "soc/uart_struct.h" +#include "GrblStream.h" //uart_dev_t * dev = (volatile uart_dev_t *)(DR_REG_UART_BASE) ; // @@ -144,6 +145,8 @@ void initMenuOptions( void) ; //prototype /**************************************************************************************************/ +GrblStream* grblStream = NULL; + void setup() { // initialiser le serial vers USB // initialiser le UART vers GRBL @@ -193,14 +196,33 @@ void setup() { telnetInit() ; } while ( Serial2.available() ) Serial2.read() ; // clear input buffer which can contains messages sent by GRBL in reply to noise captured before Serial port was initialised. - Serial2.write(0x18) ; // send a soft reset +#ifdef GRBL_IP + String grblIP = String(GRBL_IP) + ":" + String(GRBL_PORT); + String msg = String(__CONNECT) + grblIP; + while (!grblStream) { + fillMsg(msg.c_str(), TFT_YELLOW); + drawConnectPage(); + Serial.println(msg); + try { + grblStream = new TCPGrblStream(GRBL_IP, 23, 3000); + // If you send a soft reset via Telnet, the connection will drop + } catch (...) { + } + } + msg = String("Grbl @ ") + grblIP; + fillMsg(msg.c_str(), TFT_GREEN); +#else + grblStream = new SerialGrblStream(Serial2); + grblStream->print((char)0x18) ; // send a soft reset + fillMsg("Grbl @ Serial2", TFT_GREEN); +#endif + drawFullPage(); delay(100); - Serial2.println("$10=3"); // $10=3 is used in order to get available space in GRBL buffer in GRBL status messages; il also means we are asking GRBL to sent always MPos. - while (Serial2.availableForWrite() != 0x7F ) ; // wait that all char are sent - //Serial2.flush(); // this is used to avoid sending to many jogging movements when using the nunchuk + grblStream->println("$10=3"); // $10=3 is used in order to get available space in GRBL buffer in GRBL status messages; il also means we are asking GRBL to sent always MPos. + grblStream->waitSent(); // wait until all chars are sent //delay(100); - //while ( Serial2.available() ) { - // Serial.println(Serial2.read(),HEX); + //while ( grblStream->available() ) { + // Serial.println(grblStream->read(),HEX); //} // to debug // grblLastMessage[0]= 0x80 ; diff --git a/language.h b/language.h index 3bdc053..8a633e0 100644 --- a/language.h +++ b/language.h @@ -77,6 +77,7 @@ #define __SET_REPEAT_CAL "Definissez REPEAT_CAL sur false pour ne pas r\x80" "p\x80" "ter cette operation!" #define __CAL_COMPLETED "Calibration finie!" +#define __CONNECT "Connecter a " #define __SPIFFS_FORMATTED "SPIFFS format\x80" #define __CMD_NOT_LOADED "Cmd non charg\x80" "e" #define __TELENET_DISCONNECTED "Telnet d\x80" "connect\x80" @@ -236,6 +237,7 @@ #define __SET_REPEAT_CAL "REPEAT_CAL auf false setzen, um das erneute Ausf\x83" "hren zu stoppen!" #define __CAL_COMPLETED "Kalibrierung abgeschlossen!" +#define __CONNECT "Verbunden mit " #define __SPIFFS_FORMATTED "SPIFFS formatiert" #define __CMD_NOT_LOADED "Cmd nicht geladen" #define __TELENET_DISCONNECTED "Telnet getrennt" @@ -397,6 +399,7 @@ #define __SET_REPEAT_CAL "Set REPEAT_CAL to false to stop this running again!" #define __CAL_COMPLETED "Calibration complete!" +#define __CONNECT "Connect to " #define __SPIFFS_FORMATTED "SPIFFS formatted" #define __CMD_NOT_LOADED "Cmd not loaded" #define __TELENET_DISCONNECTED "Telnet disconnected" diff --git a/menu_file.cpp b/menu_file.cpp index 409a71b..2509187 100644 --- a/menu_file.cpp +++ b/menu_file.cpp @@ -289,7 +289,7 @@ boolean fileIsCmd() { // check if the file in aDir[dirLevel+1] is a cm // here we assume that the file name identifies a command; so we can store it on SPIFFS // first we remove the extension pchar = strchr(fileName , '.' ); // replace the first '.' by 0 (= skip the file extension) - if ( ! pchar == NULL ) { + if (pchar) { *pchar = 0 ; } deleteFileLike( fileName ) ; // then look in SPIFFS for files beginning by Cmdx_ and if found, delete them diff --git a/platformio.ini b/platformio.ini new file mode 100644 index 0000000..baaafab --- /dev/null +++ b/platformio.ini @@ -0,0 +1,30 @@ +; PlatformIO Project Configuration File +; +; Build options: build flags, source filter +; Upload options: custom upload port, speed and extra flags +; Library options: dependencies, extra library storages +; Advanced options: extra scripting +; +; Please visit documentation for the other options and examples +; https://docs.platformio.org/page/projectconf.html +[platformio] +src_dir = . + +[env:esp32dev] +platform = espressif32 +board = esp32dev +framework = arduino +lib_deps = + SdFat +src_filter = + +<*.h> +<*.s> +<*.S> +<*.cpp> +<*.c> +<*.ino> + + -<.git/> - - - - - +upload_speed = 921600 +upload_port = /dev/ttyS16 +monitor_speed = 115200 +monitor_flags = + --filter=esp32_exception_decoder +monitor_port = /dev/ttyS12 +build_flags = + -DCORE_DEBUG_LEVEL=0 +