]> code.bitgloo.com Git - clyne/stmdsp.git/commitdiff
using serial lib, can connect to device
authorClyne Sullivan <clyne@bitgloo.com>
Thu, 18 Jun 2020 23:35:11 +0000 (19:35 -0400)
committerClyne Sullivan <clyne@bitgloo.com>
Thu, 18 Jun 2020 23:35:11 +0000 (19:35 -0400)
.gitignore
.gitmodules [new file with mode: 0644]
gui/Makefile
gui/serial [new submodule]
gui/stmdsp.cpp
gui/stmdsp.hpp
gui/wxmain.hpp
source/main.cpp

index 804a3826c6254e63241c2e23d82d01d4fd3897e9..235fb5126622c5e411bb802caf75d2e77ce86d85 100644 (file)
@@ -2,4 +2,5 @@ build
 ChibiOS_*
 **/.*
 gui/stmdspgui
+*.o
 
diff --git a/.gitmodules b/.gitmodules
new file mode 100644 (file)
index 0000000..0c9d779
--- /dev/null
@@ -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
index a53d4129e71f84eef8a848b37387a141d7434dc1..99a02434e19da34bdb6781f786ed473b9e15fb01 100644 (file)
@@ -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 (submodule)
index 0000000..cbcca7c
--- /dev/null
@@ -0,0 +1 @@
+Subproject commit cbcca7c83745fedd75afb7a0a27ee5c4112435c2
index 267bebc3dcd71d6286d30f77fa178e4f1df49396..54452645a034c29b0c000b30172f22c7b3e10f5a 100644 (file)
@@ -1,31 +1,17 @@
 #include "stmdsp.hpp"
 
-#include <chrono>
-#include <filesystem>
-#include <thread>
-
-using namespace std::chrono_literals;
+#include <serial/serial.h>
 
 namespace stmdsp
 {
-    void scanner::scan()
+    std::list<std::string>& 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;
     }
 }
index 99a3bd33b96a84a01a767b47cb5f3c86f925bb3f..7695abe57e7507bfec8a8a08bdb16c54cab53b8d 100644 (file)
@@ -2,58 +2,24 @@
 #define STMDSP_HPP_
 
 #include <fstream>
-#include <set>
+#include <list>
 #include <string>
 
 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<std::string>& scan();
+        auto& devices() {
             return m_devices;
         }
 
     private:
-        std::set<device> m_devices;
+        std::list<std::string> m_devices;
     };
 }
 
index 7832bdb52ebb55c2f6bf57b1acbfa9428aecf401..f23ae384bedbe6558a9bf341b6e3ef31fbe159f5 100644 (file)
@@ -1,7 +1,10 @@
 #ifndef WXMAIN_HPP_
 #define WXMAIN_HPP_
 
+#include "stmdsp.hpp"
+
 #include <wx/button.h>
+#include <wx/combobox.h>
 #include <wx/dcclient.h>
 #include <wx/frame.h>
 #include <wx/stattext.h>
@@ -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);
index b8d2fa148d0f847dca09f5ecc5d19654d56846cd..dc38ddd1f510c379b0e520548be83e68a80c0af6 100644 (file)
@@ -18,6 +18,8 @@
 \r
 #include <array>\r
 \r
+static_assert(sizeof(adcsample_t) == sizeof(uint16_t));\r
+\r
 #if CACHE_LINE_SIZE > 0\r
 CC_ALIGN(CACHE_LINE_SIZE)\r
 #endif\r
@@ -50,7 +52,7 @@ int main()
             if (char cmd; usbd.read(&cmd) > 0) {\r
                 switch (cmd) {\r
                 case 'r': // Read in analog signal\r
-                    adc.getSamples(&adc_samples[0], adc_samples.size());\r
+                    adc.getSamples(&adc_samples[0], 100);//adc_samples.size());\r
                     usbd.write(adc_samples.data(), adc_samples.size());\r
                     break;\r
                 case 'i': // Identify ourself as an stmdsp device\r