#----- LakeShore 370 -----
proc makeLs370 {{name LS370} {obj ts}} {
  makenv $obj lsc370
  if {[string equal ts $obj]} {
    Layout ls370 $name
    defineTemperature ts
  } else {
    Layout ls370a $obj
  }
}

proc ls370Group {obj} {
  showStatus $obj
  if {[result $obj ighHeater] >= 0} {
    Tip "maximal heater power (mW)"
    Input "max. heater power" "ls370ighpower"
  }
  Group ls370Control "Control Parameters" $obj
  Group ls370Range "Reading Parameters" $obj
}

proc ls370aGroup {obj} {
  showStatus $obj
  Group ls370Control "Control Parameters" $obj
  Group ls370Range "Reading Parameters" $obj
}

proc ls370ControlGroup {obj} {
  RadioGroup "ts ighHeater"
  RadioButton -1 weakLink
  set val [result ts ighHeater]
  if {$val == -1} {set val 0}
  RadioButton $val noWeakLink
#  Label "IDR range must be -1 for weak link"
#  Tip "this is controlled by max power for no weak link"
#  Input "IDR range" "ts ighHeater"
  Tip "proportional control parameter (gain: the higher, the faster)" 
  Input "prop" "$obj prop"
  Tip "integral control parameter (seconds)"
  Input "integ" "$obj integ"
  Tip "derivative control parameter (seconds)"
  Input "deriv" "$obj deriv"
  Newline
  Tip "heater range (0...8)"
  Input "heater range" "$obj htrRange"
  Label "[$obj enumText htrRange] / [result $obj maxPower] mW"
  Newline
  Tip "tolerance (K) for drive command"
  Input "tolerance" "$obj tolerance"
  Tip "timeout (sec) for drive command"
  Input "drive timeout" "$obj maxwait"
  Tip "settling time (sec) for drive command"
  Input "settling time" "$obj settle"
}

proc ls370RangeGroup {obj} {
  Tip "voltage excitation (0...12)"
  Input "voltage excit." "$obj voltageEx"
  Label [$obj enumText voltageEx]
  Tip "current excitation (0...22)"
  Input "current excit." "$obj currentEx"
  Label [$obj enumText currentEx]
  CheckBox "auto range" "$obj autoRange"
  Newline
  Tip "resistance range (0...22)"
  Input "resistance range" "$obj range"
  Label [$obj enumText range]
}

proc ls370Layout args {
  Input "$args set T" "ts set"
  if {[result ts htrRange] == 0} {
    if {[result ts set] != 0} {
      Style warning
    }
    Label "heater OFF"
  }
  Group ls370 "LS370 settings" ts
}

proc ls370aLayout {obj} {
  Group ls370a "LS370a settings" $obj
}


proc ls370ighPower {{value none}} {
  if {[string compare $value none] == 0} {
    set hr [result dil htrRange]
    if {$hr == 0} {
      return "ts maxPower = 0"
    }
    return [ts maxPower]
  }
  if {$value > 0} {
    set hr 5
    while {$value < 2.01 && $hr > 1} {
      set value [expr $value * 10]
      incr hr -1
    }
    if {$value < 10.1} {
      ts htrRange 6
    } else {
      ts htrRange 7
    }
    ts ighHeater $hr
    dil htrRange $hr
  } else {
    ts htrRange 0
    dil htrRange 0
  }
  return [ts maxPower]
}

publishLazy ls370ighPower spy
