Implement Serial

This commit is contained in:
Adam Novak 2022-02-15 22:00:13 -05:00
parent d01a4b293f
commit 04c8523619
3 changed files with 55 additions and 18 deletions

View File

@ -125,8 +125,10 @@ int LoRaClass::begin(long frequency)
// start SPI
SPI.begin();
#elif LIBRARY_TYPE == LIBRARY_C
_fd = open("/dev/spidev0.0", O_RDWR);
if (_fd < 0) {
const char* spi_filename = "/dev/spidev0.0";
std::cerr << "Opening SPI device " << spi_filename << std::endl;
_fd = open(spi_filename, O_RDWR);
if (_fd <= 0) {
perror("could not open SPI device");
exit(1);
}
@ -703,6 +705,8 @@ uint8_t ISR_VECT LoRaClass::singleTransfer(uint8_t address, uint8_t value)
int status;
std::cerr << "Access SPI at " << _fd << std::endl;
// Configure SPI speed and mode to match settings
status = ioctl(_fd, SPI_IOC_WR_MODE, &_spiSettings.mode);
if (status < 0) {

View File

@ -152,4 +152,4 @@ obj/RNode_Firmware.o: RNode_Firmware.ino Utilities.h Config.h LoRa.h ROM.h Frami
bin/rnode: obj/RNode_Firmware.o obj/LoRa.o obj/MD5.o
mkdir -p bin
$(CC) -o $@ $^ -lstdc++
$(CC) -o $@ $^ -lstdc++ -lutil

View File

@ -10,6 +10,9 @@
#if LIBRARY_TYPE == LIBRARY_C
#include <time.h>
#include <poll.h>
#include <unistd.h>
#include <pty.h>
// We need a delay()
void delay(int ms) {
struct timespec interval;
@ -40,39 +43,69 @@
// We also need a Serial
class SerialClass {
public:
const char* fifoPath = "rnode_socket";
void begin(int baud) {
int status = mkfifo(fifoPath, 0666);
int other_end = 0;
int status = openpty(&_fd, &other_end, NULL, NULL, NULL);
if (status) {
perror("Making fifo failed");
exit(1);
}
// TODO: Need a bidirectional thing here: openpty???
_fd = open(fifoPath, O_RDWR);
if (_fd < 0) {
perror("could not open fifo");
perror("could not open PTY");
exit(1);
}
std::cout << "Listening on " << ttyname(other_end) << std::endl;
}
// Be truthy if connected
operator bool() {
return _fd > 0;
}
void write(int b) {
ssize_t written = ::write(_fd,
uint8_t to_write = b;
ssize_t written = ::write(_fd, &to_write, 1);
while (written != 1) {
if (written < 0) {
perror("could not write to PTY");
exit(1);
}
written = ::write(_fd, &to_write, 1);
}
}
void write(const char* data) {
throw std::runtime_error("Unimplemented");
while(*data) {
write(*data);
++data;
}
}
bool available() {
throw std::runtime_error("Unimplemented");
struct pollfd request;
request.fd = _fd;
request.events = POLLIN;
request.revents = 0;
int result = poll(&request, 1, 0);
if (result == -1) {
perror("could not poll");
exit(1);
}
return result > 0;
}
uint8_t read() {
throw std::runtime_error("Unimplemented");
uint8_t buffer;
ssize_t count = ::read(_fd, &buffer, 1);
while (count != 1) {
if (count < 0) {
perror("could not read from PTY");
exit(1);
}
count = ::read(_fd, &buffer, 1);
}
return buffer;
}
protected:
int _fd = -1;
int _fd;
};
SerialClass Serial;
// And random(below);