попытки сделать crsf, не взлетела часть с пультом

This commit is contained in:
2025-11-24 16:24:59 +03:00
parent 0f14fd0155
commit 3eaea1b966
20 changed files with 769 additions and 535 deletions

60
lib/port/unix/uart.cpp Normal file
View 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);
}