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 3, 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 (double sample_rate, gr_msg_queue_sptr msgq)
57 d_trigger_mode (gr_TRIG_MODE_AUTO),
58 d_trigger_slope (gr_TRIG_SLOPE_POS),
59 d_trigger_channel (0),
60 d_sample_rate (sample_rate),
65 d_decimator_count (0),
66 d_decimator_count_init (1),
68 d_hold_off_count_init (OUTPUT_RECORD_SIZE/2-1),
69 d_pre_trigger_count (0),
70 d_post_trigger_count (0),
71 d_post_trigger_count_init (OUTPUT_RECORD_SIZE/2)
73 for (int i = 0; i < MAX_CHANNELS; i++)
76 for (int i = 0; i < MAX_CHANNELS; i++){
77 d_buffer[i] = new float [OUTPUT_RECORD_SIZE];
78 for (int j = 0; j < OUTPUT_RECORD_SIZE; j++)
82 // be sure buffer is full before first write
84 update_rate_or_decimation_changed ();
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
113 if (d_hold_off_count <= 0)
114 enter_look_for_trigger ();
117 case LOOK_FOR_TRIGGER:
118 if (found_trigger ())
119 enter_post_trigger ();
123 d_post_trigger_count--;
124 if (d_post_trigger_count <= 0){
125 write_output_records ();
134 d_obi = incr_bi (d_obi);
138 * Functions called on state entry
142 gr_oscope_guts::enter_hold_off ()
145 d_hold_off_count = d_hold_off_count_init;
149 gr_oscope_guts::enter_look_for_trigger ()
151 d_pre_trigger_count = 0;
152 d_state = LOOK_FOR_TRIGGER;
156 gr_oscope_guts::enter_post_trigger ()
158 d_state = POST_TRIGGER;
159 d_post_trigger_count = d_post_trigger_count_init;
160 //ensure that the trigger offset is no more than than half a sample
161 if (d_trigger_off > .5) d_trigger_off -= 1;
162 else d_post_trigger_count--;
165 // ----------------------------------------------------------------
166 // returns true if trigger found
169 gr_oscope_guts::found_trigger ()
171 float prev_sample = d_buffer[d_trigger_channel][decr_bi(d_obi)];
172 float new_sample = d_buffer[d_trigger_channel][d_obi];
174 switch (d_trigger_mode){
176 case gr_TRIG_MODE_AUTO: //too many samples without a trigger
177 d_pre_trigger_count++;
178 if (d_pre_trigger_count > OUTPUT_RECORD_SIZE/2) return true;
180 case gr_TRIG_MODE_NORM: //look for trigger
181 switch (d_trigger_slope){
183 case gr_TRIG_SLOPE_POS: //trigger point in pos slope?
184 if (new_sample < d_trigger_level || prev_sample >= d_trigger_level) return false;
187 case gr_TRIG_SLOPE_NEG: //trigger point in neg slope?
188 if (new_sample > d_trigger_level || prev_sample <= d_trigger_level) return false;
192 //calculate the trigger offset in % sample
193 d_trigger_off = (d_trigger_level - prev_sample)/(new_sample - prev_sample);
196 case gr_TRIG_MODE_FREE: //free run mode, always trigger
206 // ----------------------------------------------------------------
207 // write output records (duh!)
210 gr_oscope_guts::write_output_records ()
212 // if the output queue if full, drop the data like its hot.
213 if (d_msgq->full_p())
215 // Build a message to hold the output records
216 gr_message_sptr msg =
217 gr_make_message(0, // msg type
218 d_nchannels, // arg1 for other side
219 OUTPUT_RECORD_SIZE, // arg2 for other side
220 ((d_nchannels * OUTPUT_RECORD_SIZE) + 1) * sizeof(float)); // sizeof payload
222 float *out = (float *)msg->msg(); // get pointer to raw message buffer
224 for (int ch = 0; ch < d_nchannels; ch++){
225 // note that d_obi + 1 points at the oldest sample in the buffer
226 for (int i = 0; i < OUTPUT_RECORD_SIZE; i++){
227 out[i] = d_buffer[ch][wrap_bi(d_obi + 1 + i)];
229 out += OUTPUT_RECORD_SIZE;
231 //Set the last sample as the trigger offset:
232 // The non gl scope sink will not look at this last sample.
233 // The gl scope sink will use this last sample as an offset.
234 out[0] = d_trigger_off;
235 d_msgq->handle(msg); // send the msg
238 // ----------------------------------------------------------------
241 gr_oscope_guts::set_update_rate (double update_rate)
243 d_update_rate = std::min (std::max (1./10., update_rate), d_sample_rate);
244 update_rate_or_decimation_changed ();
249 gr_oscope_guts::set_decimation_count (int decimator_count)
251 decimator_count = std::max (1, decimator_count);
252 d_decimator_count_init = decimator_count;
253 update_rate_or_decimation_changed ();
258 gr_oscope_guts::set_sample_rate(double sample_rate)
260 d_sample_rate = sample_rate;
261 return set_update_rate(update_rate());
266 gr_oscope_guts::update_rate_or_decimation_changed ()
268 d_hold_off_count_init =
269 (int) rint (d_sample_rate / d_update_rate / d_decimator_count_init);
273 gr_oscope_guts::set_trigger_channel (int channel)
275 if (channel >= 0 && channel < d_nchannels){
276 d_trigger_channel = channel;
285 gr_oscope_guts::set_trigger_mode (gr_trigger_mode mode)
287 d_trigger_mode = mode;
293 gr_oscope_guts::set_trigger_slope (gr_trigger_slope slope)
295 d_trigger_slope = slope;
301 gr_oscope_guts::set_trigger_level (double trigger_level)
303 d_trigger_level = trigger_level;
309 gr_oscope_guts::set_trigger_level_auto ()
311 // find the level 1/2 way between the min and the max
313 float min_v = d_buffer[d_trigger_channel][0];
314 float max_v = d_buffer[d_trigger_channel][0];
316 for (int i = 1; i < OUTPUT_RECORD_SIZE; i++){
317 min_v = std::min (min_v, d_buffer[d_trigger_channel][i]);
318 max_v = std::max (max_v, d_buffer[d_trigger_channel][i]);
320 return set_trigger_level((min_v + max_v) * 0.5);
324 gr_oscope_guts::set_num_channels(int nchannels)
326 if (nchannels > 0 && nchannels <= MAX_CHANNELS){
327 d_nchannels = nchannels;
335 gr_oscope_guts::trigger_changed ()
337 enter_look_for_trigger ();
343 gr_oscope_guts::num_channels () const
349 gr_oscope_guts::sample_rate () const
351 return d_sample_rate;
355 gr_oscope_guts::update_rate () const
357 return d_update_rate;
361 gr_oscope_guts::get_decimation_count () const
363 return d_decimator_count_init;
367 gr_oscope_guts::get_trigger_channel () const
369 return d_trigger_channel;
373 gr_oscope_guts::get_trigger_mode () const
375 return d_trigger_mode;
379 gr_oscope_guts::get_trigger_slope () const
381 return d_trigger_slope;
385 gr_oscope_guts::get_trigger_level () const
387 return d_trigger_level;
391 gr_oscope_guts::get_samples_per_output_record () const
393 return OUTPUT_RECORD_SIZE;