#from evtsel import *
import sys
import argparse
from sequser import *
#from seq_ca_new import *

Prefix='TPG:AS01:01'

if __name__ == '__main__':

    parser = argparse.ArgumentParser(description='simple exp seq setup')
    parser.add_argument('--pv' , help="TPG pv base", default='TPG:AS01:01')
    parser.add_argument('--seq', help="sequence number", type=int, default=13)
    args = parser.parse_args()

    global_sync_marker = 0
    ac60_sync_marker   = 5
    fix2u_sync_marker  = 7
    
    
    instrset = []
    #  Insert global sync instruction (1Hz?)
    b1 = len(instrset)
#    instrset.append(FixedRateSync(marker=global_sync_marker,occ=1))

    t = 0
    b0 = len(instrset)
    for i in [1,2,3,4,5,6]:

#       disable AC sync until the stepdown chassis is ready
        instrset.append(ACRateSync(timeslotm = (0x1<<(i-1)), marker = ac60_sync_marker, occ = 1))
        instrset.append(ControlRequest(word = 0x1<<0))
        t += 1
        instrset.append(FixedRateSync(marker = fix2u_sync_marker, occ = 460))
        instrset.append(ControlRequest(word = 0x1<<1))
        t += 1
        instrset.append(FixedRateSync(marker = fix2u_sync_marker, occ = 460))
        instrset.append(ControlRequest(word = 0x1<<2))
        t += 1
#       add additional interval, remove it when the AC sync is ready
#        instrset.append(FixedRateSync(marker = fix2u_sync_marker, occ = 460))

#    instrset.append(Branch.conditional(line = b0, counter = 0, value = 59))
    instrset.append(Branch.unconditional(line = b0))         
         


    instrset.append(Branch.unconditional(line=0))

    i=0
    for instr in instrset:
        print ('Put instruction(%d): '%i)
        instr.print_()
        i += 1


    title = 'UED 360Hz Rate'

    seq = SeqUser(args.pv+':EXP',args.seq)
    seq.execute(title,instrset)
