From 7b77f091bfb8f9d7db588d0f56aa26c6d5067227 Mon Sep 17 00:00:00 2001 From: rasmuskleist Date: Mon, 7 Aug 2023 09:33:31 +0200 Subject: [PATCH] Remove template parameter from UartDevice --- .../architecture/interface/uart_device.hpp | 37 ++++++++++++++----- 1 file changed, 28 insertions(+), 9 deletions(-) diff --git a/src/modm/architecture/interface/uart_device.hpp b/src/modm/architecture/interface/uart_device.hpp index a8879fba93..2bc800d08f 100644 --- a/src/modm/architecture/interface/uart_device.hpp +++ b/src/modm/architecture/interface/uart_device.hpp @@ -26,11 +26,15 @@ namespace modm * @author Rasmus Kleist Hørlyck Sørensen * @ingroup modm_architecture_uart_device */ -template < class Uart, uint8_t NestingLevels = 10, uint16_t TxUsTimeout = 1000, uint16_t RxUsTimeout = 10000 > +template < class Uart, uint8_t NestingLevels = 10 > class UartDevice : protected modm::NestedResumable< NestingLevels + 1 > { public: - UartDevice() = default; + UartDevice() : + txTimeout(std::chrono::microseconds(1000)), + rxTimeout(std::chrono::microseconds(10000)) + { + } bool hasReceived() @@ -38,13 +42,25 @@ class UartDevice : protected modm::NestedResumable< NestingLevels + 1 > return Uart::receiveBufferSize() > 0; } + void + setTxTimeout(ShortPreciseDuration timeout) + { + txTimeout = timeout; + } + + void + setRxTimeout(ShortPreciseDuration timeout) + { + rxTimeout = timeout; + } + protected: modm::ResumableResult write(uint8_t data) { RF_BEGIN(0); - timeout.restart(std::chrono::microseconds(TxUsTimeout)); + timeout.restart(txTimeout); RF_WAIT_UNTIL(Uart::write(data) or timeout.isExpired() or Uart::hasError()); if (timeout.isExpired() or Uart::hasError()) { @@ -62,13 +78,13 @@ class UartDevice : protected modm::NestedResumable< NestingLevels + 1 > RF_BEGIN(0); writeIndex = 0; - timeout.restart(std::chrono::microseconds(TxUsTimeout)); + timeout.restart(txTimeout); while (writeIndex < length) { if (size_t writeSize = Uart::write(&data[writeIndex], length - writeIndex)) { writeIndex += writeSize; - timeout.restart(std::chrono::microseconds(TxUsTimeout)); + timeout.restart(txTimeout); } if (timeout.isExpired() or Uart::hasError()) @@ -88,7 +104,7 @@ class UartDevice : protected modm::NestedResumable< NestingLevels + 1 > { RF_BEGIN(1); - timeout.restart(std::chrono::microseconds(RxUsTimeout)); + timeout.restart(rxTimeout); RF_WAIT_UNTIL(Uart::read(data) or timeout.isExpired() or Uart::hasError()); if (timeout.isExpired() or Uart::hasError()) { @@ -105,13 +121,13 @@ class UartDevice : protected modm::NestedResumable< NestingLevels + 1 > RF_BEGIN(1); readIndex = 0; - timeout.restart(std::chrono::microseconds(RxUsTimeout)); + timeout.restart(rxTimeout); while (readIndex < length) { if (size_t writeSize = Uart::read(&buffer[readIndex], length - readIndex)) { writeIndex += writeSize; - timeout.restart(std::chrono::microseconds(RxUsTimeout)); + timeout.restart(rxTimeout); } if (timeout.isExpired() or Uart::hasError()) @@ -129,7 +145,10 @@ class UartDevice : protected modm::NestedResumable< NestingLevels + 1 > private: std::size_t readIndex; std::size_t writeIndex; - modm::ShortPreciseTimeout timeout; + + ShortPreciseDuration txTimeout; + ShortPreciseDuration rxTimeout; + ShortPreciseTimeout timeout; }; } // namespace modm