#Programe de demonstration simple pour module TC/USB-48IO
#3CC 16/04/14: DB : V1.00 Creation 

from TCUSB48IO_Wrapper import *
from ctypes import *
import os
import time

def bin(n): 
    """Convertit un nombre en binaire""" 
    q = -1 
    res = '' 
    while q != 0: 
        q = n // 2 
        r = n % 2 
        res = `r` + res 
        n = q 
    return res

def wait():
    t1=time.clock()
    i=0
    while ((t1+0.5)>time.clock()):
        i=i+1

 


NumSerie= c_int(0)
ptrSN=pointer(NumSerie)
NModule=1
test=TCUSB48IO_OpenN(NModule,ptrSN)

DataIn32=c_int(0)
ptrDataIn32=pointer(DataIn32)

Position=c_int(0)
ptrPosition=pointer(Position)

DataANA=c_double(0)


DataTOR=0
ch='a'
str=''


if (test==0):

    while(ch!='q'):
        
        TCUSB48IO_Refresh(NModule)
        TCUSB48IO_Input(1,ptrDataIn32)
        
        print('Module TC/USB-48IO %d'%NumSerie.value)
        
        print('Etat sorties TOR: %d'%DataTOR)
        print('Etat entrees TOR: %s'%bin(DataIn32.value))

        
        print('Modifier valeur:')
        print(' 1 : sorties TOR')
        print(' Autre touche : Actualiser')
        print(' q : quitter')    

        ch=raw_input()
        if (ch=='1'):
            print(' Sorties TOR, valeur: (0->65535)')
            str=raw_input()
            DataTOR=int(str)
            TCUSB48IO_Output(NModule,DataTOR)
            TCUSB48IO_Refresh(NModule)
        wait()

    TCUSB48IO_Close(NModule)
else:
    print ('Erreur module TCUSB48IO absent')
