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) :
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) {
if (connected()) {
@ -27,11 +34,29 @@ namespace stmdsp
};
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));
m_serial.read(reinterpret_cast<uint8_t *>(data.data()), data.size() * sizeof(adcsample_t));
return data;
} else {
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() {
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);
void continuous_start();
std::vector<adcsample_t> continuous_read();
void continuous_stop();
private:
serial::Serial m_serial;
};

@ -78,7 +78,7 @@ public:
void doSingle() {
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) {
@ -87,8 +87,10 @@ public:
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_device->continuous_start();
m_device_samples_future = std::async(std::launch::async,
[]() { return decltype(m_device_samples)(); });
m_render_timer->Start(1000);
button->SetLabel("Stop");
} else {
delete m_device;
@ -96,6 +98,7 @@ public:
}
} else {
m_render_timer->Stop();
m_device->continuous_stop();
button->SetLabel("Single");
delete m_device;

@ -77,7 +77,7 @@ namespace adc
adc_operation_func = operation_func;
adc_group_config.circular = true;
adcStartConversion(adcd, &adc_group_config, buffer, count);
gptStartContinuous(gptd, 100); // 10kHz
gptStartContinuous(gptd, 1000); // 10kHz
}
void read_stop()
@ -131,13 +131,10 @@ void adc_read_callback(ADCDriver *driver)
adc_is_read_finished = true;
} else if (adc_operation_func != nullptr) {
auto half_size = adc_current_buffer_size / 2;
if (driver->state == ADC_ACTIVE) {
// Half full
adc_operation_func(adc_current_buffer, half_size);
} else if (driver->state == ADC_COMPLETE) {
// Second half full
if (adcIsBufferComplete(driver))
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>
static_assert(sizeof(adcsample_t) == sizeof(uint16_t));
static_assert(sizeof(dacsample_t) == sizeof(uint16_t));
#if CACHE_LINE_SIZE > 0
CC_ALIGN(CACHE_LINE_SIZE)
#endif
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 volatile bool signal_operate_done = false;
static void signal_operate(adcsample_t *buffer, size_t count);
int main()
@ -54,10 +60,20 @@ int main()
}
break;
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;
case 'S':
dac::write_stop();
adc::read_stop();
break;
case 'e':
break;
case 'W':
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