попытки сделать crsf, не взлетела часть с пультом
This commit is contained in:
60
lib/port/unix/uart.cpp
Normal file
60
lib/port/unix/uart.cpp
Normal file
@@ -0,0 +1,60 @@
|
||||
#include "port/uart.h"
|
||||
|
||||
#include <sys/ioctl.h>
|
||||
#include <asm/termbits.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
#include <poll.h>
|
||||
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <cstring>
|
||||
#include <atomic>
|
||||
#include <span>
|
||||
|
||||
|
||||
drivers::UartDriver::UartDriver(const std::string& path, int baud) {
|
||||
fd = open(path.c_str(), O_RDWR | O_NOCTTY | O_NONBLOCK);
|
||||
if (fd < 0) throw std::runtime_error(std::string("UartDriver open error ") + path + ": " + std::strerror(errno));
|
||||
|
||||
struct termios2 tio{};
|
||||
if (ioctl(fd, TCGETS2, &tio) != 0) {
|
||||
close(fd);
|
||||
throw std::runtime_error(std::string("UartDriver setup error: ") + std::strerror(errno));
|
||||
}
|
||||
// 8bit
|
||||
tio.c_cflag &= ~CSIZE;
|
||||
tio.c_cflag |= CS8;
|
||||
// baud rate
|
||||
tio.c_ispeed = baud;
|
||||
tio.c_ospeed = baud;
|
||||
// other
|
||||
tio.c_iflag |= (INPCK|IGNBRK|IGNCR|ISTRIP);
|
||||
tio.c_cflag &= ~CBAUD;
|
||||
tio.c_cflag |= (BOTHER|CREAD|CLOCAL);
|
||||
|
||||
if (ioctl(fd, TCSETS2, &tio) != 0) {
|
||||
close(fd);
|
||||
throw std::runtime_error(std::string("UartDriver setup error: ") + std::strerror(errno));
|
||||
}
|
||||
|
||||
events = POLLIN;
|
||||
}
|
||||
bool drivers::UartDriver::writeData(std::span<const uint8_t> data) {
|
||||
if (fd < 0) return false;
|
||||
auto w = write(fd, data.data(), data.size());
|
||||
return w == data.size();
|
||||
}
|
||||
size_t drivers::UartDriver::readChunk(std::vector<uint8_t>& out) {
|
||||
if (fd < 0) return 0;
|
||||
out.resize(1024);
|
||||
auto r = read(fd, out.data(), out.size());
|
||||
if (r <= 0) return 0;
|
||||
out.resize(r);
|
||||
return static_cast<size_t>(r);
|
||||
}
|
||||
drivers::UartDriver::~UartDriver() {
|
||||
close(fd);
|
||||
}
|
||||
Reference in New Issue
Block a user