namespace eval epc8210 {} {
}

proc stdConfig::epc8210 {portlist} {
  controller std "\n" 5 "'logout'"
  prop write epc8210::write

  obj EPC8210 rd -none
  prop read epc8210::read
  prop @portlist $portlist
  kids "pumps/compressors" {
    foreach {name i} $portlist {
      node port$i out
      prop enum 1
      prop label $name
      prop nr $i
      if {$i % 4 == 1} {
        prop newline 1
      } else {
        prop nonewline 1
      } 
    }
  }
}

proc epc8210::read {} {
  sct send "\033"
  return epc8210::update
}

proc epc8210::update {} {
  set res [regsub -all {[^FN].*?O} [sct result] " O"]
  foreach {name i} [sct @portlist] {
#    clientput [sct @portlist]
    set p [sct objectPath]/port[expr $i]
    switch [lindex $res [expr $i -1] ] {
      ON  {updateval $p 1}
      OFF {updateval $p 0}
    }
  }
  return idle
}

proc epc8210::write {} {
  set nr [sct nr]
  if {[sct target]} {
    set cmd 0x31
  } else {
    set cmd 0x32
  }
  set xor [expr $cmd ^ $nr]
  sct send [format "%c%c%c%c%c" 0x80 $cmd $nr $xor 0x1B]
  return epc8210::update
}
