# auto-generated by grc.converter

id: soapy_sink
label: Soapy Sink
flags: [python, throttle]

parameters:
-   id: type
    label: Input Type
    dtype: enum
    options: [fc32, sc16, sc8]
    option_labels: [Complex Float32, Complex Int16, Complex Byte]
    option_attributes:
        type: [fc32, sc16, sc8]
    hide: part
-   id: devname
    label: Device Type
    dtype: enum
    default: 'uhd'
    options: ['bladerf', 'hackrf', 'lime', 'plutosdr', 'sidekiq', 'uhd', 'xtrxsoapy', 'custom']
    option_labels: ['bladerf', 'hackrf', 'lime', 'plutosdr', 'sidekiq', 'uhd', 'xtrxsoapy', 'custom']
    hide: none
-   id: dev
    label: Device String
    dtype: string
    default: 'driver=uhd'
    hide: ${ 'none' if (devname == 'custom') else 'all' }
-   id: args
    label: Args
    dtype: string
-   id: samp_rate
    label: Sampling Rate
    dtype: float
    default: samp_rate
-   id: nchan
    label: Num Channels
    dtype: int
    default: '1'
    hide: part
-   id: clock_rate
    label: Master Clock Rate
    dtype: real
    default: '0'
    hide: part
-   id: clock_source
    label: Clock Source
    dtype: string
    hide: part
-   id: length_tag_name
    label: Length tag name
    dtype: string
    default: ''
-   id: center_freq0
    label: 'Ch0: Center Freq (Hz)'
    category: RF Options
    dtype: real
    default: freq
    hide: ${ 'none' if nchan > 0 else 'all' }
-   id: nco_freq0
    label: 'Ch0: NCO Freq (Hz)'
    category: RF Options
    dtype: real
    default: '0'
    hide: ${ 'all' if (devname == 'uhd' or devname == 'plutosdr') else 'part' }
-   id: manual_gain0
    label: 'Ch0: Enable Manual Gain'
    category: RF Options
    dtype: bool
    default: 'True'
    options: ['True', 'False']
    option_labels: [Manual, Auto]
    hide: part
-   id: overall_gain0
    label: 'Ch0: Overall Gain Value'
    category: RF Options
    dtype: real
    default: '10'
    hide: ${ 'none' if (not manual_gain0) else 'all' }
-   id: pga_gain0
    label: 'Ch0: PGA Gain Value'
    category: RF Options
    dtype: float
    default: '24'
    hide: ${ 'none' if ( (devname == 'plutosdr' or devname == 'lime' or devname == 'custom') and manual_gain0) else 'all' }
-   id: pad_gain0
    label: 'Ch0: PAD Gain Value'
    category: RF Options
    dtype: float
    default: '0'
    hide: ${ 'none' if ( ( devname == 'lime'  or devname == 'xtrxsoapy' or devname == 'custom') and manual_gain0 ) else 'all' }
-   id: iamp_gain0
    label: 'Ch0: IAMP Gain Value'
    category: RF Options
    dtype: float
    default: '0'
    hide: ${ 'none' if ( (devname == 'lime' or devname == 'custom') and manual_gain0 ) else 'all' }
-   id: vga_gain0
    label: 'Ch0: VGA Gain Value'
    category: RF Options
    dtype: float
    default: '10'
    hide: ${ 'none' if ( (devname == 'hackrf' or devname == 'custom') and manual_gain0 ) else 'all' }
-   id: amp_gain0
    label: 'Ch0: AMP Gain Value'
    category: RF Options
    dtype: float
    default: '0'
    hide: ${ 'none' if ( (devname == 'hackrf'  or devname == 'custom') and manual_gain0 ) else 'all' }
-   id: txvga1_gain
    label: 'Ch0: TXVGA1 Gain Value'
    category: RF Options
    dtype: float
    default: '-35'
    hide: ${ 'none' if ( (devname == 'bladerf'  or devname == 'custom') and manual_gain0 ) else 'all' }
-   id: txvga2_gain
    label: 'Ch0: TXVGA2 Gain Value'
    category: RF Options
    dtype: float
    default: '0'
    hide: ${ 'none' if ( (devname == 'bladerf'  or devname == 'custom') and manual_gain0 ) else 'all' }
-   id: ant0
    label: 'Ch0: Antenna'
    category: RF Options
    dtype: string
    default: 'TX'
    hide: ${ 'part' if nchan > 0 else 'all' }
-   id: bw0
    label: 'Ch0: Bandwidth (Hz)'
    category: RF Options
    dtype: real
    default: '0'
    hide: ${ 'part' if nchan > 0 else 'all' }
-   id: dc_offset_auto_mode0
    label: 'Ch0: Automatic DC Offset Mode'
    category: RF Options
    dtype: enum
    default: 'False'
    options: ['False', 'True']
    hide: ${ 'all' if ( devname == 'plutosdr' or devname == 'lime' ) else 'part' }
-   id: dc_offset0
    label: 'Ch0: DC Offset'
    category: RF Options
    dtype: complex
    default: '0'
    hide: ${ 'all' if ( (devname == 'uhd' or devname == 'lime' or devname == 'plutosdr') or dc_offset_auto_mode0 == 'True' ) else 'part' }
-   id: correction0
    label: 'Ch0: Frequency Correction'
    category: RF Options
    dtype: real
    default: '0'
    hide: ${ 'all' if ( devname == 'uhd' or devname == 'plutosdr' or devname == 'lime') else 'part' }
-   id: balance0
    label: 'Ch0: IQ Balance Correction'
    category: RF Options
    dtype: complex
    default: '0'
    hide: ${ 'all' if ( devname == 'uhd' or devname == 'plutosdr' or devname == 'lime') else 'part' }
-   id: gain_auto_mode0
    label: 'Ch0: Automatic Gain Control'
    category: RF Options
    dtype: bool
    default: 'False'
    options: ['False', 'True']
    hide: ${ 'all' if (devname == 'uhd' or devname=='plutosdr' or devname == 'lime') else 'part'}
-   id: gain_auto_mode1
    label: 'Ch1: Automatic Gain Control'
    category: RF Options
    dtype: bool
    default: 'False'
    options: ['False', 'True']
    hide: ${ 'all' if nchan <= 1 or devname == 'uhd' or devname=='plutosdr' or devname == 'lime' else 'part'}
-   id: center_freq1
    label: 'Ch1: Center Freq (Hz)'
    category: RF Options
    dtype: real
    default: '100.0e6'
    hide: ${ 'none' if nchan > 1 else 'all' }
-   id: nco_freq1
    label: 'Ch1: NCO Freq (Hz)'
    category: RF Options
    dtype: real
    default: '0'
    hide: 'all'
-   id: manual_gain1
    label: 'Ch1: Gain Mode'
    category: RF Options
    dtype: bool
    default: 'True'
    options: ['True', 'False']
    option_labels: [Manual, Auto]
    hide: ${ 'all' if nchan < 2 else 'part' }
-   id: overall_gain1
    label: 'Ch1: Gain Value'
    category: RF Options
    dtype: real
    default: '0'
    hide: ${ 'none' if nchan >1 and ( (devname == 'bladerf' or devname == 'uhd' or devname == 'plutosdr' or devname == 'lime' or devname == 'custom') and manual_gain1 ) else 'all' }
-   id: pga_gain1
    label: 'Ch1: PGA Gain Value'
    category: RF Options
    dtype: float
    default: '0'
    hide: ${ 'none' if ( (devname == 'plutosdr' or devname == 'lime' or devname == 'custom') and manual_gain1 and nchan > 1) else 'all' }
-   id: pad_gain1
    label: 'Ch1: PAD Gain Value'
    category: RF Options
    dtype: float
    default: '0'
    hide: ${ 'none' if (devname == 'lime' or devname == 'xtrxsoapy' or devname == 'custom') and nchan >1 and manual_gain1 else 'all' }
-   id: iamp_gain1
    label: 'Ch1: IAMP Gain Value'
    category: RF Options
    dtype: float
    default: '0'
    hide: ${ 'none' if (devname == 'lime' or devname == 'custom') and nchan >1 and manual_gain1 else 'all' }
-   id: ant1
    label: 'Ch1: Antenna'
    category: RF Options
    dtype: string
    default: 'TX'
    hide: ${ 'part' if nchan > 1 else 'all' }
-   id: bw1
    label: 'Ch1: Bandwidth (Hz)'
    category: RF Options
    dtype: real
    default: '0'
    hide: ${ 'part' if nchan > 1 else 'all' }
-   id: dc_offset_auto_mode1
    label: 'Ch1: Automatic DC Offset Mode'
    category: RF Options
    dtype: enum
    default: 'False'
    options: ['False', 'True']
    hide: ${ 'all' if nchan <=1 or (devname == 'plutosdr' or devname == 'lime' ) else 'part' }
-   id: dc_offset1
    label: 'Ch1: DC Offset'
    category: RF Options
    dtype: complex
    default: '0'
    hide: ${ 'all' if nchan <=1 or ( devname == 'uhd' or devname == 'plutosdr' or devname == 'lime' ) or dc_offset_auto_mode1 == 'True' else 'part' }
-   id: correction1
    label: 'Ch1: Frequency Correction'
    category: RF Options
    dtype: real
    default: '0'
    hide: ${ 'all' if nchan <=1 or ( devname == 'uhd' or devname == 'plutosdr' or devname == 'lime' ) else 'part' }
-   id: balance1
    label: 'Ch1: IQ Balance Correction'
    category: RF Options
    dtype: complex
    default: '0'
    hide: ${ 'all' if nchan <=1 or ( devname == 'uhd' or devname == 'plutosdr' or devname == 'lime' ) else 'part' }

inputs:
-   domain: stream
    dtype: ${ type.type }
    multiplicity: ${ nchan }
-   domain: message
    id: cmd
    optional: True
asserts:
- ${ nchan > 0 }

templates:
    imports: from gnuradio import soapy
    make: |-
        None
        if "${devname}" == 'custom':
            dev = ${dev}
            devname = dev.split("=", 1)[1]
        else:
            dev = 'driver=' + "${devname}"
            devname = "${devname}"

        self.${id} = soapy.sink(dev, "${type}", ${nchan}, ${args}, '', [''], [''])

        self.${id}.set_sample_rate(0, ${samp_rate})
        % if context.get('nchan')() > 1:
        self.${id}.set_sample_rate(1, ${samp_rate})
        % endif

        if ${length_tag_name}:
            self.${id}.set_length_tag_name(${length_tag_name})

        self.${id}.set_gain_mode(0,${gain_auto_mode0})
        if ${nchan} > 1:
            self.${id}.set_gain_mode(1,${gain_auto_mode1})

        self.${id}.set_frequency(0, ${center_freq0})
        if ${nchan} > 1:
            self.${id}.set_frequency(1, ${center_freq1})

        # Made antenna sanity check more generic
        antList = self.${id}.list_antennas(0)

        if len(antList) > 1:
            # If we have more than 1 possible antenna
            if len(${ant0}) == 0 or ${ant0} not in antList:
                print("ERROR: Please define ant0 to an allowed antenna name.")
                strAntList = str(antList).lstrip('(').rstrip(')').rstrip(',')
                print("Allowed antennas: " + strAntList)
                exit(0)

            self.${id}.set_antenna(0,${ant0})

        if ${nchan} > 1:
            antList = self.${id}.list_antennas(1)
            # If we have more than 1 possible antenna
            if len(antList) > 1:
                if len(${ant1}) == 0 or ${ant1} not in antList:
                    print("ERROR: Please define ant1 to an allowed antenna name.")
                    strAntList = str(antList).lstrip('(').rstrip(')').rstrip(',')
                    print("Allowed antennas: " + strAntList)
                    exit(0)

                self.${id}.set_antenna(1,${ant1})

        # Setup IQ Balance
        if devname != 'uhd' and devname != 'lime':
            if (self.${id}.has_iq_balance(0)):
                self.${id}.set_iq_balance(0,${balance0})

            if ${nchan} > 1:
                if (self.${id}.has_iq_balance(1)):
                    self.${id}.set_iq_balance(1,${balance1})

        # Setup Frequency correction
        if (self.${id}.has_frequency_correction(0)):
            self.${id}.set_frequency_correction(0,${correction0})

        if ${nchan} > 1:
            if (self.${id}.has_frequency_correction(1)):
                self.${id}.set_frequency_correction(1,${correction1})

        if devname == 'sidekiq' or "${manual_gain0}" == 'False':
            self.${id}.set_gain(0,${overall_gain0})
            if ${nchan} > 1:
                self.${id}.set_gain(1,${overall_gain1})
        else:
            if devname == 'bladerf':
                 self.${id}.set_gain(0,"txvga1", ${txvga1_gain})
                 self.${id}.set_gain(0,"txvga2", ${txvga2_gain})
            elif devname == 'uhd':
                self.${id}.set_gain(0,"PGA", ${pga_gain0})
                if ${nchan} > 1:
                    self.${id}.set_gain(1,"PGA", ${pga_gain1})
            else:
                self.${id}.set_gain(0,"PGA", ${pga_gain0})
                self.${id}.set_gain(0,"PAD", ${pad_gain0})
                self.${id}.set_gain(0,"IAMP", ${iamp_gain0})
                self.${id}.set_gain(0,"txvga1", ${txvga1_gain})
                self.${id}.set_gain(0,"txvga2", ${txvga2_gain})
                # Only hackrf uses VGA name, so just ch0
                self.${id}.set_gain(0,"VGA", ${vga_gain0})

                if ${nchan} > 1:
                    self.${id}.set_gain(1,"PGA", ${pga_gain1})
                    self.${id}.set_gain(1,"PAD", ${pad_gain1})
                    self.${id}.set_gain(1,"IAMP", ${iamp_gain1})


    callbacks:
    - set_frequency(0, ${center_freq0})
    - set_frequency(0,"BB",${nco_freq0})
    - set_antenna(1,${ant1})
    - set_bandwidth(1,${bw1})
    - set_antenna(0,${ant0})
    - set_bandwidth(0,${bw0})
    - set_frequency(1, ${center_freq1})
    - set_gain(0,${overall_gain0})
    - set_gain(1,${overall_gain1})
    - set_gain(0,"PGA", ${pga_gain0})
    - set_gain(1,"PGA", ${pga_gain1})
    - set_gain(0,"PAD", ${pad_gain0})
    - set_gain(1,"PAD", ${pad_gain1})
    - set_gain(0,"IAMP", ${iamp_gain0})
    - set_gain(1,"IAMP", ${iamp_gain1})
    - set_gain(0,"VGA", ${vga_gain0})
    - set_gain(0,"AMP", ${amp_gain0})
    - set_gain(0,"txvga1", ${txvga1_gain})
    - set_gain(0,"txvga2", ${txvga2_gain})
    - set_frequency_correction(0,${correction0})
    - set_frequency_correction(1,${correction1})
    - set_iq_balance(0,${balance0})
    - set_iq_balance(1,${balance1})
    - set_dc_offset(0,${dc_offset0},${dc_offset_auto_mode0} == 'True')
    - set_dc_offset(1,${dc_offset1},${dc_offset_auto_mode1} == 'True')

documentation: |-
    Soapy Sink Block:
    Device:
    The device argument is a comma delimited string used to create a device
    instance that uses the specified driver.  Device driver should always be specified.

      Usage examples:
        Device: driver=limesdr
        Device: driver=uhd,type=b200
        Device: driver=uhd,serial=12345678

    Args:
    Comma delimited list of key-word dictionary arguments specific for each device.
    Device driver handles internally invalid arguments.

      Usage examples:
        Device: driver=lime
        Args: skipCal=True
        Device: driver=airspy
        Args: biastee=true

    Sample Rate:
    The sample rate is the number of samples per second input by this block.
    If the requested rate is not possible the driver
    decides internally how to handle it

    Center frequency:
    The center frequency of the RF chain. If the device cannot tune to the
    specified frequency the driver decides ninternally how to handle it.

    Master Clock Rate:
    The rate of the master clock. Can be assigned zero for automatic tuning.

    Clock Source:
    Specifies a valid external clock source. Can be left empty.

    Input Type:
    This parameter controls the data type of the stream in GNU-Radio.

    Gain:
    Depending the device driver specified in the Device field various
    gains parameters can be configured. If a driver is not explicitly
    supported from the block an Overal Gain parameter can be configured.

      E.g 1:
        Gain Value: 10
      E.g 2:
        Device: driver=uhd
        PGA Gain: 10
    Antenna:
    Specified antenna used in RF chain.
    Can be left empty for auto assignment, else it must correspond to an
    antenna name of the device used.

    Bandwidth:
    The digital filter bandwidth of the RF chain.
    Can be left empty for auto assignment, else it must be a correct filter
    bandwidth of the device.

file_format: 1
