Statistics
| Branch: | Tag: | Revision:

root / host / examples / rx_ascii_art_dft.cpp @ 395bbbbc

History | View | Annotate | Download (5.71 KB)

1
//
2
// Copyright 2010 Ettus Research LLC
3
//
4
// This program is free software: you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation, either version 3 of the License, or
7
// (at your option) any later version.
8
//
9
// This program is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
// GNU General Public License for more details.
13
//
14
// You should have received a copy of the GNU General Public License
15
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
//
17

    
18
#include <uhd/utils/thread_priority.hpp>
19
#include <uhd/utils/safe_main.hpp>
20
#include <uhd/usrp/single_usrp.hpp>
21
#include "ascii_art_dft.hpp" //implementation
22
#include <boost/program_options.hpp>
23
#include <boost/thread.hpp> //gets time
24
#include <boost/format.hpp>
25
#include <curses.h>
26
#include <iostream>
27
#include <complex>
28

    
29
namespace po = boost::program_options;
30

    
31
int UHD_SAFE_MAIN(int argc, char *argv[]){
32
    uhd::set_thread_priority_safe();
33

    
34
    //variables to be set by po
35
    std::string args;
36
    size_t num_bins;
37
    double rate, freq, gain, frame_rate;
38
    float ref_lvl, dyn_rng;
39

    
40
    //setup the program options
41
    po::options_description desc("Allowed options");
42
    desc.add_options()
43
        ("help", "help message")
44
        ("args", po::value<std::string>(&args)->default_value(""), "single uhd device address args")
45
        // hardware parameters
46
        ("rate", po::value<double>(&rate), "rate of incoming samples (sps)")
47
        ("freq", po::value<double>(&freq)->default_value(0), "RF center frequency in Hz")
48
        ("gain", po::value<double>(&gain)->default_value(0), "gain for the RF chain")
49
        // display parameters
50
        ("num-bins", po::value<size_t>(&num_bins)->default_value(512), "the number of bins in the DFT")
51
        ("frame-rate", po::value<double>(&frame_rate)->default_value(5), "frame rate of the display (fps)")
52
        ("ref-lvl", po::value<float>(&ref_lvl)->default_value(0), "reference level for the display (dB)")
53
        ("dyn-rng", po::value<float>(&dyn_rng)->default_value(60), "dynamic range for the display (dB)")
54
    ;
55
    po::variables_map vm;
56
    po::store(po::parse_command_line(argc, argv, desc), vm);
57
    po::notify(vm);
58

    
59
    //print the help message
60
    if (vm.count("help") or not vm.count("rate")){
61
        std::cout << boost::format("UHD RX ASCII Art DFT %s") % desc << std::endl;
62
        return ~0;
63
    }
64

    
65
    //create a usrp device
66
    std::cout << std::endl;
67
    std::cout << boost::format("Creating the usrp device with: %s...") % args << std::endl;
68
    uhd::usrp::single_usrp::sptr sdev = uhd::usrp::single_usrp::make(args);
69
    std::cout << boost::format("Using Device: %s") % sdev->get_pp_string() << std::endl;
70

    
71
    //set the rx sample rate
72
    std::cout << boost::format("Setting RX Rate: %f Msps...") % (rate/1e6) << std::endl;
73
    sdev->set_rx_rate(rate);
74
    std::cout << boost::format("Actual RX Rate: %f Msps...") % (sdev->get_rx_rate()/1e6) << std::endl << std::endl;
75

    
76
    //set the rx center frequency
77
    std::cout << boost::format("Setting RX Freq: %f Mhz...") % (freq/1e6) << std::endl;
78
    sdev->set_rx_freq(freq);
79
    std::cout << boost::format("Actual RX Freq: %f Mhz...") % (sdev->get_rx_freq()/1e6) << std::endl << std::endl;
80

    
81
    //set the rx rf gain
82
    std::cout << boost::format("Setting RX Gain: %f dB...") % gain << std::endl;
83
    sdev->set_rx_gain(gain);
84
    std::cout << boost::format("Actual RX Gain: %f dB...") % sdev->get_rx_gain() << std::endl << std::endl;
85

    
86
    //allocate recv buffer and metatdata
87
    uhd::rx_metadata_t md;
88
    std::vector<std::complex<float> > buff(num_bins);
89
    //------------------------------------------------------------------
90
    //-- Initialize
91
    //------------------------------------------------------------------
92
    initscr(); //curses init
93
    sdev->issue_stream_cmd(uhd::stream_cmd_t::STREAM_MODE_START_CONTINUOUS);
94
    boost::system_time next_refresh = boost::get_system_time();
95

    
96
    //------------------------------------------------------------------
97
    //-- Main loop
98
    //------------------------------------------------------------------
99
    while (true){
100
        //read a buffer's worth of samples every iteration
101
        size_t num_rx_samps = sdev->get_device()->recv(
102
            &buff.front(), buff.size(), md,
103
            uhd::io_type_t::COMPLEX_FLOAT32,
104
            uhd::device::RECV_MODE_FULL_BUFF
105
        );
106
        if (num_rx_samps != buff.size()) continue;
107

    
108
        //check and update the display refresh condition
109
        if (boost::get_system_time() < next_refresh) continue;
110
        next_refresh = boost::get_system_time() + boost::posix_time::microseconds(long(1e6/frame_rate));
111

    
112
        //calculate the dft and create the ascii art frame
113
        acsii_art_dft::log_pwr_dft_type lpdft(
114
            acsii_art_dft::log_pwr_dft(&buff.front(), num_rx_samps)
115
        );
116
        std::string frame = acsii_art_dft::dft_to_plot(
117
            lpdft, COLS, LINES,
118
            sdev->get_rx_rate(),
119
            sdev->get_rx_freq(),
120
            dyn_rng, ref_lvl
121
        );
122

    
123
        //curses screen handling: clear and print frame
124
        clear();
125
        printw("%s", frame.c_str());
126

    
127
        //curses key handling: no timeout, any key to exit
128
        timeout(0);
129
        int ch = getch();
130
        if (ch != KEY_RESIZE and ch != ERR) break;
131
    }
132

    
133
    //------------------------------------------------------------------
134
    //-- Cleanup
135
    //------------------------------------------------------------------
136
    sdev->issue_stream_cmd(uhd::stream_cmd_t::STREAM_MODE_STOP_CONTINUOUS);
137
    endwin(); //curses done
138

    
139
    //finished
140
    std::cout << std::endl << "Done!" << std::endl << std::endl;
141

    
142
    return 0;
143
}