#!/usr/bin/tclsh

# This [APPLICATIONS]APP script is used with the library
# halfile: LIB:xhc-hb04.tcl to connect ini hal pins:
# ini.N.max_acceleration.  These ini hal pins are not
# available until task is started.

# This script waits for the creation of ini hal pins (based
# on testpin)  and then connects signals named:
#     pendant:muxed-accel-N
#  to ini.N.max_acceleration

package require Hal

set testpin ini.traj_default_acceleration ;# should always get created
set max_wait_ms 10000 ;# 10 seconds
set delay         100 ;# ms
set waited_ms       0
while {$waited_ms < $max_wait_ms} {
  if [catch { hal getp $testpin} msg] {
    #puts ${waited_ms}_MSG=$msg
  } else {
    set ok 1
    break
  }
  incr waited_ms $delay
  after $delay
}
if ![info exists ok] {
  set msg "ERROR: ini pins not found after waiting $max_wait_ms ms"
  package require Tk
  tk_messageBox \
    -title "[file tail $::argv0]" \
    -type ok \
    -message "$msg"
  puts "\n$::argv0:$msg\n"
  exit 1
}
foreach axno {0 1 2 3 4 5 6 7 8} {
  if ![catch {hal gets pendant:muxed-accel-$axno} msg] {
    hal net pendant:muxed-accel-$axno ini.$axno.max_acceleration
  }
}
