#!/usr/bin/env python
# -*- Python -*-

import sys
import time
# import os
import subprocess

#
# set up user environment
#  RtmToolsDir, MyRtcDir, etc.
#
from set_env import *
#
# import user tools
#
save_path = sys.path[:]
sys.path.append(Rh2LibDir)
from rtc_handle import *
sys.path.append(Rh2LibDir+'/tools/geo')
from geo import *

#
# import stub files
# 
sys.path.append(Rh2IdlDir)
import _GlobalIDL
sys.path = save_path

def assign_variables(env) :
  # rtc
  global ARMarkerDetectSimple, CaptureTimedCameraImage
  ARMarkerDetectSimple = env.name_space[Rh2ARNS].rtc_handles['ARMarkerDetectSimple0.rtc']
  CaptureTimedCameraImage = env.name_space[Rh2ARNS].rtc_handles['CaptureTimedCameraImage0.rtc']
  ARMarkerDetectSimple.outports['MarkerPose'].open()
  ARMarkerDetectSimple.outports['MarkerPoseAll'].open()
  #configuration
  ARMarkerDetectSimple.conf_ref.activate_configuration_set('Logicool')
  # data & class

def make_connectors(env):
  env.con_list=[]
#0
  con = IOConnector([CaptureTimedCameraImage.outports['Image'],ARMarkerDetectSimple.inports['Image']])
  env.con_list.append(con)

#
# connection control
#
def disconnect_all(env) :
  for con in env.con_list :
    con.disconnect()
def connect_list(env,ll) :
  for i in ll :
    env.con_list[i].connect()
def disconnect_list(env,ll) :
  for i in ll :
    env.con_list[i].disconnect()

def activate_seq(a_seq) :
  for tmp in a_seq :
    if isinstance(tmp,RtcHandle) :
      tmp.activate()
    elif isinstance(tmp, int) :
      time.sleep(tmp)
    elif isinstance(tmp, float) :
      time.sleep(tmp)
    else :
      eval(tmp)

def deactivate_all() :
#  slider.deactivate()
  ARMarkerDetectSimple.deactivate()
  CaptureTimedCameraImage.deactivate()

def end(env):
  disconnect_all(env)
  deactivate_all()
  ARMarkerDetectSimple.outports['MarkerPose'].close()
  ARMarkerDetectSimple.outports['MarkerPoseAll'].close()

#
# create env
#
env = RtmEnv(sys.argv,[Rh2ARNS])
for ns in env.name_space :
  env.name_space[ns].list_obj()
assign_variables(env)
make_connectors(env)
connect_list(env,[0])

