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

from TCUSB16IO_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=TCUSB16IO_OpenN(NModule,ptrSN)

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

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

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


if (test==0):
    TCUSB16IO_ResetPos(NModule)
    Position=0
    while(ch!='q'):
        
        TCUSB16IO_Refresh(NModule)
        TCUSB16IO_Pos(NModule,ptrPosition)
        TCUSB16IO_Input(1,ptrDataIn32)
        
        print('Module TC/USB-16IO %d'%NumSerie.value)
        print('Etat Codeur       %d'%Position)
        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->63)')
            str=raw_input()
            DataTOR=int(str)
            TCUSB16IO_Output(NModule,DataTOR)
            TCUSB16IO_Refresh(NModule)
        wait()

    TCUSB16IO_Close(NModule)
else:
    print ('Erreur module TCUSB16IO absent')
