diff --git a/gui/Makefile b/gui/Makefile new file mode 100644 index 0000000..419ed33 --- /dev/null +++ b/gui/Makefile @@ -0,0 +1,15 @@ +CXX = clang++-10 +CXXFLAGS = --std=c++20 -Wall -Wextra -pedantic + +CXXFILES = $(wildcard *.cpp) + +OUTELF = stmdspgui + +all: $(CXXFILES) + @echo " CXX " $(CXXFILES) + @$(CXX) $(CXXFLAGS) $(CXXFILES) -o $(OUTELF) + +clean: + @echo " CLEAN" + @rm -f $(OUTELF) + diff --git a/gui/main.cpp b/gui/main.cpp new file mode 100644 index 0000000..48f2990 --- /dev/null +++ b/gui/main.cpp @@ -0,0 +1,15 @@ +#include "stmdsp.hpp" + +#include + +int main() +{ + stmdsp::scanner scanner; + + scanner.scan(); + for (const auto& device : scanner.devices()) + std::cout << "Found stmdsp at: " << device.path() << std::endl; + + return 0; +} + diff --git a/gui/stmdsp.cpp b/gui/stmdsp.cpp new file mode 100644 index 0000000..267bebc --- /dev/null +++ b/gui/stmdsp.cpp @@ -0,0 +1,31 @@ +#include "stmdsp.hpp" + +#include +#include +#include + +using namespace std::chrono_literals; + +namespace stmdsp +{ + void scanner::scan() + { + std::string path ("/dev/ttyACM0"); + + for (unsigned int i = 0; i < 10; i++) { + path.back() = '0' + i; + if (std::filesystem::exists(path)) { + if (device dev (path); dev.open()) { + dev.write("i", 1); + std::this_thread::sleep_for(1s); + char buf[7]; + if (dev.read(buf, 6) == 6) { + buf[6] = '\0'; + if (std::string(buf) == "stmdsp") + m_devices.emplace(std::move(dev)); + } + } + } + } + } +} diff --git a/gui/stmdsp.hpp b/gui/stmdsp.hpp new file mode 100644 index 0000000..12e4578 --- /dev/null +++ b/gui/stmdsp.hpp @@ -0,0 +1,60 @@ +#ifndef STMDSPSCANNER_H +#define STMDSPSCANNER_H + +#include +#include +#include + +namespace stmdsp +{ + class device + { + public: + device(const std::string& path) : + m_path(path) {} + + bool open() { + m_stream.open(m_path, std::ios_base::in | std::ios_base::out | std::ios_base::binary); + return m_stream.is_open(); + } + + std::size_t read(char *buffer, std::size_t count) { + return m_stream.readsome(buffer, count); + } + + std::size_t write(const char *buffer, std::size_t count) { + m_stream.write(buffer, count); + return m_stream.good() ? count : 0; + } + + const std::string& path() const { + return m_path; + } + + auto operator<=>(const device& other) const { + return m_path <=> other.m_path; + } + + private: + std::string m_path; + std::fstream m_stream; + }; + + class scanner + { + private: + constexpr static unsigned int STMDSP_VENDOR_ID = 0x0483; + constexpr static unsigned int STMDSP_DEVICE_ID = 0x5740; + + public: + void scan(); + const auto& devices() const { + return m_devices; + } + + private: + std::set m_devices; + }; +} + +#endif // STMDSPSCANNER_H