From 5237f0c50352b523c6a0c3d7f931081ecdbdecaa Mon Sep 17 00:00:00 2001 From: Joel Holdsworth Date: Fri, 3 Apr 2015 15:54:22 +0100 Subject: [PATCH 1/1] Added InputFile --- CMakeLists.txt | 1 + pv/devices/device.cpp | 5 +++ pv/devices/device.hpp | 2 + pv/devices/inputfile.cpp | 92 ++++++++++++++++++++++++++++++++++++++++ pv/devices/inputfile.hpp | 64 ++++++++++++++++++++++++++++ pv/session.cpp | 2 +- 6 files changed, 165 insertions(+), 1 deletion(-) create mode 100644 pv/devices/inputfile.cpp create mode 100644 pv/devices/inputfile.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index c6e3b770..049c955e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -158,6 +158,7 @@ set(pulseview_SOURCES pv/devices/device.cpp pv/devices/file.cpp pv/devices/hardwaredevice.cpp + pv/devices/inputfile.cpp pv/devices/sessionfile.cpp pv/dialogs/about.cpp pv/dialogs/connect.cpp diff --git a/pv/devices/device.cpp b/pv/devices/device.cpp index 6a36ff38..4027a1c2 100644 --- a/pv/devices/device.cpp +++ b/pv/devices/device.cpp @@ -80,6 +80,11 @@ T Device::read_config(const ConfigKey *key, const T default_value) device_->config_get(ConfigKey::SAMPLERATE)).get(); } +void Device::start() { + assert(session_); + session_->start(); +} + void Device::run() { assert(device_); assert(session_); diff --git a/pv/devices/device.hpp b/pv/devices/device.hpp index 612d7509..0a8f2438 100644 --- a/pv/devices/device.hpp +++ b/pv/devices/device.hpp @@ -66,6 +66,8 @@ public: virtual void create() = 0; + virtual void start(); + virtual void run(); virtual void stop(); diff --git a/pv/devices/inputfile.cpp b/pv/devices/inputfile.cpp new file mode 100644 index 00000000..41187747 --- /dev/null +++ b/pv/devices/inputfile.cpp @@ -0,0 +1,92 @@ +/* + * This file is part of the PulseView project. + * + * Copyright (C) 2015 Joel Holdsworth + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include +#include + +#include + +#include "inputfile.hpp" + +namespace pv { +namespace devices { + +const std::streamsize InputFile::BufferSize = 16384; + +InputFile::InputFile(const std::shared_ptr &context, + const std::string &file_name, + std::shared_ptr format, + const std::map &options) : + context_(context), + input_(format->create_input(options)), + file_name_(file_name), + interrupt_(false) { + if (!input_) + throw QString("Failed to create input"); +} + +void InputFile::create() { + session_ = context_->create_session(); +} + +void InputFile::start() { +} + +void InputFile::run() { + char buffer[BufferSize]; + bool need_device = true; + + assert(session_); + assert(input_); + + interrupt_ = false; + std::ifstream f(file_name_); + while (!interrupt_ && f) { + f.read(buffer, BufferSize); + const std::streamsize size = f.gcount(); + if (size == 0) + break; + + input_->send(buffer, size); + + if (need_device) { + try { + device_ = input_->device(); + } catch (sigrok::Error) { + break; + } + + session_->add_device(device_); + need_device = false; + } + + if (size != BufferSize) + break; + } + + input_->end(); +} + +void InputFile::stop() { + interrupt_ = true; +} + +} // namespace devices +} // namespace pv diff --git a/pv/devices/inputfile.hpp b/pv/devices/inputfile.hpp new file mode 100644 index 00000000..55688fd4 --- /dev/null +++ b/pv/devices/inputfile.hpp @@ -0,0 +1,64 @@ +/* + * This file is part of the PulseView project. + * + * Copyright (C) 2015 Joel Holdsworth + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifndef PULSEVIEW_PV_DEVICE_INPUTFILE_HPP +#define PULSEVIEW_PV_DEVICE_INPUTFILE_HPP + +#include + +#include + +#include "device.hpp" + +namespace pv { +namespace devices { + +class InputFile final : public Device +{ +private: + static const std::streamsize BufferSize; + +public: + InputFile(const std::shared_ptr &context, + const std::string &file_name, + std::shared_ptr format, + const std::map &options); + + void create(); + + void start(); + + void run(); + + void stop(); + +private: + const std::shared_ptr context_; + const std::shared_ptr input_; + const std::string file_name_; + + std::atomic interrupt_; +}; + +} // namespace devices +} // namespace pv + +#endif // PULSEVIEW_PV_SESSIONS_INPUTFILE_HPP + diff --git a/pv/session.cpp b/pv/session.cpp index f01696b0..08bec08e 100644 --- a/pv/session.cpp +++ b/pv/session.cpp @@ -415,7 +415,7 @@ void Session::sample_thread_proc(shared_ptr device, cur_samplerate_ = device_->read_config(ConfigKey::SAMPLERATE); try { - device_->session()->start(); + device_->start(); } catch(Error e) { error_handler(e.what()); return; -- 2.30.2