3 * Copyright 2003,2005 Free Software Foundation, Inc.
5 * This file is part of GNU Radio
7 * GNU Radio 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 2, or (at your option)
12 * GNU Radio 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.
17 * You should have received a copy of the GNU General Public License
18 * along with GNU Radio; see the file COPYING. If not, write to
19 * the Free Software Foundation, Inc., 51 Franklin Street,
20 * Boston, MA 02110-1301, USA.
26 #include <gr_oscope_guts.h>
34 static const int OUTPUT_RECORD_SIZE = 2048; // must be power of 2
37 wrap_bi (int buffer_index) // wrap buffer index
39 return buffer_index & (OUTPUT_RECORD_SIZE - 1);
43 incr_bi (int buffer_index) // increment buffer index
45 return wrap_bi (buffer_index + 1);
49 decr_bi (int buffer_index) // decrement buffer index
51 return wrap_bi (buffer_index - 1);
54 gr_oscope_guts::gr_oscope_guts (int nchannels, double sample_rate, gr_msg_queue_sptr msgq)
55 : d_nchannels (nchannels),
57 d_trigger_mode (gr_TRIG_AUTO),
58 d_trigger_channel (0),
59 d_sample_rate (sample_rate),
63 d_state (LOOK_FOR_TRIGGER),
64 d_decimator_count (0),
65 d_decimator_count_init (1),
67 d_hold_off_count_init (0),
68 d_post_trigger_count (0),
69 d_post_trigger_count_init (OUTPUT_RECORD_SIZE/2),
72 if (d_nchannels > MAX_CHANNELS){
73 fprintf (stderr, "gr_oscope_guts: too many channels. MAX_CHANNELS = %d\n", MAX_CHANNELS);
74 throw std::runtime_error ("too many channels");
77 for (int i = 0; i < MAX_CHANNELS; i++)
80 for (int i = 0; i < d_nchannels; i++)
81 d_buffer[i] = new float [OUTPUT_RECORD_SIZE];
83 update_rate_or_decimation_changed ();
84 enter_look_for_trigger ();
87 gr_oscope_guts::~gr_oscope_guts ()
89 for (int i = 0; i < MAX_CHANNELS; i++)
90 delete [] d_buffer[i];
95 // \p channel_data points to nchannels float values. These are the values
96 // for each channel at this sample time.
99 gr_oscope_guts::process_sample (const float *channel_data)
102 if (d_decimator_count > 0)
105 d_decimator_count = d_decimator_count_init;
107 for (int i = 0; i < d_nchannels; i++)
108 d_buffer[i][d_obi] = channel_data[i]; // copy data into buffer
115 if (d_hold_off_count <= 0)
116 enter_look_for_trigger ();
119 case LOOK_FOR_TRIGGER:
120 trigger = found_trigger (d_buffer[d_trigger_channel][d_obi]);
122 enter_post_trigger ();
123 if (trigger < 0) // previous sample was closer
124 d_post_trigger_count--;
129 d_post_trigger_count--;
130 if (d_post_trigger_count <= 0){
131 write_output_records ();
140 d_obi = incr_bi (d_obi);
144 * Functions called on state entry
148 gr_oscope_guts::enter_hold_off ()
151 d_hold_off_count = d_hold_off_count_init;
155 gr_oscope_guts::enter_look_for_trigger ()
157 d_state = LOOK_FOR_TRIGGER;
158 d_prev_sample = d_buffer[d_trigger_channel][d_obi];
162 gr_oscope_guts::enter_post_trigger ()
164 d_state = POST_TRIGGER;
165 d_post_trigger_count = d_post_trigger_count_init;
168 // ----------------------------------------------------------------
169 // returns 0 if no trigger found.
170 // returns +1 if this sample is the trigger point
171 // returns -1 if the previous sample is the trigger point
174 gr_oscope_guts::found_trigger (float new_sample)
176 float prev_sample = d_prev_sample;
177 d_prev_sample = new_sample;
180 switch (d_trigger_mode){
182 case gr_TRIG_AUTO: // always trigger
185 case gr_TRIG_POS_SLOPE:
186 trig = prev_sample < d_trigger_level && new_sample >= d_trigger_level;
188 if (fabs (prev_sample - d_trigger_level) < fabs (new_sample - d_trigger_level))
195 case gr_TRIG_NEG_SLOPE:
196 trig = prev_sample > d_trigger_level && new_sample <= d_trigger_level;
198 if (fabs (prev_sample - d_trigger_level) < fabs (new_sample - d_trigger_level))
211 // ----------------------------------------------------------------
212 // write output records (duh!)
215 gr_oscope_guts::write_output_records ()
217 // if the output queue if full, drop the data on the ground.
218 if (d_msgq->full_p())
221 // Build a message to hold the output records
222 gr_message_sptr msg =
223 gr_make_message(0, // msg type
224 d_nchannels, // arg1 for other side
225 OUTPUT_RECORD_SIZE, // arg2 for other side
226 d_nchannels * OUTPUT_RECORD_SIZE * sizeof(float)); // sizeof payload
228 float *out = (float *)msg->msg(); // get pointer to raw message buffer
230 for (int ch = 0; ch < d_nchannels; ch++){
231 // note that d_obi points at the oldest sample in the buffer
232 for (int i = 0; i < OUTPUT_RECORD_SIZE; i++)
233 out[i] = d_buffer[ch][wrap_bi(d_obi + i)];
235 out += OUTPUT_RECORD_SIZE;
238 d_msgq->handle(msg); // send the msg
241 // ----------------------------------------------------------------
244 gr_oscope_guts::set_update_rate (double update_rate)
246 d_update_rate = std::min (std::max (1./10., update_rate), d_sample_rate);
247 update_rate_or_decimation_changed ();
252 gr_oscope_guts::set_decimation_count (int decimator_count)
254 decimator_count = std::max (1, decimator_count);
255 d_decimator_count_init = decimator_count;
256 update_rate_or_decimation_changed ();
261 gr_oscope_guts::set_sample_rate(double sample_rate)
263 d_sample_rate = sample_rate;
264 return set_update_rate(update_rate());
269 gr_oscope_guts::update_rate_or_decimation_changed ()
271 d_hold_off_count_init =
272 (int) rint (d_sample_rate / d_update_rate / d_decimator_count_init);
276 gr_oscope_guts::set_trigger_channel (int channel)
278 if (channel >= 0 && channel < d_nchannels){
279 d_trigger_channel = channel;
288 gr_oscope_guts::set_trigger_mode (gr_trigger_mode mode)
291 case gr_TRIG_POS_SLOPE:
292 case gr_TRIG_NEG_SLOPE:
294 d_trigger_mode = mode;
302 gr_oscope_guts::set_trigger_level (double trigger_level)
304 d_trigger_level = trigger_level;
310 gr_oscope_guts::set_trigger_level_auto ()
312 // find the level 1/2 way between the min and the max
314 float min_v = d_buffer[d_trigger_channel][0];
315 float max_v = d_buffer[d_trigger_channel][0];
317 for (int i = 1; i < OUTPUT_RECORD_SIZE; i++){
318 min_v = std::min (min_v, d_buffer[d_trigger_channel][i]);
319 max_v = std::max (max_v, d_buffer[d_trigger_channel][i]);
322 d_trigger_level = (min_v + max_v) * 0.5;
328 gr_oscope_guts::trigger_changed ()
330 // d_prev_sample = d_buffer[d_trigger_channel][decr_bi(d_obi)];
331 enter_look_for_trigger ();
337 gr_oscope_guts::num_channels () const
343 gr_oscope_guts::sample_rate () const
345 return d_sample_rate;
349 gr_oscope_guts::update_rate () const
351 return d_update_rate;
355 gr_oscope_guts::get_decimation_count () const
357 return d_decimator_count_init;
361 gr_oscope_guts::get_trigger_channel () const
363 return d_trigger_channel;
367 gr_oscope_guts::get_trigger_mode () const
369 return d_trigger_mode;
373 gr_oscope_guts::get_trigger_level () const
375 return d_trigger_level;
379 gr_oscope_guts::get_samples_per_output_record () const
381 return OUTPUT_RECORD_SIZE;