@ -1,4 +1,7 @@
|
|||||||
build
|
build
|
||||||
ChibiOS_*
|
ChibiOS_*
|
||||||
**/.*
|
**/.*
|
||||||
|
gui/stmdspgui
|
||||||
|
*.o
|
||||||
|
perf*
|
||||||
|
|
||||||
|
@ -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
|
@ -1,15 +1,31 @@
|
|||||||
CXX = clang++-10
|
CXX = g++-10
|
||||||
CXXFLAGS = --std=c++20 -Wall -Wextra -pedantic
|
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
|
OUTELF = stmdspgui
|
||||||
|
|
||||||
all: $(CXXFILES)
|
all: $(OUTELF)
|
||||||
@echo " CXX " $(CXXFILES)
|
|
||||||
@$(CXX) $(CXXFLAGS) $(CXXFILES) -o $(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:
|
clean:
|
||||||
@echo " CLEAN"
|
@echo " CLEAN"
|
||||||
@rm -f $(OUTELF)
|
@rm -f $(OUTELF) $(OFILES)
|
||||||
|
|
||||||
|
@ -1,15 +1,6 @@
|
|||||||
#include "stmdsp.hpp"
|
#include "wxapp.hpp"
|
||||||
|
|
||||||
#include <iostream>
|
#include <wx/app.h>
|
||||||
|
|
||||||
int main()
|
wxIMPLEMENT_APP(MainApp);
|
||||||
{
|
|
||||||
stmdsp::scanner scanner;
|
|
||||||
|
|
||||||
scanner.scan();
|
|
||||||
for (const auto& device : scanner.devices())
|
|
||||||
std::cout << "Found stmdsp at: " << device.path() << std::endl;
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
@ -0,0 +1 @@
|
|||||||
|
Subproject commit cbcca7c83745fedd75afb7a0a27ee5c4112435c2
|
@ -1,31 +1,37 @@
|
|||||||
#include "stmdsp.hpp"
|
#include "stmdsp.hpp"
|
||||||
|
|
||||||
#include <chrono>
|
#include <serial/serial.h>
|
||||||
#include <filesystem>
|
|
||||||
#include <thread>
|
|
||||||
|
|
||||||
using namespace std::chrono_literals;
|
|
||||||
|
|
||||||
namespace stmdsp
|
namespace stmdsp
|
||||||
{
|
{
|
||||||
void scanner::scan()
|
std::list<std::string>& scanner::scan()
|
||||||
{
|
{
|
||||||
std::string path ("/dev/ttyACM0");
|
auto devices = serial::list_ports();
|
||||||
|
for (auto& device : devices) {
|
||||||
|
if (device.hardware_id.find(STMDSP_USB_ID) != std::string::npos)
|
||||||
|
m_available_devices.emplace_front(device.port);
|
||||||
|
}
|
||||||
|
|
||||||
|
return m_available_devices;
|
||||||
|
}
|
||||||
|
|
||||||
|
device::device(const std::string& file) :
|
||||||
|
m_serial(file, 230400, serial::Timeout::simpleTimeout(50)) {}
|
||||||
|
|
||||||
for (unsigned int i = 0; i < 10; i++) {
|
std::vector<adcsample_t> device::sample(unsigned long int count) {
|
||||||
path.back() = '0' + i;
|
if (connected()) {
|
||||||
if (std::filesystem::exists(path)) {
|
uint8_t request[3] = {
|
||||||
if (device dev (path); dev.open()) {
|
'r',
|
||||||
dev.write("i", 1);
|
static_cast<uint8_t>(count),
|
||||||
std::this_thread::sleep_for(1s);
|
static_cast<uint8_t>(count >> 8)
|
||||||
char buf[7];
|
};
|
||||||
if (dev.read(buf, 6) == 6) {
|
m_serial.write(request, 3);
|
||||||
buf[6] = '\0';
|
std::vector<adcsample_t> data (count);
|
||||||
if (std::string(buf) == "stmdsp")
|
m_serial.read(reinterpret_cast<uint8_t *>(data.data()),
|
||||||
m_devices.emplace(std::move(dev));
|
data.size() * sizeof(adcsample_t));
|
||||||
}
|
return data;
|
||||||
}
|
} else {
|
||||||
}
|
return {};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,60 +1,49 @@
|
|||||||
#ifndef STMDSPSCANNER_H
|
#ifndef STMDSP_HPP_
|
||||||
#define STMDSPSCANNER_H
|
#define STMDSP_HPP_
|
||||||
|
|
||||||
#include <fstream>
|
#include <cstdint>
|
||||||
#include <set>
|
#include <list>
|
||||||
|
#include <serial/serial.h>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
namespace stmdsp
|
namespace stmdsp
|
||||||
{
|
{
|
||||||
class device
|
class scanner
|
||||||
{
|
{
|
||||||
public:
|
private:
|
||||||
device(const std::string& path) :
|
constexpr static const char *STMDSP_USB_ID = "USB VID:PID=0483:5740";
|
||||||
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 {
|
public:
|
||||||
return m_path <=> other.m_path;
|
std::list<std::string>& scan();
|
||||||
|
auto& devices() {
|
||||||
|
return m_available_devices;
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::string m_path;
|
std::list<std::string> m_available_devices;
|
||||||
std::fstream m_stream;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
class scanner
|
using adcsample_t = uint16_t;
|
||||||
{
|
|
||||||
private:
|
|
||||||
constexpr static unsigned int STMDSP_VENDOR_ID = 0x0483;
|
|
||||||
constexpr static unsigned int STMDSP_DEVICE_ID = 0x5740;
|
|
||||||
|
|
||||||
|
class device
|
||||||
|
{
|
||||||
public:
|
public:
|
||||||
void scan();
|
device(const std::string& file);
|
||||||
const auto& devices() const {
|
|
||||||
return m_devices;
|
~device() {
|
||||||
|
m_serial.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
bool connected() {
|
||||||
|
return m_serial.isOpen() && (m_serial.write("i"), m_serial.read(6) == "stmdsp");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::vector<adcsample_t> sample(unsigned long int count = 1);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::set<device> m_devices;
|
serial::Serial m_serial;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif // STMDSPSCANNER_H
|
#endif // STMDSP_HPP_
|
||||||
|
|
||||||
|
@ -0,0 +1,20 @@
|
|||||||
|
#ifndef WXAPP_HPP_
|
||||||
|
#define WXAPP_HPP_
|
||||||
|
|
||||||
|
#include "wxmain.hpp"
|
||||||
|
|
||||||
|
#include <wx/app.h>
|
||||||
|
|
||||||
|
class MainApp : public wxApp
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
virtual bool OnInit() final {
|
||||||
|
auto mainFrame = new MainFrame;
|
||||||
|
mainFrame->Show(true);
|
||||||
|
SetTopWindow(mainFrame);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // WXAPP_HPP_
|
||||||
|
|
@ -0,0 +1,119 @@
|
|||||||
|
#ifndef WXMAIN_HPP_
|
||||||
|
#define WXMAIN_HPP_
|
||||||
|
|
||||||
|
#include "stmdsp.hpp"
|
||||||
|
|
||||||
|
#include <future>
|
||||||
|
#include <thread>
|
||||||
|
#include <wx/button.h>
|
||||||
|
#include <wx/combobox.h>
|
||||||
|
#include <wx/dcclient.h>
|
||||||
|
#include <wx/frame.h>
|
||||||
|
#include <wx/stattext.h>
|
||||||
|
#include <wx/timer.h>
|
||||||
|
|
||||||
|
class MainFrame : public wxFrame
|
||||||
|
{
|
||||||
|
enum Id {
|
||||||
|
Welcome = 1,
|
||||||
|
Single,
|
||||||
|
SelectDevice,
|
||||||
|
RenderTimer
|
||||||
|
};
|
||||||
|
|
||||||
|
bool m_is_rendering = false;
|
||||||
|
wxTimer *m_render_timer = nullptr;
|
||||||
|
wxComboBox *m_device_combo = nullptr;
|
||||||
|
|
||||||
|
const wxRect m_clipping_region = {20, 100, 600, 360};
|
||||||
|
|
||||||
|
stmdsp::device *m_device = nullptr;
|
||||||
|
std::future<std::vector<stmdsp::adcsample_t>> m_device_samples_future;
|
||||||
|
std::vector<stmdsp::adcsample_t> m_device_samples;
|
||||||
|
|
||||||
|
public:
|
||||||
|
MainFrame() : wxFrame(nullptr, -1, "Hello world", wxPoint(50, 50), wxSize(640, 480))
|
||||||
|
{
|
||||||
|
new wxStaticText(this, Id::Welcome, "Welcome to the GUI.", wxPoint(20, 20));
|
||||||
|
new wxButton(this, Id::Single, "Single", wxPoint(20, 60));
|
||||||
|
m_device_combo = new wxComboBox(this, Id::SelectDevice, "", wxPoint(470, 20), wxSize(150, 30));
|
||||||
|
m_device_combo->SetEditable(false);
|
||||||
|
stmdsp::scanner scanner;
|
||||||
|
for (auto& dev : scanner.scan())
|
||||||
|
m_device_combo->Append(dev);
|
||||||
|
if (m_device_combo->GetCount() > 0)
|
||||||
|
m_device_combo->SetSelection(0);
|
||||||
|
|
||||||
|
m_render_timer = new wxTimer(this, Id::RenderTimer);
|
||||||
|
|
||||||
|
Bind(wxEVT_BUTTON, &MainFrame::onSinglePressed, this, Id::Single);
|
||||||
|
Bind(wxEVT_PAINT, &MainFrame::onPaint, this, wxID_ANY);
|
||||||
|
Bind(wxEVT_TIMER, &MainFrame::onRenderTimer, this, Id::RenderTimer);
|
||||||
|
}
|
||||||
|
|
||||||
|
void onPaint([[maybe_unused]] wxPaintEvent& pe) {
|
||||||
|
auto *dc = new wxClientDC(this);
|
||||||
|
dc->SetClippingRegion(m_clipping_region);
|
||||||
|
|
||||||
|
dc->SetBrush(*wxBLACK_BRUSH);
|
||||||
|
dc->SetPen(*wxBLACK_PEN);
|
||||||
|
dc->DrawRectangle(m_clipping_region);
|
||||||
|
|
||||||
|
if (m_device_samples.size() > 0) {
|
||||||
|
dc->SetPen(*wxRED_PEN);
|
||||||
|
auto points = new wxPoint[m_device_samples.size()];
|
||||||
|
const float spacing = static_cast<float>(m_clipping_region.GetWidth()) / m_device_samples.size();
|
||||||
|
float x = 0;
|
||||||
|
for (auto ptr = points; auto sample : m_device_samples) {
|
||||||
|
*ptr++ = wxPoint {
|
||||||
|
static_cast<int>(x),
|
||||||
|
m_clipping_region.GetHeight() - sample * m_clipping_region.GetHeight() / 4096
|
||||||
|
};
|
||||||
|
x += spacing;
|
||||||
|
}
|
||||||
|
dc->DrawLines(m_device_samples.size(), points, m_clipping_region.GetX(), m_clipping_region.GetY());
|
||||||
|
delete[] points;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void doSingle() {
|
||||||
|
m_device_samples_future = std::async(std::launch::async,
|
||||||
|
[this]() { return m_device->sample(250); });
|
||||||
|
}
|
||||||
|
|
||||||
|
void onSinglePressed(wxCommandEvent& ce) {
|
||||||
|
auto button = dynamic_cast<wxButton *>(ce.GetEventObject());
|
||||||
|
|
||||||
|
if (!m_render_timer->IsRunning()) {
|
||||||
|
m_device = new stmdsp::device(m_device_combo->GetStringSelection().ToStdString());
|
||||||
|
if (m_device->connected()) {
|
||||||
|
doSingle();
|
||||||
|
m_render_timer->Start(100);
|
||||||
|
button->SetLabel("Stop");
|
||||||
|
} else {
|
||||||
|
delete m_device;
|
||||||
|
m_device = nullptr;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
m_render_timer->Stop();
|
||||||
|
button->SetLabel("Single");
|
||||||
|
|
||||||
|
delete m_device;
|
||||||
|
m_device = nullptr;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void onRenderTimer([[maybe_unused]] wxTimerEvent& te) {
|
||||||
|
updateDrawing();
|
||||||
|
}
|
||||||
|
|
||||||
|
void updateDrawing() {
|
||||||
|
if (m_device_samples = m_device_samples_future.get(); m_device_samples.size() > 0)
|
||||||
|
this->RefreshRect(m_clipping_region);
|
||||||
|
|
||||||
|
doSingle();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // WXMAIN_HPP_
|
||||||
|
|
@ -1,68 +1,74 @@
|
|||||||
/**
|
/**
|
||||||
* @file main.cpp
|
* @file main.cpp
|
||||||
* @brief Program entry point.
|
* @brief Program entry point.
|
||||||
*
|
*
|
||||||
* Copyright (C) 2020 Clyne Sullivan
|
* Copyright (C) 2020 Clyne Sullivan
|
||||||
*
|
*
|
||||||
* Distributed under the GNU GPL v3 or later. You should have received a copy of
|
* Distributed under the GNU GPL v3 or later. You should have received a copy of
|
||||||
* the GNU General Public License along with this program.
|
* the GNU General Public License along with this program.
|
||||||
* If not, see <https://www.gnu.org/licenses/>.
|
* If not, see <https://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "ch.h"
|
#include "ch.h"
|
||||||
#include "hal.h"
|
#include "hal.h"
|
||||||
|
|
||||||
#include "adc.hpp"
|
#include "adc.hpp"
|
||||||
#include "dac.hpp"
|
#include "dac.hpp"
|
||||||
#include "usbserial.hpp"
|
#include "usbserial.hpp"
|
||||||
|
|
||||||
#include <array>
|
#include <array>
|
||||||
|
|
||||||
#if CACHE_LINE_SIZE > 0
|
static_assert(sizeof(adcsample_t) == sizeof(uint16_t));
|
||||||
CC_ALIGN(CACHE_LINE_SIZE)
|
|
||||||
#endif
|
#if CACHE_LINE_SIZE > 0
|
||||||
static std::array<adcsample_t, CACHE_SIZE_ALIGN(adcsample_t, 100)> adc_samples;
|
CC_ALIGN(CACHE_LINE_SIZE)
|
||||||
|
#endif
|
||||||
int main()
|
static std::array<adcsample_t, CACHE_SIZE_ALIGN(adcsample_t, 2048)> adc_samples;
|
||||||
{
|
|
||||||
halInit();
|
int main()
|
||||||
chSysInit();
|
{
|
||||||
|
halInit();
|
||||||
palSetPadMode(GPIOA, 5, PAL_MODE_OUTPUT_PUSHPULL); // LED
|
chSysInit();
|
||||||
|
|
||||||
ADCd adc (ADCD1, GPTD4);
|
palSetPadMode(GPIOA, 5, PAL_MODE_OUTPUT_PUSHPULL); // LED
|
||||||
adc.start();
|
|
||||||
|
ADCd adc (ADCD1, GPTD4);
|
||||||
//DACd dac (DACD1, {
|
adc.start();
|
||||||
// .init = 0,
|
|
||||||
// .datamode = DAC_DHRM_12BIT_RIGHT,
|
//DACd dac (DACD1, {
|
||||||
// .cr = 0
|
// .init = 0,
|
||||||
//});
|
// .datamode = DAC_DHRM_12BIT_RIGHT,
|
||||||
//dac.start();
|
// .cr = 0
|
||||||
//dac.write(0, 1024);
|
//});
|
||||||
|
//dac.start();
|
||||||
USBSeriald usbd (SDU1);
|
//dac.write(0, 1024);
|
||||||
usbd.start();
|
|
||||||
|
USBSeriald usbd (SDU1);
|
||||||
while (true) {
|
usbd.start();
|
||||||
if (usbd.active()) {
|
|
||||||
// Expect to receive a byte command 'packet'.
|
while (true) {
|
||||||
if (char cmd; usbd.read(&cmd) > 0) {
|
if (usbd.active()) {
|
||||||
switch (cmd) {
|
// Expect to receive a byte command 'packet'.
|
||||||
case 'r': // Read in analog signal
|
if (char cmd[3]; usbd.read(&cmd, 1) > 0) {
|
||||||
adc.getSamples(&adc_samples[0], adc_samples.size());
|
switch (cmd[0]) {
|
||||||
usbd.write(adc_samples.data(), adc_samples.size());
|
case 'r': // Read in analog signal
|
||||||
break;
|
if (usbd.read(&cmd[1], 2) < 2)
|
||||||
case 'i': // Identify ourself as an stmdsp device
|
break;
|
||||||
usbd.write("stmdsp", 6);
|
if (auto count = std::min(static_cast<unsigned int>(cmd[1] | (cmd[2] << 8)), adc_samples.size()); count > 0) {
|
||||||
break;
|
adc.getSamples(&adc_samples[0], count);
|
||||||
default:
|
usbd.write(adc_samples.data(), count * sizeof(adcsample_t));
|
||||||
break;
|
}
|
||||||
}
|
break;
|
||||||
}
|
case 'i': // Identify ourself as an stmdsp device
|
||||||
}
|
usbd.write("stmdsp", 6);
|
||||||
|
break;
|
||||||
chThdSleepMilliseconds(250);
|
default:
|
||||||
}
|
break;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
chThdSleepMilliseconds(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue