From 04c8523619ebac17a96bf6f97c9daaf9567e49cd Mon Sep 17 00:00:00 2001 From: Adam Novak Date: Tue, 15 Feb 2022 22:00:13 -0500 Subject: [PATCH] Implement Serial --- LoRa.cpp | 8 +++++-- Makefile | 2 +- Utilities.h | 63 ++++++++++++++++++++++++++++++++++++++++------------- 3 files changed, 55 insertions(+), 18 deletions(-) diff --git a/LoRa.cpp b/LoRa.cpp index 4788eba..8cba9e1 100644 --- a/LoRa.cpp +++ b/LoRa.cpp @@ -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) { diff --git a/Makefile b/Makefile index 040edd1..80e4659 100644 --- a/Makefile +++ b/Makefile @@ -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 diff --git a/Utilities.h b/Utilities.h index f150f7a..3f07bbf 100644 --- a/Utilities.h +++ b/Utilities.h @@ -10,6 +10,9 @@ #if LIBRARY_TYPE == LIBRARY_C #include + #include + #include + #include // 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);