Kaynağa Gözat

Refactor, fix ESP32 WebSocketSerial (#13689)

Kajetan Rzepecki 6 yıl önce
ebeveyn
işleme
20dc45bca7

+ 87
- 168
Marlin/src/HAL/HAL_ESP32/WebSocketSerial.cpp Dosyayı Görüntüle

@@ -21,213 +21,132 @@
21 21
  */
22 22
 #ifdef ARDUINO_ARCH_ESP32
23 23
 
24
-#include "../../inc/MarlinConfig.h"
24
+#include "../../inc/MarlinConfigPre.h"
25 25
 
26 26
 #if ENABLED(WIFISUPPORT)
27 27
 
28 28
 #include "WebSocketSerial.h"
29
-
30
-extern WebSocketSerial webSocketSerial;
31
-
32 29
 #include "wifi.h"
33
-#include <AsyncTCP.h>
34 30
 #include <ESPAsyncWebServer.h>
35 31
 
36
-struct ring_buffer_r {
37
-  unsigned char buffer[RX_BUFFER_SIZE];
38
-  volatile ring_buffer_pos_t head, tail;
39
-};
40
-
41
-struct ring_buffer_t {
42
-  unsigned char buffer[256];
43
-  volatile uint8_t head, tail;
44
-};
45
-
46
-ring_buffer_r rx_buffer = { { 0 }, 0, 0 };
47
-ring_buffer_t tx_buffer = { { 0 }, 0, 0 };
48
-
49
-static bool _written;
32
+WebSocketSerial webSocketSerial;
33
+AsyncWebSocket ws("/ws"); // TODO Move inside the class.
50 34
 
51
-#if ENABLED(EMERGENCY_PARSER)
52
-  static EmergencyParser::State emergency_state; // = EP_RESET
53
-#endif
35
+// RingBuffer impl
54 36
 
55
-AsyncWebSocket ws("/ws"); // access at ws://[esp ip]/ws
37
+#define NEXT_INDEX(I, SIZE) ((I + 1) & (ring_buffer_pos_t)(SIZE - 1))
56 38
 
57
-FORCE_INLINE int next_rx_index(const int i) { return (ring_buffer_pos_t)(i + 1) & (ring_buffer_pos_t)(RX_BUFFER_SIZE - 1); }
58
-FORCE_INLINE int next_tx_index(const int i) { return (ring_buffer_pos_t)(i + 1) & (ring_buffer_pos_t)(TX_BUFFER_SIZE - 1); }
39
+RingBuffer::RingBuffer(ring_buffer_pos_t size)
40
+  : data(new uint8_t[size]),
41
+    read_index(0),
42
+    write_index(0),
43
+    size(size)
44
+{}
59 45
 
60
-static void addToBuffer(uint8_t * const data, const size_t len) {
61
-  for (size_t i = 0; i < len; i++) {
62
-    ring_buffer_pos_t h = rx_buffer.head;
63
-    const ring_buffer_pos_t t = rx_buffer.tail, n = next_rx_index(h);
46
+RingBuffer::~RingBuffer() { delete[] data; }
64 47
 
65
-    if (n != t) { rx_buffer.buffer[h] = data[i]; h = n; }
66
-
67
-    // TODO: buffer is full, handle?
68
-
69
-    rx_buffer.head = h;
70
-  }
71
-}
48
+ring_buffer_pos_t RingBuffer::write(const uint8_t c) {
49
+  const ring_buffer_pos_t n = NEXT_INDEX(write_index, size);
72 50
 
73
-// Handle WebSocket event
74
-static void onEvent(AsyncWebSocket * server, AsyncWebSocketClient * client, AwsEventType type, void * arg, uint8_t *data, size_t len) {
75
-  switch (type) {
76
-    case WS_EVT_CONNECT: client->ping(); break; // client connected
77
-    case WS_EVT_DISCONNECT:                     // client disconnected
78
-    case WS_EVT_ERROR:                          // error was received from the other end
79
-    case WS_EVT_PONG: break;                    // pong message was received (in response to a ping request maybe)
80
-    case WS_EVT_DATA: {                         // data packet
81
-      AwsFrameInfo * info = (AwsFrameInfo*)arg;
82
-      if (info->opcode == WS_TEXT || info->message_opcode == WS_TEXT)
83
-        addToBuffer(data, len);
84
-    }
51
+  if (n != read_index) {
52
+    this->data[write_index] = c;
53
+    write_index = n;
54
+    return 1;
85 55
   }
86
-}
87 56
 
88
-// Public Methods
89
-void WebSocketSerial::begin(const long baud_setting) {
90
-  ws.onEvent(onEvent);
91
-  server.addHandler(&ws); // attach AsyncWebSocket
57
+  // TODO: buffer is full, handle?
58
+  return 0;
92 59
 }
93 60
 
94
-void WebSocketSerial::end() { }
95
-
96
-int WebSocketSerial::peek(void) {
97
-  const int v = rx_buffer.head == rx_buffer.tail ? -1 : rx_buffer.buffer[rx_buffer.tail];
98
-  return v;
61
+ring_buffer_pos_t RingBuffer::write(const uint8_t *buffer, ring_buffer_pos_t size) {
62
+  ring_buffer_pos_t written = 0;
63
+  for (ring_buffer_pos_t i = 0; i < size; i++) {
64
+    written += write(buffer[i]);
65
+  }
66
+  return written;
99 67
 }
100 68
 
101
-int WebSocketSerial::read(void) {
102
-  const ring_buffer_pos_t h = rx_buffer.head, t = rx_buffer.tail;
103
-  if (h == t) return -1;  // Nothing to read? Return now
104
-
105
-  const int v = rx_buffer.buffer[t];
106
-
107
-  rx_buffer.tail = (ring_buffer_pos_t)(t + 1) & (RX_BUFFER_SIZE - 1); // Advance tail
108
-
109
-  return v;
69
+int RingBuffer::available(void) {
70
+  return (size - read_index + write_index) & (size - 1);
110 71
 }
111 72
 
112
-bool WebSocketSerial::available(void) {
113
-  const ring_buffer_pos_t h = rx_buffer.head, t = rx_buffer.tail;
114
-  return (ring_buffer_pos_t)(RX_BUFFER_SIZE + h - t) & (RX_BUFFER_SIZE - 1);
73
+int RingBuffer::peek(void) {
74
+  return available() ? data[read_index] : -1;
115 75
 }
116 76
 
117
-void WebSocketSerial::flush(void) {
118
-  ws.textAll("flush");
119
-  rx_buffer.tail = rx_buffer.head;
77
+int RingBuffer::read(void) {
78
+  if (available()) {
79
+    const int ret = data[read_index];
80
+    read_index = NEXT_INDEX(read_index, size);
81
+    return ret;
82
+  }
83
+  return -1;
120 84
 }
121 85
 
122
-#if TX_BUFFER_SIZE
123
-
124
-  void WebSocketSerial::write(const uint8_t c) {
125
-    _written = true;
126
-
127
-    const uint8_t i = (tx_buffer.head + 1) & (TX_BUFFER_SIZE - 1);
86
+ring_buffer_pos_t RingBuffer::read(uint8_t *buffer) {
87
+   ring_buffer_pos_t len = available();
128 88
 
129
-    // Store new char. head is always safe to move
130
-    tx_buffer.buffer[tx_buffer.head] = c;
131
-    tx_buffer.head = i;
132
-
133
-    if (c == '\n') {
134
-      ws.textAll(tx_buffer.buffer, tx_buffer.head);
135
-      tx_buffer.head = 0;
136
-    }
89
+  for(ring_buffer_pos_t i = 0; read_index != write_index; i++) {
90
+    buffer[i] = data[read_index];
91
+    read_index = NEXT_INDEX(read_index, size);
137 92
   }
138 93
 
139
-  void WebSocketSerial::flushTx(void) {
140
-    ws.textAll("flushTx");
141
-    if (!_written) return;
142
-  }
143
-
144
-#else
94
+  return len;
95
+}
145 96
 
146
- //void WebSocketSerial::write(const uint8_t c) { _written = true; }
147
- //void WebSocketSerial::flushTx(void) { if (!_written) return; }
97
+void RingBuffer::flush(void) { read_index = write_index; }
148 98
 
149
-#endif
99
+// WebSocketSerial impl
100
+WebSocketSerial::WebSocketSerial()
101
+    : rx_buffer(RingBuffer(RX_BUFFER_SIZE)),
102
+      tx_buffer(RingBuffer(TX_BUFFER_SIZE))
103
+{}
150 104
 
151
-/**
152
- * Imports from print.h
153
- */
105
+void WebSocketSerial::begin(const long baud_setting) {
106
+  ws.onEvent([this](AsyncWebSocket *server, AsyncWebSocketClient *client, AwsEventType type, void *arg, uint8_t *data, size_t len) {
107
+    switch (type) {
108
+      case WS_EVT_CONNECT: client->ping(); break; // client connected
109
+      case WS_EVT_DISCONNECT:                     // client disconnected
110
+      case WS_EVT_ERROR:                          // error was received from the other end
111
+      case WS_EVT_PONG: break;                    // pong message was received (in response to a ping request maybe)
112
+      case WS_EVT_DATA: {                         // data packet
113
+        AwsFrameInfo * info = (AwsFrameInfo*)arg;
114
+        if (info->opcode == WS_TEXT || info->message_opcode == WS_TEXT)
115
+          this->rx_buffer.write(data, len);
116
+      }
117
+    }
118
+  });
119
+  server.addHandler(&ws);
120
+}
154 121
 
155
-void WebSocketSerial::print(char c, int base) { print((long)c, base); }
156
-void WebSocketSerial::print(unsigned char b, int base) { print((unsigned long)b, base); }
157
-void WebSocketSerial::print(int n, int base) { print((long)n, base); }
158
-void WebSocketSerial::print(unsigned int n, int base) { print((unsigned long)n, base); }
159
-void WebSocketSerial::print(long n, int base) {
160
-  if (base == 0)
161
-    write(n);
162
-  else if (base == 10) {
163
-    if (n < 0) { print('-'); n = -n; }
164
-    printNumber(n, 10);
122
+void WebSocketSerial::end() { }
123
+int WebSocketSerial::peek(void) { return rx_buffer.peek(); }
124
+int WebSocketSerial::read(void) { return rx_buffer.read(); }
125
+int WebSocketSerial::available(void) { return rx_buffer.available(); }
126
+void WebSocketSerial::flush(void) { rx_buffer.flush(); }
127
+
128
+size_t WebSocketSerial::write(const uint8_t c) {
129
+  size_t ret = tx_buffer.write(c);
130
+
131
+  if (ret && c == '\n') {
132
+    uint8_t tmp[TX_BUFFER_SIZE];
133
+    ring_buffer_pos_t size = tx_buffer.read(tmp);
134
+    ws.textAll(tmp, size);
165 135
   }
166
-  else
167
-    printNumber(n, base);
168
-}
169 136
 
170
-void WebSocketSerial::print(unsigned long n, int base) {
171
-  if (base == 0) write(n); else printNumber(n, base);
137
+  return ret;
172 138
 }
173 139
 
174
-void WebSocketSerial::print(double n, int digits)         { printFloat(n, digits); }
175
-
176
-void WebSocketSerial::println(void)                       { print('\r'); print('\n'); }
177
-void WebSocketSerial::println(const String& s)            { print(s); println(); }
178
-void WebSocketSerial::println(const char c[])             { print(c); println(); }
179
-void WebSocketSerial::println(char c, int base)           { print(c, base); println(); }
180
-void WebSocketSerial::println(unsigned char b, int base)  { print(b, base); println(); }
181
-void WebSocketSerial::println(int n, int base)            { print(n, base); println(); }
182
-void WebSocketSerial::println(unsigned int n, int base)   { print(n, base); println(); }
183
-void WebSocketSerial::println(long n, int base)           { print(n, base); println(); }
184
-void WebSocketSerial::println(unsigned long n, int base)  { print(n, base); println(); }
185
-void WebSocketSerial::println(double n, int digits)       { print(n, digits); println(); }
186
-
187
-// Private Methods
188
-
189
-void WebSocketSerial::printNumber(unsigned long n, uint8_t base) {
190
-  if (n) {
191
-    unsigned char buf[8 * sizeof(long)]; // Enough space for base 2
192
-    int8_t i = 0;
193
-    while (n) {
194
-      buf[i++] = n % base;
195
-      n /= base;
196
-    }
197
-    while (i--)
198
-      print((char)(buf[i] + (buf[i] < 10 ? '0' : 'A' - 10)));
140
+size_t WebSocketSerial::write(const uint8_t* buffer, size_t size) {
141
+  size_t written = 0;
142
+  for(size_t i = 0; i < size; i++) {
143
+    written += write(buffer[i]);
199 144
   }
200
-  else
201
-    print('0');
145
+  return written;
202 146
 }
203 147
 
204
-void WebSocketSerial::printFloat(double number, uint8_t digits) {
205
-  // Handle negative numbers
206
-  if (number < 0.0) { print('-'); number = -number; }
207
-
208
-  // Round correctly so that print(1.999, 2) prints as "2.00"
209
-  // Use a lookup table for performance
210
-  constexpr double rounds[] = { 0.5, 0.05, 0.005, 0.0005, 0.00005, 0.000005, 0.0000005, 0.00000005 };
211
-  number += rounds[digits];
212
-
213
-  //number += pow(10, -(digits + 1)); // slower single-line equivalent
214
-
215
-  // Extract the integer part of the number and print it
216
-  unsigned long int_part = (unsigned long)number;
217
-  print(int_part);
218
-
219
-  // Print the decimal point, but only if there are digits beyond
220
-  double remainder = number - (double)int_part;
221
-  if (digits) {
222
-    print('.');
223
-    // Extract digits from the remainder one at a time
224
-    while (digits--) {
225
-      remainder *= 10.0;
226
-      const int toPrint = int(remainder);
227
-      print(toPrint);
228
-      remainder -= toPrint;
229
-    }
230
-  }
148
+void WebSocketSerial::flushTX(void) {
149
+  // No need to do anything as there's no benefit to sending partial lines over the websocket connection.
231 150
 }
232 151
 
233 152
 #endif // WIFISUPPORT

+ 37
- 52
Marlin/src/HAL/HAL_ESP32/WebSocketSerial.h Dosyayı Görüntüle

@@ -23,12 +23,7 @@
23 23
 
24 24
 #include "../../inc/MarlinConfig.h"
25 25
 
26
-#include <WString.h>
27
-
28
-#define DEC 10
29
-#define HEX 16
30
-#define OCT 8
31
-#define BIN 2
26
+#include "Stream.h"
32 27
 
33 28
 #ifndef RX_BUFFER_SIZE
34 29
   #define RX_BUFFER_SIZE 128
@@ -40,60 +35,50 @@
40 35
   #error "TX_BUFFER_SIZE is required for the WebSocket."
41 36
 #endif
42 37
 
43
-#if RX_BUFFER_SIZE > 256
44
-  typedef uint16_t ring_buffer_pos_t;
45
-#else
46
-  typedef uint8_t ring_buffer_pos_t;
47
-#endif
38
+typedef uint16_t ring_buffer_pos_t;
48 39
 
49
-class WebSocketSerial {
50
-public:
51
-  WebSocketSerial() {};
52
-  static void begin(const long);
53
-  static void end();
54
-  static int peek(void);
55
-  static int read(void);
56
-  static void flush(void);
57
-  static void flushTx(void);
58
-  static bool available(void);
59
-  static void write(const uint8_t c);
40
+class RingBuffer {
41
+  uint8_t *data;
42
+  ring_buffer_pos_t size, read_index, write_index;
60 43
 
61
-  #if ENABLED(SERIAL_STATS_DROPPED_RX)
62
-    FORCE_INLINE static uint32_t dropped() { return 0; }
63
-  #endif
44
+public:
45
+  RingBuffer(ring_buffer_pos_t size);
46
+  ~RingBuffer();
64 47
 
65
-  #if ENABLED(SERIAL_STATS_MAX_RX_QUEUED)
66
-    FORCE_INLINE static int rxMaxEnqueued() { return 0; }
67
-  #endif
48
+  int available(void);
49
+  int peek(void);
50
+  int read(void);
51
+  ring_buffer_pos_t read(uint8_t *buffer);
52
+  void flush(void);
53
+  ring_buffer_pos_t write(const uint8_t c);
54
+  ring_buffer_pos_t write(const uint8_t* buffer, ring_buffer_pos_t size);
55
+};
68 56
 
69
-  FORCE_INLINE static void write(const char* str) { while (*str) write(*str++); }
70
-  FORCE_INLINE static void write(const uint8_t* buffer, size_t size) { while (size--) write(*buffer++); }
71
-  FORCE_INLINE static void print(const String& s) { for (int i = 0; i < (int)s.length(); i++) write(s[i]); }
72
-  FORCE_INLINE static void print(const char* str) { write(str); }
57
+class WebSocketSerial: public Stream {
58
+  RingBuffer rx_buffer;
59
+  RingBuffer tx_buffer;
73 60
 
74
-  static void print(char, int = 0);
75
-  static void print(unsigned char, int = 0);
76
-  static void print(int, int = DEC);
77
-  static void print(unsigned int, int = DEC);
78
-  static void print(long, int = DEC);
79
-  static void print(unsigned long, int = DEC);
80
-  static void print(double, int = 2);
61
+public:
62
+  WebSocketSerial();
63
+  void begin(const long);
64
+  void end();
65
+  int available(void);
66
+  int peek(void);
67
+  int read(void);
68
+  void flush(void);
69
+  void flushTX(void);
70
+  size_t write(const uint8_t c);
71
+  size_t write(const uint8_t* buffer, size_t size);
81 72
 
82
-  static void println(const String& s);
83
-  static void println(const char[]);
84
-  static void println(char, int = 0);
85
-  static void println(unsigned char, int = 0);
86
-  static void println(int, int = DEC);
87
-  static void println(unsigned int, int = DEC);
88
-  static void println(long, int = DEC);
89
-  static void println(unsigned long, int = DEC);
90
-  static void println(double, int = 2);
91
-  static void println(void);
92 73
   operator bool() { return true; }
93 74
 
94
-private:
95
-  static void printNumber(unsigned long, const uint8_t);
96
-  static void printFloat(double, uint8_t);
75
+  #if ENABLED(SERIAL_STATS_DROPPED_RX)
76
+    FORCE_INLINE uint32_t dropped() { return 0; }
77
+  #endif
78
+
79
+  #if ENABLED(SERIAL_STATS_MAX_RX_QUEUED)
80
+    FORCE_INLINE int rxMaxEnqueued() { return 0; }
81
+  #endif
97 82
 };
98 83
 
99 84
 extern WebSocketSerial webSocketSerial;

Loading…
İptal
Kaydet