#!/usr/bin/env python
#
# Copyright 2005 Free Software Foundation, Inc.
#
# This file is part of GNU Radio
#
# GNU Radio is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
#
# GNU Radio is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with GNU Radio; see the file COPYING.  If not, write to
# the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.
#

from gnuradio import gr, audio, eng_notation
from gnuradio.eng_option import eng_option
from optparse import OptionParser
import os

def bpsk_tx_graph (tone):

	fg = gr.flow_graph()

	sps = 256
        samp_freq = 8000
	data_rate = 31.25  # optimum IMD with 180 taps in filter, -42 to -50db

	# this uses a named pipe fifo (mkfifo) to get data from keyboard.py
	pipefd = os.open("/tmp/pskpipe", os.O_RDONLY)
	src = gr.file_descriptor_source(gr.sizeof_char,pipefd)

	bytes_to_syms = gr.bytes_to_syms()

	interp_taps = (1,) * sps
	interp = gr.interp_fir_filter_fff(sps, interp_taps)

	ps_coeffs = gr.firdes_root_raised_cosine ( \
		1,		# gain
		samp_freq,	# 
		data_rate,	# = baud for bpsk
		.36,		# .36 alpha = 1 - 16/25
		180 )		# number of taps 
	pulse_shaper = gr.fir_filter_fff(1,ps_coeffs)

	carrier = gr.sig_source_f(samp_freq, gr.GR_SIN_WAVE,tone,.9,0)
	mixer = gr.multiply_ff()

	dst = audio.sink(samp_freq)


        fg.connect(src, bytes_to_syms)
	fg.connect(bytes_to_syms, interp)
	fg.connect(interp,pulse_shaper)
	fg.connect(pulse_shaper,(mixer,0))
	fg.connect(carrier, (mixer,1))
	fg.connect(mixer,dst)

	return fg

def main ():

        parser = OptionParser (option_class=eng_option)
        parser.add_option ("-t", "--tone", type="eng_float", default=1e3,
                           help="output frequency")
        (options, args) = parser.parse_args ()

	fg = bpsk_tx_graph(options.tone)
	fg.start()
	raw_input('Press Enter to quit')
	fg.stop()

if __name__ == '__main__':
    main()
