# lambda point control

proc makeLPR {lpr lnv port sensors} {
  set abovelam [lindex $sensors 0]
  GraphAdd ${lnv}.flow mbar LPR_flow orange
  GraphAdd ${lnv}.autoflow.flowtarget mbar LPR_set brown
  makenv $lnv -driver nv_ccu -controller ccu2 -port $port "lambda point refrigerator"
  makenv $lpr -driver piloop -invar $abovelam -outvar "lpr_set $lnv $lpr" -prop 0.5 -int 600 -outmin 35 -outmax 12 -title "lpr" 
  hsetprop /$lnv switchgraph "lpr_switchgraph $lnv {$sensors}"
#  _$lpr updatescript $abovelam "lpr_update $sensors"
# set lnv to off by default
  hset /$lnv 5
  hsetprop /$lnv @outvar_start $lnv 1
}

proc lpr_update {sens value} {
  foreach s $sens {
    set s [hvali $s]
    if {$s > $value} {
      set value $s
    }
  }
  loop::updateinput1 $value
}

proc lpr_set {lnv lpr {setpoint ""}} {
  if {$setpoint eq ""} {
    return [result $lnv set]
  }
  if {[hvali $lnv] != 1} {
    $lpr mode 0
    error "lambda point refrigerator is switched off - must be controlling while lpr control is on"
  }
  $lnv set $setpoint
}

proc lpr_switchgraph {nvname sensors value} {
  foreach ls $sensors {
    set ls [join [lrange [split $ls /] 1 end] .]
    GraphItem shown $ls $value
  }
  GraphItem shown ${nvname}.flow $value
  GraphItem shown ${nvname}.autoflow.flowtarget $value
  if {$value} {
    ignoreMsg $nvname 0
  } else {
    ignoreMsg $nvname always
  }
}
