Skip to content

Commit

Permalink
feat: inherit from Print instead of overloading print and fix crash
Browse files Browse the repository at this point in the history
* 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: 杨成锴 <homeboyc@foxmail.com>
  • Loading branch information
nullstalgia and asjdf authored Feb 22, 2023
1 parent be5f58d commit 758a173
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 58 deletions.
6 changes: 6 additions & 0 deletions examples/demo/demo.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
6 changes: 6 additions & 0 deletions examples/demo_ap/demo_ap.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
33 changes: 13 additions & 20 deletions src/WebSerial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -94,4 +87,4 @@ void WebSerialClass::DEBUG_WEB_SERIAL(const char *message) {
}
#endif

WebSerialClass WebSerial;
WebSerialClass WebSerial;
41 changes: 3 additions & 38 deletions src/WebSerialLite.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ typedef std::function<void(AsyncWebSocketClient *, uint16_t code,
// Uncomment to enable webserial debug mode
// #define WEBSERIAL_DEBUG

class WebSerialClass {
class WebSerialClass : public Print {
public:
void begin(AsyncWebServer *server, const char *url = "/webserial");

Expand All @@ -43,43 +43,8 @@ class WebSerialClass {

// Print

void print(String m = "");

void print(const char *m);

void print(char *m);

void print(int m);

void print(uint8_t m);

void print(uint16_t m);

void print(uint32_t m);

void print(double m);

void print(float m);

// Print with New Line

void println(String m = "");

void println(const char *m);

void println(char *m);

void println(int m);

void println(uint8_t m);

void println(uint16_t m);

void println(uint32_t m);

void println(float m);

void println(double m);
size_t write(uint8_t);
size_t write(const uint8_t* buffer, size_t size);

private:
AsyncWebServer *_server;
Expand Down

0 comments on commit 758a173

Please sign in to comment.