#LS370 simple driver

namespace eval ls370 {} {
}

proc stdConfig::ls370 {} {
  variable ctrl
  controller std "\n" 5
  prop startcmd "*IDN?"

  obj calib370 rd
  default 0
  prop read calib370::read
  prop period 15
  prop period0 0
  kids "Sensor Channels" {
    node chan1 upd
    prop newline 1
    node active1 par 1
    prop enum 1
    node chan2 upd
    node active2 par 1
    prop enum 1
    node chan3 upd
    node active3 par 1
    prop enum 1
    node chan4 upd
    node active4 par 1
    prop enum 1
    node chan5 upd
    node active5 par 1
    prop enum 1
    node chan6 upd
    node active6 par 1
    prop enum 1
    node chan7 upd
    node active7 par 1
    prop enum 1
    node chan8 upd
    node active8 par 1
    prop enum 1
  }
}

proc ls370::read {} {
  if {[sct period] ne [sct period0]} {
    [sct controller] poll [sct] [sct period] read read
    sct period0 [sct period]
  }
  if {[hvali [sct]] > 0} {
    sct send "RDGK?[hvali [sct]]"
    return ls370::update
  }
  sct update 1
  sct send "SCAN 1,0;SCAN?"
  return stdSct::complete
}

proc ls370::update {} {
  set chan [hvali [sct]]
  hdelprop [sct]/chan$chan geterror
  hupdate [sct]/chan$chan [sct result]
  for {set i 0} {$i < 8} {incr i} {
    incr chan
    if {$chan > 8} {
      set chan 1
    }
    if {[hvali [sct]/active$chan]} {
      break
    }
  }
  sct update $chan
  sct send "SCAN $chan,0;SCAN?"
  return stdSct::complete
}
