# keithley 2182 cmnnanovoltmeter
namespace eval cmnnanov {} {
}

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

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

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

proc cmnnanov::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} {
    hupdate [sct]/temp [expr 0.001*(1.62 + 15790.0/($volt*1000. - 58.92))]
  }
  if {$mode != 0} {
    return idle
  }
  set chan [expr 3-$chan]
  sct send ";SENS:CHAN $chan;:SENS:CHAN?"
  sct channel $chan
  return stdSct::complete
}

proc cmnnanov::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 cmnnanov::updatechan
}

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


