From 6473b57cefe4c13cb262ed78bf0e49fe18a20267 Mon Sep 17 00:00:00 2001 From: Clyne Sullivan Date: Thu, 18 Jun 2020 19:35:11 -0400 Subject: [PATCH] using serial lib, can connect to device --- .gitignore | 1 + .gitmodules | 6 ++++++ gui/Makefile | 22 +++++++++++++++++----- gui/serial | 1 + gui/stmdsp.cpp | 30 ++++++++---------------------- gui/stmdsp.hpp | 44 +++++--------------------------------------- gui/wxmain.hpp | 12 ++++++++++++ source/main.cpp | 4 +++- 8 files changed, 53 insertions(+), 67 deletions(-) create mode 100644 .gitmodules create mode 160000 gui/serial diff --git a/.gitignore b/.gitignore index 804a382..235fb51 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,5 @@ build ChibiOS_* **/.* gui/stmdspgui +*.o diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..0c9d779 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,6 @@ +[submodule "gui/gui/serial"] + path = gui/gui/serial + url = https://github.com/wjwwood/serial +[submodule "gui/serial"] + path = gui/serial + url = https://github.com/wjwwood/serial diff --git a/gui/Makefile b/gui/Makefile index a53d412..99a0243 100644 --- a/gui/Makefile +++ b/gui/Makefile @@ -2,18 +2,30 @@ CXX = g++-10 CXXFLAGS = --std=c++20 -ggdb -Og \ -Wall -Wextra -pedantic \ -Wno-deprecated-copy \ + -Iserial/include \ $(shell wx-config --cxxflags) -CXXFILES = $(wildcard *.cpp) +CXXFILES = $(shell find serial/src -name "*.cc") $(wildcard *.cpp) +OFILES = $(patsubst %.cc, %.o, $(patsubst %.cpp, %.o, $(CXXFILES))) LIBS = $(shell wx-config --libs) OUTELF = stmdspgui -all: $(CXXFILES) - @echo " CXX " $(CXXFILES) - @$(CXX) $(CXXFLAGS) $(CXXFILES) $(LIBS) -o $(OUTELF) +all: $(OUTELF) + +$(OUTELF): $(OFILES) + @echo " CXX " $(OUTELF) + @$(CXX) $(CXXFLAGS) $(OFILES) $(LIBS) -o $(OUTELF) + +.cc.o: + @echo " CXX " $< + @$(CXX) $(CXXFLAGS) -c $< -o $@ + +.cpp.o: + @echo " CXX " $< + @$(CXX) $(CXXFLAGS) -c $< -o $@ clean: @echo " CLEAN" - @rm -f $(OUTELF) + @rm -f $(OUTELF) $(OFILES) diff --git a/gui/serial b/gui/serial new file mode 160000 index 0000000..cbcca7c --- /dev/null +++ b/gui/serial @@ -0,0 +1 @@ +Subproject commit cbcca7c83745fedd75afb7a0a27ee5c4112435c2 diff --git a/gui/stmdsp.cpp b/gui/stmdsp.cpp index 267bebc..5445264 100644 --- a/gui/stmdsp.cpp +++ b/gui/stmdsp.cpp @@ -1,31 +1,17 @@ #include "stmdsp.hpp" -#include -#include -#include - -using namespace std::chrono_literals; +#include namespace stmdsp { - void scanner::scan() + std::list& 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)); - } - } - } + auto serialDevices = serial::list_ports(); + for (auto& device : serialDevices) { + if (device.hardware_id.find(STMDSP_USB_ID) != std::string::npos) + m_devices.emplace_front(device.port); } + + return m_devices; } } diff --git a/gui/stmdsp.hpp b/gui/stmdsp.hpp index 99a3bd3..7695abe 100644 --- a/gui/stmdsp.hpp +++ b/gui/stmdsp.hpp @@ -2,58 +2,24 @@ #define STMDSP_HPP_ #include -#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; + constexpr static const char *STMDSP_USB_ID = "USB VID:PID=0483:5740"; public: - void scan(); - const auto& devices() const { + std::list& scan(); + auto& devices() { return m_devices; } private: - std::set m_devices; + std::list m_devices; }; } diff --git a/gui/wxmain.hpp b/gui/wxmain.hpp index 7832bdb..f23ae38 100644 --- a/gui/wxmain.hpp +++ b/gui/wxmain.hpp @@ -1,7 +1,10 @@ #ifndef WXMAIN_HPP_ #define WXMAIN_HPP_ +#include "stmdsp.hpp" + #include +#include #include #include #include @@ -12,6 +15,7 @@ class MainFrame : public wxFrame enum Id { Welcome = 1, Single, + SelectDevice, RenderTimer }; @@ -26,6 +30,14 @@ public: { new wxStaticText(this, Id::Welcome, "Welcome to the GUI.", wxPoint(20, 20)); new wxButton(this, Id::Single, "Single", wxPoint(20, 60)); + auto combo = new wxComboBox(this, Id::SelectDevice, "", wxPoint(470, 20), wxSize(150, 30)); + combo->SetEditable(false); + stmdsp::scanner scanner; + for (auto& dev : scanner.scan()) + combo->Append(dev); + if (combo->GetCount() > 0) + combo->SetSelection(0); + m_render_timer = new wxTimer(this, Id::RenderTimer); Bind(wxEVT_BUTTON, &MainFrame::onSinglePressed, this, Id::Single); diff --git a/source/main.cpp b/source/main.cpp index b8d2fa1..dc38ddd 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -18,6 +18,8 @@ #include +static_assert(sizeof(adcsample_t) == sizeof(uint16_t)); + #if CACHE_LINE_SIZE > 0 CC_ALIGN(CACHE_LINE_SIZE) #endif @@ -50,7 +52,7 @@ int main() if (char cmd; usbd.read(&cmd) > 0) { switch (cmd) { case 'r': // Read in analog signal - adc.getSamples(&adc_samples[0], adc_samples.size()); + adc.getSamples(&adc_samples[0], 100);//adc_samples.size()); usbd.write(adc_samples.data(), adc_samples.size()); break; case 'i': // Identify ourself as an stmdsp device