namespace eval irfcurrent {
}

proc stdConfig::irfcurrent {} {
  variable name

  controller std timeout=20
  prop startcmd v

  obj IRFCURRENT rd
  prop read irfcurrent::poll

  kids "current monitor ($name)" {
    node i1 upd
    node i2 upd
    node i3 upd
    node i4 upd
    node ib upd
  }
}

proc irfcurrent::poll {} {
  sct send "?"
  return irfcurrent::update
}

proc irfcurrent::update {} {
  if {[scan [sct result] "%f,%f,%f,%f,%f" i1 i2 ib i3 i4] != 5} {
    error "[sct]: illegal answer"
  }
  set i1 [expr $i1 - 854]
  set i2 [expr $i2 - 855.5]
  set i3 [expr $i3 - 852]
  set i4 [expr $i4 - 851.2]
  # there is some cross talk between i1-i2 and i3-i4
  #updateval [sct]/i1 [expr 0.0312 * $i1 - 0.0011 * $i2]
  #updateval [sct]/i2 [expr 0.0248 * $i2 - 0.0028 * $i1]
  #updateval [sct]/i3 [expr 0.0262 * $i3 - 0.0016 * $i4]
  #updateval [sct]/i4 [expr 0.0286 * $i4 - 0.0007 * $i3]
  updateval [sct]/i1 [expr 0.0405 * $i1 - 0.0014 * $i2]
  updateval [sct]/i2 [expr 0.0323 * $i2 - 0.0037 * $i1]
  updateval [sct]/i3 [expr 0.0341 * $i3 - 0.0021 * $i4]
  updateval [sct]/i4 [expr 0.0372 * $i4 - 0.0010 * $i3]
  # not sure if the background sensor would help to correct for offset shifts.
  # record for reference
  updateval [sct]/ib [expr ($ib - 850) * 0.025]
  sct update [expr [hval [sct]/i1] + [hval [sct]/i2] + [hval [sct]/i3] + [hval [sct]/i4]]
  return idle
}
