'
' Windows script for generating arbitary waveform s19 file 
' automaticly.
'
' Todo: add or modify the key points ( turning points ) into/
' in the arrays named "Amplitude" and "Timeline".
'
' This script will auto fill the whole time scale with linear
' value according to the key points.
'
' Copyright Tan Yi, SUNIST LABORATORY
' http://www.sunist.org
' 2005-06-14

' Key points definition, make sure these two array have the 
' same length.
' Modification Needed Here Only!!!!!!!!!!
' ToDo: set the two array to match the waveform you need.
' Amplitude array, unit: Ampere
Dim Amplitude
Amplitude = Array(	0,	100,	100,	100,	100,	100,	0)
' Timeline array, unit: ms
Dim Timeline
Timeline = Array(	0,	1,	5,	10,	20,	99,	100)

' Constants definition.
Const ForReading = 1, ForWriting = 2
Const NMAX = 255	' 8 bits DAC's max digital value.

' ¦¤T=0.1ms, maximum output current when digits equal to NMAX 
' is 100 amperes.
Const DeltaT = 0.1, Imax = 100

' Some variables.
Dim fso, wsh, f, length, org, i, k, j, tmp, MainSource, DataSource, Assembler

' Three key files' name.
MainSource = "PulseGen.asm"
DataSource = "Waveform.inc"
Assembler = "CASM08Z.EXE"

Set wsh = WScript.CreateObject("WScript.Shell")

' Determine If the two arrays are with same length >= 2
If Not Ubound(Amplitude) = Ubound(Timeline) Or Not Ubound(Amplitude)>=1 _
	Or Not Ubound(Timeline)>=1 Then
	wsh.Popup "Amplitude array and Timeline array must have the same length >= 2£¡", , "Error", 0 + 16
	WScript.Quit
End If

' Determine If the Amplitude array is below the Imax.
For i= 0 to Ubound(Amplitude)
	If Not Amplitude(i) <= Imax Or Not Amplitude(i) >= 0 Then
		wsh.Popup "Amplitude must be more than 0 and less than Imax: "&Imax&"A£¡", , "Error", 0 + 16
		WScript.Quit
	End If
Next

' Determine If the Timeline array is increasing.
For i= 1 to Ubound(Timeline)
	If Not Timeline(i) > Timeline(i-1) Then
		wsh.Popup "Timeline must be positive and increasing£¡", , "Error", 0 + 16
		WScript.Quit
	End If
Next


' File objects.
Set fso = CreateObject("Scripting.FileSystemObject")

' Two key file must be in the same directory.
If Not fso.FileExists(MainSource) Or Not fso.FileExists(Assembler) Then
	wsh.Popup "Key file " & MainSource & " or " & Assembler & " not found!", , "Error", 0 + 16
	WScript.Quit
End If

' Assemble the new waveform.
wsh.Run Assembler & " " & MainSource & " " & "s", 1, false
WScript.Sleep 1000
wsh.SendKeys "{ENTER}"

' Done.
wsh.Popup "Congratulations! New s19 file including waveform generated!", 2, "Done", 0 + 64
Set wsh = nothing
WScript.Quit
