ESP3D  3.0
Firmware for ESP boards connected to 3D Printer
esp3d.cpp
Go to the documentation of this file.
1 /*
2  This file is part of ESP3D Firmware for 3D printer.
3 
4  ESP3D Firmware for 3D printer is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 3 of the License, or
7  (at your option) any later version.
8 
9  ESP3D Firmware for 3D printer is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with this Firmware. If not, see <http://www.gnu.org/licenses/>.
16 
17  This firmware is using the standard arduino IDE with module to support ESP8266/ESP32:
18  https://github.com/esp8266/Arduino
19  https://github.com/espressif/arduino-esp32
20 
21  Latest version of the code and documentation can be found here :
22  https://github.com/luc-github/ESP3D
23 
24  Main author: luc lebosse
25 
26 */
27 #include "esp3d.h"
28 #include "../include/esp3d_config.h"
29 #include "settings_esp3d.h"
30 #include "../modules/serial/serial_service.h"
31 #if defined (WIFI_FEATURE) || defined(ETH_FEATURE)
32 #include "../modules/network/netconfig.h"
33 #endif //WIFI_FEATURE || ETH FEATURE
34 #if defined(FILESYSTEM_FEATURE)
35 #include "../modules/filesystem/esp_filesystem.h"
36 #endif //FILESYSTEM_FEATURE
37 #ifdef CONNECTED_DEVICES_FEATURE
38 #include "../modules/devices/devices_services.h"
39 #endif //CONNECTED_DEVICES_FEATURE
40 #ifdef DISPLAY_DEVICE
41 #include "../modules/display/display.h"
42 #endif //DISPLAY_DEVICE
43 #ifdef ESP_GCODE_HOST_FEATURE
44 #include "../modules/gcode_host/gcode_host.h"
45 #endif //ESP_GCODE_HOST_FEATURE
46 #ifdef ESP_LUA_INTERPRETER_FEATURE
47 #include "../modules/lua_interpreter/lua_interpreter_service.h"
48 #endif //#ifdef
49 #include "esp3doutput.h"
50 #include "../modules/boot_delay/boot_delay.h"
51 
52 
53 bool Esp3D::restart = false;
54 
55 //Contructor
57 {
58 
59 }
60 
61 //Destructor
63 {
64  end();
65 }
66 
67 //Begin which setup everything
69 {
70  BootDelay bd;
71  Hal::begin();
73  //init output
75  bool res = true;
76 #if defined(CONNECTED_DEVICES_FEATURE)
77  if (!DevicesServices::begin()) {
78  log_esp3d("Error setup connected devices");
79  res = false;
80  }
81 #endif //CONNECTED_DEVICES_FEATURE
82  //delay to avoid to disturb printer
83  bd.begin();
84  log_esp3d("Mode %d", WiFi.getMode());
85  if (!Settings_ESP3D::begin()) {
86  log_esp3d("Need reset settings");
87  reset();
88  //Restart ESP3D
89  restart_esp();
90  }
91  //BT do not start automaticaly so should be OK
92  //Serial service
93  if (!serial_service.begin()) {
94  log_esp3d("Error with serial service");
95  res = false;
96  }
97  //Setup Filesystem
98 #if defined(FILESYSTEM_FEATURE)
99  if (!ESP_FileSystem::begin()) {
100  log_esp3d("Error with filesystem service");
101  res = false;
102  }
103 #endif //FILESYSTEM_FEATURE
104 #ifdef DISPLAY_DEVICE
106  log_esp3d("Main screen");
107 #endif //DISPLAY_DEVICE
108  //Setup Network
109 #if defined(WIFI_FEATURE) || defined(ETH_FEATURE)
110  if (!NetConfig::begin()) {
111  log_esp3d("Error setup network");
112  res = false;
113  }
114 #endif //WIFI_FEATURE
115 #if defined(ESP_AUTOSTART_SCRIPT)
116  esp3d_gcode_host.processscript(ESP_AUTOSTART_SCRIPT);
117 #endif //ESP_AUTOSTART_FEATURE
118  return res;
119 }
120 
121 //Process which handle all input
123 {
124  //if need restart
125  if (restart) {
126  restart_now();
127  }
129 #if defined(WIFI_FEATURE) || defined(ETH_FEATURE)
131 #endif //WIFI_FEATURE || ETH_FEATURE
132 #if defined(CONNECTED_DEVICES_FEATURE)
134 #endif //CONNECTED_DEVICES_FEATURE
135 }
136 
137 //End ESP3D
139 {
140 #if defined(CONNECTED_DEVICES_FEATURE)
142 #endif //CONNECTED_DEVICES_FEATURE
143 #if defined(WIFI_FEATURE) || defined(ETH_FEATURE)
144  NetConfig::end();
145 #endif //WIFI_FEATURE || ETH_FEATURE
146 #if defined(FILESYSTEM_FEATURE)
148 #endif //FILESYSTEM_FEATURE
150  return true;
151 }
152 
153 //Reset ESP3D settings
155 {
156  bool resetOk = true;
157  if (!serial_service.reset()) {
158  resetOk = false;
159  log_esp3d("Reset serial error");
160  }
161  if (!Settings_ESP3D::reset()) {
162  log_esp3d("Reset settings error");
163  resetOk = false;
164  }
165  return resetOk;
166 }
167 
168 //Set Restart flag
169 void Esp3D::restart_esp(bool need_restart)
170 {
171  restart = need_restart;
172 }
173 
174 void Esp3D::restart_now()
175 {
176  log_esp3d("Restarting");
177  end();
179  ESP.restart();
180  while (1) {
181  delay (1);
182  }
183 }
184 
esp3d.h
Esp3D::~Esp3D
~Esp3D()
Definition: esp3d.cpp:62
Display::show_screenID
void show_screenID(uint8_t screenID)
ESP3DOutput::isOutput
static bool isOutput(uint8_t flag, bool fromsettings=false)
Definition: esp3doutput.cpp:78
Esp3D::begin
bool begin()
Definition: esp3d.cpp:68
BootDelay
Definition: boot_delay.h:28
ESP_FileSystem::begin
static bool begin()
SerialService::begin
bool begin()
Definition: serial_service.cpp:61
SerialService::end
bool end()
Definition: serial_service.cpp:87
DevicesServices::end
static void end()
Definition: devices_services.cpp:102
SerialService::reset
bool reset()
Definition: serial_service.cpp:189
Esp3D::handle
void handle()
Definition: esp3d.cpp:122
Esp3D::reset
static bool reset()
Definition: esp3d.cpp:154
Esp3D::end
bool end()
Definition: esp3d.cpp:138
SerialService::handle
void handle()
Definition: serial_service.cpp:120
NetConfig::end
static void end()
Definition: netconfig.cpp:308
Esp3D::restart_esp
static void restart_esp(bool need_restart=true)
Definition: esp3d.cpp:169
settings_esp3d.h
SerialService::swap
void swap()
Definition: serial_service.cpp:268
NetConfig::handle
static void handle()
Definition: netconfig.cpp:361
DevicesServices::begin
static bool begin()
Definition: devices_services.cpp:56
Settings_ESP3D::begin
static bool begin()
Definition: settings_esp3d.cpp:171
esp3d_gcode_host
GcodeHost esp3d_gcode_host
Definition: gcode_host.cpp:30
NetConfig::begin
static bool begin()
Definition: netconfig.cpp:215
Hal::begin
static bool begin()
Definition: hal.cpp:195
serial_service
SerialService serial_service
Definition: serial_service.cpp:42
DEBUG_ESP3D_INIT
#define DEBUG_ESP3D_INIT
Definition: debug_esp3d.h:32
BootDelay::begin
bool begin()
Definition: boot_delay.cpp:42
GcodeHost::processscript
bool processscript(const char *line)
Definition: gcode_host.cpp:340
MAIN_SCREEN
#define MAIN_SCREEN
Definition: advanceddisplay.h:23
log_esp3d
#define log_esp3d(format,...)
Definition: debug_esp3d.h:29
Esp3D::Esp3D
Esp3D()
Definition: esp3d.cpp:56
ESP_FileSystem::end
static void end()
Settings_ESP3D::reset
static bool reset()
Definition: settings_esp3d.cpp:1014
ESP_ALL_CLIENTS
#define ESP_ALL_CLIENTS
Definition: esp3doutput.h:30
esp3d_display
Display esp3d_display
esp3doutput.h
DevicesServices::handle
static void handle()
Definition: devices_services.cpp:128