]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * This file is part of the libsigrokflow project. | |
3 | * | |
4 | * Copyright (C) 2018 Martin Ling <martin-sigrok@earth.li> | |
5 | * Copyright (C) 2018 Uwe Hermann <uwe@hermann-uwe.de> | |
6 | * | |
7 | * This program is free software: you can redistribute it and/or modify | |
8 | * it under the terms of the GNU General Public License as published by | |
9 | * the Free Software Foundation, either version 3 of the License, or | |
10 | * (at your option) any later version. | |
11 | * | |
12 | * This program is distributed in the hope that it will be useful, | |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | * GNU General Public License for more details. | |
16 | * | |
17 | * You should have received a copy of the GNU General Public License | |
18 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
19 | */ | |
20 | ||
21 | #include <config.h> | |
22 | #include <libsigrokflow/legacy_output.hpp> | |
23 | #include <glib.h> | |
24 | #include <iostream> | |
25 | #include <map> | |
26 | #include <memory> | |
27 | #include <stdexcept> | |
28 | #include <string> | |
29 | #include <gstreamermm/private/basesink_p.h> | |
30 | ||
31 | namespace Srf | |
32 | { | |
33 | ||
34 | using std::cout; | |
35 | using std::map; | |
36 | using std::runtime_error; | |
37 | using std::shared_ptr; | |
38 | using std::string; | |
39 | ||
40 | #ifdef HAVE_LIBSIGROKCXX | |
41 | void LegacyOutput::class_init(Gst::ElementClass<LegacyOutput> *klass) | |
42 | { | |
43 | klass->set_metadata("sigrok legacy output", | |
44 | "Sink", "Wrapper for outputs using legacy libsigrok APIs", | |
45 | "Martin Ling"); | |
46 | ||
47 | klass->add_pad_template(Gst::PadTemplate::create( | |
48 | "sink", | |
49 | Gst::PAD_SINK, | |
50 | Gst::PAD_ALWAYS, | |
51 | Gst::Caps::create_any())); | |
52 | } | |
53 | ||
54 | bool LegacyOutput::register_element(Glib::RefPtr<Gst::Plugin> plugin) | |
55 | { | |
56 | Gst::ElementFactory::register_element(plugin, "sigrok_legacy_output", | |
57 | 0, Gst::register_mm_type<LegacyOutput>( | |
58 | "sigrok_legacy_output")); | |
59 | ||
60 | return true; | |
61 | } | |
62 | ||
63 | LegacyOutput::LegacyOutput(GstBaseSink *gobj) : | |
64 | Glib::ObjectBase(typeid(LegacyOutput)), | |
65 | Sink(gobj) | |
66 | { | |
67 | } | |
68 | ||
69 | Glib::RefPtr<LegacyOutput>LegacyOutput::create( | |
70 | shared_ptr<sigrok::OutputFormat> libsigrok_output_format, | |
71 | map<string, Glib::VariantBase> options) | |
72 | { | |
73 | auto element = Gst::ElementFactory::create_element("sigrok_legacy_output"); | |
74 | if (!element) | |
75 | throw runtime_error("Failed to create element - plugin not registered?"); | |
76 | auto output = Glib::RefPtr<LegacyOutput>::cast_static(element); | |
77 | output->libsigrok_output_format_ = libsigrok_output_format; | |
78 | auto context = libsigrok_output_format->parent(); | |
79 | output->libsigrok_device_ = context->create_user_device("Vendor", "Model", "Version"); | |
80 | for (int i = 0; i < 8; ++i) { | |
81 | gchar *name = g_strdup_printf("D%d", i); | |
82 | output->libsigrok_device_->add_channel(i, sigrok::ChannelType::LOGIC, name); | |
83 | g_free(name); | |
84 | } | |
85 | output->options_ = options; | |
86 | ||
87 | return output; | |
88 | } | |
89 | ||
90 | bool LegacyOutput::start_vfunc() | |
91 | { | |
92 | libsigrok_output_ = libsigrok_output_format_->create_output( | |
93 | libsigrok_device_, options_); | |
94 | ||
95 | return true; | |
96 | } | |
97 | ||
98 | Gst::FlowReturn LegacyOutput::render_vfunc(const Glib::RefPtr<Gst::Buffer> &buffer) | |
99 | { | |
100 | Gst::MapInfo info; | |
101 | buffer->map(info, Gst::MAP_READ); | |
102 | auto context = libsigrok_output_format_->parent(); | |
103 | auto packet = context->create_logic_packet( | |
104 | info.get_data(), info.get_size(), 2); | |
105 | auto result = libsigrok_output_->receive(packet); | |
106 | cout << result; | |
107 | buffer->unmap(info); | |
108 | ||
109 | return Gst::FLOW_OK; | |
110 | } | |
111 | ||
112 | bool LegacyOutput::stop_vfunc() | |
113 | { | |
114 | auto context = libsigrok_output_format_->parent(); | |
115 | auto end_packet = context->create_end_packet(); | |
116 | auto result = libsigrok_output_->receive(end_packet); | |
117 | cout << result; | |
118 | ||
119 | return true; | |
120 | } | |
121 | #endif | |
122 | ||
123 | } |