Refactor, fix ESP32 WebSocketSerial (#13689)

This commit is contained in:
Kajetan Rzepecki
2019-04-15 23:32:20 +02:00
committed by Scott Lahteine
parent db373f130c
commit 20dc45bca7
2 changed files with 132 additions and 228 deletions

View File

@@ -23,12 +23,7 @@
#include "../../inc/MarlinConfig.h"
#include <WString.h>
#define DEC 10
#define HEX 16
#define OCT 8
#define BIN 2
#include "Stream.h"
#ifndef RX_BUFFER_SIZE
#define RX_BUFFER_SIZE 128
@@ -40,60 +35,50 @@
#error "TX_BUFFER_SIZE is required for the WebSocket."
#endif
#if RX_BUFFER_SIZE > 256
typedef uint16_t ring_buffer_pos_t;
#else
typedef uint8_t ring_buffer_pos_t;
#endif
typedef uint16_t ring_buffer_pos_t;
class RingBuffer {
uint8_t *data;
ring_buffer_pos_t size, read_index, write_index;
class WebSocketSerial {
public:
WebSocketSerial() {};
static void begin(const long);
static void end();
static int peek(void);
static int read(void);
static void flush(void);
static void flushTx(void);
static bool available(void);
static void write(const uint8_t c);
RingBuffer(ring_buffer_pos_t size);
~RingBuffer();
int available(void);
int peek(void);
int read(void);
ring_buffer_pos_t read(uint8_t *buffer);
void flush(void);
ring_buffer_pos_t write(const uint8_t c);
ring_buffer_pos_t write(const uint8_t* buffer, ring_buffer_pos_t size);
};
class WebSocketSerial: public Stream {
RingBuffer rx_buffer;
RingBuffer tx_buffer;
public:
WebSocketSerial();
void begin(const long);
void end();
int available(void);
int peek(void);
int read(void);
void flush(void);
void flushTX(void);
size_t write(const uint8_t c);
size_t write(const uint8_t* buffer, size_t size);
operator bool() { return true; }
#if ENABLED(SERIAL_STATS_DROPPED_RX)
FORCE_INLINE static uint32_t dropped() { return 0; }
FORCE_INLINE uint32_t dropped() { return 0; }
#endif
#if ENABLED(SERIAL_STATS_MAX_RX_QUEUED)
FORCE_INLINE static int rxMaxEnqueued() { return 0; }
FORCE_INLINE int rxMaxEnqueued() { return 0; }
#endif
FORCE_INLINE static void write(const char* str) { while (*str) write(*str++); }
FORCE_INLINE static void write(const uint8_t* buffer, size_t size) { while (size--) write(*buffer++); }
FORCE_INLINE static void print(const String& s) { for (int i = 0; i < (int)s.length(); i++) write(s[i]); }
FORCE_INLINE static void print(const char* str) { write(str); }
static void print(char, int = 0);
static void print(unsigned char, int = 0);
static void print(int, int = DEC);
static void print(unsigned int, int = DEC);
static void print(long, int = DEC);
static void print(unsigned long, int = DEC);
static void print(double, int = 2);
static void println(const String& s);
static void println(const char[]);
static void println(char, int = 0);
static void println(unsigned char, int = 0);
static void println(int, int = DEC);
static void println(unsigned int, int = DEC);
static void println(long, int = DEC);
static void println(unsigned long, int = DEC);
static void println(double, int = 2);
static void println(void);
operator bool() { return true; }
private:
static void printNumber(unsigned long, const uint8_t);
static void printFloat(double, uint8_t);
};
extern WebSocketSerial webSocketSerial;