-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Pilkki utility cicd, win&macos implementations, code cleanup, deps vi…
…a FetchContent (#1) * Create pilkki-software-multiplatform.yml * Windows and MacOS implementations * Code cleanup * Use FetchContent instead of submodules
- Loading branch information
1 parent
19707dd
commit 22dadad
Showing
9 changed files
with
269 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,60 @@ | ||
cmake_minimum_required(VERSION 3.24) | ||
|
||
cmake_minimum_required(VERSION 3.20) | ||
|
||
project(pilkki LANGUAGES C CXX) | ||
|
||
set(CMAKE_CXX_STANDARD 23) | ||
set(CMAKE_CXX_STANDARD 17) | ||
set(CMAKE_CXX_STANDARD_REQUIRED ON) | ||
|
||
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/deps/argparse/include) | ||
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/deps/magic_enum/include) | ||
|
||
include(FetchContent) | ||
|
||
FetchContent_Declare( | ||
magic_enum | ||
GIT_REPOSITORY https://github.com/Neargye/magic_enum.git | ||
GIT_TAG v0.9.3 | ||
) | ||
FetchContent_Declare( | ||
argparse | ||
GIT_REPOSITORY https://github.com/morrisfranken/argparse.git | ||
GIT_TAG 5d3710809364e694f33243810bbd1345975ae1c3 | ||
) | ||
|
||
FetchContent_GetProperties(argparse) | ||
FetchContent_GetProperties(magic_enum) | ||
if(NOT argparse_POPULATED) | ||
FetchContent_Populate(argparse) | ||
add_subdirectory(${argparse_SOURCE_DIR} ${argparse_BINARY_DIR} EXCLUDE_FROM_ALL) | ||
endif() | ||
|
||
if(NOT magic_enum_POPULATED) | ||
FetchContent_Populate(magic_enum) | ||
add_subdirectory(${magic_enum_SOURCE_DIR} ${magic_enum_BINARY_DIR} EXCLUDE_FROM_ALL) | ||
endif() | ||
|
||
if (WIN32) | ||
set(SERIAL_IMPL src/serial_win.cpp) | ||
add_library(serial_impl OBJECT src/serial_win.cpp) | ||
elseif(APPLE) | ||
add_library(serial_impl OBJECT src/serial_mac.cpp) | ||
elseif(UNIX) | ||
set(SERIAL_IMPL src/serial_linux.cpp) | ||
add_library(serial_impl OBJECT src/serial_linux.cpp) | ||
target_link_libraries(serial_impl PRIVATE udev) | ||
else() | ||
message(FATAL "There is no implementation of serial for that platform") | ||
message(FATAL "There is no implementation of serial for that platform") | ||
endif() | ||
|
||
if ( | ||
CMAKE_CXX_COMPILER_ID MATCHES "(Clang|GNU)" | ||
) | ||
add_compile_options(-Wall -Wextra -pedantic) | ||
elseif (CMAKE_CXX_COMPILER_ID MATCHES "MSVC") | ||
# using Visual Studio C++ | ||
endif() | ||
|
||
add_executable( | ||
pilkki | ||
src/pilkki.cpp | ||
${SERIAL_IMPL} | ||
pilkki | ||
src/pilkki.cpp | ||
) | ||
|
||
target_link_libraries(pilkki PRIVATE udev) | ||
target_link_libraries(pilkki PRIVATE serial_impl argparse magic_enum::magic_enum) | ||
|
||
install(TARGETS pilkki DESTINATION bin) | ||
install(TARGETS pilkki DESTINATION bin) |
Submodule argparse
deleted from
977062
Submodule magic_enum
deleted from
490482
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
|
||
#include "serial.hpp" | ||
|
||
#include <cstring> | ||
#include <unistd.h> | ||
#include <fcntl.h> | ||
#include <errno.h> | ||
#include <termios.h> | ||
|
||
#include <iostream> | ||
#include <string_view> | ||
|
||
#include <stdexcept> | ||
|
||
struct Serial::Impl_ { | ||
int fd; | ||
|
||
Impl_(int descriptor): fd(descriptor) { | ||
if (fd<=0) { | ||
throw std::runtime_error("Failed to open Serial"); | ||
} | ||
} | ||
|
||
Impl_(const Impl_&) = delete; | ||
|
||
Impl_(Impl_&& impl): fd(impl.fd) { | ||
impl.fd = 0; | ||
} | ||
|
||
~Impl_() { | ||
if (fd>0) { | ||
::close(fd); | ||
} | ||
|
||
} | ||
}; | ||
|
||
Serial::Serial(const std::string& name): | ||
impl_(new Impl_(::open(name.c_str(), O_RDWR | O_NOCTTY))) | ||
{ | ||
auto fd = impl_->fd; | ||
|
||
struct termios tty; | ||
memset (&tty, 0, sizeof(tty)); | ||
|
||
/* Error Handling */ | ||
if ( tcgetattr ( fd, &tty ) != 0 ) { | ||
throw std::runtime_error("Failed to tcgetattr: " + std::string(strerror(errno))); | ||
} | ||
|
||
// /* Set Baud Rate */ | ||
// cfsetospeed (&tty, (speed_t)B9600); | ||
// cfsetispeed (&tty, (speed_t)B9600); | ||
|
||
/* Setting other Port Stuff */ | ||
tty.c_cflag &= ~PARENB; // Make 8n1 | ||
tty.c_cflag &= ~CSTOPB; | ||
tty.c_cflag &= ~CSIZE; | ||
tty.c_cflag |= CS8; | ||
|
||
tty.c_cflag &= ~CRTSCTS; // no flow control | ||
tty.c_cc[VMIN] = 1; // read doesn't block | ||
tty.c_cc[VTIME] = 5; // 0.5 seconds read timeout | ||
tty.c_cflag |= CREAD | CLOCAL; // turn on READ & ignore ctrl lines | ||
|
||
// /* Make raw */ | ||
cfmakeraw(&tty); | ||
|
||
// /* Flush Port, then applies attributes */ | ||
tcflush( fd, TCIFLUSH ); | ||
if ( tcsetattr ( fd, TCSANOW, &tty ) != 0) { | ||
throw std::runtime_error("Failed to tcsetattr: " + std::string(strerror(errno))); | ||
} | ||
} | ||
|
||
Serial::Serial(Serial&& s): impl_(std::move(s.impl_)) { | ||
s.impl_ = nullptr; | ||
} | ||
Serial::~Serial() {} | ||
|
||
void Serial::write(const void * data, size_t len) { | ||
size_t n = len; | ||
while(n > 0) { | ||
int res = ::write(impl_->fd, data, n); | ||
if ( res<0 ) { | ||
throw std::runtime_error("Failed to write: " + std::string(strerror(errno))); | ||
} | ||
if (!res) { | ||
throw std::runtime_error("Failed to write: wrote nothing this time"); | ||
} | ||
n -= res; | ||
} | ||
} | ||
|
||
size_t Serial::read(void * data, size_t maxLen) { | ||
int res = ::read(impl_->fd, data, maxLen); | ||
if ( res<0 ) { | ||
throw std::runtime_error("Failed to read: " + std::string(strerror(errno))); | ||
} | ||
return res; | ||
} | ||
|
||
std::optional<std::string> Serial::autodetect() { | ||
// TODO: implement autodetect | ||
return std::nullopt; | ||
} |
Oops, something went wrong.