# motorized valve (DC-motor with endswitch)
namespace eval motvalve {} {
}

proc stdConfig::motvalve {} {
  controller std "\n" 5
  prop startcmd "t"

  obj MotValve wr
  prop write motvalve::write
  prop read motvalve::read
  prop enum close,open,closing,opening
  kids "motor valve" {
    node pos upd
  }
}

proc motvalve::write {} {
  if {[sct target] == 1 || [sct target] == 3} {
    sct send o
  } else {
    sct send c
  }
  return motvalve::read
}

proc motvalve::read {} {
  sct send ep
  return motvalve::update
}

proc motvalve::update {} {
  set pos -1
  set state -1
  set error ""
  scan [sct result] "e%d p%f %s" state pos error 
  updateval [sct]/pos $pos
  switch $state {
    0 {sct enum closed,open}
    1 {sct enum close,opened}
    2 {sct enum close,open,closing}
    3 {sct enum close,open,opening=3}
  }
  sct update $state
  if {"$error" ne ""} {
    sct geterror $error
  }
  return idle
}
