diff options
Diffstat (limited to 'source/stmdsp/stmdsp.cpp')
-rw-r--r-- | source/stmdsp/stmdsp.cpp | 268 |
1 files changed, 192 insertions, 76 deletions
diff --git a/source/stmdsp/stmdsp.cpp b/source/stmdsp/stmdsp.cpp index d219d38..650dbc4 100644 --- a/source/stmdsp/stmdsp.cpp +++ b/source/stmdsp/stmdsp.cpp @@ -13,6 +13,8 @@ #include <serial/serial.h> +extern void log(const std::string& str); + namespace stmdsp { std::list<std::string>& scanner::scan() @@ -26,25 +28,44 @@ namespace stmdsp return m_available_devices; } - device::device(const std::string& file) : - m_serial(file, 8'000'000, 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! + m_serial.reset(new serial::Serial(file, 8'000'000, serial::Timeout::simpleTimeout(50))); + + 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(); } } + 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(); + } + void device::continuous_set_buffer_size(unsigned int size) { if (connected()) { m_buffer_size = size; @@ -54,7 +75,13 @@ namespace stmdsp static_cast<uint8_t>(size), static_cast<uint8_t>(size >> 8) }; - m_serial.write(request, 3); + + try { + m_serial->write(request, 3); + } catch (...) { + m_serial.release(); + log("Lost connection!"); + } } } @@ -64,7 +91,13 @@ namespace stmdsp 'r', static_cast<uint8_t>(id) }; - m_serial.write(request, 2); + + try { + m_serial->write(request, 2); + } catch (...) { + m_serial.release(); + log("Lost connection!"); + } } } @@ -73,10 +106,16 @@ namespace stmdsp uint8_t request[2] = { 'r', 0xFF }; - m_serial.write(request, 2); unsigned char result = 0xFF; - m_serial.read(&result, 1); + try { + m_serial->write(request, 2); + m_serial->read(&result, 1); + } catch (...) { + m_serial.release(); + log("Lost connection!"); + } + m_sample_rate = result; } @@ -85,23 +124,38 @@ namespace stmdsp void device::continuous_start() { if (connected()) { - m_serial.write("R"); - m_is_running = true; + try { + m_serial->write("R"); + m_is_running = true; + } catch (...) { + m_serial.release(); + log("Lost connection!"); + } } } void device::continuous_start_measure() { if (connected()) { - m_serial.write("M"); - m_is_running = true; + try { + m_serial->write("M"); + m_is_running = true; + } catch (...) { + m_serial.release(); + log("Lost connection!"); + } } } uint32_t device::continuous_start_get_measurement() { uint32_t count = 0; if (connected()) { - m_serial.write("m"); - m_serial.read(reinterpret_cast<uint8_t *>(&count), sizeof(uint32_t)); + try { + m_serial->write("m"); + m_serial->read(reinterpret_cast<uint8_t *>(&count), sizeof(uint32_t)); + } catch (...) { + m_serial.release(); + log("Lost connection!"); + } } return count / 2; @@ -109,25 +163,30 @@ namespace stmdsp 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 (...) { + m_serial.release(); + log("Lost connection!"); } } @@ -136,25 +195,30 @@ 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 (...) { + m_serial.release(); + log("Lost connection!"); } } @@ -163,8 +227,13 @@ namespace stmdsp void device::continuous_stop() { if (connected()) { - m_serial.write("S"); - m_is_running = false; + try { + m_serial->write("S"); + m_is_running = false; + } catch (...) { + m_serial.release(); + log("Lost connection!"); + } } } @@ -175,26 +244,39 @@ namespace stmdsp 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)); - // TODO - if (!m_is_running) - m_serial.write((uint8_t *)buffer, size * sizeof(dacsample_t)); + try { + m_serial->write(request, 3); + // TODO different write size if feeding audio? + m_serial->write((uint8_t *)buffer, size * sizeof(dacsample_t)); + } catch (...) { + m_serial.release(); + log("Lost connection!"); + } } } void device::siggen_start() { if (connected()) { - m_is_siggening = true; - m_serial.write("W"); + try { + m_serial->write("W"); + m_is_siggening = true; + } catch (...) { + m_serial.release(); + log("Lost connection!"); + } } } void device::siggen_stop() { if (connected()) { - m_is_siggening = false; - m_serial.write("w"); + try { + m_serial->write("w"); + m_is_siggening = false; + } catch (...) { + m_serial.release(); + log("Lost connection!"); + } } } @@ -205,14 +287,48 @@ 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 (...) { + m_serial.release(); + log("Lost connection!"); + } } } void device::unload_filter() { - if (connected()) - m_serial.write("e"); + if (connected()) { + try { + m_serial->write("e"); + } catch (...) { + m_serial.release(); + log("Lost connection!"); + } + } + } + + std::pair<RunStatus, Error> device::get_status() { + std::pair<RunStatus, Error> ret; + + if (connected()) { + try { + m_serial->write("I"); + auto result = m_serial->read(2); + ret = {static_cast<RunStatus>(result[0]), + static_cast<Error>(result[1])}; + + bool running = ret.first == RunStatus::Running; + if (m_is_running != running) + m_is_running = running; + } catch (...) { + m_serial.release(); + log("Lost connection!"); + } + } + + return ret; } } + |