Refactor serial class with templates (#20783)
This commit is contained in:
		@@ -24,10 +24,7 @@
 | 
			
		||||
 | 
			
		||||
#ifdef ARDUINO_ARCH_ESP32
 | 
			
		||||
 | 
			
		||||
FlushableHardwareSerial::FlushableHardwareSerial(int uart_nr)
 | 
			
		||||
    : HardwareSerial(uart_nr)
 | 
			
		||||
{}
 | 
			
		||||
 | 
			
		||||
FlushableHardwareSerial flushableSerial(0);
 | 
			
		||||
Serial0Type<FlushableHardwareSerial> flushableSerial(false, 0);
 | 
			
		||||
 | 
			
		||||
#endif // ARDUINO_ARCH_ESP32
 | 
			
		||||
 
 | 
			
		||||
@@ -24,14 +24,13 @@
 | 
			
		||||
#ifdef ARDUINO_ARCH_ESP32
 | 
			
		||||
 | 
			
		||||
#include <HardwareSerial.h>
 | 
			
		||||
#include "../../core/serial_hook.h"
 | 
			
		||||
 | 
			
		||||
class FlushableHardwareSerial : public HardwareSerial {
 | 
			
		||||
public:
 | 
			
		||||
  FlushableHardwareSerial(int uart_nr);
 | 
			
		||||
 | 
			
		||||
  inline void flushTX() { /* No need to flush the hardware serial, but defined here for compatibility. */ }
 | 
			
		||||
  FlushableHardwareSerial(int uart_nr) : HardwareSerial(uart_nr) {}
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
extern FlushableHardwareSerial flushableSerial;
 | 
			
		||||
extern Serial0Type<FlushableHardwareSerial> flushableSerial;
 | 
			
		||||
 | 
			
		||||
#endif // ARDUINO_ARCH_ESP32
 | 
			
		||||
 
 | 
			
		||||
@@ -40,6 +40,10 @@
 | 
			
		||||
  #endif
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
#if ENABLED(ESP3D_WIFISUPPORT)
 | 
			
		||||
  DefaultSerial MSerial(false, Serial2Socket);
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
// ------------------------
 | 
			
		||||
// Externs
 | 
			
		||||
// ------------------------
 | 
			
		||||
 
 | 
			
		||||
@@ -55,7 +55,9 @@ extern portMUX_TYPE spinlock;
 | 
			
		||||
 | 
			
		||||
#if EITHER(WIFISUPPORT, ESP3D_WIFISUPPORT)
 | 
			
		||||
  #if ENABLED(ESP3D_WIFISUPPORT)
 | 
			
		||||
    #define MYSERIAL1 Serial2Socket
 | 
			
		||||
    typedef ForwardSerial0Type< decltype(Serial2Socket) > DefaultSerial;
 | 
			
		||||
    extern DefaultSerial MSerial;
 | 
			
		||||
    #define MYSERIAL1 MSerial
 | 
			
		||||
  #else
 | 
			
		||||
    #define MYSERIAL1 webSocketSerial
 | 
			
		||||
  #endif
 | 
			
		||||
 
 | 
			
		||||
@@ -29,7 +29,7 @@
 | 
			
		||||
#include "wifi.h"
 | 
			
		||||
#include <ESPAsyncWebServer.h>
 | 
			
		||||
 | 
			
		||||
WebSocketSerial webSocketSerial;
 | 
			
		||||
MSerialT webSocketSerial(false);
 | 
			
		||||
AsyncWebSocket ws("/ws"); // TODO Move inside the class.
 | 
			
		||||
 | 
			
		||||
// RingBuffer impl
 | 
			
		||||
@@ -144,9 +144,5 @@ size_t WebSocketSerial::write(const uint8_t* buffer, size_t size) {
 | 
			
		||||
  return written;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void WebSocketSerial::flushTX() {
 | 
			
		||||
  // No need to do anything as there's no benefit to sending partial lines over the websocket connection.
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#endif // WIFISUPPORT
 | 
			
		||||
#endif // ARDUINO_ARCH_ESP32
 | 
			
		||||
 
 | 
			
		||||
@@ -22,6 +22,7 @@
 | 
			
		||||
#pragma once
 | 
			
		||||
 | 
			
		||||
#include "../../inc/MarlinConfig.h"
 | 
			
		||||
#include "../../core/serial_hook.h"
 | 
			
		||||
 | 
			
		||||
#include <Stream.h>
 | 
			
		||||
 | 
			
		||||
@@ -68,12 +69,9 @@ public:
 | 
			
		||||
  int peek();
 | 
			
		||||
  int read();
 | 
			
		||||
  void flush();
 | 
			
		||||
  void flushTX();
 | 
			
		||||
  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 uint32_t dropped() { return 0; }
 | 
			
		||||
  #endif
 | 
			
		||||
@@ -83,4 +81,5 @@ public:
 | 
			
		||||
  #endif
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
extern WebSocketSerial webSocketSerial;
 | 
			
		||||
typedef Serial0Type<WebSocketSerial> MSerialT;
 | 
			
		||||
extern MSerialT webSocketSerial;
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user