from ctypes import *
b2 = CDLL("libpcie40.so")
dma_reset = b2.pcie40_b2dmabufferreset
dma_read = b2.pcie40_b2dmabufferread
DMASIZE = 0x10000
import os

import socket
hname = socket.gethostname()

b2.ecs_open( 0 , 2 ) 
b2.ecs_open( 0 , 0 ) 

for nev in range( 1 ): 
    # Reset DMA buffer
    dma_reset( 0 )
    # Send trigger (adapt this line to your system)
    if ( hname == 'belle2daq.local' ):
        os.system( "ssh vme \"trigft -80 pulse 1 1\"" ) ### ADD this to suppress output >/dev/null 2>&1" )
    else:   ### KEK 
        os.system( "ssh vme \"trigft -53 pulse 1 1\"" )

    # wait for data to arrive
    size = 0
    ## temporisation of 2s
    import time
    time.sleep( 2 )

    ## read data
    data = ( c_int * DMASIZE )( *range( DMASIZE ) )  
    dma_read( 0 , data )
    
    # Display data:
    print '==================================='
    for i in range( DMASIZE/8 ):
        print 'DMA: {7:08x}{6:08x}{5:08x}{4:08x}{3:08x}{2:08x}{1:08x}{0:08x}'.format(data[8*i],data[8*i+1],data[8*i+2],data[8*i+3],data[8*i+4],data[8*i+5],data[8*i+6],data[8*i+7])
    print '==================================='