]>
Commit | Line | Data |
---|---|---|
2953961c JH |
1 | /* |
2 | * This file is part of the sigrok project. | |
3 | * | |
4 | * Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk> | |
5 | * | |
6 | * This program is free software; you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License as published by | |
8 | * the Free Software Foundation; either version 2 of the License, or | |
9 | * (at your option) any later version. | |
10 | * | |
11 | * This program is distributed in the hope that it will be useful, | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | * GNU General Public License for more details. | |
15 | * | |
16 | * You should have received a copy of the GNU General Public License | |
17 | * along with this program; if not, write to the Free Software | |
18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
19 | */ | |
20 | ||
21 | #include "sigsession.h" | |
22 | ||
28a4c9c5 JH |
23 | #include "logicdata.h" |
24 | #include "logicdatasnapshot.h" | |
e3f65ace | 25 | #include "logicsignal.h" |
28a4c9c5 | 26 | |
009e1503 JH |
27 | #include <QDebug> |
28 | ||
2953961c JH |
29 | #include <assert.h> |
30 | ||
28a4c9c5 | 31 | using namespace boost; |
e3f65ace | 32 | using namespace std; |
28a4c9c5 | 33 | |
2953961c JH |
34 | // TODO: This should not be necessary |
35 | SigSession* SigSession::session = NULL; | |
36 | ||
28a4c9c5 | 37 | SigSession::SigSession() |
2953961c JH |
38 | { |
39 | // TODO: This should not be necessary | |
40 | session = this; | |
41 | } | |
42 | ||
43 | SigSession::~SigSession() | |
44 | { | |
45 | // TODO: This should not be necessary | |
46 | session = NULL; | |
47 | } | |
48 | ||
49 | void SigSession::loadFile(const std::string &name) | |
50 | { | |
51 | if (sr_session_load(name.c_str()) == SR_OK) { | |
52 | /* sigrok session file */ | |
53 | sr_session_datafeed_callback_add(dataFeedInProc); | |
54 | sr_session_start(); | |
55 | sr_session_run(); | |
56 | sr_session_stop(); | |
57 | } | |
58 | } | |
59 | ||
274d4f13 JH |
60 | void SigSession::start_capture(struct sr_dev_inst *sdi, |
61 | uint64_t sample_rate) | |
62 | { | |
63 | sr_session_new(); | |
64 | sr_session_datafeed_callback_add(dataFeedInProc); | |
65 | ||
66 | if (sr_session_dev_add(sdi) != SR_OK) { | |
67 | qDebug() << "Failed to use device."; | |
68 | sr_session_destroy(); | |
69 | return; | |
70 | } | |
71 | ||
72 | uint64_t limit_samples = 10000; | |
73 | if (sr_dev_config_set(sdi, SR_HWCAP_LIMIT_SAMPLES, | |
74 | &limit_samples) != SR_OK) { | |
75 | qDebug() << "Failed to configure time-based sample limit."; | |
76 | sr_session_destroy(); | |
77 | return; | |
78 | } | |
79 | ||
80 | if (sr_dev_config_set(sdi, SR_HWCAP_SAMPLERATE, | |
81 | &sample_rate) != SR_OK) { | |
82 | qDebug() << "Failed to configure samplerate."; | |
83 | sr_session_destroy(); | |
84 | return; | |
85 | } | |
86 | ||
87 | if (sr_session_start() != SR_OK) { | |
88 | qDebug() << "Failed to start session."; | |
89 | return; | |
90 | } | |
91 | ||
92 | sr_session_run(); | |
93 | sr_session_destroy(); | |
94 | } | |
95 | ||
e3f65ace JH |
96 | vector< shared_ptr<Signal> >& SigSession::get_signals() |
97 | { | |
98 | return _signals; | |
99 | } | |
100 | ||
2953961c JH |
101 | void SigSession::dataFeedIn(const struct sr_dev_inst *sdi, |
102 | struct sr_datafeed_packet *packet) | |
103 | { | |
104 | assert(sdi); | |
105 | assert(packet); | |
106 | ||
107 | switch (packet->type) { | |
28a4c9c5 | 108 | case SR_DF_HEADER: |
e3f65ace | 109 | _signals.clear(); |
009e1503 JH |
110 | break; |
111 | ||
28a4c9c5 | 112 | case SR_DF_META_LOGIC: |
009e1503 | 113 | { |
28a4c9c5 | 114 | assert(packet->payload); |
009e1503 | 115 | |
e3f65ace JH |
116 | const sr_datafeed_meta_logic &meta_logic = |
117 | *(sr_datafeed_meta_logic*)packet->payload; | |
009e1503 | 118 | |
e3f65ace JH |
119 | // Create an empty LogiData for coming data snapshots |
120 | _logic_data.reset(new LogicData(meta_logic)); | |
28a4c9c5 JH |
121 | assert(_logic_data); |
122 | if(!_logic_data) | |
123 | break; | |
009e1503 | 124 | |
e3f65ace JH |
125 | // Add the signals |
126 | for (int i = 0; i < meta_logic.num_probes; i++) | |
127 | { | |
128 | const sr_probe *const probe = | |
129 | (const sr_probe*)g_slist_nth_data( | |
130 | sdi->probes, i); | |
131 | if(probe->enabled) | |
132 | { | |
133 | boost::shared_ptr<LogicSignal> signal( | |
134 | new LogicSignal(probe->name, | |
135 | _logic_data, | |
136 | probe->index)); | |
137 | _signals.push_back(signal); | |
138 | } | |
139 | } | |
140 | ||
28a4c9c5 | 141 | break; |
2953961c | 142 | } |
28a4c9c5 JH |
143 | |
144 | case SR_DF_LOGIC: | |
f556bc6a | 145 | |
28a4c9c5 | 146 | assert(packet->payload); |
f556bc6a JH |
147 | if(!_cur_logic_snapshot) |
148 | { | |
149 | // Create a new data snapshot | |
150 | _cur_logic_snapshot = shared_ptr<LogicDataSnapshot>( | |
151 | new LogicDataSnapshot( | |
152 | *(sr_datafeed_logic*)packet->payload)); | |
153 | _logic_data->push_snapshot(_cur_logic_snapshot); | |
154 | } | |
155 | else | |
156 | { | |
157 | // Append to the existing data snapshot | |
28a4c9c5 JH |
158 | _cur_logic_snapshot->append_payload( |
159 | *(sr_datafeed_logic*)packet->payload); | |
f556bc6a JH |
160 | } |
161 | ||
2953961c JH |
162 | break; |
163 | ||
164 | case SR_DF_END: | |
28a4c9c5 | 165 | _cur_logic_snapshot.reset(); |
009e1503 | 166 | dataUpdated(); |
2953961c JH |
167 | break; |
168 | } | |
169 | } | |
170 | ||
171 | void SigSession::dataFeedInProc(const struct sr_dev_inst *sdi, | |
172 | struct sr_datafeed_packet *packet) | |
173 | { | |
174 | assert(session); | |
175 | session->dataFeedIn(sdi, packet); | |
176 | } |