diff options
Diffstat (limited to 'source/stmdsp/stmdsp.cpp')
-rw-r--r-- | source/stmdsp/stmdsp.cpp | 334 |
1 files changed, 225 insertions, 109 deletions
diff --git a/source/stmdsp/stmdsp.cpp b/source/stmdsp/stmdsp.cpp index b3fc8c3..294e98f 100644 --- a/source/stmdsp/stmdsp.cpp +++ b/source/stmdsp/stmdsp.cpp @@ -13,120 +13,186 @@ #include <serial/serial.h> +#include <algorithm> + +extern void log(const std::string& str); + +std::array<unsigned int, 6> sampleRateInts {{ + 8'000, + 16'000, + 20'000, + 32'000, + 48'000, + 96'000 +}}; + namespace stmdsp { - std::list<std::string>& scanner::scan() + const std::forward_list<std::string>& scanner::scan() { 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); - } - + auto foundDevicesEnd = std::remove_if( + devices.begin(), devices.end(), + [](const auto& dev) { + return dev.hardware_id.find(STMDSP_USB_ID) == std::string::npos; + }); + std::transform(devices.begin(), foundDevicesEnd, + std::front_inserter(m_available_devices), + [](const auto& dev) { return dev.port; }); return m_available_devices; } - device::device(const std::string& file) : - m_serial(file, 1000000/*230400*/, serial::Timeout::simpleTimeout(50)) + device::device(const std::string& file) { - if (m_serial.isOpen()) { - m_serial.flush(); - m_serial.write("i"); - if (auto id = m_serial.read(7); id.starts_with("stmdsp")) { - if (id.back() == 'h') - m_platform = platform::H7; - else if (id.back() == 'l') - m_platform = platform::L4; - else - m_serial.close(); - } else { - m_serial.close(); - } + // This could throw! + // Note: Windows needs a not-simple, positive timeout like this to + // ensure that reads block. + m_serial.reset(new serial::Serial(file, 921'600 /*8'000'000*/, serial::Timeout(1000, 1000, 1, 1000, 1))); + + // Test the ID command. + m_serial->flush(); + m_serial->write("i"); + auto id = m_serial->read(7); + + if (id.starts_with("stmdsp")) { + if (id.back() == 'h') + m_platform = platform::H7; + else if (id.back() == 'l') + m_platform = platform::L4; + else + m_serial.release(); + } else { + m_serial.release(); } } - void device::continuous_set_buffer_size(unsigned int size) { + device::~device() + { + disconnect(); + } + + bool device::connected() { + if (m_serial && !m_serial->isOpen()) + m_serial.release(); + + return m_serial ? true : false; + } + + void device::disconnect() { + if (m_serial) + m_serial.release(); + } + + bool device::try_command(std::basic_string<uint8_t> cmd) { + bool success = false; + if (connected()) { - m_buffer_size = size; + try { + std::scoped_lock lock (m_lock); + m_serial->write(cmd.data(), cmd.size()); + success = true; + } catch (...) { + handle_disconnect(); + } + } - uint8_t request[3] = { + return success; + } + + bool device::try_read(std::basic_string<uint8_t> cmd, uint8_t *dest, unsigned int dest_size) { + bool success = false; + + if (connected() && dest && dest_size > 0) { + try { + std::scoped_lock lock (m_lock); + m_serial->write(cmd.data(), cmd.size()); + m_serial->read(dest, dest_size); + success = true; + } catch (...) { + handle_disconnect(); + } + } + + return success; + } + + void device::continuous_set_buffer_size(unsigned int size) { + if (try_command({ 'B', static_cast<uint8_t>(size), - static_cast<uint8_t>(size >> 8) - }; - m_serial.write(request, 3); + static_cast<uint8_t>(size >> 8)})) + { + m_buffer_size = size; } } - void device::set_sample_rate(unsigned int id) { - if (connected()) { - uint8_t request[2] = { + void device::set_sample_rate(unsigned int rate) { + auto it = std::find( + sampleRateInts.cbegin(), + sampleRateInts.cend(), + rate); + + if (it != sampleRateInts.cend()) { + const auto i = std::distance(sampleRateInts.cbegin(), it); + try_command({ 'r', - static_cast<uint8_t>(id) - }; - m_serial.write(request, 2); + static_cast<uint8_t>(i) + }); } } unsigned int device::get_sample_rate() { - unsigned char result = 0xFF; - - if (connected()) { - uint8_t request[2] = { - 'r', 0xFF - }; - m_serial.write(request, 2); - m_serial.read(&result, 1); + if (!is_running()) { + uint8_t result = 0xFF; + if (try_read({'r', 0xFF}, &result, 1)) + m_sample_rate = result; } - return result; + return m_sample_rate < sampleRateInts.size() ? + sampleRateInts[m_sample_rate] : + 0; } void device::continuous_start() { - if (connected()) { - m_serial.write("R"); + if (try_command({'R'})) m_is_running = true; - } } - void device::continuous_start_measure() { - if (connected()) { - m_serial.write("M"); - m_is_running = true; - } + void device::measurement_start() { + try_command({'M'}); } - uint32_t device::continuous_start_get_measurement() { + uint32_t device::measurement_read() { uint32_t count = 0; - if (connected()) { - m_serial.write("m"); - m_serial.read(reinterpret_cast<uint8_t *>(&count), sizeof(uint32_t)); - } - + try_read({'m'}, reinterpret_cast<uint8_t *>(&count), sizeof(uint32_t)); return count / 2; } std::vector<adcsample_t> device::continuous_read() { if (connected()) { - m_serial.write("s"); - unsigned char sizebytes[2]; - m_serial.read(sizebytes, 2); - unsigned int size = sizebytes[0] | (sizebytes[1] << 8); - if (size > 0) { - std::vector<adcsample_t> data (size); - unsigned int total = size * sizeof(adcsample_t); - unsigned int offset = 0; - - while (total > 512) { - m_serial.read(reinterpret_cast<uint8_t *>(&data[0]) + offset, 512); - m_serial.write("n"); - offset += 512; - total -= 512; - } - m_serial.read(reinterpret_cast<uint8_t *>(&data[0]) + offset, total); - m_serial.write("n"); - return data; + try { + m_serial->write("s"); + unsigned char sizebytes[2]; + m_serial->read(sizebytes, 2); + unsigned int size = sizebytes[0] | (sizebytes[1] << 8); + if (size > 0) { + std::vector<adcsample_t> data (size); + unsigned int total = size * sizeof(adcsample_t); + unsigned int offset = 0; + while (total > 512) { + m_serial->read(reinterpret_cast<uint8_t *>(&data[0]) + offset, 512); + m_serial->write("n"); + offset += 512; + total -= 512; + } + m_serial->read(reinterpret_cast<uint8_t *>(&data[0]) + offset, total); + m_serial->write("n"); + return data; + + } + } catch (...) { + handle_disconnect(); } } @@ -135,25 +201,29 @@ namespace stmdsp std::vector<adcsample_t> device::continuous_read_input() { if (connected()) { - m_serial.write("t"); - unsigned char sizebytes[2]; - m_serial.read(sizebytes, 2); - unsigned int size = sizebytes[0] | (sizebytes[1] << 8); - if (size > 0) { - std::vector<adcsample_t> data (size); - unsigned int total = size * sizeof(adcsample_t); - unsigned int offset = 0; - - while (total > 512) { - m_serial.read(reinterpret_cast<uint8_t *>(&data[0]) + offset, 512); - m_serial.write("n"); - offset += 512; - total -= 512; - } - m_serial.read(reinterpret_cast<uint8_t *>(&data[0]) + offset, total); - m_serial.write("n"); - return data; + try { + m_serial->write("t"); + unsigned char sizebytes[2]; + m_serial->read(sizebytes, 2); + unsigned int size = sizebytes[0] | (sizebytes[1] << 8); + if (size > 0) { + std::vector<adcsample_t> data (size); + unsigned int total = size * sizeof(adcsample_t); + unsigned int offset = 0; + while (total > 512) { + m_serial->read(reinterpret_cast<uint8_t *>(&data[0]) + offset, 512); + m_serial->write("n"); + offset += 512; + total -= 512; + } + m_serial->read(reinterpret_cast<uint8_t *>(&data[0]) + offset, total); + m_serial->write("n"); + return data; + + } + } catch (...) { + handle_disconnect(); } } @@ -161,37 +231,51 @@ namespace stmdsp } void device::continuous_stop() { - if (connected()) { - m_serial.write("S"); + if (try_command({'S'})) m_is_running = false; - } } - void device::siggen_upload(dacsample_t *buffer, unsigned int size) { + bool device::siggen_upload(dacsample_t *buffer, unsigned int size) { if (connected()) { uint8_t request[3] = { 'D', static_cast<uint8_t>(size), static_cast<uint8_t>(size >> 8) }; - m_serial.write(request, 3); - m_serial.write((uint8_t *)buffer, size * sizeof(dacsample_t)); + if (!m_is_siggening) { + try { + m_serial->write(request, 3); + m_serial->write((uint8_t *)buffer, size * sizeof(dacsample_t)); + } catch (...) { + handle_disconnect(); + } + } else { + try { + m_serial->write(request, 3); + if (m_serial->read(1)[0] == 0) + return false; + else + m_serial->write((uint8_t *)buffer, size * sizeof(dacsample_t)); + } catch (...) { + handle_disconnect(); + } + } + + return true; + } else { + return false; } } void device::siggen_start() { - if (connected()) { + if (try_command({'W'})) m_is_siggening = true; - m_serial.write("W"); - } } void device::siggen_stop() { - if (connected()) { + if (try_command({'w'})) m_is_siggening = false; - m_serial.write("w"); - } } void device::upload_filter(unsigned char *buffer, size_t size) { @@ -201,14 +285,46 @@ namespace stmdsp static_cast<uint8_t>(size), static_cast<uint8_t>(size >> 8) }; - m_serial.write(request, 3); - m_serial.write(buffer, size); + try { + m_serial->write(request, 3); + m_serial->write(buffer, size); + } catch (...) { + handle_disconnect(); + } } } void device::unload_filter() { - if (connected()) - m_serial.write("e"); + try_command({'e'}); + } + + std::pair<RunStatus, Error> device::get_status() { + std::pair<RunStatus, Error> ret; + + unsigned char buf[2]; + if (try_read({'I'}, buf, 2)) { + ret = { + static_cast<RunStatus>(buf[0]), + static_cast<Error>(buf[1]) + }; + + bool running = ret.first == RunStatus::Running; + if (m_is_running != running) + m_is_running = running; + } else if (m_disconnect_error_flag) { + m_disconnect_error_flag = false; + return {RunStatus::Idle, Error::GUIDisconnect}; + } + + return ret; } -} + + void device::handle_disconnect() + { + m_disconnect_error_flag = true; + m_serial.release(); + log("Lost connection!"); + } +} // namespace stmdsp + |