GUI reads ADC channel

pull/1/head
Clyne 4 years ago
parent da8d7ffe8a
commit 875b6a0620

1
.gitignore vendored

@ -3,4 +3,5 @@ ChibiOS_*
**/.* **/.*
gui/stmdspgui gui/stmdspgui
*.o *.o
perf*

@ -14,4 +14,24 @@ namespace stmdsp
return m_available_devices; return m_available_devices;
} }
device::device(const std::string& file) :
m_serial(file, 230400, serial::Timeout::simpleTimeout(50)) {}
std::vector<adcsample_t> device::sample(unsigned long int count) {
if (connected()) {
uint8_t request[3] = {
'r',
static_cast<uint8_t>(count),
static_cast<uint8_t>(count >> 8)
};
m_serial.write(request, 3);
std::vector<adcsample_t> data (count);
m_serial.read(reinterpret_cast<uint8_t *>(data.data()),
data.size() * sizeof(adcsample_t));
return data;
} else {
return {};
}
}
} }

@ -28,8 +28,7 @@ namespace stmdsp
class device class device
{ {
public: public:
device(const std::string& file) : device(const std::string& file);
m_serial(file, 115200, serial::Timeout::simpleTimeout(1000)) {}
~device() { ~device() {
m_serial.close(); m_serial.close();
@ -39,19 +38,7 @@ namespace stmdsp
return m_serial.isOpen() && (m_serial.write("i"), m_serial.read(6) == "stmdsp"); return m_serial.isOpen() && (m_serial.write("i"), m_serial.read(6) == "stmdsp");
} }
std::vector<adcsample_t> sample(unsigned long int count = 1) { std::vector<adcsample_t> sample(unsigned long int count = 1);
if (connected()) {
m_serial.write(std::vector<uint8_t> {'r',
static_cast<uint8_t>(count),
static_cast<uint8_t>(count >> 8)});
std::vector<adcsample_t> data (count);
m_serial.read(reinterpret_cast<uint8_t *>(data.data()),
data.size() * sizeof(adcsample_t));
return data;
} else {
return {};
}
}
private: private:
serial::Serial m_serial; serial::Serial m_serial;

@ -3,6 +3,8 @@
#include "stmdsp.hpp" #include "stmdsp.hpp"
#include <future>
#include <thread>
#include <wx/button.h> #include <wx/button.h>
#include <wx/combobox.h> #include <wx/combobox.h>
#include <wx/dcclient.h> #include <wx/dcclient.h>
@ -21,22 +23,26 @@ class MainFrame : public wxFrame
bool m_is_rendering = false; bool m_is_rendering = false;
wxTimer *m_render_timer = nullptr; wxTimer *m_render_timer = nullptr;
int m_radius = 10; wxComboBox *m_device_combo = nullptr;
const wxRect m_clipping_region = {20, 100, 600, 360}; 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: public:
MainFrame() : wxFrame(nullptr, -1, "Hello world", wxPoint(50, 50), wxSize(640, 480)) 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 wxStaticText(this, Id::Welcome, "Welcome to the GUI.", wxPoint(20, 20));
new wxButton(this, Id::Single, "Single", wxPoint(20, 60)); new wxButton(this, Id::Single, "Single", wxPoint(20, 60));
auto combo = new wxComboBox(this, Id::SelectDevice, "", wxPoint(470, 20), wxSize(150, 30)); m_device_combo = new wxComboBox(this, Id::SelectDevice, "", wxPoint(470, 20), wxSize(150, 30));
combo->SetEditable(false); m_device_combo->SetEditable(false);
stmdsp::scanner scanner; stmdsp::scanner scanner;
for (auto& dev : scanner.scan()) for (auto& dev : scanner.scan())
combo->Append(dev); m_device_combo->Append(dev);
if (combo->GetCount() > 0) if (m_device_combo->GetCount() > 0)
combo->SetSelection(0); m_device_combo->SetSelection(0);
m_render_timer = new wxTimer(this, Id::RenderTimer); m_render_timer = new wxTimer(this, Id::RenderTimer);
@ -53,19 +59,47 @@ public:
dc->SetPen(*wxBLACK_PEN); dc->SetPen(*wxBLACK_PEN);
dc->DrawRectangle(m_clipping_region); dc->DrawRectangle(m_clipping_region);
if (m_device_samples.size() > 0) {
dc->SetPen(*wxRED_PEN); dc->SetPen(*wxRED_PEN);
dc->DrawCircle(320, 240, m_radius); 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) { void onSinglePressed(wxCommandEvent& ce) {
auto button = dynamic_cast<wxButton *>(ce.GetEventObject()); auto button = dynamic_cast<wxButton *>(ce.GetEventObject());
if (!m_render_timer->IsRunning()) { 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); m_render_timer->Start(100);
button->SetLabel("Stop"); button->SetLabel("Stop");
} else {
delete m_device;
m_device = nullptr;
}
} else { } else {
m_render_timer->Stop(); m_render_timer->Stop();
button->SetLabel("Single"); button->SetLabel("Single");
delete m_device;
m_device = nullptr;
} }
} }
@ -74,8 +108,10 @@ public:
} }
void updateDrawing() { void updateDrawing() {
m_radius++; if (m_device_samples = m_device_samples_future.get(); m_device_samples.size() > 0)
this->RefreshRect(m_clipping_region); this->RefreshRect(m_clipping_region);
doSingle();
} }
}; };

@ -23,7 +23,7 @@ static_assert(sizeof(adcsample_t) == sizeof(uint16_t));
#if CACHE_LINE_SIZE > 0 #if CACHE_LINE_SIZE > 0
CC_ALIGN(CACHE_LINE_SIZE) CC_ALIGN(CACHE_LINE_SIZE)
#endif #endif
static std::array<adcsample_t, CACHE_SIZE_ALIGN(adcsample_t, 100)> adc_samples; static std::array<adcsample_t, CACHE_SIZE_ALIGN(adcsample_t, 2048)> adc_samples;
int main() int main()
{ {
@ -49,11 +49,13 @@ int main()
while (true) { while (true) {
if (usbd.active()) { if (usbd.active()) {
// Expect to receive a byte command 'packet'. // Expect to receive a byte command 'packet'.
if (char cmd; usbd.read(&cmd) > 0) { if (char cmd[3]; usbd.read(&cmd, 3) > 0) {
switch (cmd) { switch (cmd[0]) {
case 'r': // Read in analog signal case 'r': // Read in analog signal
adc.getSamples(&adc_samples[0], 100);//adc_samples.size()); if (auto count = std::min(static_cast<unsigned int>(cmd[1] | (cmd[2] << 8)), adc_samples.size()); count > 0) {
usbd.write(adc_samples.data(), adc_samples.size()); adc.getSamples(&adc_samples[0], count);
usbd.write(adc_samples.data(), count * sizeof(adcsample_t));
}
break; break;
case 'i': // Identify ourself as an stmdsp device case 'i': // Identify ourself as an stmdsp device
usbd.write("stmdsp", 6); usbd.write("stmdsp", 6);
@ -64,7 +66,7 @@ int main()
} }
} }
chThdSleepMilliseconds(250); chThdSleepMilliseconds(1);
} }
} }

Loading…
Cancel
Save