Skip to content

Commit

Permalink
new feat && new function name
Browse files Browse the repository at this point in the history
  • Loading branch information
asjdf committed Aug 28, 2022
1 parent f12d29f commit 86790c9
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 35 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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:

Expand Down
2 changes: 1 addition & 1 deletion examples/demo/demo.ino
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ void setup() {
// WebSerial is accessible at "<IP Address>/webserial" in browser
WebSerial.begin(&server);
/* Attach Message Callback */
WebSerial.msgCallback(recvMsg);
WebSerial.onMessage(recvMsg);
server.begin();
}

Expand Down
2 changes: 1 addition & 1 deletion examples/demo_ap/demo_ap.ino
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ void setup() {
// WebSerial is accessible at "<IP Address>/webserial" in browser
WebSerial.begin(&server);
/* Attach Message Callback */
WebSerial.msgCallback(recvMsg);
WebSerial.onMessage(recvMsg);
server.begin();
}

Expand Down
13 changes: 8 additions & 5 deletions keywords.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
WebSerial KEYWORD1
begin KEYWORD2
print KEYWORD2
println KEYWORD2
msgCallback KEYWORD2
WebSerial KEYWORD1
begin KEYWORD2
print KEYWORD2
println KEYWORD2
onConnect KEYWORD2
onDisconnect KEYWORD2
onMessage KEYWORD2
onError KEYWORD2
2 changes: 1 addition & 1 deletion library.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"maintainer": true
}
],
"version": "1.0.1",
"version": "2.0.0",
"frameworks": "arduino",
"platforms": "espressif"
}
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=WebSerialLite
version=1.0.1
version=2.0.0
author=HomeboyC
category=Communication
maintainer=HomeboyC <homeboyc@foxmail.com>
Expand Down
50 changes: 27 additions & 23 deletions src/WebSerial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,71 +19,75 @@ 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)
DEBUG_WEB_SERIAL("Attached AsyncWebServer along with Websockets");
#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

Expand Down
20 changes: 18 additions & 2 deletions src/WebSerialLite.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,15 @@
#include "WiFi.h"
#endif

typedef std::function<void(AsyncWebSocketClient *)> ConnHandler;
typedef std::function<void(AsyncWebSocketClient *)> DisconnHandler;
typedef std::function<void(uint8_t *data, size_t len)> RecvMsgHandler;
typedef std::function<void(AsyncWebSocketClient *, uint8_t *data,
size_t length)>
RecvMsgHandlerPlus;
typedef std::function<void(AsyncWebSocketClient *, uint16_t code,
const char *reason, size_t reason_length)>
ErrHandler;

// Uncomment to enable webserial debug mode
// #define WEBSERIAL_DEBUG
Expand All @@ -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

Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 86790c9

Please sign in to comment.