using serial lib, can connect to device

pull/1/head
Clyne 4 years ago
parent 65b755658c
commit 6473b57cef

1
.gitignore vendored

@ -2,4 +2,5 @@ build
ChibiOS_*
**/.*
gui/stmdspgui
*.o

6
.gitmodules vendored

@ -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

@ -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)

@ -0,0 +1 @@
Subproject commit cbcca7c83745fedd75afb7a0a27ee5c4112435c2

@ -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;
}
}

@ -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;
};
}

@ -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);

@ -18,6 +18,8 @@
#include <array>
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

Loading…
Cancel
Save