# keithley 2400 sourcemeter
namespace eval ksm2400 {} {
}

proc stdConfig::ksm2400 {} {
  controller std "\n" 5
  prop startcmd ":CONF:CURR:DC;;:SOUR:CURR:LEV 0;:OUTP 0;*IDN?"

  obj Keithley2400 rd -float
  prop read ksm2400::read
  kids ksm {
    node Enable wr -int
    default 0
    prop label Current
    prop writecmd ":OUTP %d;:OUTP?"
    prop readcmd ":OUTP?"
    prop readfmt "%d"
    prop enum off,on
    
    node Llim wr
    default 2.0
    prop label set_Voltage_Limit
    prop writecmd ":SENS:VOLT:PROT:LEV %g;:SENS:VOLT:PROT:LEV?"
    prop readcmd ":SENS:VOLT:PROT:LEV?"
     prop readfmt "%g"

    node Vtrip rd
    prop label VoltageTrip
    prop readcmd ":SENS:VOLT:PROT:TRIP?"
    prop readfmt "%g"
        

    node iset wr
    prop writecmd ":SOUR:CURR:LEV %g;:SOUR:CURR:LEV:AMPL?"
    prop readcmd ":SOUR:CURR:LEV:AMPL?"
    prop readfmt "%g"

   node  Vmas rd
   prop label Voltagemeas
   prop readcmd ":READ?"
   prop readfmt "%g"

  }
}

proc ksm2400::read {} {
  set mode [hvali [sct]/Enable]
  if {$mode == 0} {
   sct send ":SOUR:CURR:LEV?"
  } else {
   sct send ":READ?"
  }
  return ksm2400::update 
}

proc ksm2400::update {} {
  set mode [hvali [sct]/Enable]
  if {$mode ==0} {
     sct update [sct result]
  } else {
    set dat [split [sct result] "," ]
    sct update [lindex $dat 1]
  }
  if { [hvali [sct]/Vtrip] } {
    sct geterror "Voltage Compliance reached"
  } 
  return idle
}

proc ksm2400::enable {} {

 set Enable  [sct target]
 sct send ":OUTP $Enable;:OUTP?"
  return ksm2400::updateenable
}  

proc ksm2400::updateenable {} {
  sct update [sct result]
  return idle
}


