From 758a1732eb621273457987fbc27e3d93e54e97bf Mon Sep 17 00:00:00 2001 From: nullstalgia Date: Wed, 22 Feb 2023 07:27:25 -0800 Subject: [PATCH] feat: inherit from Print instead of overloading print and fix crash MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Update WebSerial.cpp * Update WebSerialLite.h * Only send if the websocket exists * Properly inherit from Print * Update demos with examples of new functionality * Accidentally undid the changes in the Header * remove redundant space * update: fix build in esp32 * update: fix build in esp32 --------- Co-authored-by: 杨成锴 --- examples/demo/demo.ino | 6 ++++++ examples/demo_ap/demo_ap.ino | 6 ++++++ src/WebSerial.cpp | 33 ++++++++++++----------------- src/WebSerialLite.h | 41 +++--------------------------------- 4 files changed, 28 insertions(+), 58 deletions(-) diff --git a/examples/demo/demo.ino b/examples/demo/demo.ino index 7a63d54..a7d962e 100644 --- a/examples/demo/demo.ino +++ b/examples/demo/demo.ino @@ -51,4 +51,10 @@ void setup() { } void loop() { + delay(2000); + + WebSerial.print(F("IP address: ")); + WebSerial.println(WiFi.localIP()); + WebSerial.printf("Millis=%lu\n", millis()); + WebSerial.printf("Free heap=[%u]\n", ESP.getFreeHeap()); } \ No newline at end of file diff --git a/examples/demo_ap/demo_ap.ino b/examples/demo_ap/demo_ap.ino index 54de41d..507895a 100644 --- a/examples/demo_ap/demo_ap.ino +++ b/examples/demo_ap/demo_ap.ino @@ -48,4 +48,10 @@ void setup() { } void loop() { + delay(2000); + + WebSerial.print(F("IP address: ")); + WebSerial.println(WiFi.localIP()); + WebSerial.printf("Millis=%lu\n", millis()); + WebSerial.printf("Free heap=[%u]\n", ESP.getFreeHeap()); } \ No newline at end of file diff --git a/src/WebSerial.cpp b/src/WebSerial.cpp index 22c8a16..d31b19e 100644 --- a/src/WebSerial.cpp +++ b/src/WebSerial.cpp @@ -66,26 +66,19 @@ void WebSerialClass::onError(ErrHandler callbackFunc) { } // Print -void WebSerialClass::print(String m) { _ws->textAll(m); } -void WebSerialClass::print(const char *m) { _ws->textAll(m); } -void WebSerialClass::print(char *m) { _ws->textAll(m); } -void WebSerialClass::print(int m) { _ws->textAll(String(m)); } -void WebSerialClass::print(uint8_t m) { _ws->textAll(String(m)); } -void WebSerialClass::print(uint16_t m) { _ws->textAll(String(m)); } -void WebSerialClass::print(uint32_t m) { _ws->textAll(String(m)); } -void WebSerialClass::print(double m) { _ws->textAll(String(m)); } -void WebSerialClass::print(float m) { _ws->textAll(String(m)); } +size_t WebSerialClass::write(uint8_t m) { + if (_ws != NULL) { + _ws->textAll((const char *)&(m), 1); + } + return(1); +} -// Print with New Line -void WebSerialClass::println(String m) { _ws->textAll(m + "\n"); } -void WebSerialClass::println(const char *m) { _ws->textAll(String(m) + "\n"); } -void WebSerialClass::println(char *m) { _ws->textAll(String(m) + "\n"); } -void WebSerialClass::println(int m) { _ws->textAll(String(m) + "\n"); } -void WebSerialClass::println(uint8_t m) { _ws->textAll(String(m) + "\n"); } -void WebSerialClass::println(uint16_t m) { _ws->textAll(String(m) + "\n"); } -void WebSerialClass::println(uint32_t m) { _ws->textAll(String(m) + "\n"); } -void WebSerialClass::println(float m) { _ws->textAll(String(m) + "\n"); } -void WebSerialClass::println(double m) { _ws->textAll(String(m) + "\n"); } +size_t WebSerialClass::write(const uint8_t* buffer, size_t size) { + if (_ws != NULL) { + _ws->textAll((const char *)buffer, size); + } + return(size); +} #if defined(WEBSERIAL_DEBUG) void WebSerialClass::DEBUG_WEB_SERIAL(const char *message) { @@ -94,4 +87,4 @@ void WebSerialClass::DEBUG_WEB_SERIAL(const char *message) { } #endif -WebSerialClass WebSerial; \ No newline at end of file +WebSerialClass WebSerial; diff --git a/src/WebSerialLite.h b/src/WebSerialLite.h index b0d43ea..8f8df2d 100644 --- a/src/WebSerialLite.h +++ b/src/WebSerialLite.h @@ -31,7 +31,7 @@ typedef std::function