From af17fff2e022650b339542ef8cc4d2ec69670608 Mon Sep 17 00:00:00 2001 From: luc lebosse Date: Thu, 8 Sep 2016 15:15:56 +0200 Subject: [PATCH] put serial/tcp bridge in a class --- esp3d/bridge.cpp | 88 ++++++++++++++++++++++++++++++++++++++++++ esp3d/bridge.h | 38 ++++++++++++++++++ esp3d/config.h | 14 ++++++- esp3d/esp3d.ino | 66 ++++--------------------------- esp3d/webinterface.cpp | 11 ++++-- 5 files changed, 154 insertions(+), 63 deletions(-) create mode 100644 esp3d/bridge.cpp create mode 100644 esp3d/bridge.h diff --git a/esp3d/bridge.cpp b/esp3d/bridge.cpp new file mode 100644 index 00000000..e22b0dc4 --- /dev/null +++ b/esp3d/bridge.cpp @@ -0,0 +1,88 @@ +/* + bridge.cpp - esp3d bridge serial/tcp class + + Copyright (c) 2014 Luc Lebosse. All rights reserved. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +#include "bridge.h" +#include "command.h" +#include "webinterface.h" + +#ifdef TCP_IP_DATA_FEATURE +WiFiServer * data_server; +WiFiClient serverClients[MAX_SRV_CLIENTS]; +#endif + + bool BRIDGE::processFromSerial2TCP(){ + uint8_t i; + //check UART for data + if(Serial.available()) { + size_t len = Serial.available(); + uint8_t sbuf[len]; + Serial.readBytes(sbuf, len); +#ifdef TCP_IP_DATA_FEATURE + //push UART data to all connected tcp clients + for(i = 0; i < MAX_SRV_CLIENTS; i++) { + if (serverClients[i] && serverClients[i].connected()) { + serverClients[i].write(sbuf, len); + delay(1); + } + } +#endif + //process data if any + COMMAND::read_buffer_serial(sbuf, len); + return true; + } + else return false; + } + #ifdef TCP_IP_DATA_FEATURE + void BRIDGE::processFromTCP2Serial(){ + uint8_t i,data; + //check if there are any new clients + if (data_server->hasClient()) { + for(i = 0; i < MAX_SRV_CLIENTS; i++) { + //find free/disconnected spot + if (!serverClients[i] || !serverClients[i].connected()) { + if(serverClients[i]) { + serverClients[i].stop(); + } + serverClients[i] = data_server->available(); + continue; + } + } + //no free/disconnected spot so reject + WiFiClient serverClient = data_server->available(); + serverClient.stop(); + } + //check clients for data + //to avoid any pollution if Uploading file to SDCard + if ((web_interface->blockserial) == false){ + for(i = 0; i < MAX_SRV_CLIENTS; i++) { + if (serverClients[i] && serverClients[i].connected()) { + if(serverClients[i].available()) { + //get data from the tcp client and push it to the UART + while(serverClients[i].available()) { + data = serverClients[i].read(); + Serial.write(data); + COMMAND::read_buffer_tcp(data); + } + } + } + } + } + } +#endif diff --git a/esp3d/bridge.h b/esp3d/bridge.h new file mode 100644 index 00000000..d789ad6d --- /dev/null +++ b/esp3d/bridge.h @@ -0,0 +1,38 @@ +/* + + Copyright (c) 2014 Luc Lebosse. All rights reserved. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +#include "config.h" + +#ifndef BRIDGE_H +#define BRIDGE_H + +#ifdef TCP_IP_DATA_FEATURE +extern WiFiServer * data_server; +#endif + +class BRIDGE +{ +public: + static bool processFromSerial2TCP(); + #ifdef TCP_IP_DATA_FEATURE + static void processFromTCP2Serial(); +#endif +}; + +#endif diff --git a/esp3d/config.h b/esp3d/config.h index 14e51679..0b4cd890 100644 --- a/esp3d/config.h +++ b/esp3d/config.h @@ -1,5 +1,5 @@ /* - CONFIG.H - esp8266 configuration class + config.h - ESP3D configuration class Copyright (c) 2014 Luc Lebosse. All rights reserved. @@ -28,6 +28,9 @@ //FIRMWARE_TARGET: the targeted FW, can be REPETIER (Original Repetier)/ REPETIER4DV (Repetier for Davinci) / MARLIN (Marlin)/ SMOOTHIEWARE (Smoothieware) #define FIRMWARE_TARGET SMOOTHIEWARE +//number of clients allowed to use data port at once +#define MAX_SRV_CLIENTS 1 + //comment to disable //MDNS_FEATURE: this feature allow type the name defined //in web browser by default: http:\\esp8266.local and connect @@ -85,7 +88,9 @@ //#define DEBUG_ESP3D //#define DEBUG_OUTPUT_SPIFFS //#define DEBUG_OUTPUT_SD -#define DEBUG_OUTPUT_SERIAL +//#define DEBUG_OUTPUT_SERIAL + +#include #ifdef DEBUG_ESP3D #ifdef DEBUG_OUTPUT_SPIFFS @@ -115,6 +120,11 @@ #define FSINFO FSInfo #endif +#ifndef TCP_IP_DATA_FEATURE +#undef MAX_SRV_CLIENTS +#define MAX_SRV_CLIENTS 0 +#endif + #ifndef CONFIG_h #define CONFIG_h diff --git a/esp3d/esp3d.ino b/esp3d/esp3d.ino index 753392c1..ee035460 100644 --- a/esp3d/esp3d.ino +++ b/esp3d/esp3d.ino @@ -1,12 +1,12 @@ /* - This file is part of ESP8266 Firmware for 3D printer. + This file is part of ESP3D Firmware for 3D printer. - ESP8266 Firmware for 3D printer is free software: you can redistribute it and/or modify + ESP3D Firmware for 3D printer is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. - ESP8266 Firmware for 3D printer is distributed in the hope that it will be useful, + ESP3D Firmware for 3D printer is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. @@ -30,6 +30,7 @@ #include #include "config.h" #include "wifi.h" +#include "bridge.h" #include "webinterface.h" #include "command.h" #include @@ -49,16 +50,15 @@ DNSServer dnsServer; #ifdef NETBIOS_FEATURE #include #endif -#define MAX_SRV_CLIENTS 1 -WiFiServer * data_server; -WiFiClient serverClients[MAX_SRV_CLIENTS]; void setup() { bool breset_config=false; long baud_rate=0; web_interface = NULL; +#ifdef TCP_IP_DATA_FEATURE data_server = NULL; +#endif // init: #ifdef DEBUG_ESP3D Serial.begin(DEFAULT_BAUD_RATE); @@ -197,60 +197,10 @@ void loop() #endif //web requests web_interface->WebServer.handleClient(); - -//TODO use a method to handle serial also in class and call it instead of this one - uint8_t i,data; #ifdef TCP_IP_DATA_FEATURE - //check if there are any new clients - if (data_server->hasClient()) { - for(i = 0; i < MAX_SRV_CLIENTS; i++) { - //find free/disconnected spot - if (!serverClients[i] || !serverClients[i].connected()) { - if(serverClients[i]) { - serverClients[i].stop(); - } - serverClients[i] = data_server->available(); - continue; - } - } - //no free/disconnected spot so reject - WiFiClient serverClient = data_server->available(); - serverClient.stop(); - } - //check clients for data - //to avoid any pollution if Uploading file to SDCard - if ((web_interface->blockserial) == false){ - for(i = 0; i < MAX_SRV_CLIENTS; i++) { - if (serverClients[i] && serverClients[i].connected()) { - if(serverClients[i].available()) { - //get data from the tcp client and push it to the UART - while(serverClients[i].available()) { - data = serverClients[i].read(); - Serial.write(data); - COMMAND::read_buffer_tcp(data); - } - } - } - } - } + BRIDGE::processFromTCP2Serial(); #endif - //check UART for data - if(Serial.available()) { - size_t len = Serial.available(); - uint8_t sbuf[len]; - Serial.readBytes(sbuf, len); -#ifdef TCP_IP_DATA_FEATURE - //push UART data to all connected tcp clients - for(i = 0; i < MAX_SRV_CLIENTS; i++) { - if (serverClients[i] && serverClients[i].connected()) { - serverClients[i].write(sbuf, len); - delay(1); - } - } -#endif - //process data if any - COMMAND::read_buffer_serial(sbuf, len); - } + BRIDGE::processFromSerial2TCP(); if (web_interface->restartmodule) { CONFIG::esp_restart(); } diff --git a/esp3d/webinterface.cpp b/esp3d/webinterface.cpp index 40f27f44..2edebf02 100644 --- a/esp3d/webinterface.cpp +++ b/esp3d/webinterface.cpp @@ -1,5 +1,5 @@ /* - webinterface.cpp - esp8266 configuration class + webinterface.cpp - ESP3D configuration class Copyright (c) 2014 Luc Lebosse. All rights reserved. @@ -30,6 +30,7 @@ #include "LinkedList.h" #include "storestrings.h" #include "command.h" +#include "bridge.h" #ifdef SSDP_FEATURE #include @@ -3087,7 +3088,8 @@ void handle_not_found() } else page_not_found = true; if (page_not_found ) - { + { + LOG("Page not found it \n") if (SPIFFS.exists("/404.tpl")) { STORESTRINGS_CLASS KeysList ; STORESTRINGS_CLASS ValuesList ; @@ -3536,7 +3538,10 @@ String WEBINTERFACE_CLASS::getContentType(String filename) return "image/png"; } else if(filename.endsWith(".gif")) { return "image/gif"; - } else if(filename.endsWith(".jpg")) { + } else if(filename.endsWith(".jpeg")) { + return "image/jpeg"; + } + else if(filename.endsWith(".jpg")) { return "image/jpeg"; } else if(filename.endsWith(".ico")) { return "image/x-icon";