GeneratorArbitrary.vb

' GeneratorArbitrary.vb
'
' This example generates an arbitrary waveform.
'
' Find more information on http://www.tiepie.com/LibTiePie .

Imports System
Imports System.Math
Imports TiePie.LibTiePie

Module GeneratorArbitraryExample

    Sub Main()
        ' Print library information:
        PrintLibraryInfo()

        ' Enable network search:
        Network.AutoDetectEnabled = True

        ' Update device list:
        DeviceList.Update()

        ' Try to open a generator with arbitrary support:
        Dim gen As Generator = Nothing

        If DeviceList.Count <> 0 Then
            For i As UInt32 = 0 To DeviceList.Count - 1
                Dim item As DeviceListItem = DeviceList.GetItemByIndex(i)
                If item.CanOpen(DeviceType.Generator) Then
                    gen = item.OpenGenerator()

                    ' Check for arbitrary support:
                    If (gen.SignalTypes And Constants.ST_ARBITRARY) <> 0 Then
                        Exit For
                    Else
                        gen.Dispose()
                        gen = Nothing
                    End If
                End If
            Next
        End If

        If Not IsNothing(gen) Then
            Try
                ' Set signal type:
                gen.SignalType = SignalType.Arbitrary

                ' Select frequency mode:
                gen.FrequencyMode = FrequencyMode.SampleFrequency

                ' Set frequency:
                gen.Frequency = 100000.0 ' 100 kHz

                ' Set amplitude:
                gen.Amplitude = 2.0 ' 2 V

                ' Set offset:
                gen.Offset = 0.0 ' 0 V

                ' Enable output:
                gen.OutputOn = True

                ' Create signal array:
                Dim data(8191) As Single
                For i As UInt16 = 0 To data.Length - 1
                    data(i) = Sin(i / 100) * (1 - (i / 8192))
                Next

                ' Load the signal array into the generator:
                gen.SetData(data)

                ' Print generator info:
                PrintDeviceInfo(gen)

                ' Start signal generation:
                gen.Start()

                ' Wait for keystroke:
                Console.WriteLine("Press Enter to stop signal generation...")
                Console.ReadLine()

                ' Stop generator:
                gen.Stop()

                ' Disable output:
                gen.OutputOn = False

            Catch e As System.Exception
                Console.WriteLine("Exception: " + e.Message)
                Environment.Exit(1)
            End Try

            ' Close generator:
            gen.Dispose()
            gen = Nothing
        Else
            Console.WriteLine("No generator available with arbitrary support!")
            Environment.Exit(1)
        End If

        Environment.Exit(0)
    End Sub

End Module