From 86790c9051346df7dca8234d71601f543859592e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=A8=E6=88=90=E9=94=B4?= Date: Sun, 28 Aug 2022 10:21:27 +0800 Subject: [PATCH] new feat && new function name --- README.md | 4 ++- examples/demo/demo.ino | 2 +- examples/demo_ap/demo_ap.ino | 2 +- keywords.txt | 13 ++++++---- library.json | 2 +- library.properties | 2 +- src/WebSerial.cpp | 50 +++++++++++++++++++----------------- src/WebSerialLite.h | 20 +++++++++++++-- 8 files changed, 60 insertions(+), 35 deletions(-) diff --git a/README.md b/README.md index e566512..146e02a 100644 --- a/README.md +++ b/README.md @@ -17,6 +17,8 @@ I'm sorry that the current version is already the Pro version. XD - Any number of Serial Monitors can be opened on the browser - Uses Async Webserver for better performance - Light weight (<3k) +- Timestamp +- Event driven ## Dependencies @@ -36,7 +38,7 @@ For ESP32: WebSerial is very similar to the default Serial Monitor Library of Arduino. -Please see `Demo` examples for better understanding on how to setup WebSerial for your Project: Click Here +Please see `Demo` examples for better understanding on how to setup WebSerial for your Project. WebSerial has 2 main functions: diff --git a/examples/demo/demo.ino b/examples/demo/demo.ino index cd73b7a..55c25b0 100644 --- a/examples/demo/demo.ino +++ b/examples/demo/demo.ino @@ -46,7 +46,7 @@ void setup() { // WebSerial is accessible at "/webserial" in browser WebSerial.begin(&server); /* Attach Message Callback */ - WebSerial.msgCallback(recvMsg); + WebSerial.onMessage(recvMsg); server.begin(); } diff --git a/examples/demo_ap/demo_ap.ino b/examples/demo_ap/demo_ap.ino index 9796f10..d9d3677 100644 --- a/examples/demo_ap/demo_ap.ino +++ b/examples/demo_ap/demo_ap.ino @@ -43,7 +43,7 @@ void setup() { // WebSerial is accessible at "/webserial" in browser WebSerial.begin(&server); /* Attach Message Callback */ - WebSerial.msgCallback(recvMsg); + WebSerial.onMessage(recvMsg); server.begin(); } diff --git a/keywords.txt b/keywords.txt index 1901081..eac2b3d 100644 --- a/keywords.txt +++ b/keywords.txt @@ -1,5 +1,8 @@ -WebSerial KEYWORD1 -begin KEYWORD2 -print KEYWORD2 -println KEYWORD2 -msgCallback KEYWORD2 \ No newline at end of file +WebSerial KEYWORD1 +begin KEYWORD2 +print KEYWORD2 +println KEYWORD2 +onConnect KEYWORD2 +onDisconnect KEYWORD2 +onMessage KEYWORD2 +onError KEYWORD2 \ No newline at end of file diff --git a/library.json b/library.json index 3239393..b1d1f1a 100644 --- a/library.json +++ b/library.json @@ -15,7 +15,7 @@ "maintainer": true } ], - "version": "1.0.1", + "version": "2.0.0", "frameworks": "arduino", "platforms": "espressif" } \ No newline at end of file diff --git a/library.properties b/library.properties index bd219ac..0dab967 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=WebSerialLite -version=1.0.1 +version=2.0.0 author=HomeboyC category=Communication maintainer=HomeboyC diff --git a/src/WebSerial.cpp b/src/WebSerial.cpp index 8fcf946..4f22843 100644 --- a/src/WebSerial.cpp +++ b/src/WebSerial.cpp @@ -19,20 +19,26 @@ void WebSerialClass::begin(AsyncWebServer *server, const char *url) { #if defined(WEBSERIAL_DEBUG) DEBUG_WEB_SERIAL("Client connection received"); #endif + if (_connCallback != NULL) _connCallback(client); } else if (type == WS_EVT_DISCONNECT) { #if defined(WEBSERIAL_DEBUG) DEBUG_WEB_SERIAL("Client disconnected"); #endif + if (_disconnCallback != NULL) _disconnCallback(client); } else if (type == WS_EVT_DATA) { #if defined(WEBSERIAL_DEBUG) DEBUG_WEB_SERIAL("Received Websocket Data"); #endif - if (_RecvFunc != NULL) { - _RecvFunc(data, len); - } + if (_recvMsgCallback != NULL) _recvMsgCallback(data, len); + if (_recvMsgCallbackPlus != NULL) _recvMsgCallbackPlus(client, data, len); + } else if (type == WS_EVT_ERROR) { +#if defined(WEBSERIAL_DEBUG) + DEBUG_WEB_SERIAL("Client error"); +#endif + if (_errCallback != NULL) + _errCallback(client, *(uint16_t *)arg, (const char *)data, len); } }); - _server->addHandler(_ws); #if defined(WEBSERIAL_DEBUG) @@ -40,50 +46,48 @@ void WebSerialClass::begin(AsyncWebServer *server, const char *url) { #endif } -void WebSerialClass::msgCallback(RecvMsgHandler _recv) { _RecvFunc = _recv; } +void WebSerialClass::onConnect(ConnHandler callbackFunc) { + _connCallback = callbackFunc; +} +void WebSerialClass::onDisconnect(DisconnHandler callbackFunc) { + _disconnCallback = callbackFunc; +} +void WebSerialClass::onMessage(RecvMsgHandler callbackFunc) { + _recvMsgCallback = callbackFunc; +} +void WebSerialClass::onMessage(RecvMsgHandlerPlus callbackFunc) { + _recvMsgCallbackPlus = callbackFunc; +} +void WebSerialClass::onError(ErrHandler callbackFunc) { + _errCallback = 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)); } // 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"); } #if defined(WEBSERIAL_DEBUG) void WebSerialClass::DEBUG_WEB_SERIAL(const char *message) { - Serial.print("[WebSerial] ");Serial.println(message); + Serial.print("[WebSerial] "); + Serial.println(message); } #endif diff --git a/src/WebSerialLite.h b/src/WebSerialLite.h index dd2acd9..b0d43ea 100644 --- a/src/WebSerialLite.h +++ b/src/WebSerialLite.h @@ -18,7 +18,15 @@ #include "WiFi.h" #endif +typedef std::function ConnHandler; +typedef std::function DisconnHandler; typedef std::function RecvMsgHandler; +typedef std::function + RecvMsgHandlerPlus; +typedef std::function + ErrHandler; // Uncomment to enable webserial debug mode // #define WEBSERIAL_DEBUG @@ -27,7 +35,11 @@ class WebSerialClass { public: void begin(AsyncWebServer *server, const char *url = "/webserial"); - void msgCallback(RecvMsgHandler _recv); + void onConnect(ConnHandler callbackFunc); + void onDisconnect(DisconnHandler callbackFunc); + void onMessage(RecvMsgHandler callbackFunc); + void onMessage(RecvMsgHandlerPlus callbackFunc); + void onError(ErrHandler callbackFunc); // Print @@ -72,7 +84,11 @@ class WebSerialClass { private: AsyncWebServer *_server; AsyncWebSocket *_ws; - RecvMsgHandler _RecvFunc = NULL; + ConnHandler _connCallback = NULL; + DisconnHandler _disconnCallback = NULL; + RecvMsgHandler _recvMsgCallback = NULL; + RecvMsgHandlerPlus _recvMsgCallbackPlus = NULL; + ErrHandler _errCallback = NULL; #if defined(WEBSERIAL_DEBUG) void DEBUG_WEB_SERIAL(const char *message);