UART refactored (#236)
`Uart` is now an abstract class with a `UartEsp` and a `UartPosix` implementation.
This commit is contained in:
committed by
GitHub
parent
de46401d85
commit
b85ef7a2e7
@@ -2,6 +2,8 @@
|
||||
|
||||
#include "Tactility/hal/gps/GpsDevice.h"
|
||||
|
||||
namespace tt::hal::uart { class Uart; }
|
||||
|
||||
namespace tt::hal::gps {
|
||||
|
||||
/**
|
||||
@@ -14,6 +16,6 @@ bool init(const std::vector<GpsDevice::Configuration>& configurations);
|
||||
/**
|
||||
* Init sequence on UART for a specific GPS model.
|
||||
*/
|
||||
bool init(uart_port_t port, GpsModel type);
|
||||
bool init(uart::Uart& uart, GpsModel type);
|
||||
|
||||
}
|
||||
|
||||
@@ -5,8 +5,6 @@
|
||||
|
||||
namespace tt::hal::gps {
|
||||
|
||||
struct GpsInfo;
|
||||
|
||||
GpsModel probe(uart_port_t port);
|
||||
GpsModel probe(uart::Uart& iart);
|
||||
|
||||
}
|
||||
|
||||
@@ -17,11 +17,11 @@ template<size_t DataSize>
|
||||
inline void sendPacket(uart_port_t port, uint8_t type, uint8_t id, uint8_t data[DataSize], const char* errorMessage, TickType_t timeout) {
|
||||
static uint8_t buffer[250] = {0};
|
||||
size_t length = makePacket(type, id, data, DataSize, buffer);
|
||||
hal::uart::writeBytes(port, buffer, length);
|
||||
// hal::uart::writeBytes(port, buffer, length);
|
||||
}
|
||||
|
||||
GpsModel probe(uart_port_t port);
|
||||
GpsModel probe(uart::Uart& uart);
|
||||
|
||||
bool init(uart_port_t port, GpsModel model);
|
||||
bool init(uart::Uart& uart, GpsModel model);
|
||||
|
||||
} // namespace tt::service::gps
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
#pragma once
|
||||
#ifdef ESP_PLATFORM
|
||||
|
||||
#include "Tactility/Mutex.h"
|
||||
#include "Tactility/hal/uart/Uart.h"
|
||||
#include "Tactility/hal/uart/Configuration.h"
|
||||
|
||||
namespace tt::hal::uart {
|
||||
|
||||
class UartEsp final : public Uart {
|
||||
|
||||
private:
|
||||
|
||||
Mutex mutex;
|
||||
const Configuration& configuration;
|
||||
bool started = false;
|
||||
|
||||
public:
|
||||
|
||||
explicit UartEsp(const Configuration& configuration) : configuration(configuration) {}
|
||||
|
||||
bool start() final;
|
||||
bool isStarted() const final;
|
||||
bool stop() final;
|
||||
size_t readBytes(std::byte* buffer, size_t bufferSize, TickType_t timeout) final;
|
||||
bool readByte(std::byte* output, TickType_t timeout) final;
|
||||
size_t writeBytes(const std::byte* buffer, size_t bufferSize, TickType_t timeout) final;
|
||||
size_t available(TickType_t timeout) final;
|
||||
bool setBaudRate(uint32_t baudRate, TickType_t timeout) final;
|
||||
uint32_t getBaudRate() final;
|
||||
void flushInput() final;
|
||||
};
|
||||
|
||||
std::unique_ptr<Uart> create(const Configuration& configuration);
|
||||
|
||||
} // namespace tt::hal::uart
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,47 @@
|
||||
#pragma once
|
||||
#ifndef ESP_PLATFORM
|
||||
|
||||
#include "Tactility/Mutex.h"
|
||||
#include "Tactility/hal/uart/Configuration.h"
|
||||
#include "Tactility/hal/uart/Uart.h"
|
||||
|
||||
#include <termios.h>
|
||||
|
||||
namespace tt::hal::uart {
|
||||
|
||||
class UartPosix final : public Uart {
|
||||
|
||||
private:
|
||||
struct AutoCloseFileDeleter {
|
||||
void operator()(FILE* file) {
|
||||
fclose(file);
|
||||
}
|
||||
};
|
||||
|
||||
Mutex mutex;
|
||||
const Configuration& configuration;
|
||||
std::unique_ptr<FILE, AutoCloseFileDeleter> device;
|
||||
|
||||
bool awaitAvailable(TickType_t timeout);
|
||||
|
||||
public:
|
||||
|
||||
explicit UartPosix(const Configuration& configuration) : configuration(configuration) {}
|
||||
|
||||
bool start() final;
|
||||
bool isStarted() const final;
|
||||
bool stop() final;
|
||||
size_t readBytes(std::byte* buffer, size_t bufferSize, TickType_t timeout) final;
|
||||
bool readByte(std::byte* output, TickType_t timeout) final;
|
||||
size_t writeBytes(const std::byte* buffer, size_t bufferSize, TickType_t timeout) final;
|
||||
size_t available(TickType_t timeout) final;
|
||||
bool setBaudRate(uint32_t baudRate, TickType_t timeout) final;
|
||||
uint32_t getBaudRate() final;
|
||||
void flushInput() final;
|
||||
};
|
||||
|
||||
std::unique_ptr<Uart> create(const Configuration& configuration);
|
||||
|
||||
} // namespace tt::hal::uart
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user