mirror of
https://github.com/emsesp/EMS-ESP32.git
synced 2025-12-06 07:49:52 +03:00
formatting
This commit is contained in:
@@ -90,80 +90,79 @@ int vsnprintf_P(char * str, size_t size, const char * format, va_list ap);
|
|||||||
|
|
||||||
class Print;
|
class Print;
|
||||||
|
|
||||||
class NativeConsole: public Stream {
|
class NativeConsole : public Stream {
|
||||||
public:
|
public:
|
||||||
void begin(unsigned long baud __attribute__((unused))) {
|
void begin(unsigned long baud __attribute__((unused))) {
|
||||||
|
}
|
||||||
|
|
||||||
}
|
int available() override {
|
||||||
|
if (peek_ != -1)
|
||||||
|
return 1;
|
||||||
|
|
||||||
int available() override {
|
struct timeval timeout;
|
||||||
if (peek_ != -1)
|
fd_set rfds;
|
||||||
return 1;
|
|
||||||
|
|
||||||
struct timeval timeout;
|
FD_ZERO(&rfds);
|
||||||
fd_set rfds;
|
FD_SET(STDIN_FILENO, &rfds);
|
||||||
|
|
||||||
FD_ZERO(&rfds);
|
timeout.tv_sec = 0;
|
||||||
FD_SET(STDIN_FILENO, &rfds);
|
timeout.tv_usec = 1000;
|
||||||
|
|
||||||
timeout.tv_sec = 0;
|
return ::select(STDIN_FILENO + 1, &rfds, NULL, NULL, &timeout) > 0 ? 1 : 0;
|
||||||
timeout.tv_usec = 1000;
|
}
|
||||||
|
|
||||||
return ::select(STDIN_FILENO + 1, &rfds, NULL, NULL, &timeout) > 0 ? 1 : 0;
|
int read() override {
|
||||||
}
|
if (peek_ != -1) {
|
||||||
|
uint8_t c = peek_;
|
||||||
|
peek_ = -1;
|
||||||
|
return c;
|
||||||
|
}
|
||||||
|
|
||||||
int read() override {
|
if (available() > 0) {
|
||||||
if (peek_ != -1) {
|
uint8_t c;
|
||||||
uint8_t c = peek_;
|
int ret = ::read(STDIN_FILENO, &c, 1);
|
||||||
peek_ = -1;
|
|
||||||
return c;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (available() > 0) {
|
if (ret == 0) {
|
||||||
uint8_t c;
|
/* Ctrl+D */
|
||||||
int ret = ::read(STDIN_FILENO, &c, 1);
|
return '\x04';
|
||||||
|
} else if (ret == 1) {
|
||||||
|
/* Remap Ctrl+Z to Ctrl-\ */
|
||||||
|
if (c == '\x1A')
|
||||||
|
c = '\x1C';
|
||||||
|
return c;
|
||||||
|
} else {
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (ret == 0) {
|
return -1;
|
||||||
/* Ctrl+D */
|
}
|
||||||
return '\x04';
|
|
||||||
} else if (ret == 1) {
|
|
||||||
/* Remap Ctrl+Z to Ctrl-\ */
|
|
||||||
if (c == '\x1A')
|
|
||||||
c = '\x1C';
|
|
||||||
return c;
|
|
||||||
} else {
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return -1;
|
int peek() override {
|
||||||
}
|
if (peek_ == -1)
|
||||||
|
peek_ = read();
|
||||||
|
|
||||||
int peek() override {
|
return peek_;
|
||||||
if (peek_ == -1)
|
}
|
||||||
peek_ = read();
|
|
||||||
|
|
||||||
return peek_;
|
size_t write(uint8_t c) override {
|
||||||
}
|
if (::write(STDOUT_FILENO, &c, 1) == 1) {
|
||||||
|
return 1;
|
||||||
|
} else {
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
size_t write(uint8_t c) override {
|
size_t write(const uint8_t * buffer, size_t size) {
|
||||||
if (::write(STDOUT_FILENO, &c, 1) == 1) {
|
if (::write(STDOUT_FILENO, buffer, size) == (ssize_t)size) {
|
||||||
return 1;
|
return size;
|
||||||
} else {
|
} else {
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t write(const uint8_t *buffer, size_t size) {
|
private:
|
||||||
if (::write(STDOUT_FILENO, buffer, size) == (ssize_t)size) {
|
int peek_ = -1;
|
||||||
return size;
|
|
||||||
} else {
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
|
||||||
int peek_ = -1;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#include "Network.h"
|
#include "Network.h"
|
||||||
|
|||||||
@@ -30,46 +30,85 @@
|
|||||||
#include <Printable.h>
|
#include <Printable.h>
|
||||||
#include <WString.h>
|
#include <WString.h>
|
||||||
|
|
||||||
int vsnprintf_P(char *str, size_t size, const char *format, va_list ap);
|
int vsnprintf_P(char * str, size_t size, const char * format, va_list ap);
|
||||||
|
|
||||||
class Print {
|
class Print {
|
||||||
public:
|
public:
|
||||||
virtual size_t write(uint8_t c) = 0;
|
virtual size_t write(uint8_t c) = 0;
|
||||||
virtual size_t write(const uint8_t *buffer, size_t size) { for (size_t i = 0; i < size; i++) { write(*buffer); buffer++; }; return size; };
|
virtual size_t write(const uint8_t * buffer, size_t size) {
|
||||||
void setWriteError(int err = 1) { err_ = err; }
|
for (size_t i = 0; i < size; i++) {
|
||||||
int getWriteError() { return err_; }
|
write(*buffer);
|
||||||
void clearWriteError() {}
|
buffer++;
|
||||||
size_t print(char c) { return write((uint8_t)c); }
|
};
|
||||||
size_t print(const char *data) { return write(reinterpret_cast<const uint8_t *>(data), strlen(data)); }
|
return size;
|
||||||
size_t print(const Printable &printable) { return printable.printTo(*this); }
|
};
|
||||||
size_t print(int value) { return print(std::to_string(value).c_str()); }
|
void setWriteError(int err = 1) {
|
||||||
size_t print(unsigned int value) { return print(std::to_string(value).c_str()); }
|
err_ = err;
|
||||||
size_t print(long value) { return print(std::to_string(value).c_str()); }
|
}
|
||||||
size_t print(unsigned long value) { return print(std::to_string(value).c_str()); }
|
int getWriteError() {
|
||||||
size_t printf(const char * format, ...) {
|
return err_;
|
||||||
char str[4096] = { 0 };
|
}
|
||||||
va_list ap;
|
void clearWriteError() {
|
||||||
|
}
|
||||||
|
size_t print(char c) {
|
||||||
|
return write((uint8_t)c);
|
||||||
|
}
|
||||||
|
size_t print(const char * data) {
|
||||||
|
return write(reinterpret_cast<const uint8_t *>(data), strlen(data));
|
||||||
|
}
|
||||||
|
size_t print(const Printable & printable) {
|
||||||
|
return printable.printTo(*this);
|
||||||
|
}
|
||||||
|
size_t print(int value) {
|
||||||
|
return print(std::to_string(value).c_str());
|
||||||
|
}
|
||||||
|
size_t print(unsigned int value) {
|
||||||
|
return print(std::to_string(value).c_str());
|
||||||
|
}
|
||||||
|
size_t print(long value) {
|
||||||
|
return print(std::to_string(value).c_str());
|
||||||
|
}
|
||||||
|
size_t print(unsigned long value) {
|
||||||
|
return print(std::to_string(value).c_str());
|
||||||
|
}
|
||||||
|
size_t printf(const char * format, ...) {
|
||||||
|
char str[4096] = {0};
|
||||||
|
va_list ap;
|
||||||
|
|
||||||
va_start(ap, format);
|
va_start(ap, format);
|
||||||
int ret = vsnprintf_P(str, sizeof(str), format, ap);
|
int ret = vsnprintf_P(str, sizeof(str), format, ap);
|
||||||
va_end(ap);
|
va_end(ap);
|
||||||
|
|
||||||
if (ret < 0)
|
if (ret < 0)
|
||||||
return ret;
|
return ret;
|
||||||
|
|
||||||
return print(str);
|
return print(str);
|
||||||
}
|
}
|
||||||
size_t println() { return print("\r\n"); }
|
size_t println() {
|
||||||
size_t println(const char *data) { return print(data) + println(); }
|
return print("\r\n");
|
||||||
size_t println(const Printable &printable) { return printable.printTo(*this) + println(); }
|
}
|
||||||
size_t println(int value) { return print(std::to_string(value).c_str()) + println(); }
|
size_t println(const char * data) {
|
||||||
size_t println(unsigned int value) { return print(std::to_string(value).c_str()) + println(); }
|
return print(data) + println();
|
||||||
size_t println(long value) { return print(std::to_string(value).c_str()) + println(); }
|
}
|
||||||
size_t println(unsigned long value) { return print(std::to_string(value).c_str()) + println(); }
|
size_t println(const Printable & printable) {
|
||||||
virtual void flush() { };
|
return printable.printTo(*this) + println();
|
||||||
|
}
|
||||||
|
size_t println(int value) {
|
||||||
|
return print(std::to_string(value).c_str()) + println();
|
||||||
|
}
|
||||||
|
size_t println(unsigned int value) {
|
||||||
|
return print(std::to_string(value).c_str()) + println();
|
||||||
|
}
|
||||||
|
size_t println(long value) {
|
||||||
|
return print(std::to_string(value).c_str()) + println();
|
||||||
|
}
|
||||||
|
size_t println(unsigned long value) {
|
||||||
|
return print(std::to_string(value).c_str()) + println();
|
||||||
|
}
|
||||||
|
virtual void flush(){};
|
||||||
|
|
||||||
private:
|
private:
|
||||||
int err_{0};
|
int err_{0};
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -30,12 +30,11 @@ class Print;
|
|||||||
Print::print and Print::println methods.
|
Print::print and Print::println methods.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
class Printable
|
class Printable {
|
||||||
{
|
public:
|
||||||
public:
|
virtual ~Printable() {
|
||||||
virtual ~Printable() {}
|
}
|
||||||
virtual size_t printTo(Print& p) const = 0;
|
virtual size_t printTo(Print & p) const = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|||||||
@@ -36,69 +36,64 @@
|
|||||||
readBytesBetween( pre_string, terminator, buffer, length)
|
readBytesBetween( pre_string, terminator, buffer, length)
|
||||||
*/
|
*/
|
||||||
|
|
||||||
class Stream: public Print
|
class Stream : public Print {
|
||||||
{
|
protected:
|
||||||
protected:
|
unsigned long _timeout; // number of milliseconds to wait for the next char before aborting timed read
|
||||||
unsigned long _timeout; // number of milliseconds to wait for the next char before aborting timed read
|
unsigned long _startMillis; // used for timeout measurement
|
||||||
unsigned long _startMillis; // used for timeout measurement
|
int timedRead(); // private method to read stream with timeout
|
||||||
int timedRead(); // private method to read stream with timeout
|
int timedPeek(); // private method to peek stream with timeout
|
||||||
int timedPeek(); // private method to peek stream with timeout
|
int peekNextDigit(); // returns the next numeric digit in the stream or -1 if timeout
|
||||||
int peekNextDigit(); // returns the next numeric digit in the stream or -1 if timeout
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
virtual int available() = 0;
|
virtual int available() = 0;
|
||||||
virtual int read() = 0;
|
virtual int read() = 0;
|
||||||
virtual int peek() = 0;
|
virtual int peek() = 0;
|
||||||
|
|
||||||
Stream():_startMillis(0)
|
Stream()
|
||||||
{
|
: _startMillis(0) {
|
||||||
_timeout = 1000;
|
_timeout = 1000;
|
||||||
}
|
}
|
||||||
virtual ~Stream() {}
|
virtual ~Stream() {
|
||||||
|
}
|
||||||
|
|
||||||
// parsing methods
|
// parsing methods
|
||||||
|
|
||||||
void setTimeout(unsigned long timeout); // sets maximum milliseconds to wait for stream data, default is 1 second
|
void setTimeout(unsigned long timeout); // sets maximum milliseconds to wait for stream data, default is 1 second
|
||||||
unsigned long getTimeout(void);
|
unsigned long getTimeout(void);
|
||||||
|
|
||||||
bool find(const char *target); // reads data from the stream until the target string is found
|
bool find(const char * target); // reads data from the stream until the target string is found
|
||||||
bool find(uint8_t *target)
|
bool find(uint8_t * target) {
|
||||||
{
|
return find((char *)target);
|
||||||
return find((char *) target);
|
|
||||||
}
|
}
|
||||||
// returns true if target string is found, false if timed out (see setTimeout)
|
// returns true if target string is found, false if timed out (see setTimeout)
|
||||||
|
|
||||||
bool find(const char *target, size_t length); // reads data from the stream until the target string of given length is found
|
bool find(const char * target, size_t length); // reads data from the stream until the target string of given length is found
|
||||||
bool find(const uint8_t *target, size_t length)
|
bool find(const uint8_t * target, size_t length) {
|
||||||
{
|
return find((char *)target, length);
|
||||||
return find((char *) target, length);
|
|
||||||
}
|
}
|
||||||
// returns true if target string is found, false if timed out
|
// returns true if target string is found, false if timed out
|
||||||
|
|
||||||
bool find(char target)
|
bool find(char target) {
|
||||||
{
|
return find(&target, 1);
|
||||||
return find (&target, 1);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool findUntil(const char *target, const char *terminator); // as find but search ends if the terminator string is found
|
bool findUntil(const char * target, const char * terminator); // as find but search ends if the terminator string is found
|
||||||
bool findUntil(const uint8_t *target, const char *terminator)
|
bool findUntil(const uint8_t * target, const char * terminator) {
|
||||||
{
|
return findUntil((char *)target, terminator);
|
||||||
return findUntil((char *) target, terminator);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool findUntil(const char *target, size_t targetLen, const char *terminate, size_t termLen); // as above but search ends if the terminate string is found
|
bool findUntil(const char * target, size_t targetLen, const char * terminate, size_t termLen); // as above but search ends if the terminate string is found
|
||||||
bool findUntil(const uint8_t *target, size_t targetLen, const char *terminate, size_t termLen)
|
bool findUntil(const uint8_t * target, size_t targetLen, const char * terminate, size_t termLen) {
|
||||||
{
|
return findUntil((char *)target, targetLen, terminate, termLen);
|
||||||
return findUntil((char *) target, targetLen, terminate, termLen);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
long parseInt(); // returns the first valid (long) integer value from the current position.
|
long parseInt(); // returns the first valid (long) integer value from the current position.
|
||||||
// initial characters that are not digits (or the minus sign) are skipped
|
// initial characters that are not digits (or the minus sign) are skipped
|
||||||
// integer is terminated by the first character that is not a digit.
|
// integer is terminated by the first character that is not a digit.
|
||||||
|
|
||||||
float parseFloat(); // float version of parseInt
|
float parseFloat(); // float version of parseInt
|
||||||
|
|
||||||
virtual size_t readBytes(char *buffer, size_t length) // read chars from stream into buffer
|
virtual size_t readBytes(char * buffer, size_t length) // read chars from stream into buffer
|
||||||
{
|
{
|
||||||
size_t total = 0;
|
size_t total = 0;
|
||||||
|
|
||||||
@@ -113,14 +108,13 @@ public:
|
|||||||
|
|
||||||
return total;
|
return total;
|
||||||
}
|
}
|
||||||
virtual size_t readBytes(uint8_t *buffer, size_t length)
|
virtual size_t readBytes(uint8_t * buffer, size_t length) {
|
||||||
{
|
return readBytes((char *)buffer, length);
|
||||||
return readBytes((char *) buffer, length);
|
|
||||||
}
|
}
|
||||||
// terminates if length characters have been read or timeout (see setTimeout)
|
// terminates if length characters have been read or timeout (see setTimeout)
|
||||||
// returns the number of characters placed in the buffer (0 means no valid data found)
|
// returns the number of characters placed in the buffer (0 means no valid data found)
|
||||||
|
|
||||||
size_t readBytesUntil(char terminator, char *buffer, size_t length) // as readBytes with terminator character
|
size_t readBytesUntil(char terminator, char * buffer, size_t length) // as readBytes with terminator character
|
||||||
{
|
{
|
||||||
size_t total = 0;
|
size_t total = 0;
|
||||||
|
|
||||||
@@ -135,9 +129,8 @@ public:
|
|||||||
|
|
||||||
return total;
|
return total;
|
||||||
}
|
}
|
||||||
size_t readBytesUntil(char terminator, uint8_t *buffer, size_t length)
|
size_t readBytesUntil(char terminator, uint8_t * buffer, size_t length) {
|
||||||
{
|
return readBytesUntil(terminator, (char *)buffer, length);
|
||||||
return readBytesUntil(terminator, (char *) buffer, length);
|
|
||||||
}
|
}
|
||||||
// terminates if length characters have been read, timeout, or if the terminator character detected
|
// terminates if length characters have been read, timeout, or if the terminator character detected
|
||||||
// returns the number of characters placed in the buffer (0 means no valid data found)
|
// returns the number of characters placed in the buffer (0 means no valid data found)
|
||||||
@@ -175,23 +168,22 @@ public:
|
|||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
long parseInt(char skipChar); // as above but the given skipChar is ignored
|
long parseInt(char skipChar); // as above but the given skipChar is ignored
|
||||||
// as above but the given skipChar is ignored
|
// as above but the given skipChar is ignored
|
||||||
// this allows format characters (typically commas) in values to be ignored
|
// this allows format characters (typically commas) in values to be ignored
|
||||||
|
|
||||||
float parseFloat(char skipChar); // as above but the given skipChar is ignored
|
float parseFloat(char skipChar); // as above but the given skipChar is ignored
|
||||||
|
|
||||||
struct MultiTarget {
|
struct MultiTarget {
|
||||||
const char *str; // string you're searching for
|
const char * str; // string you're searching for
|
||||||
size_t len; // length of string you're searching for
|
size_t len; // length of string you're searching for
|
||||||
size_t index; // index used by the search routine.
|
size_t index; // index used by the search routine.
|
||||||
};
|
};
|
||||||
|
|
||||||
// This allows you to search for an arbitrary number of strings.
|
// This allows you to search for an arbitrary number of strings.
|
||||||
// Returns index of the target that is found first or -1 if timeout occurs.
|
// Returns index of the target that is found first or -1 if timeout occurs.
|
||||||
int findMulti(struct MultiTarget *targets, int tCount);
|
int findMulti(struct MultiTarget * targets, int tCount);
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
Reference in New Issue
Block a user