namespace eval lauda {} {
}

proc stdConfig::lauda {} {
  variable name

  controller std "\r\n"
  prop startcmd "TYPE"

  obj Lauda wr -drive
  prop readcmd "IN_PV_00"
  prop readfmt "%g"
  prop write lauda::write
  prop checklimits lauda::checklimits
  prop halt lauda::halt

  kids "LAUDA thermostat" {
    node set wr
    prop readcmd "IN_SP_00"
    prop readfmt "%g"
    prop writecmd "OUT_SP_00_%.2f"

    node ramp par 1
    prop help "ramp (K/min)"

    node target par 0

  }
}

proc lauda::checklimits {} {
  if {[hval [sct]/ramp] == 0} {
    hupdate [sct]/ramp 9999
    clientput "ramp was zero, set to infinite"
  }
  hset [sct]/set [hval [sct]]
  sct status run
}

proc lauda::write {} {
  hset [sct]/target [sct target]
  [sct controller] poll [sct] 1.2 write lauda::progress
  return idle
}

proc lauda::halt {} {
  hupdate [sct]/ramp 0
  return idle
}

proc lauda::progress {} {
  set target [hval [sct]/target]
  set setp [silent [hval [sct]/set] hgetpropval [sct]/set target]
  set ramp [hval [sct]/ramp]
  if {$ramp == 0} {
    sct status idle
    return unpoll
  }
  set done 0
  if {$setp < $target} {
    set setp [expr $setp + $ramp * 0.02]
    if {$setp >= $target} {
      set setp $target
      set done 1
    }
  } elseif {$setp > $target} {
    set setp [expr $setp - $ramp * 0.02]
    if {$setp <= $target} {
      set setp $target
      set done 1
    }
  } else {
    set done 1
  }
  hset [sct]/set $setp
  if {$done} {
    sct status idle
    return unpoll
  }
  return idle
}


