# keithley 2182 nanovoltmeter with resistivity
namespace eval resnanov {} {
}

proc stdConfig::resnanov {} {
  controller std "\n" 5
  prop startcmd "*IDN?"

  obj Keithley2182 rd -none
  prop read resnanov::read
  kids resistivity {
    node u1 upd
    node res upd
    node u2 upd
    node chan out
    default 0
    prop write resnanov::setchan
    prop enum auto,chan1,chan2
  }
}

proc resnanov::read {} {
  sct send ":FETCH?"
  return resnanov::update 
}

proc resnanov::update {} {
  set mode [hvali [sct]/chan]
  if {$mode == 0} {
    set chan [silent 1 sct channel]
  } else {
    set chan $mode
  }
  hupdate [sct]/u$chan [sct result]
  hdelprop [sct]/u$chan geterror
  set volt [hvali [sct]/u1]
  if {$chan == 1} {
    set cur [result tt current/manualpower]
    if {$cur} {
      set res [expr $volt / $cur]
    } else {
      set res 0
    }
    hupdate [sct]/res $res
  }
  if {$mode != 0} {
    return idle
  }
  set chan [expr 3-$chan]
  sct send ";SENS:CHAN $chan;:SENS:CHAN?"
  sct channel $chan
  return stdSct::complete
}

proc resnanov::setchan {} {
  set mode [sct target]
  if {$mode == 0} {
    sct update $mode
    return idle
  }
  set chan $mode
  sct send ";SENS:CHAN $chan;:SENS:CHAN?"
  sct channel $chan
  return resnanov::updatechan
}

proc resnanov::updatechan {} {
  sct update [sct result]
  return idle
}


