working: continuous signal passthrough

pull/3/head
Clyne 4 years ago
parent 989f4038c6
commit b3eccbc1b9

@ -16,7 +16,14 @@ namespace stmdsp
} }
device::device(const std::string& file) : device::device(const std::string& file) :
m_serial(file, 230400, serial::Timeout::simpleTimeout(50)) {} m_serial(file, 230400, serial::Timeout::simpleTimeout(50))
{
if (m_serial.isOpen()) {
m_serial.write("i");
if (m_serial.read(6) != "stmdsp")
m_serial.close();
}
}
std::vector<adcsample_t> device::sample(unsigned long int count) { std::vector<adcsample_t> device::sample(unsigned long int count) {
if (connected()) { if (connected()) {
@ -27,11 +34,29 @@ namespace stmdsp
}; };
m_serial.write(request, 3); m_serial.write(request, 3);
std::vector<adcsample_t> data (count); std::vector<adcsample_t> data (count);
m_serial.read(reinterpret_cast<uint8_t *>(data.data()), m_serial.read(reinterpret_cast<uint8_t *>(data.data()), data.size() * sizeof(adcsample_t));
data.size() * sizeof(adcsample_t));
return data; return data;
} else { } else {
return {}; return {};
} }
} }
void device::continuous_start() {
if (connected())
m_serial.write("R");
}
std::vector<adcsample_t> device::continuous_read() {
if (connected()) {
m_serial.write("s");
std::vector<adcsample_t> data (2048);
m_serial.read(reinterpret_cast<uint8_t *>(data.data()), 2048 * sizeof(adcsample_t));
return data;
}
}
void device::continuous_stop() {
if (connected())
m_serial.write("S");
}
} }

@ -35,11 +35,15 @@ namespace stmdsp
} }
bool connected() { bool connected() {
return m_serial.isOpen() && (m_serial.write("i"), m_serial.read(6) == "stmdsp"); return m_serial.isOpen();
} }
std::vector<adcsample_t> sample(unsigned long int count = 1); std::vector<adcsample_t> sample(unsigned long int count = 1);
void continuous_start();
std::vector<adcsample_t> continuous_read();
void continuous_stop();
private: private:
serial::Serial m_serial; serial::Serial m_serial;
}; };

@ -78,7 +78,7 @@ public:
void doSingle() { void doSingle() {
m_device_samples_future = std::async(std::launch::async, m_device_samples_future = std::async(std::launch::async,
[this]() { return m_device->sample(250); }); [this]() { return m_device->continuous_read(); });
} }
void onSinglePressed(wxCommandEvent& ce) { void onSinglePressed(wxCommandEvent& ce) {
@ -87,8 +87,10 @@ public:
if (!m_render_timer->IsRunning()) { if (!m_render_timer->IsRunning()) {
m_device = new stmdsp::device(m_device_combo->GetStringSelection().ToStdString()); m_device = new stmdsp::device(m_device_combo->GetStringSelection().ToStdString());
if (m_device->connected()) { if (m_device->connected()) {
doSingle(); m_device->continuous_start();
m_render_timer->Start(100); m_device_samples_future = std::async(std::launch::async,
[]() { return decltype(m_device_samples)(); });
m_render_timer->Start(1000);
button->SetLabel("Stop"); button->SetLabel("Stop");
} else { } else {
delete m_device; delete m_device;
@ -96,6 +98,7 @@ public:
} }
} else { } else {
m_render_timer->Stop(); m_render_timer->Stop();
m_device->continuous_stop();
button->SetLabel("Single"); button->SetLabel("Single");
delete m_device; delete m_device;

@ -77,7 +77,7 @@ namespace adc
adc_operation_func = operation_func; adc_operation_func = operation_func;
adc_group_config.circular = true; adc_group_config.circular = true;
adcStartConversion(adcd, &adc_group_config, buffer, count); adcStartConversion(adcd, &adc_group_config, buffer, count);
gptStartContinuous(gptd, 100); // 10kHz gptStartContinuous(gptd, 1000); // 10kHz
} }
void read_stop() void read_stop()
@ -131,13 +131,10 @@ void adc_read_callback(ADCDriver *driver)
adc_is_read_finished = true; adc_is_read_finished = true;
} else if (adc_operation_func != nullptr) { } else if (adc_operation_func != nullptr) {
auto half_size = adc_current_buffer_size / 2; auto half_size = adc_current_buffer_size / 2;
if (driver->state == ADC_ACTIVE) { if (adcIsBufferComplete(driver))
// Half full
adc_operation_func(adc_current_buffer, half_size);
} else if (driver->state == ADC_COMPLETE) {
// Second half full
adc_operation_func(adc_current_buffer + half_size, half_size); adc_operation_func(adc_current_buffer + half_size, half_size);
} else
adc_operation_func(adc_current_buffer, half_size);
} }
} }

@ -19,13 +19,19 @@
#include <array> #include <array>
static_assert(sizeof(adcsample_t) == sizeof(uint16_t)); static_assert(sizeof(adcsample_t) == sizeof(uint16_t));
static_assert(sizeof(dacsample_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, 2048)> adc_samples; static std::array<adcsample_t, CACHE_SIZE_ALIGN(adcsample_t, 2048)> adc_samples;
#if CACHE_LINE_SIZE > 0
CC_ALIGN(CACHE_LINE_SIZE)
#endif
static std::array<dacsample_t, CACHE_SIZE_ALIGN(dacsample_t, 2048)> dac_samples; static std::array<dacsample_t, CACHE_SIZE_ALIGN(dacsample_t, 2048)> dac_samples;
static volatile bool signal_operate_done = false;
static void signal_operate(adcsample_t *buffer, size_t count); static void signal_operate(adcsample_t *buffer, size_t count);
int main() int main()
@ -54,10 +60,20 @@ int main()
} }
break; break;
case 'R': case 'R':
adc::read_start(signal_operate, &adc_samples[0], adc_samples.size() * sizeof(adcsample_t)); dac_samples.fill(0);
adc::read_start(signal_operate, &adc_samples[0], adc_samples.size());
dac::write_start(&dac_samples[0], dac_samples.size());
break;
case 's':
while (!signal_operate_done);
usbserial::write(dac_samples.data(), dac_samples.size() * sizeof(adcsample_t));
break; break;
case 'S': case 'S':
dac::write_stop();
adc::read_stop(); adc::read_stop();
break;
case 'e':
break; break;
case 'W': case 'W':
if (usbserial::read(&cmd[1], 2) < 2) if (usbserial::read(&cmd[1], 2) < 2)
@ -85,8 +101,10 @@ int main()
} }
} }
void signal_operate([[maybe_unused]] adcsample_t *buffer, [[maybe_unused]] size_t count) void signal_operate(adcsample_t *buffer, size_t count)
{ {
auto dac_buffer = &dac_samples[buffer == &adc_samples[0] ? 0 : 1024];
std::copy(buffer, buffer + count, dac_buffer);
signal_operate_done = buffer == &adc_samples[1024];
} }

Loading…
Cancel
Save