: GW Instek GPD-3303S power supply
; 
; MarkW 	22 Dec 2021		  
; Controls work ok and data logs.  

; ***************************** Design Notes ****************************************
; *	3 Output supply but only 2 are controllable                                 *
; *     1mV/1mA resolution on controllable supplies                                 *
; *     Serial an parallel operation supported                                      *
; *	Fixed 9600N81                                                               *
; *                                                                                 *
; ***********************************************************************************
; *    Example data                                                                 *
; *    *idn?                                                                        *
; *    GW INSTEK,GPD-3303S,SN:EK901062,V1.05                                        *
; *    vout1?                                                                       *
; *    10.000V                                                                      *
; *    iout1?                                                                       *
; *    0.000A                                                                       *
; *    status?                                                                      *
; *    1 1 0 1 1 X 1 X                                                              *
; *    bit0:(CH1)0=CC,1=CV;bit1:(CH2)0=CC,1=CV;bit23=(TRACK)01=INDEP,11=SER,10=PAR; *
; *    bit4:(BEEP)0=OFF,1=ON;bit6:(OUT)0=OFF,1=ON;                                  *
; *                                                                                 *
; ***********************************************************************************

#author MarkW
#idString GW INSTEK,GPD-3303S, 
#name GWInstek GPD-3303S
#handle GPD3303
#port comfixedbaud
#baudrate 9600N81

; A list of possible column name with unit and formatter (SI, Time, Int, D0..D6)
#value CH1_V V d5V
#value CH1_I A d4A
#value CH2_V V d5V
#value CH2_I A d4A


; How to poll for data, this is used for table and #values?
; a #askMode, #cmdMode and #prepareSample is used before this is string is used.
; This is a single line command
#askValues VOUT1?; IOUT1?; VOUT2?; IOUT2?; 

; Format of answer: f=float, u=remove trailing letters, x=skip
;#askValuesReadFormat fufufufu
#askValuesReadFormat uuuu


; Accept this delay when reading values (seconds)
#readingDelay 5

; Mode change have a longer delay on reading values (seconds)
;#modeChangeDelay 10

; Switch meter to this mode during start, leave empty to avoid any switching
#initialMode

; String to ask about actual meter mode, 
; This is a single line command
#askMode 

; When one of these commands are used through the command interface a new configuration will be done before using #askMode
; Only one word for each #mayModifyMode
; Specify command without initial colon and in the shortest possible form
;#mayModifyMode 

; Prepare the meter to response to #askValues
#prepareSample

; Initial commands to meter when establishing connection
#initCmd  

; Final command to meter before breaking connection
#finalCmd 

#outputOff OUT0

; Strings to configure device in different modes
; First parameter must match a #value (4 parameter) and second parameter must match what #askMode returns
; First parameter is also used in shortcut menu
;#cmdMode 


#interfaceType PS PS:2
#interface setVoltage VSET(channel):(value)
#interface setCurrent ISET(channel):(value)
#interface getVoltage VOUT(channel)? 
#interface getCurrent IOUT(channel)?
;#interface getStatus STATUS?
;:readmath: listIndex(value,"OFF ON")
; The columns for the following two commands reflect the 4-1-2-3 ordering of the table
;fixme pickup here
;#interface readVoltage 3 6 9 0
;#interface readCurrent 4 7 10 1

; ************************* Menu functions follow ***************************************
; Setup menu functions
; The parameters on the first line is: type name page 
; Settings on the following lines are:
; read: SCPI to read value from device, is used to synchronize when opening setup page or setting equal named fields. Not always used
; readformat: Parse read value.
; write: Send the value to device, this field is used in combination with data fields in the details to send updates to device.
; tip: Add this tip to all components for this input field.


; *******************************************************************
; *                         CHANNEL 1                               *
; *   Independent:  Volts: 0 - 30      Amps: 0 - 3                  *
; *   Series:       Volts: 0 - 60      Amps: 0 - 3                  *
; *   Parallel:     Volts: 0 - 30      Amps: 0 - 6                  *
; *                          Yellow                                 *
; *******************************************************************

#cmdSetup color - Channel_1
(255,255,16)


#cmdSetup radio Voltage Channel_1
:read: VSET1?;
:readFormat: u
:write: VSET1:#;
:update: Output
:tip: Setup this voltage, turn output 1 on
3.3V 3.300
5V 5.000
12V 12.000
15V 15.000

#cmdSetup radio Current Channel_1
:read: ISET1?; 
:readFormat: u
:write: ISET1:#
:tip: Output 1 will current limit at this current
50mA 0.0500
500mA 0.5000
1A 1.0000
3A 3.0000

#cmdSetup number Voltage Channel_1
:read: VSET1?; 
:readformat: u
:write: VSET1:#
:tip: Setup this voltage, do not turn output 1 on/off
Volt 0 30
;:enable: inList(Channel_1.Channel_Mode, "IND,SER,PAR","[,]")

#cmdSetup number Current Channel_1
:read: ISET1?; 
:readformat: u
:write: ISET1:#
:tip: Setup this current, do not turn output 1 on/off
Amps 0.0001 3.0
;:enable: inList(Channel_1.Channel_Mode, "IND,SER,PAR","[,]")



; *******************************************************************
; *                         CHANNEL 2                               *
; *   Independent:  Volts: 0 - 30      Amps: 0 - 3                  *
; *   Series:       Slaved to Channel 1                             *
; *   Parallel:	    Slaved to Channel 1                             *
; *                           Cyan                                  *
; *******************************************************************

#cmdSetup color - Channel_2
(9,255,249)

#cmdSetup radio Voltage Channel_2
:read: VSET2?
:readFormat: u
:write: VSET2:#;
:update: Output
:tip: Setup this voltage, turn output 2 on
3.3V 3.300
5V 5.000
12V 12.000
15V 15.000
;:enable: inList(Channel_2.Channel_Mode, "IND,CV","[,]")

#cmdSetup radio Current Channel_2
:read: ISET2?
:readFormat: u
:write: ISET2:#
:tip: Output 2 will current limit at this current
50mA 0.0500
500mA 0.5000
1A 1.0000
3A 3.0000
;:enable: inList(Channel_2.Channel_Mode, "IND,SER,CC","[,]")

#cmdSetup number Voltage Channel_2
:read: VSET2?
:readformat: u
:write: VSET2:#
:tip: Setup this voltage, do not turn output 2 on/off
Volt 0 30
;:enable: inList(Channel_2.Channel_Mode, "IND,CV","[,]")

#cmdSetup number Current Channel_2
:read: ISET2?
:readformat: u
:write: ISET2:#
:tip: Setup this current, do not turn output 2 on/off
Amps 0.0001 3.0
;:enable: inList(Channel_2.Channel_Mode, "IND,SER,CC","[,]")



; *******************************************************************
; *                    COMMON CONTROLS                              *
; *******************************************************************

;#cmdSetup info Channel_Mode
;:read: STAT?
;;:readmath: getElement("Independent Source, Series Source (Master Ch1), Parallel Source (Master Ch1), CC Load, CV Load, CR Load",listIndex(value, "IND,SER,PAR,CC,CV,CR","[,]"),"[,]"))
;
;:readmath: getElement(value,0," ")
;:update: Status


;;bit0:(CH1)0=CC,1=CV;bit1:(CH2)0=CC,1=CV;bit23=(TRACK)01=INDEP,11=SER,10=PAR; *
;; *    bit4:(BEEP)0=OFF,1=ON;bit6:(OUT)0=OFF,1=ON;  
;#value Ch1CV
;#value Ch2CV
;#value TrackState 
;#value BeepState
;#value OutputState










#cmdSetup indicatornum PSMode
:read: STATUS?
:readmath: 10*getElement(value,2," ")+1*getElement(value,3," ")
;Indep
PSMode 01 green
;Series
PSMode 11 blue
;Parallel
PSMode 10 red


#cmdSetup indicatornum Ch1CC
:read: STATUS?
:readmath: getElement(value,0," ")
Ch1CC 0 red
Ch1CC 1 green

#cmdSetup indicatornum Ch2CC
:read: STATUS?
:readmath: getElement(value,1," ")
Ch2CC 0 red
Ch2CC 1 green

#cmdSetup indicatornum Beeper
:read: STATUS?
:readmath: getElement(value,4," ")
Beeper 1 green
Beeper 0 red


#cmdSetup indicatornum OutputOn
:read: STATUS?
:readmath: getElement(value,6," ")
OutputOn 1 green
OutputOn 0 red



#cmdSetup buttons ModeCtrl
Independent_Source_GREEN TRACK0
Series_Source_(MasterCh1)_BLUE TRACK1
Parallel_Source_(MasterCh1)_RED TRACK2
:updatealloff:
:update: Channel_1.Voltage Channel_1.Current Channel_2.Voltage Channel_2.Current PSMode 


#cmdSetup buttons BeepCtrl
:tip: Beeper on or off
:update: Beeper
Off BEEP0
On BEEP1



#cmdSetup buttons All_Outputs
:tip: Turn all outputs on or off
:update: Output PSMode OutputOn Ch1CC Ch1CC
Off OUT0
On OUT1

;add this so that constant current events are displayed properly or comment it out to cut down on traffic
#cmdSetup Updater update
:update:Ch1CC Ch2CC 
1

