From 569b3ccf71ebc0b5eef687bf99fb258aa8a6254a Mon Sep 17 00:00:00 2001 From: Mitch Bradley Date: Tue, 13 Jul 2021 15:35:46 -1000 Subject: [PATCH] Moved Native code into NativeStubs library. This helps with supporting radio config --- Grbl_Esp32/src/Config.h | 10 +- Grbl_Esp32/src/DumpConfig.cpp | 69 +++- Grbl_Esp32/src/Grbl.h | 16 +- Grbl_Esp32/src/I2SOut.cpp | 24 +- Grbl_Esp32/src/Motors/Solenoid.h | 3 +- Grbl_Esp32/src/Report.cpp | 4 +- Grbl_Esp32/src/Report.h | 2 + Grbl_Esp32/src/SDCard.h | 8 +- Grbl_Esp32/src/Settings.h | 2 +- Grbl_Esp32/src/Spindles/BESCSpindle.cpp | 1 - Grbl_Esp32/src/Spindles/BESCSpindle.h | 2 + Grbl_Esp32/src/Spindles/PWMSpindle.cpp | 2 +- Grbl_Esp32/src/Spindles/VFDSpindle.cpp | 8 +- Grbl_Esp32/src/Uart.cpp | 4 +- Grbl_Esp32/src/Uart.h | 6 +- Grbl_Esp32/src/WebUI/BTConfig.cpp | 21 +- Grbl_Esp32/src/WebUI/NotificationsService.cpp | 15 +- Grbl_Esp32/src/WebUI/Serial2Socket.cpp | 9 - Grbl_Esp32/src/WebUI/TelnetServer.cpp | 6 - Grbl_Esp32/src/WebUI/WebServer.cpp | 159 ++++----- Grbl_Esp32/src/WebUI/WebServer.h | 48 +-- Grbl_Esp32/src/WebUI/WebSettings.cpp | 82 +++-- Grbl_Esp32/src/WebUI/WifiConfig.cpp | 37 ++- Grbl_Esp32/src/WebUI/WifiServices.cpp | 97 +++--- Grbl_Esp32/src/native.h | 309 ------------------ libraries/NativeStubs/BluetoothSerial.cpp | 1 + libraries/NativeStubs/BluetoothSerial.h | 32 ++ libraries/NativeStubs/ESP.cpp | 3 + libraries/NativeStubs/ESP.h | 49 +++ .../NativeStubs/FS.cpp | 56 +--- libraries/NativeStubs/FS.h | 63 ++++ libraries/NativeStubs/IPAddress.cpp | 7 + libraries/NativeStubs/IPAddress.h | 57 ++++ libraries/NativeStubs/SD.cpp | 3 + libraries/NativeStubs/SD.h | 12 + libraries/NativeStubs/SPIFFS.cpp | 3 + libraries/NativeStubs/SPIFFS.h | 24 ++ libraries/NativeStubs/TMCStepper.cpp | 16 + libraries/NativeStubs/WebServer.h | 40 +++ libraries/NativeStubs/WebSocketsServer.h | 7 + libraries/NativeStubs/WiFi.cpp | 3 + libraries/NativeStubs/WiFi.h | 19 ++ libraries/NativeStubs/WiFiClient.h | 24 ++ libraries/NativeStubs/WiFiClientSecure.h | 1 + libraries/NativeStubs/WiFiServer.h | 27 ++ .../src => libraries/NativeStubs}/binary.h | 0 libraries/NativeStubs/driver/rmt.h | 1 + libraries/NativeStubs/driver/timer.h | 28 ++ libraries/NativeStubs/driver/uart.h | 5 + libraries/NativeStubs/esp_task_wdt.h | 5 + libraries/NativeStubs/freertos/task.h | 43 +++ libraries/NativeStubs/library.properties | 10 + libraries/NativeStubs/native.h | 29 ++ libraries/NativeStubs/nvs.h | 35 ++ platformio.ini | 3 + 55 files changed, 937 insertions(+), 613 deletions(-) delete mode 100644 Grbl_Esp32/src/native.h create mode 100644 libraries/NativeStubs/BluetoothSerial.cpp create mode 100644 libraries/NativeStubs/BluetoothSerial.h create mode 100644 libraries/NativeStubs/ESP.cpp create mode 100644 libraries/NativeStubs/ESP.h rename Grbl_Esp32/src/Native.cpp => libraries/NativeStubs/FS.cpp (61%) create mode 100644 libraries/NativeStubs/FS.h create mode 100644 libraries/NativeStubs/IPAddress.cpp create mode 100644 libraries/NativeStubs/IPAddress.h create mode 100644 libraries/NativeStubs/SD.cpp create mode 100644 libraries/NativeStubs/SD.h create mode 100644 libraries/NativeStubs/SPIFFS.cpp create mode 100644 libraries/NativeStubs/SPIFFS.h create mode 100644 libraries/NativeStubs/TMCStepper.cpp create mode 100644 libraries/NativeStubs/WebServer.h create mode 100644 libraries/NativeStubs/WebSocketsServer.h create mode 100644 libraries/NativeStubs/WiFi.cpp create mode 100644 libraries/NativeStubs/WiFi.h create mode 100644 libraries/NativeStubs/WiFiClient.h create mode 100644 libraries/NativeStubs/WiFiClientSecure.h create mode 100644 libraries/NativeStubs/WiFiServer.h rename {Grbl_Esp32/src => libraries/NativeStubs}/binary.h (100%) create mode 100644 libraries/NativeStubs/driver/rmt.h create mode 100644 libraries/NativeStubs/driver/timer.h create mode 100644 libraries/NativeStubs/driver/uart.h create mode 100644 libraries/NativeStubs/esp_task_wdt.h create mode 100644 libraries/NativeStubs/freertos/task.h create mode 100644 libraries/NativeStubs/library.properties create mode 100644 libraries/NativeStubs/native.h create mode 100644 libraries/NativeStubs/nvs.h diff --git a/Grbl_Esp32/src/Config.h b/Grbl_Esp32/src/Config.h index f7ada2fa..a5370ca3 100644 --- a/Grbl_Esp32/src/Config.h +++ b/Grbl_Esp32/src/Config.h @@ -41,7 +41,7 @@ Some features should not be changed. See notes below. #include "NutsBolts.h" #ifdef NATIVE -# include "native.h" +# include #endif // It is no longer necessary to edit this file to choose @@ -101,13 +101,11 @@ const int MAX_N_AXIS = 6; //#define CONNECT_TO_SSID "your SSID" //#define SSID_PASSWORD "your SSID password" //CONFIGURE_EYECATCH_BEGIN (DO NOT MODIFY THIS LINE) -#ifndef NATIVE -# define ENABLE_BLUETOOTH // enable bluetooth +#define ENABLE_BLUETOOTH // enable bluetooth -# define ENABLE_SD_CARD // enable use of SD Card to run jobs +#define ENABLE_SD_CARD // enable use of SD Card to run jobs -# define ENABLE_WIFI //enable wifi -#endif +#define ENABLE_WIFI //enable wifi #if defined(ENABLE_WIFI) || defined(ENABLE_BLUETOOTH) # define WIFI_OR_BLUETOOTH diff --git a/Grbl_Esp32/src/DumpConfig.cpp b/Grbl_Esp32/src/DumpConfig.cpp index dcf099cc..439447a2 100644 --- a/Grbl_Esp32/src/DumpConfig.cpp +++ b/Grbl_Esp32/src/DumpConfig.cpp @@ -19,9 +19,11 @@ #include "Spindles/VFDSpindle.h" #include "Spindles/YL620Spindle.h" #include "Spindles/Laser.h" +#include "WebUI/WebSettings.h" using namespace Motors; using namespace Spindles; +using namespace WebUI; #define p(...) \ do { \ @@ -410,11 +412,61 @@ void print_probe() { item("check_mode_start", cms); end_section(); } +void print_ips(const char* ssid, const char* ip, const char* gateway, const char* netmask, bool dhcp) { + item("ssid", ssid); + item("ip_address", ip); + item("gateway", gateway); + item("netmask", netmask); + item("dhcp", dhcp); +} +void print_ap() { +#ifdef ENABLE_WIFI + section("wifi_ap"); + print_ips(wifi_ap_ssid->get(), wifi_ap_ip->getStringValue(), "", "", true); + item("channel", wifi_ap_channel->get()); + end_section(); +#endif +} +void print_sta() { +#ifdef ENABLE_WIFI + section("wifi_sta"); + print_ips(wifi_sta_ssid->get(), + wifi_sta_ip->getStringValue(), + wifi_sta_gateway->getStringValue(), + wifi_sta_netmask->getStringValue(), + wifi_sta_mode->get() == DHCP_MODE); + end_section(); +#endif +} +void print_bt() { +#ifdef ENABLE_BLUETOOTH + section("bluetooth"); + item("name", bt_name->get()); + end_section(); +#endif +} void print_comms() { + // Radio mode??? + section("comms"); - // XXX +#ifdef ENABLE_WIFI + item("telnet_enable", telnet_enable->get()); + item("telnet_port", telnet_port->get()); + + item("http_enable", http_enable->get()); + item("http_port", http_port->get()); + + item("hostname", wifi_hostname->get()); + + print_ap(); + print_sta(); +#endif +#ifdef ENABLE_BLUETOOTH + print_bt(); +#endif end_section(); } +// notifications? void print_macros() { section("macros"); item("n0", startup_line_0->get()); @@ -426,7 +478,18 @@ void print_macros() { end_section(); } const char* makeSpeedMap(PWM* s) { - return "0=0% 1000=100%"; + static char temp[100]; + float off_percent = spindle_pwm_off_value->get(); + float min_percent = spindle_pwm_min_value->get(); + float max_percent = spindle_pwm_max_value->get(); + float min_rpm = rpm_min->get(); + float max_rpm = rpm_max->get(); + if (min_rpm == 0.0f && off_percent == min_percent) { + sprintf(temp, "%d=%.1f%% %d=%.1f%%", (int)min_rpm, min_percent, (int)max_rpm, max_percent); + } else { + sprintf(temp, "%d=%.1f%% %d=%.1f%% %d=%.1f%%", 0, off_percent, (int)min_rpm, min_percent, (int)max_rpm, max_percent); + } + return temp; } void print_spindle(const char* name, Spindle* s) { section(name); @@ -464,8 +527,8 @@ void print_dac_spindle(Dac* s) { end_section(); } void print_besc_spindle(BESC* s) { + s->_pwm_freq = BESC_PWM_FREQ; // Override in parent class print_pwm_spindle("besc", s); - // XXX override frequency to BEDC_PWM_FREQ and period to BESC_PULSE_PERIOD item("min_pulse_us", int(BESC_MIN_PULSE_SECS * 1000000)); item("max_pulse_us", int(BESC_MAX_PULSE_SECS * 1000000)); diff --git a/Grbl_Esp32/src/Grbl.h b/Grbl_Esp32/src/Grbl.h index 532eff27..42b89545 100644 --- a/Grbl_Esp32/src/Grbl.h +++ b/Grbl_Esp32/src/Grbl.h @@ -28,18 +28,20 @@ const char* const GRBL_VERSION_BUILD = "20210424"; #include #ifdef NATIVE -# include "native.h" +# include +# include #else -# include -# include -# include -# include - -# include +// # include # define WEAK_FUNC __attribute__((weak)) #endif +#include +#include +#include + +#include + // Define the Grbl system include files. NOTE: Do not alter organization. #include "Config.h" #include "NutsBolts.h" diff --git a/Grbl_Esp32/src/I2SOut.cpp b/Grbl_Esp32/src/I2SOut.cpp index 27d2ebc4..79aaf03f 100644 --- a/Grbl_Esp32/src/I2SOut.cpp +++ b/Grbl_Esp32/src/I2SOut.cpp @@ -42,17 +42,18 @@ */ #include "I2SOut.h" -// This block of #includes is necessary for Report.h -#include "Error.h" -#include "WebUI/Authentication.h" -#include "WebUI/ESPResponse.h" -#include "Probe.h" -#include "System.h" -#include "Serial.h" -#include "Report.h" -#include "Pins.h" - #ifndef NATIVE + +// This block of #includes is necessary for Report.h +# include "Error.h" +# include "WebUI/Authentication.h" +# include "WebUI/ESPResponse.h" +# include "Probe.h" +# include "System.h" +# include "Serial.h" +# include "Report.h" +# include "Pins.h" + # include # include # include @@ -525,15 +526,14 @@ static void IRAM_ATTR i2sOutTask(void* parameter) { } I2S_OUT_PULSER_EXIT_CRITICAL(); // Unlock pulser status - static UBaseType_t uxHighWaterMark = 0; # ifdef DEBUG_TASK_STACK + static UBaseType_t uxHighWaterMark = 0; reportTaskStackSize(uxHighWaterMark); # endif } } # endif -// // External funtions // void IRAM_ATTR i2s_out_delay() { diff --git a/Grbl_Esp32/src/Motors/Solenoid.h b/Grbl_Esp32/src/Motors/Solenoid.h index 3f9e5029..3564060f 100644 --- a/Grbl_Esp32/src/Motors/Solenoid.h +++ b/Grbl_Esp32/src/Motors/Solenoid.h @@ -11,7 +11,8 @@ namespace Motors { void update() override; void init() override; void set_disable(bool disable) override; - void name() override { return "unipolar"; } + + void name() override { return "solenoid"; } float _transition_poiont; diff --git a/Grbl_Esp32/src/Report.cpp b/Grbl_Esp32/src/Report.cpp index 4206aedb..dfbf6dcb 100644 --- a/Grbl_Esp32/src/Report.cpp +++ b/Grbl_Esp32/src/Report.cpp @@ -927,15 +927,15 @@ char* reportAxisNameMsg(uint8_t axis) { return name; } -void reportTaskStackSize(UBaseType_t& saved) { #ifdef DEBUG_TASK_STACK +void reportTaskStackSize(UBaseType_t& saved) { UBaseType_t newHighWater = uxTaskGetStackHighWaterMark(NULL); if (newHighWater != saved) { saved = newHighWater; grbl_msg_sendf(CLIENT_SERIAL, MsgLevel::Info, "%s Min Stack Space: %d", pcTaskGetTaskName(NULL), saved); } -#endif } +#endif void mpos_to_wpos(float* position) { float* wco = get_wco(); diff --git a/Grbl_Esp32/src/Report.h b/Grbl_Esp32/src/Report.h index eecd905a..c064665b 100644 --- a/Grbl_Esp32/src/Report.h +++ b/Grbl_Esp32/src/Report.h @@ -127,7 +127,9 @@ char* reportAxisLimitsMsg(uint8_t axis); char* reportAxisNameMsg(uint8_t axis); char* reportAxisNameMsg(uint8_t axis, uint8_t dual_axis); +#ifdef DEBUG_TASK_STACK void reportTaskStackSize(UBaseType_t& saved); +#endif char* report_state_text(); float* get_wco(); diff --git a/Grbl_Esp32/src/SDCard.h b/Grbl_Esp32/src/SDCard.h index 410ead1a..a680e0a4 100644 --- a/Grbl_Esp32/src/SDCard.h +++ b/Grbl_Esp32/src/SDCard.h @@ -16,12 +16,8 @@ */ #include "Grbl.h" -#ifdef NATIVE -# include "native.h" -#else -# include -# include -#endif +#include +#include #include //#define SDCARD_DET_PIN -1 diff --git a/Grbl_Esp32/src/Settings.h b/Grbl_Esp32/src/Settings.h index 8ae76e38..886d7c9f 100644 --- a/Grbl_Esp32/src/Settings.h +++ b/Grbl_Esp32/src/Settings.h @@ -14,8 +14,8 @@ inline nvs_get_stats(void* p, nvs_stats_t* r) { } inline void nvs_erase_all(nvs_handle h) {} #else -# include #endif +#include #include "WebUI/ESPResponse.h" // Initialize the configuration subsystem diff --git a/Grbl_Esp32/src/Spindles/BESCSpindle.cpp b/Grbl_Esp32/src/Spindles/BESCSpindle.cpp index 8d04edec..581c1f71 100644 --- a/Grbl_Esp32/src/Spindles/BESCSpindle.cpp +++ b/Grbl_Esp32/src/Spindles/BESCSpindle.cpp @@ -33,7 +33,6 @@ #include "BESCSpindle.h" // don't change these -const double BESC_PWM_FREQ = 50.0; // Hz const double BESC_PULSE_PERIOD = (1.0 / BESC_PWM_FREQ); //calculations...don't change diff --git a/Grbl_Esp32/src/Spindles/BESCSpindle.h b/Grbl_Esp32/src/Spindles/BESCSpindle.h index 4bc36531..c2306f07 100644 --- a/Grbl_Esp32/src/Spindles/BESCSpindle.h +++ b/Grbl_Esp32/src/Spindles/BESCSpindle.h @@ -61,3 +61,5 @@ namespace Spindles { #ifndef BESC_MAX_PULSE_SECS # define BESC_MAX_PULSE_SECS 0.0022f // in seconds #endif + +const double BESC_PWM_FREQ = 50.0; // Hz diff --git a/Grbl_Esp32/src/Spindles/PWMSpindle.cpp b/Grbl_Esp32/src/Spindles/PWMSpindle.cpp index f336b572..2ff2b534 100644 --- a/Grbl_Esp32/src/Spindles/PWMSpindle.cpp +++ b/Grbl_Esp32/src/Spindles/PWMSpindle.cpp @@ -21,7 +21,7 @@ */ #include "PWMSpindle.h" #ifdef NATIVE -# include "../native.h" +# include #else # include "soc/ledc_struct.h" #endif diff --git a/Grbl_Esp32/src/Spindles/VFDSpindle.cpp b/Grbl_Esp32/src/Spindles/VFDSpindle.cpp index 514e3b8a..76983470 100644 --- a/Grbl_Esp32/src/Spindles/VFDSpindle.cpp +++ b/Grbl_Esp32/src/Spindles/VFDSpindle.cpp @@ -35,11 +35,11 @@ #include "VFDSpindle.h" #ifdef NATIVE -# include "../native.h" -#else -# include +# include #endif +#include + // Timing and modbus... The manual states that between communications, we should respect a // silent interval of 3,5 characters. If we received communications between these times, we // have to assume that the message is broken. We use a poll rate of 250 ms here, which should @@ -273,8 +273,10 @@ namespace Spindles { // if we should use a different value... vTaskDelay(VFD_RS485_POLL_RATE / portTICK_PERIOD_MS); +#ifdef REPORT_TASK_STACK static UBaseType_t uxHighWaterMark = 0; reportTaskStackSize(uxHighWaterMark); +#endif } } diff --git a/Grbl_Esp32/src/Uart.cpp b/Grbl_Esp32/src/Uart.cpp index 45e8ba78..0b6918d7 100644 --- a/Grbl_Esp32/src/Uart.cpp +++ b/Grbl_Esp32/src/Uart.cpp @@ -5,8 +5,6 @@ #include "Grbl.h" #ifdef NATIVE -# include "native.h" -// # include # include Uart::Uart(int uart_num) : _uart_num(uart_port_t(uart_num)), _pushback(-1) {} @@ -23,6 +21,7 @@ int Uart::read(TickType_t timeout) { return -1; } int c = getch(); + // This is putch() not putchar() because we want to echo to the console putch(c); return c; } @@ -50,6 +49,7 @@ size_t Uart::write(uint8_t c) { if (_uart_num) { return 0; } + // This is putchar() not putch() because we want it to go to stdout int result = putchar(c); return (result == EOF) ? 0 : 1; } diff --git a/Grbl_Esp32/src/Uart.h b/Grbl_Esp32/src/Uart.h index 3b892f12..6163afe2 100644 --- a/Grbl_Esp32/src/Uart.h +++ b/Grbl_Esp32/src/Uart.h @@ -1,10 +1,6 @@ #pragma once -#ifdef NATIVE -# include "native.h" -#else -# include -#endif +#include class Uart : public Stream { private: diff --git a/Grbl_Esp32/src/WebUI/BTConfig.cpp b/Grbl_Esp32/src/WebUI/BTConfig.cpp index 85a2a7e6..8197fb57 100644 --- a/Grbl_Esp32/src/WebUI/BTConfig.cpp +++ b/Grbl_Esp32/src/WebUI/BTConfig.cpp @@ -25,15 +25,27 @@ # include "BTConfig.h" namespace WebUI { +# ifdef NATIVE + BluetoothSerial SerialBT; + BTConfig::BTConfig() {} + void BTConfig::begin() {} + void BTConfig::end() {} + void BTConfig::handle() {} + void BTConfig::reset_settings() {} + bool BTConfig::isBTnameValid(const char* hostname) { return true; } + const char* BTConfig::info() { return ""; } + BTConfig::~BTConfig() {} + +# else BTConfig bt_config; BluetoothSerial SerialBT; -# ifdef __cplusplus +# ifdef __cplusplus extern "C" { -# endif +# endif const uint8_t* esp_bt_dev_get_address(void); -# ifdef __cplusplus +# ifdef __cplusplus } -# endif +# endif String BTConfig::_btname = ""; String BTConfig::_btclient = ""; @@ -157,5 +169,6 @@ namespace WebUI { } BTConfig::~BTConfig() { end(); } +# endif } #endif // ENABLE_BLUETOOTH diff --git a/Grbl_Esp32/src/WebUI/NotificationsService.cpp b/Grbl_Esp32/src/WebUI/NotificationsService.cpp index 27a4a7fc..21b25655 100644 --- a/Grbl_Esp32/src/WebUI/NotificationsService.cpp +++ b/Grbl_Esp32/src/WebUI/NotificationsService.cpp @@ -32,8 +32,18 @@ #ifdef ENABLE_NOTIFICATIONS # include "NotificationsService.h" -# include -# include +# ifdef NATIVE +namespace WebUI { + NotificationsService notificationsservice; + + NotificationsService::NotificationsService() {} + bool NotificationsService::sendMSG(const char* title, const char* message) { return false; } + NotificationsService::~NotificationsService() { end(); } + void NotificationsService::end() {} +} +# else +# include +# include namespace WebUI { static const int PUSHOVERTIMEOUT = 5000; @@ -369,4 +379,5 @@ namespace WebUI { NotificationsService::~NotificationsService() { end(); } } +# endif #endif //ENABLE_NOTIFICATIONS diff --git a/Grbl_Esp32/src/WebUI/Serial2Socket.cpp b/Grbl_Esp32/src/WebUI/Serial2Socket.cpp index 08810910..47d0d3cd 100644 --- a/Grbl_Esp32/src/WebUI/Serial2Socket.cpp +++ b/Grbl_Esp32/src/WebUI/Serial2Socket.cpp @@ -79,12 +79,6 @@ namespace WebUI { size_t Serial_2_Socket::write(const uint8_t* buffer, size_t size) { if ((buffer == NULL) || (!_web_socket)) { - if (buffer == NULL) { - log_i("[SOCKET]No buffer"); - } - if (!_web_socket) { - log_i("[SOCKET]No socket"); - } return 0; } @@ -103,7 +97,6 @@ namespace WebUI { _TXbuffer[_TXbufferSize] = buffer[i]; _TXbufferSize++; } - log_i("[SOCKET]buffer size %d", _TXbufferSize); handle_flush(); # endif return size; @@ -160,13 +153,11 @@ namespace WebUI { void Serial_2_Socket::handle_flush() { if (_TXbufferSize > 0 && ((_TXbufferSize >= TXBUFFERSIZE) || ((millis() - _lastflush) > FLUSHTIMEOUT))) { - log_i("[SOCKET]need flush, buffer size %d", _TXbufferSize); flush(); } } void Serial_2_Socket::flush(void) { if (_TXbufferSize > 0) { - log_i("[SOCKET]flush data, buffer size %d", _TXbufferSize); _web_socket->broadcastBIN(_TXbuffer, _TXbufferSize); //refresh timout diff --git a/Grbl_Esp32/src/WebUI/TelnetServer.cpp b/Grbl_Esp32/src/WebUI/TelnetServer.cpp index b5cfaa1e..7dd42a21 100644 --- a/Grbl_Esp32/src/WebUI/TelnetServer.cpp +++ b/Grbl_Esp32/src/WebUI/TelnetServer.cpp @@ -103,17 +103,14 @@ namespace WebUI { size_t Telnet_Server::write(const uint8_t* buffer, size_t size) { size_t wsize = 0; if (!_setupdone || _telnetserver == NULL) { - log_d("[TELNET out blocked]"); return 0; } clearClients(); - //log_d("[TELNET out]"); //push UART data to all connected telnet clients for (uint8_t i = 0; i < MAX_TLNT_CLIENTS; i++) { if (_telnetClients[i] && _telnetClients[i].connected()) { - //log_d("[TELNET out connected]"); wsize = _telnetClients[i].write(buffer, size); COMMANDS::wait(0); } @@ -180,7 +177,6 @@ namespace WebUI { int Telnet_Server::get_rx_buffer_available() { return TELNETRXBUFFERSIZE - _RXbufferSize; } bool Telnet_Server::push(uint8_t data) { - log_i("[TELNET]push %c", data); if ((1 + _RXbufferSize) <= TELNETRXBUFFERSIZE) { int current = _RXbufferpos + _RXbufferSize; if (current > TELNETRXBUFFERSIZE) { @@ -191,7 +187,6 @@ namespace WebUI { } _RXbuffer[current] = data; _RXbufferSize++; - log_i("[TELNET]buffer size %d", _RXbufferSize); return true; } return false; @@ -225,7 +220,6 @@ namespace WebUI { int Telnet_Server::read(void) { if (_RXbufferSize > 0) { int v = _RXbuffer[_RXbufferpos]; - //log_d("[TELNET]read %c",char(v)); _RXbufferpos++; if (_RXbufferpos > (TELNETRXBUFFERSIZE - 1)) { _RXbufferpos = 0; diff --git a/Grbl_Esp32/src/WebUI/WebServer.cpp b/Grbl_Esp32/src/WebUI/WebServer.cpp index 1a3f2e99..215b5b69 100644 --- a/Grbl_Esp32/src/WebUI/WebServer.cpp +++ b/Grbl_Esp32/src/WebUI/WebServer.cpp @@ -21,44 +21,53 @@ #include "../Grbl.h" #if defined(ENABLE_WIFI) && defined(ENABLE_HTTP) +# ifdef NATIVE +# include "WebServer.h" +namespace WebUI { + Web_Server::Web_Server() {} + Web_Server::~Web_Server() {} + bool Web_Server::begin() { return false; } + void Web_Server::end() {} -# include "WifiServices.h" +} +# else -# include "ESPResponse.h" -# include "Serial2Socket.h" -# include "WebServer.h" -# include -# include -# include -# include -# ifdef ENABLE_SD_CARD -# include -# include "../SDCard.h" -# endif -# include -# include -# include -# include -# include -# ifdef ENABLE_MDNS -# include -# endif -# ifdef ENABLE_SSDP -# include -# endif -# ifdef ENABLE_CAPTIVE_PORTAL -# include +# include "WifiServices.h" + +# include "ESPResponse.h" +# include "Serial2Socket.h" +# include "WebServer.h" +# include +# include +# include +# include +# ifdef ENABLE_SD_CARD +# include +# include "../SDCard.h" +# endif +# include +# include +# include +# include +# ifdef ENABLE_MDNS +# include +# endif +# ifdef ENABLE_SSDP +# include +# endif +# ifdef ENABLE_CAPTIVE_PORTAL +# include +# endif namespace WebUI { const byte DNS_PORT = 53; DNSServer dnsServer; } -# endif -# include +# include //embedded response file if no files on SPIFFS -# include "NoFile.h" +# include "NoFile.h" namespace WebUI { //Default 404 @@ -91,11 +100,11 @@ namespace WebUI { UploadStatusType Web_Server::_upload_status = UploadStatusType::NONE; WebServer* Web_Server::_webserver = NULL; WebSocketsServer* Web_Server::_socket_server = NULL; -# ifdef ENABLE_AUTHENTICATION - AuthenticationIP* Web_Server::_head = NULL; - uint8_t Web_Server::_nb_ip = 0; - const int MAX_AUTH_IP = 10; -# endif +# ifdef ENABLE_AUTHENTICATION + AuthenticationIP* Web_Server::_head = NULL; + uint8_t Web_Server::_nb_ip = 0; + const int MAX_AUTH_IP = 10; +# endif Web_Server::Web_Server() {} Web_Server::~Web_Server() { end(); } @@ -111,13 +120,13 @@ namespace WebUI { //create instance _webserver = new WebServer(_port); -# ifdef ENABLE_AUTHENTICATION +# ifdef ENABLE_AUTHENTICATION //here the list of headers to be recorded const char* headerkeys[] = { "Cookie" }; size_t headerkeyssize = sizeof(headerkeys) / sizeof(char*); //ask server to track these headers _webserver->collectHeaders(headerkeys, headerkeyssize); -# endif +# endif _socket_server = new WebSocketsServer(_port + 1); _socket_server->begin(); _socket_server->onEvent(handle_Websocket_Event); @@ -155,13 +164,13 @@ namespace WebUI { //web update _webserver->on("/updatefw", HTTP_ANY, handleUpdate, WebUpdateUpload); -# ifdef ENABLE_SD_CARD +# ifdef ENABLE_SD_CARD //Direct SD management _webserver->on("/upload", HTTP_ANY, handle_direct_SDFileList, SDFile_direct_upload); //_webserver->on("/SD", HTTP_ANY, handle_SDCARD); -# endif +# endif -# ifdef ENABLE_CAPTIVE_PORTAL +# ifdef ENABLE_CAPTIVE_PORTAL if (WiFi.getMode() == WIFI_AP) { // if DNSServer is started with "*" for domain name, it will reply with // provided IP to all DNS request @@ -172,9 +181,9 @@ namespace WebUI { //do not forget the / at the end _webserver->on("/fwlink/", HTTP_ANY, handle_root); } -# endif +# endif -# ifdef ENABLE_SSDP +# ifdef ENABLE_SSDP //SSDP service presentation if (WiFi.getMode() == WIFI_STA) { _webserver->on("/description.xml", HTTP_GET, handle_SSDP); @@ -196,29 +205,29 @@ namespace WebUI { grbl_send(CLIENT_ALL, "[MSG:SSDP Started]\r\n"); SSDP.begin(); } -# endif +# endif grbl_send(CLIENT_ALL, "[MSG:HTTP Started]\r\n"); //start webserver _webserver->begin(); -# ifdef ENABLE_MDNS +# ifdef ENABLE_MDNS //add mDNS if (WiFi.getMode() == WIFI_STA) { MDNS.addService("http", "tcp", _port); } -# endif +# endif _setupdone = true; return no_error; } void Web_Server::end() { _setupdone = false; -# ifdef ENABLE_SSDP +# ifdef ENABLE_SSDP SSDP.end(); -# endif //ENABLE_SSDP -# ifdef ENABLE_MDNS +# endif //ENABLE_SSDP +# ifdef ENABLE_MDNS //remove mDNS mdns_service_remove("_http", "_tcp"); -# endif +# endif if (_socket_server) { delete _socket_server; _socket_server = NULL; @@ -229,14 +238,14 @@ namespace WebUI { _webserver = NULL; } -# ifdef ENABLE_AUTHENTICATION +# ifdef ENABLE_AUTHENTICATION while (_head) { AuthenticationIP* current = _head; _head = _head->_next; delete current; } _nb_ip = 0; -# endif +# endif } //Root of Webserver///////////////////////////////////////////////////// @@ -276,7 +285,7 @@ namespace WebUI { String contentType = getContentType(path); String pathWithGz = path + ".gz"; -# ifdef ENABLE_SD_CARD +# ifdef ENABLE_SD_CARD if ((path.substring(0, 4) == "/SD/")) { //remove /SD path = path.substring(3); @@ -328,7 +337,7 @@ namespace WebUI { _webserver->send(404, "text/plain", content); return; } else -# endif +# endif if (SPIFFS.exists(pathWithGz) || SPIFFS.exists(path)) { if (SPIFFS.exists(pathWithGz)) { path = pathWithGz; @@ -342,7 +351,7 @@ namespace WebUI { } if (page_not_found) { -# ifdef ENABLE_CAPTIVE_PORTAL +# ifdef ENABLE_CAPTIVE_PORTAL if (WiFi.getMode() == WIFI_AP) { String contentType = PAGE_CAPTIVE; String stmp = WiFi.softAPIP().toString(); @@ -361,7 +370,7 @@ namespace WebUI { //_webserver->client().stop(); return; } -# endif +# endif path = "/404.htm"; contentType = getContentType(path); pathWithGz = path + ".gz"; @@ -396,7 +405,7 @@ namespace WebUI { } } -# ifdef ENABLE_SSDP +# ifdef ENABLE_SSDP //http SSDP xml presentation void Web_Server::handle_SSDP() { StreamString sschema; @@ -437,7 +446,7 @@ namespace WebUI { _webserver->send(500); } } -# endif +# endif void Web_Server::_handle_web_command(bool silent) { //to save time if already disconnected @@ -484,16 +493,17 @@ namespace WebUI { } else { espresponse->flush(); } - if(espresponse) delete(espresponse); + if (espresponse) + delete (espresponse); } else { //execute GCODE if (auth_level == AuthenticationLevel::LEVEL_GUEST) { _webserver->send(401, "text/plain", "Authentication failed!\n"); return; } //Instead of send several commands one by one by web / send full set and split here - String scmd; - bool hasError =false; - uint8_t sindex = 0; + String scmd; + bool hasError = false; + uint8_t sindex = 0; // TODO Settings - this is very inefficient. get_Splited_Value() is O(n^2) // when it could easily be O(n). Also, it would be just as easy to push // the entire string into Serial2Socket and pull off lines from there. @@ -516,13 +526,13 @@ namespace WebUI { hasError = true; } } - _webserver->send(200, "text/plain", hasError?"Error":""); + _webserver->send(200, "text/plain", hasError ? "Error" : ""); } } //login status check void Web_Server::handle_login() { -# ifdef ENABLE_AUTHENTICATION +# ifdef ENABLE_AUTHENTICATION String smsg; String sUser, sPassword; String auths; @@ -685,10 +695,10 @@ namespace WebUI { buffer2send += "\"}"; _webserver->send(code, "application/json", buffer2send); } -# else +# else _webserver->sendHeader("Cache-Control", "no-cache"); _webserver->send(200, "application/json", "{\"status\":\"Ok\",\"authentication_lvl\":\"admin\"}"); -# endif +# endif } //SPIFFS //SPIFFS files list and file commands @@ -1172,7 +1182,7 @@ namespace WebUI { COMMANDS::wait(0); } -# ifdef ENABLE_SD_CARD +# ifdef ENABLE_SD_CARD //Function to delete not empty directory on SD card bool Web_Server::deleteRecursive(String path) { @@ -1224,9 +1234,9 @@ namespace WebUI { String path = "/"; String sstatus = "Ok"; if ((_upload_status == UploadStatusType::FAILED) || (_upload_status == UploadStatusType::FAILED)) { - sstatus = "Upload failed"; + sstatus = "Upload failed"; } - _upload_status = UploadStatusType::NONE; + _upload_status = UploadStatusType::NONE; bool list_files = true; uint64_t totalspace = 0; uint64_t usedspace = 0; @@ -1544,16 +1554,16 @@ namespace WebUI { } COMMANDS::wait(0); } -# endif +# endif void Web_Server::handle() { static uint32_t timeout = millis(); COMMANDS::wait(0); -# ifdef ENABLE_CAPTIVE_PORTAL +# ifdef ENABLE_CAPTIVE_PORTAL if (WiFi.getMode() == WIFI_AP) { dnsServer.processNextRequest(); } -# endif +# endif if (_webserver) { _webserver->handleClient(); } @@ -1663,7 +1673,7 @@ namespace WebUI { //check authentification AuthenticationLevel Web_Server::is_authenticated() { -# ifdef ENABLE_AUTHENTICATION +# ifdef ENABLE_AUTHENTICATION if (_webserver->hasHeader("Cookie")) { String cookie = _webserver->header("Cookie"); int pos = cookie.indexOf("ESPSESSIONID="); @@ -1676,12 +1686,12 @@ namespace WebUI { } } return AuthenticationLevel::LEVEL_GUEST; -# else +# else return AuthenticationLevel::LEVEL_ADMIN; -# endif +# endif } -# ifdef ENABLE_AUTHENTICATION +# ifdef ENABLE_AUTHENTICATION //add the information in the linked list if possible bool Web_Server::AddAuthIP(AuthenticationIP* item) { @@ -1799,6 +1809,7 @@ namespace WebUI { } return AuthenticationLevel::LEVEL_GUEST; } -# endif +# endif } +# endif #endif // Enable HTTP && ENABLE_WIFI diff --git a/Grbl_Esp32/src/WebUI/WebServer.h b/Grbl_Esp32/src/WebUI/WebServer.h index c4bd8745..44563710 100644 --- a/Grbl_Esp32/src/WebUI/WebServer.h +++ b/Grbl_Esp32/src/WebUI/WebServer.h @@ -55,6 +55,9 @@ namespace WebUI { ~Web_Server(); private: +#ifdef NATIVE + static uint16_t _port; +#else static bool _setupdone; static WebServer* _webserver; static long _id_connection; @@ -64,7 +67,7 @@ namespace WebUI { static String getContentType(String filename); static String get_Splited_Value(String data, char separator, int index); static AuthenticationLevel is_authenticated(); -#ifdef ENABLE_AUTHENTICATION +# ifdef ENABLE_AUTHENTICATION static AuthenticationIP* _head; static uint8_t _nb_ip; static bool AddAuthIP(AuthenticationIP* item); @@ -72,27 +75,28 @@ namespace WebUI { static bool ClearAuthIP(IPAddress ip, const char* sessionID); static AuthenticationIP* GetAuth(IPAddress ip, const char* sessionID); static AuthenticationLevel ResetAuthIP(IPAddress ip, const char* sessionID); -#endif -#ifdef ENABLE_SSDP - static void handle_SSDP(); -#endif - static void handle_root(); - static void handle_login(); - static void handle_not_found(); - static void _handle_web_command(bool); - static void handle_web_command() { _handle_web_command(false); } - static void handle_web_command_silent() { _handle_web_command(true); } - static void handle_Websocket_Event(uint8_t num, uint8_t type, uint8_t* payload, size_t length); - static void SPIFFSFileupload(); - static void handleFileList(); - static void handleUpdate(); - static void WebUpdateUpload(); - static void pushError(int code, const char* st, bool web_error = 500, uint16_t timeout = 1000); - static void cancelUpload(); -#ifdef ENABLE_SD_CARD - static void handle_direct_SDFileList(); - static void SDFile_direct_upload(); - static bool deleteRecursive(String path); +# endif +# ifdef ENABLE_SSDP + static void handle_SSDP(); +# endif + static void handle_root(); + static void handle_login(); + static void handle_not_found(); + static void _handle_web_command(bool); + static void handle_web_command() { _handle_web_command(false); } + static void handle_web_command_silent() { _handle_web_command(true); } + static void handle_Websocket_Event(uint8_t num, uint8_t type, uint8_t* payload, size_t length); + static void SPIFFSFileupload(); + static void handleFileList(); + static void handleUpdate(); + static void WebUpdateUpload(); + static void pushError(int code, const char* st, bool web_error = 500, uint16_t timeout = 1000); + static void cancelUpload(); +# ifdef ENABLE_SD_CARD + static void handle_direct_SDFileList(); + static void SDFile_direct_upload(); + static bool deleteRecursive(String path); +# endif #endif }; diff --git a/Grbl_Esp32/src/WebUI/WebSettings.cpp b/Grbl_Esp32/src/WebUI/WebSettings.cpp index df836fa0..a6b84a76 100644 --- a/Grbl_Esp32/src/WebUI/WebSettings.cpp +++ b/Grbl_Esp32/src/WebUI/WebSettings.cpp @@ -25,11 +25,9 @@ #ifdef ENABLE_WIFI # include #endif -#ifdef NATIVE -# include "../native.h" -#else -# include -# include +#include +#include +#ifndef NATIVE # include # include #endif @@ -250,8 +248,9 @@ namespace WebUI { #else webPrint("no"); #endif -#if defined(ENABLE_WIFI) -# if defined(ENABLE_HTTP) +#ifndef NATIVE +# if defined(ENABLE_WIFI) +# if defined(ENABLE_HTTP) webPrint(" # webcommunication: Sync: ", String(web_server.port() + 1)); webPrint(":"); switch (WiFi.getMode()) { @@ -268,11 +267,12 @@ namespace WebUI { webPrint("0.0.0.0"); break; } -# endif +# endif webPrint(" # hostname:", wifi_config.Hostname()); if (WiFi.getMode() == WIFI_AP) { webPrint("(AP mode)"); } +# endif #endif //to save time in decoding `?` webPrintln(" # axis:", String(number_axis->get())); @@ -298,6 +298,7 @@ namespace WebUI { } static Error runLocalFile(char* parameter, AuthenticationLevel auth_level) { // ESP700 +#ifndef NATIVE if (sys.state != State::Idle) { webPrintln("Busy"); return Error::IdleError; @@ -332,6 +333,9 @@ namespace WebUI { } currentfile.close(); return accumErr; +#else + Error::Ok; +#endif } static Error showLocalFile(char* parameter, AuthenticationLevel auth_level) { // ESP701 @@ -427,6 +431,7 @@ namespace WebUI { } static Error showSysStats(char* parameter, AuthenticationLevel auth_level) { // ESP420 +#ifndef NATIVE webPrintln("Chip ID: ", String((uint16_t)(ESP.getEfuseMac() >> 32))); webPrintln("CPU Frequency: ", String(ESP.getCpuFreqMHz()) + "Mhz"); webPrintln("CPU Temperature: ", String(temperatureRead(), 1) + "C"); @@ -434,13 +439,11 @@ namespace WebUI { webPrintln("SDK: ", ESP.getSdkVersion()); webPrintln("Flash Size: ", ESPResponseStream::formatBytes(ESP.getFlashChipSize())); -#ifndef NATIVE // Round baudRate to nearest 100 because ESP32 can say e.g. 115201 webPrintln("Baud rate: ", String((Serial.baudRate() / 100) * 100)); webPrintln("Sleep mode: ", WiFi.getSleep() ? "Modem" : "None"); -#endif -#ifdef ENABLE_WIFI +# ifdef ENABLE_WIFI int mode = WiFi.getMode(); if (mode != WIFI_MODE_NULL) { //Is OTA available ? @@ -454,12 +457,12 @@ namespace WebUI { webPrintln("Available Size for update: ", ESPResponseStream::formatBytes(flashsize)); webPrintln("Available Size for SPIFFS: ", ESPResponseStream::formatBytes(SPIFFS.totalBytes())); -# if defined(ENABLE_HTTP) +# if defined(ENABLE_HTTP) webPrintln("Web port: ", String(web_server.port())); -# endif -# if defined(ENABLE_TELNET) +# endif +# if defined(ENABLE_TELNET) webPrintln("Data port: ", String(telnet_server.port())); -# endif +# endif webPrintln("Hostname: ", wifi_config.Hostname()); } @@ -568,8 +571,8 @@ namespace WebUI { webPrintln("Off"); break; } -#endif // ENABLE_WIFI -#ifdef ENABLE_BLUETOOTH +# endif // ENABLE_WIFI +# ifdef ENABLE_BLUETOOTH webPrint("Current BT Mode: "); if (bt_config.Is_BT_on()) { webPrintln("On"); @@ -589,8 +592,8 @@ namespace WebUI { } else { webPrintln("Off"); } -#endif -#ifdef ENABLE_NOTIFICATIONS +# endif +# ifdef ENABLE_NOTIFICATIONS webPrint("Notifications: "); webPrint(notificationsservice.started() ? "Enabled" : "Disabled"); if (notificationsservice.started()) { @@ -599,18 +602,20 @@ namespace WebUI { webPrint(")"); } webPrintln(""); -#endif +# endif webPrint("FW version: "); webPrint(GRBL_VERSION); webPrint(" ("); webPrint(GRBL_VERSION_BUILD); webPrint(") (ESP32)"); webPrintln(""); +#endif return Error::Ok; } #ifdef ENABLE_WIFI static Error listAPs(char* parameter, AuthenticationLevel auth_level) { // ESP410 +# ifndef NATIVE JSONencoder j(espresponse->client() != CLIENT_WEBUI); j.begin(); j.begin_array("AP_LIST"); @@ -646,6 +651,7 @@ namespace WebUI { if (espresponse->client() != CLIENT_WEBUI) { espresponse->println(""); } +# endif return Error::Ok; } #endif @@ -792,6 +798,7 @@ namespace WebUI { } static Error listSDFiles(char* parameter, AuthenticationLevel auth_level) { // ESP210 +# ifndef NATIVE SDState state = get_sd_state(true); if (state != SDState::Idle) { if (state == SDState::NotPresent) { @@ -810,6 +817,7 @@ namespace WebUI { ssd += "]"; webPrintln(ssd); SD.end(); +# endif return Error::Ok; } #endif @@ -908,20 +916,21 @@ namespace WebUI { } static Error setRadioState(char* parameter, AuthenticationLevel auth_level) { // ESP115 +#ifndef NATIVE parameter = trim(parameter); if (*parameter == '\0') { // Display the radio state bool on = false; -#if defined(ENABLE_WIFI) +# if defined(ENABLE_WIFI) if (WiFi.getMode() != WIFI_MODE_NULL) { on = true; } -#endif -#if defined(ENABLE_BLUETOOTH) +# endif +# if defined(ENABLE_BLUETOOTH) if (bt_config.Is_BT_on()) { on = true; } -#endif +# endif webPrintln(on ? "ON" : "OFF"); return Error::Ok; } @@ -937,53 +946,56 @@ namespace WebUI { } //Stop everything -#if defined(ENABLE_WIFI) +# if defined(ENABLE_WIFI) if (WiFi.getMode() != WIFI_MODE_NULL) { wifi_config.StopWiFi(); } -#endif -#if defined(ENABLE_BLUETOOTH) +# endif +# if defined(ENABLE_BLUETOOTH) if (bt_config.Is_BT_on()) { bt_config.end(); } -#endif +# endif //if On start proper service if (!on) { webPrintln("[MSG: Radio is Off]"); return Error::Ok; } //On -#ifdef WIFI_OR_BLUETOOTH +# ifdef WIFI_OR_BLUETOOTH switch (wifi_radio_mode->get()) { case ESP_WIFI_AP: case ESP_WIFI_STA: -# if !defined(ENABLE_WIFI) +# if !defined(ENABLE_WIFI) webPrintln("WiFi is not enabled!"); return Error::WifiFailBegin; -# else +# else wifi_config.begin(); return Error::Ok; -# endif +# endif case ESP_BT: -# if !defined(ENABLE_BLUETOOTH) +# if !defined(ENABLE_BLUETOOTH) webPrintln("Bluetooth is not enabled!"); return Error::BtFailBegin; -# else +# else bt_config.begin(); return Error::Ok; -# endif +# endif default: webPrintln("[MSG: Radio is Off]"); return Error::Ok; } +# endif #endif return Error::Ok; } #ifdef ENABLE_WIFI static Error showIP(char* parameter, AuthenticationLevel auth_level) { // ESP111 +# ifndef NATIVE webPrintln(parameter, WiFi.getMode() == WIFI_STA ? WiFi.localIP() : WiFi.softAPIP()); +# endif return Error::Ok; } diff --git a/Grbl_Esp32/src/WebUI/WifiConfig.cpp b/Grbl_Esp32/src/WebUI/WifiConfig.cpp index f3ca256c..f089b6a8 100644 --- a/Grbl_Esp32/src/WebUI/WifiConfig.cpp +++ b/Grbl_Esp32/src/WebUI/WifiConfig.cpp @@ -23,9 +23,11 @@ #ifdef ENABLE_WIFI # include -# include -# include -# include +# ifndef NATIVE +# include +# include +# include +# endif # include # include # include "WifiServices.h" @@ -52,6 +54,7 @@ namespace WebUI { String tmp; result = "[MSG:"; +# ifndef NATIVE if ((WiFi.getMode() == WIFI_MODE_STA) || (WiFi.getMode() == WIFI_MODE_APSTA)) { result += "Mode=STA:SSID="; result += WiFi.SSID(); @@ -83,6 +86,7 @@ namespace WebUI { if (WiFi.getMode() == WIFI_MODE_NULL) { result += "No Wifi"; } +# endif result += "]\r\n"; return result.c_str(); } @@ -208,6 +212,7 @@ namespace WebUI { */ void WiFiConfig::WiFiEvent(WiFiEvent_t event) { +# ifndef NATIVE switch (event) { case SYSTEM_EVENT_STA_GOT_IP: grbl_sendf(CLIENT_ALL, "[MSG:Connected with %s]\r\n", WiFi.localIP().toString().c_str()); @@ -218,6 +223,7 @@ namespace WebUI { default: break; } +# endif } /* @@ -238,6 +244,9 @@ namespace WebUI { */ bool WiFiConfig::ConnectSTA2AP() { +# ifdef NATIVE + return false; +# else String msg, msg_out; uint8_t count = 0; uint8_t dot = 0; @@ -268,6 +277,7 @@ namespace WebUI { status = WiFi.status(); } return status == WL_CONNECTED; +# endif } /* @@ -275,6 +285,9 @@ namespace WebUI { */ bool WiFiConfig::StartSTA() { +# ifdef NATIVE + return false; +# else //stop active service wifi_services.end(); //Sanity check @@ -313,6 +326,7 @@ namespace WebUI { grbl_send(CLIENT_ALL, "[MSG:Starting client failed]\r\n"); return false; } +# endif } /** @@ -320,6 +334,9 @@ namespace WebUI { */ bool WiFiConfig::StartAP() { +# ifdef NATIVE + return false; +# else //stop active services wifi_services.end(); //Sanity check @@ -359,6 +376,7 @@ namespace WebUI { grbl_send(CLIENT_ALL, "[MSG:Starting AP failed]\r\n"); return false; } +# endif } /** @@ -366,6 +384,7 @@ namespace WebUI { */ void WiFiConfig::StopWiFi() { +# ifndef NATIVE //Sanity check if ((WiFi.getMode() == WIFI_STA) || (WiFi.getMode() == WIFI_AP_STA)) { WiFi.disconnect(true); @@ -378,12 +397,14 @@ namespace WebUI { WiFi.enableAP(false); WiFi.mode(WIFI_OFF); grbl_send(CLIENT_ALL, "\n[MSG:WiFi Off]\r\n"); +# endif } /** * begin WiFi setup */ void WiFiConfig::begin() { +# ifndef NATIVE //stop active services wifi_services.end(); //setup events @@ -409,6 +430,7 @@ namespace WebUI { } else { WiFi.mode(WIFI_OFF); } +# endif } /** @@ -432,8 +454,13 @@ namespace WebUI { } grbl_send(CLIENT_ALL, "[MSG:WiFi reset done]\r\n"); } - bool WiFiConfig::Is_WiFi_on() { return !(WiFi.getMode() == WIFI_MODE_NULL); } - + bool WiFiConfig::Is_WiFi_on() { +# ifdef NATIVE + return false; +# else + return !(WiFi.getMode() == WIFI_MODE_NULL); +# endif + } /** * Handle not critical actions that must be done in sync environement */ diff --git a/Grbl_Esp32/src/WebUI/WifiServices.cpp b/Grbl_Esp32/src/WebUI/WifiServices.cpp index d9c01fa1..2e117682 100644 --- a/Grbl_Esp32/src/WebUI/WifiServices.cpp +++ b/Grbl_Esp32/src/WebUI/WifiServices.cpp @@ -26,22 +26,32 @@ # include # include # include "WifiServices.h" -# ifdef ENABLE_MDNS -# include -# endif -# ifdef ENABLE_OTA -# include -# endif -# ifdef ENABLE_HTTP -# include "WebServer.h" -# endif -# ifdef ENABLE_TELNET -# include "TelnetServer.h" -# endif -# ifdef ENABLE_NOTIFICATIONS -# include "NotificationsService.h" -# endif -# include "Commands.h" +# ifdef NATIVE +using namespace WebUI; +WiFiServices::WiFiServices() {} +WiFiServices::~WiFiServices() {} +bool WiFiServices::begin() { + return false; +} +void WiFiServices::end() {} +void WiFiServices::handle() {} +# else +# ifdef ENABLE_MDNS +# include +# endif +# ifdef ENABLE_OTA +# include +# endif +# ifdef ENABLE_HTTP +# include "WebServer.h" +# endif +# ifdef ENABLE_TELNET +# include "TelnetServer.h" +# endif +# ifdef ENABLE_NOTIFICATIONS +# include "NotificationsService.h" +# endif +# include "Commands.h" namespace WebUI { WiFiServices wifi_services; @@ -59,7 +69,7 @@ namespace WebUI { //Start SPIFFS SPIFFS.begin(true); -# ifdef ENABLE_OTA +# ifdef ENABLE_OTA ArduinoOTA .onStart([]() { String type; @@ -97,8 +107,8 @@ namespace WebUI { } }); ArduinoOTA.begin(); -# endif -# ifdef ENABLE_MDNS +# endif +# ifdef ENABLE_MDNS //no need in AP mode if (WiFi.getMode() == WIFI_STA) { //start mDns @@ -109,40 +119,40 @@ namespace WebUI { grbl_sendf(CLIENT_ALL, "[MSG:Start mDNS with hostname:http://%s.local/]\r\n", h.c_str()); } } -# endif -# ifdef ENABLE_HTTP +# endif +# ifdef ENABLE_HTTP web_server.begin(); -# endif -# ifdef ENABLE_TELNET +# endif +# ifdef ENABLE_TELNET telnet_server.begin(); -# endif -# ifdef ENABLE_NOTIFICATIONS +# endif +# ifdef ENABLE_NOTIFICATIONS notificationsservice.begin(); -# endif +# endif //be sure we are not is mixed mode in setup WiFi.scanNetworks(true); return no_error; } void WiFiServices::end() { -# ifdef ENABLE_NOTIFICATIONS +# ifdef ENABLE_NOTIFICATIONS notificationsservice.end(); -# endif -# ifdef ENABLE_TELNET +# endif +# ifdef ENABLE_TELNET telnet_server.end(); -# endif -# ifdef ENABLE_HTTP +# endif +# ifdef ENABLE_HTTP web_server.end(); -# endif +# endif //stop OTA -# ifdef ENABLE_OTA +# ifdef ENABLE_OTA ArduinoOTA.end(); -# endif +# endif //Stop SPIFFS SPIFFS.end(); -# ifdef ENABLE_MDNS +# ifdef ENABLE_MDNS //Stop mDNS MDNS.end(); -# endif +# endif } void WiFiServices::handle() { @@ -156,15 +166,16 @@ namespace WebUI { WiFi.enableSTA(false); } } -# ifdef ENABLE_OTA +# ifdef ENABLE_OTA ArduinoOTA.handle(); -# endif -# ifdef ENABLE_HTTP +# endif +# ifdef ENABLE_HTTP web_server.handle(); -# endif -# ifdef ENABLE_TELNET +# endif +# ifdef ENABLE_TELNET telnet_server.handle(); -# endif +# endif } } -#endif // ENABLE_WIFI +# endif // ENABLE_WIFI +#endif diff --git a/Grbl_Esp32/src/native.h b/Grbl_Esp32/src/native.h deleted file mode 100644 index 6cb3b5eb..00000000 --- a/Grbl_Esp32/src/native.h +++ /dev/null @@ -1,309 +0,0 @@ -#pragma once -#include -#include "binary.h" - -// The native compiler might not support __attribute__ ((weak)) -#define WEAK_FUNC - -inline int temperatureRead() { - return 27; -} - -inline long long esp_timer_get_time() { - return 0LL; -} - -typedef int esp_err_t; - -extern "C" { -inline esp_err_t esp_task_wdt_reset() {} -} - -const esp_err_t ESP_ERR_NVS_INVALID_HANDLE = 3; -const esp_err_t ESP_ERR_NVS_INVALID_NAME = 2; -const esp_err_t ESP_ERR_NVS_INVALID_LENGTH = 1; -const esp_err_t ESP_OK = 0; -inline uint32_t getApbFrequency() { - return 80000000; -} - -// Timer stuff used by Stepper.cpp -typedef struct { - int divider; - int counter_dir; - int counter_en; - int alarm_en; - int intr_type; - bool auto_reload; -} timer_config_t; - -const int TIMER_COUNT_UP = 0; -const int TIMER_PAUSE = 0; -const int TIMER_ALARM_EN = 0; -const int TIMER_INTR_LEVEL = 0; -typedef int timer_group_t; -typedef int timer_idx_t; -const timer_group_t TIMER_GROUP_0 = 0; -const timer_idx_t TIMER_0 = 0; - -inline void timer_set_counter_value(timer_group_t group, timer_idx_t idx, uint64_t ticks) {} -inline void timer_set_alarm_value(timer_group_t group, timer_idx_t idx, uint64_t ticks) {} -inline void timer_init(timer_group_t group, timer_idx_t idx, timer_config_t* conf) {} -inline void timer_enable_intr(timer_group_t group, timer_idx_t idx) {} -inline void timer_start(timer_group_t group, timer_idx_t idx) {} -inline void timer_pause(timer_group_t group, timer_idx_t idx) {} -inline void timer_isr_register(timer_group_t group, timer_idx_t idx, void (*handler)(void*), void* arg, int a, void* arg1) {} - -inline void gpio_reset_pin(uint8_t pin) {} -inline int digitalPinToInterrupt(uint8_t pin) {} - -#define IRAM_ATTR - -// Most of the EspClass stuff is used by information reports, -// except for restart() -class EspClass { -public: - const char* getSdkVersion() { return "native"; } - inline void restart() {} - inline uint64_t getEfuseMac() { return 0ULL; } - inline uint32_t getCpuFreqMHz() { return 240000000; } - inline uint32_t getFreeHeap() { return 30000; } - inline uint32_t getFlashChipSize() { return 0x400000; } -}; -extern EspClass ESP; - -typedef int uart_port_t; - -#define NO_TASKS - -// Stub versions of FreeRTOS functions -typedef int TickType_t; - -const int portMUX_INITIALIZER_UNLOCKED = 0; -const int portMAX_DELAY = 0; -const int portTICK_PERIOD_MS = 1; -const int portTICK_RATE_MS = 1; -typedef int portMUX_TYPE; -typedef int BaseType_t; -typedef unsigned int UBaseType_t; -typedef void* xQueueHandle; -typedef void* QueueHandle_t; -typedef void* TaskHandle_t; -typedef int TickType_t; -const BaseType_t pdTRUE = 1; -const BaseType_t pdFALSE = 0; -const BaseType_t pdPASS = 0; -inline void vTaskDelay(TickType_t ticks) {} -inline BaseType_t xQueueReceive(QueueHandle_t xQueue, void* pvBuffer, TickType_t xTicksToWait) {} -inline xQueueHandle xQueueCreate(int n, int len) {} -inline BaseType_t xQueueReset(QueueHandle_t queue) { - return pdPASS; -} -inline BaseType_t xQueueSend(QueueHandle_t queue, void* item, TickType_t ticks) { - return pdTRUE; -} -inline BaseType_t xQueueSendFromISR(QueueHandle_t queue, void* item, void* p) { - return pdTRUE; -} -inline TaskHandle_t xTaskCreate(void (*task)(void*), const char* name, int stacksize, void* arg0, int pri, TaskHandle_t* th) {} -inline TaskHandle_t xTaskCreatePinnedToCore( - void (*task)(void*), const char* name, int stacksize, void* arg0, int pri, TaskHandle_t* th, int core) {} -inline int xTaskGetTickCount() { - return 0; -} -inline void vTaskDelayUntil(TickType_t* pxPreviousWakeTime, const TickType_t xTimeIncrement) {} -inline int xPortGetFreeHeapSize() { - return 0; -} -inline void vTaskEnterCritical(int* mutex) {} -inline void vTaskExitCritical(int* mutex) {} - -inline void attachInterrupt(int pin, void (*isr_limit_switches)(), int change) {} -inline void detachInterrupt(int pin) {} - -inline void NOP() {} - -// dacWrite stub - used by DacSpindle -inline void dacWrite(int _output_pin, uint8_t duty) {} - -inline void ledcSetup(int channel_num, double freq, int bits) {} -inline void ledcWrite(int channel_num, int duty) {} -inline void ledcAttachPin(int pwm_pin, int channel_num) {} - -// NVS used by Settings -const int NVS_READWRITE = 0; -inline esp_err_t nvs_open(const char* s, int mode, int* handle) { - return 0; -} -inline esp_err_t nvs_get_i32(int _handle, const char* _keyName, int* value) { - return -1; -} -inline esp_err_t nvs_set_i32(int _handle, const char* _keyName, int value) { - return -1; -} -inline esp_err_t nvs_get_i8(int _handle, const char* _keyName, signed char* value) { - return -1; -} -inline esp_err_t nvs_set_i8(int _handle, const char* _keyName, int value) { - return -1; -} -inline esp_err_t nvs_get_str(int _handle, const char* _keyName, void* p, unsigned int* len) { - return -1; -} -inline esp_err_t nvs_set_str(int _handle, const char* _keyName, const char* value) { - return -1; -} -inline void nvs_erase_key(int _handle, const char* key) {} -inline esp_err_t nvs_get_blob(int handle, const char* _keyName, void* currentValue, size_t* len) { - return -1; -} -inline esp_err_t nvs_set_blob(int handle, const char* _keyName, void* currentValue, size_t len) { - return -1; -} - -// Unlike the ESP32 Arduino framework, EpoxyDuino does not have contrain() and map() - -// Templates don't work because of float/double promotion -#define constrain(amt, low, high) ((amt) < (low) ? (low) : ((amt) > (high) ? (high) : (amt))) - -inline long map(long x, long in_min, long in_max, long out_min, long out_max) { - const long dividend = out_max - out_min; - const long divisor = in_max - in_min; - const long delta = x - in_min; - if (divisor == 0) { - return -1; //AVR returns -1, SAM returns 0 - } - return (delta * dividend + (divisor / 2)) / divisor + out_min; -} - -// The IPAddress implementation in EpoxyDuino does not have toString() -// so we include the implementation from the esp32 arduino framework -#include -#include -#include -class IPAddress : public Printable { -private: - union { - uint8_t bytes[4]; // IPv4 address - uint32_t dword; - } _address; - - // Access the raw byte array containing the address. Because this returns a pointer - // to the internal structure rather than a copy of the address this function should only - // be used when you know that the usage of the returned uint8_t* will be transient and not - // stored. - uint8_t* raw_address() { return _address.bytes; } - -public: - // Constructors - IPAddress(); - IPAddress(uint8_t first_octet, uint8_t second_octet, uint8_t third_octet, uint8_t fourth_octet); - IPAddress(uint32_t address); - IPAddress(const uint8_t* address); - virtual ~IPAddress() {} - - bool fromString(const char* address); - bool fromString(const String& address) { return fromString(address.c_str()); } - - // Overloaded cast operator to allow IPAddress objects to be used where a pointer - // to a four-byte uint8_t array is expected - operator uint32_t() const { return _address.dword; } - bool operator==(const IPAddress& addr) const { return _address.dword == addr._address.dword; } - bool operator==(const uint8_t* addr) const; - - // Overloaded index operator to allow getting and setting individual octets of the address - uint8_t operator[](int index) const { return _address.bytes[index]; } - uint8_t& operator[](int index) { return _address.bytes[index]; } - - // Overloaded copy operators to allow initialisation of IPAddress objects from other types - IPAddress& operator=(const uint8_t* address); - IPAddress& operator=(uint32_t address); - - virtual size_t printTo(Print& p) const; - String toString() const; - -#if 0 - friend class EthernetClass; - friend class UDP; - friend class Client; - friend class Server; - friend class DhcpClass; - friend class DNSClient; -#endif -}; - -#define FILE_READ "r" - -namespace fs { - enum SeekMode { SeekSet = 0, SeekCur = 1, SeekEnd = 2 }; - - class File : public Stream { - public: - File() {} - - size_t write(uint8_t) override; - size_t write(const uint8_t* buf, size_t size) override; - int available() override; - int read() override; - int peek() override; - void flush() override; - size_t read(uint8_t* buf, size_t size); - size_t readBytes(char* buffer, size_t length) { return read((uint8_t*)buffer, length); } - - bool seek(uint32_t pos, SeekMode mode); - bool seek(uint32_t pos) { return seek(pos, SeekSet); } - size_t position() const; - size_t size() const; - void close(); - operator bool() const; - time_t getLastWrite(); - const char* name() const; - - bool isDirectory(void); - File openNextFile(const char* mode = FILE_READ); - void rewindDirectory(void); - }; - class FS { - public: - File open(const char* path, const char* mode = FILE_READ); - File open(const String& path, const char* mode = FILE_READ); - - bool exists(const char* path); - bool exists(const String& path); - - bool remove(const char* path); - bool remove(const String& path); - - bool rename(const char* pathFrom, const char* pathTo); - bool rename(const String& pathFrom, const String& pathTo); - - bool mkdir(const char* path); - bool mkdir(const String& path); - - bool rmdir(const char* path); - bool rmdir(const String& path); - }; - - class SPIFFSFS : public fs::FS { - public: - SPIFFSFS(); - ~SPIFFSFS(); - bool begin(bool formatOnFail = false, const char* basePath = "/spiffs", uint8_t maxOpenFiles = 10, const char* partitionLabel = NULL); - bool format(); - size_t totalBytes(); - size_t usedBytes(); - void end(); - }; -} -using fs::File; -using fs::FS; -using fs::SeekCur; -using fs::SeekEnd; -using fs::SeekMode; -using fs::SeekSet; - -extern fs::SPIFFSFS SPIFFS; - -#define UART_NUM_1 1 - -#define M_PI 3.1415926536 diff --git a/libraries/NativeStubs/BluetoothSerial.cpp b/libraries/NativeStubs/BluetoothSerial.cpp new file mode 100644 index 00000000..d9ada9e1 --- /dev/null +++ b/libraries/NativeStubs/BluetoothSerial.cpp @@ -0,0 +1 @@ +#include "BluetoothSerial.h" diff --git a/libraries/NativeStubs/BluetoothSerial.h b/libraries/NativeStubs/BluetoothSerial.h new file mode 100644 index 00000000..38c87def --- /dev/null +++ b/libraries/NativeStubs/BluetoothSerial.h @@ -0,0 +1,32 @@ +#pragma once +#include "Arduino.h" +#include "Stream.h" + +inline bool btStarted() { + return false; +} + +typedef int esp_spp_cb_event_t; + +typedef struct { + struct { + uint8_t* rem_bda; + } srv_open; +} esp_spp_cb_param_t; +enum { ESP_SPP_SRV_OPEN_EVT, ESP_SPP_SRV_CLOSE_EVT, ESP_SPP_CLOSE_EVT }; + +class BluetoothSerial : public Stream { +public: + BluetoothSerial(void) = default; + ~BluetoothSerial(void) = default; + + bool begin(String localName = String(), bool isMaster = false) {} + void register_callback(void (*callback)(esp_spp_cb_event_t event, esp_spp_cb_param_t* param)) {} + bool hasClient(void) { return false; } + void end(void) {} + + size_t write(uint8_t) override { return 0; } + int available() override { return 0; } + int read() override { return -1; } + int peek() override { return -1; } +}; diff --git a/libraries/NativeStubs/ESP.cpp b/libraries/NativeStubs/ESP.cpp new file mode 100644 index 00000000..a7d60aa1 --- /dev/null +++ b/libraries/NativeStubs/ESP.cpp @@ -0,0 +1,3 @@ +#include "ESP.h" + +EspClass ESP; diff --git a/libraries/NativeStubs/ESP.h b/libraries/NativeStubs/ESP.h new file mode 100644 index 00000000..685504e7 --- /dev/null +++ b/libraries/NativeStubs/ESP.h @@ -0,0 +1,49 @@ +#pragma once + +#include + +inline int temperatureRead() { + return 27; +} + +inline long long esp_timer_get_time() { + return 0LL; +} + +typedef int esp_err_t; + +const esp_err_t ESP_OK = 0; + +inline uint32_t getApbFrequency() { + return 80000000; +} + +inline void gpio_reset_pin(uint8_t pin) {} +inline int digitalPinToInterrupt(uint8_t pin) {} + +// Most of the EspClass stuff is used by information reports, +// except for restart() +class EspClass { +public: + const char* getSdkVersion() { return "native"; } + inline void restart() {} + inline uint64_t getEfuseMac() { return 0ULL; } + inline uint32_t getCpuFreqMHz() { return 240000000; } + inline uint32_t getFreeHeap() { return 30000; } + inline uint32_t getFlashChipSize() { return 0x400000; } +}; +extern EspClass ESP; + +#define NO_TASKS + +inline void attachInterrupt(int pin, void (*isr_limit_switches)(), int change) {} +inline void detachInterrupt(int pin) {} + +inline void NOP() {} + +// dacWrite stub - used by DacSpindle +inline void dacWrite(int _output_pin, uint8_t duty) {} + +inline void ledcSetup(int channel_num, double freq, int bits) {} +inline void ledcWrite(int channel_num, int duty) {} +inline void ledcAttachPin(int pwm_pin, int channel_num) {} diff --git a/Grbl_Esp32/src/Native.cpp b/libraries/NativeStubs/FS.cpp similarity index 61% rename from Grbl_Esp32/src/Native.cpp rename to libraries/NativeStubs/FS.cpp index ca093117..17f878f9 100644 --- a/Grbl_Esp32/src/Native.cpp +++ b/libraries/NativeStubs/FS.cpp @@ -1,14 +1,4 @@ -#ifdef NATIVE -# include "native.h" -# include - -EspClass ESP; - -String IPAddress::toString() const { - char szRet[16]; - sprintf(szRet, "%u.%u.%u.%u", _address.bytes[0], _address.bytes[1], _address.bytes[2], _address.bytes[3]); - return String(szRet); -} +#include using namespace fs; @@ -122,52 +112,10 @@ bool FS::rmdir(const String& path) { return rmdir(path.c_str()); } -# if 0 +#if 0 void FSImpl::mountpoint(const char* mp) {} const char* FSImpl::mountpoint() { return ""; } -# endif - -SPIFFSFS::SPIFFSFS() {} - -SPIFFSFS::~SPIFFSFS() {} - -bool SPIFFSFS::begin(bool formatOnFail, const char* basePath, uint8_t maxOpenFiles, const char* partitionLabel) { - return true; -} - -void SPIFFSFS::end() {} - -bool SPIFFSFS::format() { - return true; -} - -size_t SPIFFSFS::totalBytes() { - return 0; -} - -size_t SPIFFSFS::usedBytes() { - return 0; -} - -fs::SPIFFSFS SPIFFS; - -# include - -// Implementation of weak functions in libraries -// The compiler use for native compilation doesn't support -// __attribute__ ((weak)) -void TMC2130Stepper::switchCSpin(bool state) { - // digitalWrite(_pinCS, state); -} -void TMC2130Stepper::setSPISpeed(uint32_t speed) { - // spi_speed = speed; -} -void TMC2130Stepper::write(uint8_t addressByte, uint32_t config) {} - -uint32_t TMC2130Stepper::read(uint8_t addressByte) { - return 0UL; -} #endif diff --git a/libraries/NativeStubs/FS.h b/libraries/NativeStubs/FS.h new file mode 100644 index 00000000..3977831d --- /dev/null +++ b/libraries/NativeStubs/FS.h @@ -0,0 +1,63 @@ +#pragma once + +#include + +#define FILE_READ "r" + +namespace fs { + enum SeekMode { SeekSet = 0, SeekCur = 1, SeekEnd = 2 }; + + class File : public Stream { + public: + File() {} + + size_t write(uint8_t) override; + size_t write(const uint8_t* buf, size_t size) override; + int available() override; + int read() override; + int peek() override; + void flush() override; + size_t read(uint8_t* buf, size_t size); + size_t readBytes(char* buffer, size_t length) { return read((uint8_t*)buffer, length); } + + bool seek(uint32_t pos, SeekMode mode); + bool seek(uint32_t pos) { return seek(pos, SeekSet); } + size_t position() const; + size_t size() const; + void close(); + operator bool() const; + time_t getLastWrite(); + const char* name() const; + + bool isDirectory(void); + File openNextFile(const char* mode = FILE_READ); + void rewindDirectory(void); + }; + class FS { + public: + File open(const char* path, const char* mode = FILE_READ); + File open(const String& path, const char* mode = FILE_READ); + + bool exists(const char* path); + bool exists(const String& path); + + bool remove(const char* path); + bool remove(const String& path); + + bool rename(const char* pathFrom, const char* pathTo); + bool rename(const String& pathFrom, const String& pathTo); + + bool mkdir(const char* path); + bool mkdir(const String& path); + + bool rmdir(const char* path); + bool rmdir(const String& path); + }; +} + +using fs::File; +using fs::FS; +using fs::SeekCur; +using fs::SeekEnd; +using fs::SeekMode; +using fs::SeekSet; diff --git a/libraries/NativeStubs/IPAddress.cpp b/libraries/NativeStubs/IPAddress.cpp new file mode 100644 index 00000000..c1f8a8cc --- /dev/null +++ b/libraries/NativeStubs/IPAddress.cpp @@ -0,0 +1,7 @@ +#include + +String IPAddress::toString() const { + char szRet[16]; + sprintf(szRet, "%u.%u.%u.%u", _address.bytes[0], _address.bytes[1], _address.bytes[2], _address.bytes[3]); + return String(szRet); +} diff --git a/libraries/NativeStubs/IPAddress.h b/libraries/NativeStubs/IPAddress.h new file mode 100644 index 00000000..bdde949f --- /dev/null +++ b/libraries/NativeStubs/IPAddress.h @@ -0,0 +1,57 @@ +#pragma once +#include +#include +#include +#include +// The IPAddress implementation in EpoxyDuino does not have toString() +// so we include the implementation from the esp32 arduino framework +class IPAddress : public Printable { +private: + union { + uint8_t bytes[4]; // IPv4 address + uint32_t dword; + } _address; + + // Access the raw byte array containing the address. Because this returns a pointer + // to the internal structure rather than a copy of the address this function should only + // be used when you know that the usage of the returned uint8_t* will be transient and not + // stored. + uint8_t* raw_address() { return _address.bytes; } + +public: + // Constructors + IPAddress(); + IPAddress(uint8_t first_octet, uint8_t second_octet, uint8_t third_octet, uint8_t fourth_octet); + IPAddress(uint32_t address); + IPAddress(const uint8_t* address); + virtual ~IPAddress() {} + + bool fromString(const char* address); + bool fromString(const String& address) { return fromString(address.c_str()); } + + // Overloaded cast operator to allow IPAddress objects to be used where a pointer + // to a four-byte uint8_t array is expected + operator uint32_t() const { return _address.dword; } + bool operator==(const IPAddress& addr) const { return _address.dword == addr._address.dword; } + bool operator==(const uint8_t* addr) const; + + // Overloaded index operator to allow getting and setting individual octets of the address + uint8_t operator[](int index) const { return _address.bytes[index]; } + uint8_t& operator[](int index) { return _address.bytes[index]; } + + // Overloaded copy operators to allow initialisation of IPAddress objects from other types + IPAddress& operator=(const uint8_t* address); + IPAddress& operator=(uint32_t address); + + virtual size_t printTo(Print& p) const; + String toString() const; + +#if 0 + friend class EthernetClass; + friend class UDP; + friend class Client; + friend class Server; + friend class DhcpClass; + friend class DNSClient; +#endif +}; diff --git a/libraries/NativeStubs/SD.cpp b/libraries/NativeStubs/SD.cpp new file mode 100644 index 00000000..aa8cfcd1 --- /dev/null +++ b/libraries/NativeStubs/SD.cpp @@ -0,0 +1,3 @@ +#include + +SDFS SD; diff --git a/libraries/NativeStubs/SD.h b/libraries/NativeStubs/SD.h new file mode 100644 index 00000000..ef6d5432 --- /dev/null +++ b/libraries/NativeStubs/SD.h @@ -0,0 +1,12 @@ +#pragma once +#include +#include + +class SDFS : public FS { +public: + bool begin(uint8_t sspin, SPIClass& spi, int freq, const char* mountpoint, int n_files) { return false; }; + void end() {} + size_t cardSize() { return 0; } +}; + +extern SDFS SD; diff --git a/libraries/NativeStubs/SPIFFS.cpp b/libraries/NativeStubs/SPIFFS.cpp new file mode 100644 index 00000000..54f858d0 --- /dev/null +++ b/libraries/NativeStubs/SPIFFS.cpp @@ -0,0 +1,3 @@ +#include + +fs::SPIFFSFS SPIFFS; diff --git a/libraries/NativeStubs/SPIFFS.h b/libraries/NativeStubs/SPIFFS.h new file mode 100644 index 00000000..53f84738 --- /dev/null +++ b/libraries/NativeStubs/SPIFFS.h @@ -0,0 +1,24 @@ +#pragma once + +#include + +namespace fs { + class SPIFFSFS : public FS { + public: + SPIFFSFS() = default; + + ~SPIFFSFS() = default; + + bool begin(bool formatOnFail, const char* basePath, uint8_t maxOpenFiles, const char* partitionLabel) { return true; } + + void end() {} + + bool format() { return true; } + + size_t totalBytes() { return 0; } + + size_t usedBytes() { return 0; } + }; +} + +extern fs::SPIFFSFS SPIFFS; diff --git a/libraries/NativeStubs/TMCStepper.cpp b/libraries/NativeStubs/TMCStepper.cpp new file mode 100644 index 00000000..9ae9a658 --- /dev/null +++ b/libraries/NativeStubs/TMCStepper.cpp @@ -0,0 +1,16 @@ +#include + +// Implementation of weak functions in libraries +// The compiler use for native compilation doesn't support +// __attribute__ ((weak)) +void TMC2130Stepper::switchCSpin(bool state) { + // digitalWrite(_pinCS, state); +} +void TMC2130Stepper::setSPISpeed(uint32_t speed) { + // spi_speed = speed; +} +void TMC2130Stepper::write(uint8_t addressByte, uint32_t config) {} + +uint32_t TMC2130Stepper::read(uint8_t addressByte) { + return 0UL; +} diff --git a/libraries/NativeStubs/WebServer.h b/libraries/NativeStubs/WebServer.h new file mode 100644 index 00000000..0109a808 --- /dev/null +++ b/libraries/NativeStubs/WebServer.h @@ -0,0 +1,40 @@ +#pragma once + +typedef int HTTPMethod; +typedef const char* Uri; +typedef void (*THandlerFunction)(void); + +#define CONTENT_LENGTH_UNKNOWN ((size_t)-1) + +#define HTTP_ANY 1 +class WebServer { +public: + WebServer(IPAddress addr, int port = 80); + WebServer(int port = 80) {} + ~WebServer() = default; + + void begin() {}; + + void setContentLength(const size_t contentLength) {} + void collectHeaders(const char* headerKeys[], const size_t headerKeysCount) {} // set the request headers to collect + void on(const Uri& uri, HTTPMethod method, THandlerFunction fn) {} + void onNotFound(THandlerFunction fn) {} //called when handler is not assigned + void onFileUpload(THandlerFunction fn) {} //handle file uploads + + String arg(String name) { return name; } // get request argument value by name + bool hasArg(String name) { return false; } // check if argument exists + + template + size_t streamFile(T& file, const String& contentType) { + return 0; + } + + void sendHeader(const String& name, const String& value, bool first = false) {} + void sendContentLength(size_t len) {} + void sendContent(const String& content) {} + void send_P(int code, const char* content_type, const char* content, size_t contentLength) {} + void send(int n) {} + + // WiFiClient client() { return _currentClient; } + void handleClient() {} +}; diff --git a/libraries/NativeStubs/WebSocketsServer.h b/libraries/NativeStubs/WebSocketsServer.h new file mode 100644 index 00000000..6271ef4a --- /dev/null +++ b/libraries/NativeStubs/WebSocketsServer.h @@ -0,0 +1,7 @@ +class WebSocketsServer { +public: + WebSocketsServer(uint16_t port, String origin = "", String protocol = "arduino") {} + ~WebSocketsServer(void) {} + + void broadcastBIN(uint8_t* buf, size_t len) {} +}; diff --git a/libraries/NativeStubs/WiFi.cpp b/libraries/NativeStubs/WiFi.cpp new file mode 100644 index 00000000..c5e9b3e2 --- /dev/null +++ b/libraries/NativeStubs/WiFi.cpp @@ -0,0 +1,3 @@ +#include "WiFi.h" + +WiFiStuff WiFi; diff --git a/libraries/NativeStubs/WiFi.h b/libraries/NativeStubs/WiFi.h new file mode 100644 index 00000000..d3124bae --- /dev/null +++ b/libraries/NativeStubs/WiFi.h @@ -0,0 +1,19 @@ +#pragma once + +#include +#include +#include + +#define WiFiEvent_t int +#define WIFI_OFF false + +class WiFiStuff { +public: + void persistent(bool on) {}; + void disconnect(bool on) {} + void enableSTA(bool on) {} + void enableAP(bool on) {} + void mode(bool mode) {} +}; + +extern WiFiStuff WiFi; diff --git a/libraries/NativeStubs/WiFiClient.h b/libraries/NativeStubs/WiFiClient.h new file mode 100644 index 00000000..c0adc152 --- /dev/null +++ b/libraries/NativeStubs/WiFiClient.h @@ -0,0 +1,24 @@ +#pragma once +#include + +class WiFiClient { +public: + WiFiClient() {} + WiFiClient(int fd) {} + ~WiFiClient() {} + + int available() { return 0; } + int read(uint8_t* buf, size_t size) { return -1; } + void stop() {} + size_t write(const uint8_t* buf, size_t size) { return 0; } + uint8_t connected() { return 0; } + IPAddress remoteIP() const { return IPAddress(uint32_t(0)); } + + operator bool() { return connected(); } + + // WiFiClient& operator=(const WiFiClient& other); + // bool operator==(const bool value) { return bool() == value; } + // bool operator!=(const bool value) { return bool() != value; } + // bool operator==(const WiFiClient&); + // bool operator!=(const WiFiClient& rhs) { return !this->operator==(rhs); } +}; diff --git a/libraries/NativeStubs/WiFiClientSecure.h b/libraries/NativeStubs/WiFiClientSecure.h new file mode 100644 index 00000000..6f70f09b --- /dev/null +++ b/libraries/NativeStubs/WiFiClientSecure.h @@ -0,0 +1 @@ +#pragma once diff --git a/libraries/NativeStubs/WiFiServer.h b/libraries/NativeStubs/WiFiServer.h new file mode 100644 index 00000000..71135cfc --- /dev/null +++ b/libraries/NativeStubs/WiFiServer.h @@ -0,0 +1,27 @@ +#pragma once + +class WiFiServer { +public: + WiFiServer(uint16_t port = 80, uint8_t max_clients = 4) {} + ~WiFiServer() { end(); } + + void listenOnLocalhost() {} + WiFiClient available() { return WiFiClient(); } + WiFiClient accept() { return available(); } + void begin(uint16_t port = 0) {} + void begin(uint16_t port, int reuse_enable) {} + void setNoDelay(bool nodelay) {} + bool getNoDelay() { return false; } + bool hasClient() { return false; } + size_t write(const uint8_t* data, size_t len) { return 0; } + size_t write(uint8_t data) { return write(&data, 1); } + + void end() {} + void close() {} + void stop() {} + + operator bool() { return false; } + + int setTimeout(uint32_t seconds) { return seconds; } + void stopAll() {} +}; diff --git a/Grbl_Esp32/src/binary.h b/libraries/NativeStubs/binary.h similarity index 100% rename from Grbl_Esp32/src/binary.h rename to libraries/NativeStubs/binary.h diff --git a/libraries/NativeStubs/driver/rmt.h b/libraries/NativeStubs/driver/rmt.h new file mode 100644 index 00000000..6f70f09b --- /dev/null +++ b/libraries/NativeStubs/driver/rmt.h @@ -0,0 +1 @@ +#pragma once diff --git a/libraries/NativeStubs/driver/timer.h b/libraries/NativeStubs/driver/timer.h new file mode 100644 index 00000000..3eedee18 --- /dev/null +++ b/libraries/NativeStubs/driver/timer.h @@ -0,0 +1,28 @@ +#pragma once + +// Timer stuff used by Stepper.cpp +typedef struct { + int divider; + int counter_dir; + int counter_en; + int alarm_en; + int intr_type; + bool auto_reload; +} timer_config_t; + +const int TIMER_COUNT_UP = 0; +const int TIMER_PAUSE = 0; +const int TIMER_ALARM_EN = 0; +const int TIMER_INTR_LEVEL = 0; +typedef int timer_group_t; +typedef int timer_idx_t; +const timer_group_t TIMER_GROUP_0 = 0; +const timer_idx_t TIMER_0 = 0; + +inline void timer_set_counter_value(timer_group_t group, timer_idx_t idx, uint64_t ticks) {} +inline void timer_set_alarm_value(timer_group_t group, timer_idx_t idx, uint64_t ticks) {} +inline void timer_init(timer_group_t group, timer_idx_t idx, timer_config_t* conf) {} +inline void timer_enable_intr(timer_group_t group, timer_idx_t idx) {} +inline void timer_start(timer_group_t group, timer_idx_t idx) {} +inline void timer_pause(timer_group_t group, timer_idx_t idx) {} +inline void timer_isr_register(timer_group_t group, timer_idx_t idx, void (*handler)(void*), void* arg, int a, void* arg1) {} diff --git a/libraries/NativeStubs/driver/uart.h b/libraries/NativeStubs/driver/uart.h new file mode 100644 index 00000000..0bcb29a3 --- /dev/null +++ b/libraries/NativeStubs/driver/uart.h @@ -0,0 +1,5 @@ +#pragma once + +typedef int uart_port_t; + +#define UART_NUM_1 1 diff --git a/libraries/NativeStubs/esp_task_wdt.h b/libraries/NativeStubs/esp_task_wdt.h new file mode 100644 index 00000000..f5975924 --- /dev/null +++ b/libraries/NativeStubs/esp_task_wdt.h @@ -0,0 +1,5 @@ +#pragma once + +extern "C" { +inline esp_err_t esp_task_wdt_reset() {} +} diff --git a/libraries/NativeStubs/freertos/task.h b/libraries/NativeStubs/freertos/task.h new file mode 100644 index 00000000..1e5244f7 --- /dev/null +++ b/libraries/NativeStubs/freertos/task.h @@ -0,0 +1,43 @@ +#pragma once + +// Stub versions of FreeRTOS functions +typedef int TickType_t; + +const int portMUX_INITIALIZER_UNLOCKED = 0; +const int portMAX_DELAY = 0; +const int portTICK_PERIOD_MS = 1; +const int portTICK_RATE_MS = 1; +typedef int portMUX_TYPE; +typedef int BaseType_t; +typedef unsigned int UBaseType_t; +typedef void* xQueueHandle; +typedef void* QueueHandle_t; +typedef void* TaskHandle_t; +typedef int TickType_t; +const BaseType_t pdTRUE = 1; +const BaseType_t pdFALSE = 0; +const BaseType_t pdPASS = 0; +inline void vTaskDelay(TickType_t ticks) {} +inline BaseType_t xQueueReceive(QueueHandle_t xQueue, void* pvBuffer, TickType_t xTicksToWait) {} +inline xQueueHandle xQueueCreate(int n, int len) {} +inline BaseType_t xQueueReset(QueueHandle_t queue) { + return pdPASS; +} +inline BaseType_t xQueueSend(QueueHandle_t queue, void* item, TickType_t ticks) { + return pdTRUE; +} +inline BaseType_t xQueueSendFromISR(QueueHandle_t queue, void* item, void* p) { + return pdTRUE; +} +inline TaskHandle_t xTaskCreate(void (*task)(void*), const char* name, int stacksize, void* arg0, int pri, TaskHandle_t* th) {} +inline TaskHandle_t xTaskCreatePinnedToCore( + void (*task)(void*), const char* name, int stacksize, void* arg0, int pri, TaskHandle_t* th, int core) {} +inline int xTaskGetTickCount() { + return 0; +} +inline void vTaskDelayUntil(TickType_t* pxPreviousWakeTime, const TickType_t xTimeIncrement) {} +inline int xPortGetFreeHeapSize() { + return 0; +} +inline void vTaskEnterCritical(int* mutex) {} +inline void vTaskExitCritical(int* mutex) {} diff --git a/libraries/NativeStubs/library.properties b/libraries/NativeStubs/library.properties new file mode 100644 index 00000000..09eb0c36 --- /dev/null +++ b/libraries/NativeStubs/library.properties @@ -0,0 +1,10 @@ +name=NativeStubs +version=1.0 +author=MitchBradley +maintainer=MitchBradley +sentence=Stubs to compile Grbl_Esp32 on Windows +paragraph= +category= +url=https://github.com/bdring/Grbl_Esp32 +architectures=windows_amd64 + diff --git a/libraries/NativeStubs/native.h b/libraries/NativeStubs/native.h new file mode 100644 index 00000000..b9db8406 --- /dev/null +++ b/libraries/NativeStubs/native.h @@ -0,0 +1,29 @@ +#pragma once +#include +#include + +inline bool isPrintable(int c) { + return (isprint(c) == 0 ? false : true); +} + +#define IRAM_ATTR + +// The native compiler might not support __attribute__ ((weak)) +#define WEAK_FUNC + +// Unlike the ESP32 Arduino framework, EpoxyDuino does not have contrain() and map() + +// Templates don't work because of float/double promotion +#define constrain(amt, low, high) ((amt) < (low) ? (low) : ((amt) > (high) ? (high) : (amt))) + +inline long map(long x, long in_min, long in_max, long out_min, long out_max) { + const long dividend = out_max - out_min; + const long divisor = in_max - in_min; + const long delta = x - in_min; + if (divisor == 0) { + return -1; //AVR returns -1, SAM returns 0 + } + return (delta * dividend + (divisor / 2)) / divisor + out_min; +} + +#define M_PI 3.1415926536 diff --git a/libraries/NativeStubs/nvs.h b/libraries/NativeStubs/nvs.h new file mode 100644 index 00000000..df1f57ba --- /dev/null +++ b/libraries/NativeStubs/nvs.h @@ -0,0 +1,35 @@ +#pragma once + +const esp_err_t ESP_ERR_NVS_INVALID_HANDLE = 3; +const esp_err_t ESP_ERR_NVS_INVALID_NAME = 2; +const esp_err_t ESP_ERR_NVS_INVALID_LENGTH = 1; + +const int NVS_READWRITE = 0; +inline esp_err_t nvs_open(const char* s, int mode, int* handle) { + return 0; +} +inline esp_err_t nvs_get_i32(int _handle, const char* _keyName, int* value) { + return -1; +} +inline esp_err_t nvs_set_i32(int _handle, const char* _keyName, int value) { + return -1; +} +inline esp_err_t nvs_get_i8(int _handle, const char* _keyName, signed char* value) { + return -1; +} +inline esp_err_t nvs_set_i8(int _handle, const char* _keyName, int value) { + return -1; +} +inline esp_err_t nvs_get_str(int _handle, const char* _keyName, void* p, unsigned int* len) { + return -1; +} +inline esp_err_t nvs_set_str(int _handle, const char* _keyName, const char* value) { + return -1; +} +inline void nvs_erase_key(int _handle, const char* key) {} +inline esp_err_t nvs_get_blob(int handle, const char* _keyName, void* currentValue, size_t* len) { + return -1; +} +inline esp_err_t nvs_set_blob(int handle, const char* _keyName, void* currentValue, size_t len) { + return -1; +} diff --git a/platformio.ini b/platformio.ini index 9d29b396..2460ff9b 100644 --- a/platformio.ini +++ b/platformio.ini @@ -30,6 +30,7 @@ lib_deps = [env:release] lib_deps = ESP8266 and ESP32 OLED driver for SSD1306 displays@^4.2.0 +lib_ignore = NativeStubs platform = espressif32@3.0.0 ; temporary fix for lost uart rx characters board = esp32dev framework = arduino @@ -76,6 +77,8 @@ build_flags = ${common.build_flags} --std=c++17 -DNATIVE + -DCONFIG_BT_ENABLED + -DCONFIG_BLUEDROID_ENABLED lib_ldf_mode = deep lib_deps = EpoxyDuino=https://github.com/bxparks/EpoxyDuino.git